-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWindow.php
More file actions
64 lines (50 loc) · 1.53 KB
/
Window.php
File metadata and controls
64 lines (50 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php declare(strict_types=1);
namespace DynamicComponents;
use InvalidArgumentException;
use UI\Control;
use UI\Size;
use function get_class;
use function UI\quit;
class Window extends \UI\Window
{
/** @var bool */
private $menu;
/** @var callable|null */
private $onClosing;
public function __construct(string $title, Size $size, bool $menu = false, ?callable $onClosing = null)
{
parent::__construct($title, $size, $menu);
$this->menu = $menu;
$this->onClosing = $onClosing;
}
public function add(Control $control): void
{
// While ext-ui is ok with this, libui is not. If we don't prevent it, php crashes.
if ($control instanceof \UI\Window) {
$child = get_class($control);
$parent = static::class;
throw new InvalidArgumentException(
"Cannot add a Window ({$child}: {$control->getTitle()}) to a Window ({$parent}: {$this->getTitle()})!"
);
}
parent::add($control);
}
public function hasMenu(): bool
{
return $this->menu;
}
public function setOnClosing(callable $onClosing): void
{
$this->onClosing = $onClosing;
}
protected function onClosing(): void
{
if ($this->onClosing && ($this->onClosing)($this) === false) {
return;
}
// Default behaviour when not having implemented onClosing.
// Explicitly return false in the callback to prevent.
$this->destroy();
quit();
}
}