|
| 1 | +<?php |
| 2 | + |
| 3 | +use Tkui\Widgets\Container; |
| 4 | +use Tkui\Widgets\Scrollbar; |
| 5 | +use Tkui\Widgets\TreeView\Column; |
| 6 | +use Tkui\Widgets\TreeView\Item; |
| 7 | +use Tkui\Widgets\TreeView\TreeView; |
| 8 | + |
| 9 | +require_once dirname(__FILE__) . '/DemoAppWindow.php'; |
| 10 | + |
| 11 | +$demo = new class extends DemoAppWindow |
| 12 | +{ |
| 13 | + private array $data = [ |
| 14 | + ['Argentina', 'Buenos Aires', 'ARS'], |
| 15 | + ['Australia', 'Canberra', 'AUD'], |
| 16 | + ['Brazil', 'Brazilia', 'BRL'], |
| 17 | + ['Canada', 'Ottawa', 'CAD'], |
| 18 | + ['China', 'Beijing', 'CNY'], |
| 19 | + ['France', 'Paris', 'EUR'], |
| 20 | + ['Germany', 'Berlin', 'EUR'], |
| 21 | + ['India', 'New Delhi', 'INR'], |
| 22 | + ['Italy', 'Rome', 'EUR'], |
| 23 | + ['Japan', 'Tokyo', 'JPY'], |
| 24 | + ['Mexico', 'Mexico City', 'MXN'], |
| 25 | + ['South Africa', 'Pretoria', 'ZAR'], |
| 26 | + ['United Kingdom', 'London', 'GBP'], |
| 27 | + ['United States', 'Washington, D.C.', 'USD'], |
| 28 | + ]; |
| 29 | + |
| 30 | + private TreeView $tv; |
| 31 | + |
| 32 | + public function __construct() |
| 33 | + { |
| 34 | + parent::__construct('TreeView demo'); |
| 35 | + $this->buildUI(); |
| 36 | + $this->addValues(); |
| 37 | + |
| 38 | + $this->tv->onSelect([$this, 'onSelectItem']); |
| 39 | + } |
| 40 | + |
| 41 | + private function buildUI(): void |
| 42 | + { |
| 43 | + $this->tv = $this->buildTreeView($this); |
| 44 | + } |
| 45 | + |
| 46 | + private function buildTreeView(Container $parent, array $options = []): TreeView |
| 47 | + { |
| 48 | + $columns = $this->createColumns(); |
| 49 | + |
| 50 | + $tv = new TreeView($parent, $columns, $options); |
| 51 | + $tv->xScrollCommand = new Scrollbar($parent, ['orient' => Scrollbar::ORIENT_HORIZONTAL]); |
| 52 | + $tv->yScrollCommand = new Scrollbar($parent, ['orient' => Scrollbar::ORIENT_VERTICAL]); |
| 53 | + |
| 54 | + $parent->grid($tv, ['sticky' => 'news', 'row' => 0, 'column' => 0]) |
| 55 | + ->rowConfigure($parent, 0, ['weight' => 1]) |
| 56 | + ->columnConfigure($parent, 0, ['weight' => 1]); |
| 57 | + $parent->grid($tv->yScrollCommand, ['sticky' => 'nsew', 'row' => 0, 'column' => 1]); |
| 58 | + $parent->grid($tv->xScrollCommand, ['sticky' => 'nsew', 'row' => 1, 'column' => 0]); |
| 59 | + |
| 60 | + return $tv; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return array<Column> |
| 65 | + */ |
| 66 | + private function createColumns(): array |
| 67 | + { |
| 68 | + return [ |
| 69 | + Column::create('country', 'Country'), |
| 70 | + Column::create('capital', 'Capital'), |
| 71 | + Column::create('currency', 'Currency'), |
| 72 | + ]; |
| 73 | + } |
| 74 | + |
| 75 | + private function addValues(): void |
| 76 | + { |
| 77 | + foreach ($this->data as $values) { |
| 78 | + $this->tv->add(new Item($values)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public function onSelectItem(array $selected): void |
| 83 | + { |
| 84 | + foreach ($selected as $itemId) { |
| 85 | + echo "$itemId" . PHP_EOL; |
| 86 | + } |
| 87 | + } |
| 88 | +}; |
| 89 | + |
| 90 | +$demo->run(); |
0 commit comments