|
2 | 2 |
|
3 | 3 | namespace PhpGui\Widget;
|
4 | 4 |
|
5 |
| -use PhpGui\ProcessTCL; |
| 5 | +use PhpGui\ProcessTCL; |
6 | 6 |
|
7 | 7 | /**
|
8 | 8 | * Class Button
|
9 | 9 | * Represents a button widget in the GUI.
|
10 | 10 | *
|
11 | 11 | * @package PhpGui\Widget
|
12 | 12 | */
|
13 |
| -class Button extends AbstractWidget { |
| 13 | +class Button extends AbstractWidget |
| 14 | +{ |
14 | 15 | private $callback;
|
15 | 16 |
|
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); |
18 | 20 | $this->callback = $options['command'] ?? null;
|
19 | 21 | $this->create();
|
20 | 22 | }
|
21 | 23 |
|
22 |
| - protected function create(): void { |
| 24 | + protected function create(): void |
| 25 | + { |
23 | 26 | $text = $this->options['text'] ?? 'Button';
|
24 |
| - $extra = $this->getOptionString(); |
25 |
| - |
| 27 | + $extra = $this->getOptionString(); |
| 28 | + |
26 | 29 | 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 | + }); |
28 | 34 | $this->tcl->evalTcl("button .{$this->parentId}.{$this->id} -text \"{$text}\" {$extra} -command {php::executeCallback {$this->id}}");
|
29 | 35 | } else {
|
30 | 36 | $this->tcl->evalTcl("button .{$this->parentId}.{$this->id} -text \"{$text}\" {$extra}");
|
31 | 37 | }
|
32 | 38 | }
|
33 | 39 |
|
34 |
| - protected function getOptionString(): string { |
| 40 | + protected function getOptionString(): string |
| 41 | + { |
35 | 42 | $opts = "";
|
36 | 43 | foreach ($this->options as $key => $value) {
|
37 | 44 | if (in_array($key, ['text', 'command'])) {
|
|
0 commit comments