ES
games/xencenter-xenadmin-xcp-jumphp / src / Gui / ToolButton.php · commit fb1b7c9
<?phpnamespace App\Gui;use JumPHP\Gui\Pointer;use JumPHP\Gui\Theme;use JumPHP\Gui\Widget;use JumPHP\Renderer\Renderer;/** * A button of the toolbar: an icon and a text, flat until the pointer is over it, greyed when not possible. * * @property string $text * @property string $icon */final class ToolButton extends Widget{    public const DEFAULTS = ['text' => '', 'icon' => '', 'background' => '', 'block' => false, 'active' => false];    protected function content(Theme $t, float $maxWidth, string $breakpoint): array    {        $text = $this->showsText($t) ? \JumPHP\Util\Lang::t((string)$this->props['text']) : '';        $w = $t->px(14) + ($this->props['icon'] !== '' ? $t->px(22) : 0) + ($text !== '' ? $t->font()->width($text) + $t->px(6) : 0);        return [$w + $t->px(4), $t->px($this->props['block'] ? 36 : 34)];    }    /** A narrow window: the toolbar's buttons keep only their icon (their text becomes the tooltip). */    private function showsText(Theme $t): bool    {        if ($this->props['block'] || $this->props['icon'] === '' || $this->form === null) {            return true;        }        $narrow = $this->form->viewWidth() > 0 && $this->form->viewWidth() / max(0.01, $t->scale) < 1100;        $this->props['tooltip'] = $narrow ? (string)$this->props['text'] : '';        return !$narrow;    }    public function stretches(string $axis): bool    {        return $axis === 'width' && (bool)$this->props['block'];    }    public function render(Renderer $r, Theme $t): void    {        $enabled = $this->isEnabled();        $radius = $t->px(6);        if ($this->props['active']) {            $t->box($r, $this->x, $this->y, $this->w, $this->h, '#dce8fb', $radius);            $r->fillRect($this->x, $this->y + $t->px(8), $t->px(3), $this->h - $t->px(16), '#2f6fd6');        } elseif ($this->props['background'] !== '') {            $t->box($r, $this->x, $this->y, $this->w, $this->h, (string)$this->props['background'], $radius);        }        if ($enabled && ($this->hovered || $this->pressed) && !$this->props['active']) {            $t->box($r, $this->x, $this->y, $this->w, $this->h, $this->pressed ? '#d3e2f8' : '#e8eef8', $radius);        }        $x = $this->x + $t->px($this->props['block'] ? 12 : 8);        if ($this->props['icon'] !== '') {            Icons::draw($r, (string)$this->props['icon'], $x, $this->y + ($this->h - $t->px(18)) / 2, $t->px(18));            if (!$enabled) {                $r->fillRect($x, $this->y + ($this->h - $t->px(18)) / 2, $t->px(18), $t->px(18), [245, 246, 247, 170]);            }            $x += $t->px(23);        }        $font = $t->font();        $font = $this->props['active'] ? $t->font(null, true) : $font;        $font->draw($r, $this->showsText($t) ? \JumPHP\Util\Lang::t((string)$this->props['text']) : '', $x, $this->y + ($this->h - $font->height()) / 2, 1,            $enabled ? ($this->props['active'] ? '#1d3c6e' : '#24292f') : '#a6adb7');    }    public function pointerUp(Pointer $p, bool $inside): void    {        if ($inside && $this->isEnabled()) {            $this->emit('click');        }    }}