|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Tkui\Layouts\Pack; |
| 6 | +use Tkui\TclTk\Variable; |
| 7 | +use Tkui\Widgets\Buttons\Button; |
| 8 | +use Tkui\Widgets\Container; |
| 9 | +use Tkui\Widgets\Frame; |
| 10 | +use Tkui\Widgets\Label; |
| 11 | +use Tkui\Widgets\LabelFrame; |
| 12 | +use Tkui\Widgets\Progressbar; |
| 13 | + |
| 14 | +require_once __DIR__ . '/DemoAppWindow.php'; |
| 15 | + |
| 16 | +$demo = new class extends DemoAppWindow |
| 17 | +{ |
| 18 | + const MAX_VALUE = 100; |
| 19 | + |
| 20 | + private Variable $progressValue; |
| 21 | + /** @var array<Progressbar> */ |
| 22 | + private array $autoincrementBars; |
| 23 | + |
| 24 | + public function __construct() |
| 25 | + { |
| 26 | + parent::__construct('Progressbar Demo'); |
| 27 | + |
| 28 | + $this->progressValue = $this->app->registerVar('progressValue'); |
| 29 | + $this->progressValue->set(0); |
| 30 | + |
| 31 | + $this->buildActionButtons($this); |
| 32 | + |
| 33 | + $this->autoincrementBars = array_merge( |
| 34 | + $this->buildHorizontalProgressBars($this), |
| 35 | + $this->buildVerticalProgressbars($this) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @return array<Progressbar> |
| 41 | + */ |
| 42 | + private function buildHorizontalProgressBars(Container $parent): array |
| 43 | + { |
| 44 | + $f = new LabelFrame($parent, 'Horizontal'); |
| 45 | + |
| 46 | + $pbar1 = new Progressbar($f, [ |
| 47 | + 'mode' => Progressbar::MODE_DETERMINATE, |
| 48 | + 'orient' => Progressbar::ORIENT_HORIZONTAL, |
| 49 | + 'variable' => $this->progressValue, |
| 50 | + 'maximum' => self::MAX_VALUE, |
| 51 | + ]); |
| 52 | + |
| 53 | + $pbar2 = new Progressbar($f, [ |
| 54 | + 'mode' => Progressbar::MODE_INDETERMINATE, |
| 55 | + 'orient' => Progressbar::ORIENT_HORIZONTAL, |
| 56 | + ]); |
| 57 | + |
| 58 | + $f->grid(new Label($f, 'Determinate'), [ |
| 59 | + 'row' => 0, |
| 60 | + 'column' => 0, |
| 61 | + 'sticky' => 'w' |
| 62 | + ]); |
| 63 | + $f->grid($pbar1, [ |
| 64 | + 'pady' => 4, |
| 65 | + 'row' => 0, |
| 66 | + 'column' => 1, |
| 67 | + ]); |
| 68 | + $f->grid(new Label($f, 'Value:'), ['row' => 1, 'column' => 0, 'sticky' => 'w']); |
| 69 | + $f->grid(new Label($f, '-', ['textVariable' => $this->progressValue]), [ |
| 70 | + 'row' => 1, |
| 71 | + 'column' => 1, |
| 72 | + 'sticky' => 'w', |
| 73 | + ]); |
| 74 | + |
| 75 | + $f->grid(new Label($f, 'Indeterminate'), [ |
| 76 | + 'row' => 2, |
| 77 | + 'column' => 0, |
| 78 | + 'sticky' => 'w', |
| 79 | + ]); |
| 80 | + $f->grid($pbar2, [ |
| 81 | + 'pady' => 4, |
| 82 | + 'row' => 2, |
| 83 | + 'column' => 1, |
| 84 | + ]); |
| 85 | + |
| 86 | + $parent->pack($f, [ |
| 87 | + 'side' => Pack::SIDE_LEFT, |
| 88 | + 'padx' => 4, |
| 89 | + 'pady' => 6, |
| 90 | + 'anchor' => 'n', |
| 91 | + ]); |
| 92 | + |
| 93 | + return [$pbar1, $pbar2]; |
| 94 | + } |
| 95 | + |
| 96 | + private function buildVerticalProgressbars(Container $parent): array |
| 97 | + { |
| 98 | + $f = new LabelFrame($parent, 'Vertical'); |
| 99 | + |
| 100 | + $pbar1 = new Progressbar($f, [ |
| 101 | + 'mode' => Progressbar::MODE_DETERMINATE, |
| 102 | + 'orient' => Progressbar::ORIENT_VERTICAL, |
| 103 | + 'variable' => $this->progressValue, |
| 104 | + 'maximum' => self::MAX_VALUE, |
| 105 | + ]); |
| 106 | + |
| 107 | + $pbar2 = new Progressbar($f, [ |
| 108 | + 'mode' => Progressbar::MODE_INDETERMINATE, |
| 109 | + 'orient' => Progressbar::ORIENT_VERTICAL, |
| 110 | + ]); |
| 111 | + |
| 112 | + $f->grid(new Label($f, 'Determinate'), [ |
| 113 | + 'row' => 0, |
| 114 | + 'column' => 0, |
| 115 | + 'sticky' => 'w' |
| 116 | + ]); |
| 117 | + $f->grid($pbar1, [ |
| 118 | + 'pady' => 4, |
| 119 | + 'row' => 0, |
| 120 | + 'column' => 1, |
| 121 | + ]); |
| 122 | + $f->grid(new Label($f, '-', ['textVariable' => $this->progressValue]), [ |
| 123 | + 'row' => 1, |
| 124 | + 'column' => 1, |
| 125 | + ]); |
| 126 | + |
| 127 | + $f->grid(new Label($f, 'Indeterminate'), [ |
| 128 | + 'row' => 0, |
| 129 | + 'column' => 3, |
| 130 | + 'sticky' => 'w', |
| 131 | + ]); |
| 132 | + $f->grid($pbar2, [ |
| 133 | + 'pady' => 4, |
| 134 | + 'row' => 0, |
| 135 | + 'column' => 4, |
| 136 | + ]); |
| 137 | + |
| 138 | + $parent->pack($f, [ |
| 139 | + 'side' => Pack::SIDE_LEFT, |
| 140 | + 'padx' => 4, |
| 141 | + 'pady' => 6, |
| 142 | + 'anchor' => 'n', |
| 143 | + ]); |
| 144 | + |
| 145 | + return [$pbar1, $pbar2]; |
| 146 | + } |
| 147 | + |
| 148 | + private function buildActionButtons(Container $parent): void |
| 149 | + { |
| 150 | + $f = new Frame($parent); |
| 151 | + |
| 152 | + $btnStep = new Button($f, 'Step'); |
| 153 | + $btnStep->onClick([$this, 'stepProgress']); |
| 154 | + |
| 155 | + $btnStart = new Button($f, 'Start'); |
| 156 | + $btnStart->onClick([$this, 'startProgress']); |
| 157 | + |
| 158 | + $btnStop = new Button($f, 'Stop'); |
| 159 | + $btnStop->onClick([$this, 'stopProgress']); |
| 160 | + |
| 161 | + $f->pack([$btnStep, $btnStart, $btnStop], [ |
| 162 | + 'side' => Pack::SIDE_LEFT, |
| 163 | + ]); |
| 164 | + |
| 165 | + $parent->pack($f, [ |
| 166 | + 'side' => Pack::SIDE_BOTTOM, |
| 167 | + 'pady' => 8, |
| 168 | + 'padx' => 4, |
| 169 | + ]); |
| 170 | + } |
| 171 | + |
| 172 | + public function startProgress(): void |
| 173 | + { |
| 174 | + $this->progressValue->set(0); |
| 175 | + foreach ($this->autoincrementBars as $progressBar) { |
| 176 | + $progressBar->start(); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + public function stopProgress(): void |
| 181 | + { |
| 182 | + foreach ($this->autoincrementBars as $progressBar) { |
| 183 | + $progressBar->stop(); |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + public function stepProgress(): void |
| 188 | + { |
| 189 | + $value = $this->progressValue->asFloat() + 5.0; |
| 190 | + if ($value >= self::MAX_VALUE) { |
| 191 | + $value = 0; |
| 192 | + } |
| 193 | + $this->progressValue->set($value); |
| 194 | + } |
| 195 | +}; |
| 196 | + |
| 197 | +$demo->run(); |
0 commit comments