Skip to content

Commit 78ceb3a

Browse files
Refactor Label class to improve structure and enhance option handling
1 parent c7b68d7 commit 78ceb3a

File tree

1 file changed

+42
-7
lines changed

1 file changed

+42
-7
lines changed

src/Widget/Label.php

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,53 @@
88
*
99
* @package PhpGui\Widget
1010
*/
11-
class Label extends AbstractWidget {
12-
public function __construct(string $parentId, array $options = []) {
13-
parent::__construct($parentId, $options);
11+
class Label extends AbstractWidget
12+
{
13+
public function __construct(string $parentId, array $options = [])
14+
{
15+
parent::__construct($parentId, $options);
1416
$this->create();
1517
}
1618

17-
protected function create(): void {
19+
protected function create(): void
20+
{
1821
$text = $this->options['text'] ?? '';
19-
$this->tcl->evalTcl("label .{$this->parentId}.{$this->id} -text \"{$text}\""); // Correct Tcl path
22+
$extra = $this->getOptionString();
23+
$this->tcl->evalTcl("label .{$this->parentId}.{$this->id} -text \"{$text}\" {$extra}");
2024
}
2125

22-
public function setText(string $text): void {
23-
$this->tcl->evalTcl(".{$this->parentId}.{$this->id} configure -text \"{$text}\""); // Correct Tcl path
26+
protected function getOptionString(): string
27+
{
28+
$opts = "";
29+
foreach ($this->options as $key => $value) {
30+
if ($key === 'text') continue;
31+
$opts .= " -$key \"$value\"";
32+
}
33+
return $opts;
34+
}
35+
36+
public function setText(string $text): void
37+
{
38+
$this->tcl->evalTcl(".{$this->parentId}.{$this->id} configure -text \"{$text}\"");
39+
}
40+
41+
public function setFont(string $font): void
42+
{
43+
$this->tcl->evalTcl(".{$this->parentId}.{$this->id} configure -font \"{$font}\"");
44+
}
45+
46+
public function setForeground(string $color): void
47+
{
48+
$this->tcl->evalTcl(".{$this->parentId}.{$this->id} configure -fg \"{$color}\"");
49+
}
50+
51+
public function setBackground(string $color): void
52+
{
53+
$this->tcl->evalTcl(".{$this->parentId}.{$this->id} configure -bg \"{$color}\"");
54+
}
55+
56+
public function setState(string $state): void
57+
{
58+
$this->tcl->evalTcl(".{$this->parentId}.{$this->id} configure -state \"{$state}\"");
2459
}
2560
}

0 commit comments

Comments
 (0)