Skip to content

Commit 8e5aa4f

Browse files
Refactor Button class for improved readability and consistency
1 parent 154d7f7 commit 8e5aa4f

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

src/Widget/Button.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,43 @@
22

33
namespace PhpGui\Widget;
44

5-
use PhpGui\ProcessTCL;
5+
use PhpGui\ProcessTCL;
66

77
/**
88
* Class Button
99
* Represents a button widget in the GUI.
1010
*
1111
* @package PhpGui\Widget
1212
*/
13-
class Button extends AbstractWidget {
13+
class Button extends AbstractWidget
14+
{
1415
private $callback;
1516

16-
public function __construct(string $parentId, array $options = []) {
17-
parent::__construct($parentId, $options);
17+
public function __construct(string $parentId, array $options = [])
18+
{
19+
parent::__construct($parentId, $options);
1820
$this->callback = $options['command'] ?? null;
1921
$this->create();
2022
}
2123

22-
protected function create(): void {
24+
protected function create(): void
25+
{
2326
$text = $this->options['text'] ?? 'Button';
24-
$extra = $this->getOptionString();
25-
27+
$extra = $this->getOptionString();
28+
2629
if ($this->callback) {
27-
ProcessTCL::getInstance()->registerCallback($this->id, $this->callback);
30+
ProcessTCL::getInstance()->registerCallback($this->id, function () {
31+
call_user_func($this->callback);
32+
$this->tcl->evalTcl("update"); // Force widget updates
33+
});
2834
$this->tcl->evalTcl("button .{$this->parentId}.{$this->id} -text \"{$text}\" {$extra} -command {php::executeCallback {$this->id}}");
2935
} else {
3036
$this->tcl->evalTcl("button .{$this->parentId}.{$this->id} -text \"{$text}\" {$extra}");
3137
}
3238
}
3339

34-
protected function getOptionString(): string {
40+
protected function getOptionString(): string
41+
{
3542
$opts = "";
3643
foreach ($this->options as $key => $value) {
3744
if (in_array($key, ['text', 'command'])) {

0 commit comments

Comments
 (0)