EN
apps/winzik / src / Gui / PopupMenu.php · commit 6576d6f
<?phpnamespace App\Gui;/** * The right click menu of WinZik (Form7.abc of 2001: "Menu", "Options", "The Web Browser", "A propos", "Quitter"), * drawn like the menus of Windows 98: grey, a 3D frame, the line under the pointer in navy blue, submenus to the right. */final class PopupMenu{    public const ROW = 17;    public const SEPARATOR = 7;    public const WIDTH = 210;    /** @var list<array{label: string, run?: \Closure, sub?: list<array>, checked?: bool, enabled?: bool, separator?: bool, back?: bool}> */    public array $items;    public float $x;    public float $y;    public int $hover = -1;    public ?PopupMenu $child = null;    private int $childOf = -1;    /** @var array{0: float, 1: float, 2: float, 3: float} the screen it opens on (desktop pixels) */    private array $bounds;    /** it opened to the left of the pointer (near the right edge): its submenus open to the left too */    public bool $leftwards = false;    /**     * Made for a finger (the original interface on a phone): bigger rows and letters, as wide as the screen allows, a     * submenu opens in its place (its first row goes back), a finger slides its rows when it is taller than the screen.     */    public bool $touch = false;    public float $width = self::WIDTH;    public float $row = self::ROW;    public float $sep = self::SEPARATOR;    public float $font = 9.0;    /** a submenu of a touch menu: its first row goes back to its parent */    private ?PopupMenu $parent = null;    /**     * @param list<array> $items     * @param array{0: float, 1: float, 2: float, 3: float}|null $bounds the screen of the pointer (null: the whole desktop)     * @param bool|null $leftwards a submenu: the side already chosen by its parent (null: chosen here)     */    public function __construct(array $items, float $x, float $y, float $deskWidth, float $deskHeight, ?array $bounds = null, ?bool $leftwards = null, bool $touch = false)    {        $this->items = $items;        $this->bounds = $bounds ?? [0.0, 0.0, $deskWidth, $deskHeight];        [$mx, $my, $mw, $mh] = $this->bounds;        if ($touch) {            // a finger: rows of 26 pixels of the skins (a finger's width on a phone), across the screen            [$this->touch, $this->row, $this->sep, $this->font] = [true, 26.0, 9.0, 12.0];            $this->width = max(120.0, min($mw - 8, 320.0));            $x = $mx + ($mw - $this->width) / 2;            $leftwards = false;        }        $h = $this->height();        if ($leftwards === null) {            // too near the right edge of its screen: it opens to the left of the pointer (near the left one: to the right);            // too near the bottom: upwards            $this->leftwards = $x + $this->width > $mx + $mw - 2;            $x = $this->leftwards ? $x - $this->width : $x;            $y = $y + $h > $my + $mh - 2 ? $y - $h : $y;        } else {            $this->leftwards = $leftwards;        }        // whole on its screen        $this->x = max($mx, min($x, $mx + $mw - $this->width - 2));        $this->y = max($my, min($y, $my + $mh - $h - 2));    }    /** how far its rows are scrolled up (a menu taller than its screen: zoom 200 % on a small screen) */    public float $scroll = 0.0;    /** The height of all its rows (the menu's own height when it fits on its screen). */    public function fullHeight(): float    {        $h = 6;        foreach ($this->items as $item) {            $h += !empty($item['separator']) ? $this->sep : $this->row;        }        return $h;    }    /** Its height on the screen: all its rows, or the screen's height when they do not fit (then it scrolls). */    public function height(): float    {        return min($this->fullHeight(), max(6 + 3 * $this->row, $this->bounds[3] - 2));    }    /** taller than its screen: an arrow at the top and at the bottom scroll its rows (as the menus of Windows do) */    public function scrolls(): bool    {        return $this->fullHeight() > $this->height();    }    private function maxScroll(): float    {        return $this->scrolls() ? $this->fullHeight() - $this->height() + 2 * $this->row : 0.0;    }    /** a row fully between the arrows (a scrolling menu shows only those) */    private function rowShown(int $index): bool    {        if (!$this->scrolls()) {            return true;        }        $top = $this->rowTop($index);        $h = !empty($this->items[$index]['separator']) ? $this->sep : $this->row;        return $top >= $this->y + 3 + $this->row - 0.01 && $top + $h <= $this->y + $this->height() - 3 - $this->row + 0.01;    }    /** The row under a point (desktop pixels), -1 = none. */    public function rowAt(float $x, float $y): int    {        if ($x < $this->x || $x >= $this->x + $this->width) {            return -1;        }        foreach ($this->items as $i => $item) {            $top = $this->rowTop($i);            $h = !empty($item['separator']) ? $this->sep : $this->row;            if ($y >= $top && $y < $top + $h) {                return !empty($item['separator']) || !$this->rowShown($i) ? -1 : $i;            }        }        return -1;    }    private function rowTop(int $index): float    {        $top = $this->y + 3 + ($this->scrolls() ? $this->row - $this->scroll : 0.0);        foreach ($this->items as $i => $item) {            if ($i === $index) {                return $top;            }            $top += !empty($item['separator']) ? $this->sep : $this->row;        }        return $top;    }    public function contains(float $x, float $y): bool    {        if ($this->touch && $this->child !== null) {            return $this->child->contains($x, $y);     // the submenu is in its place        }        return ($x >= $this->x && $x < $this->x + $this->width && $y >= $this->y && $y < $this->y + $this->height())            || ($this->child?->contains($x, $y) ?? false);    }    /** The pointer moves: the row lights, a submenu opens. */    public function hoverAt(float $x, float $y, float $deskWidth, float $deskHeight): void    {        if ($this->child !== null && $this->child->contains($x, $y)) {            $this->child->hoverAt($x, $y, $deskWidth, $deskHeight);            return;        }        // on an arrow of a menu taller than its screen: its rows scroll (a few pixels at each frame the pointer stays)        if ($this->scrolls() && $x >= $this->x && $x < $this->x + $this->width) {            $dir = $y >= $this->y && $y < $this->y + 3 + $this->row ? -1 : ($y >= $this->y + $this->height() - 3 - $this->row && $y < $this->y + $this->height() ? 1 : 0);            if ($dir !== 0) {                $this->scrollBy($dir * 4);                return;            }        }        $row = $this->rowAt($x, $y);        if ($row < 0) {            return;        }        $this->hover = $row;        $item = $this->items[$row];        if (!empty($item['sub']) && $this->childOf !== $row) {            $this->childOf = $row;            // on the side the menu opened to; the other side when there is no room there            [$mx, , $mw] = $this->bounds;            $left = $this->leftwards;            if ($left && $this->x - $this->width + 3 < $mx) {                $left = false;            } elseif (!$left && $this->x + 2 * $this->width - 3 > $mx + $mw) {                $left = true;            }            $childX = $left ? $this->x - $this->width + 3 : $this->x + $this->width - 3;            $this->child = new self($item['sub'], $childX, $this->rowTop($row) - 3, $deskWidth, $deskHeight, $this->bounds, $left);        } elseif (empty($item['sub'])) {            $this->child = null;            $this->childOf = -1;        }    }    /** Its rows scrolled by $dy pixels (the wheel, an arrow), within its rows; its submenu closes. */    public function scrollBy(float $dy): void    {        $scroll = max(0.0, min($this->maxScroll(), $this->scroll + $dy));        if ($scroll !== $this->scroll) {            $this->scroll = $scroll;            $this->child = null;            $this->childOf = -1;        }    }    /**     * A click: runs the row's action.     * @return bool true when the menu should close     */    public function click(float $x, float $y, float $deskWidth, float $deskHeight): bool    {        if ($this->touch && $this->child !== null) {            return $this->child->click($x, $y, $deskWidth, $deskHeight);        }        if ($this->child !== null && $this->child->contains($x, $y)) {            return $this->child->click($x, $y, $deskWidth, $deskHeight);        }        $row = $this->rowAt($x, $y);        if ($row < 0) {            return !$this->contains($x, $y);        }        $item = $this->items[$row];        if (($item['enabled'] ?? true) === false) {            return false;        }        if (!empty($item['back']) && $this->parent !== null) {            $this->parent->back();            return false;        }        if (!empty($item['sub']) && $this->touch) {            $this->openInPlace($row, $deskWidth, $deskHeight);            return false;        }        if (!empty($item['sub'])) {            $this->hoverAt($x, $y, $deskWidth, $deskHeight);            return false;        }        if (isset($item['run'])) {            ($item['run'])();        }        return true;    }    /** A touch menu: the submenu of a row opens in its place, its first row goes back. */    private function openInPlace(int $row, float $deskWidth, float $deskHeight): void    {        $item = $this->items[$row];        $items = array_merge([['label' => (string)$item['label'], 'back' => true], ['separator' => true, 'label' => '']], $item['sub']);        $child = new self($items, $this->x, $this->y, $deskWidth, $deskHeight, $this->bounds, null, true);        $child->parent = $this;        $this->child = $child;        $this->childOf = $row;        $this->hover = -1;    }    /** The menu a finger is on: the deepest submenu opened in its place (a touch menu), else this one. */    public function active(): PopupMenu    {        return $this->touch && $this->child !== null ? $this->child->active() : $this;    }    /** Back (the phone's back button): the submenu opened in its place closes. @return bool false: none was open */    public function back(): bool    {        if ($this->child === null || !$this->touch) {            return false;        }        if (!$this->child->back()) {            $this->child = null;            $this->childOf = -1;        }        return true;    }    /** Where a row is (the middle of it, desktop pixels) in the menu shown (a touch menu: its submenu in its place), null: not shown. @return array{0: float, 1: float}|null */    public function rowCenter(string $label): ?array    {        $menu = $this->active();        foreach ($menu->items as $i => $item) {            if (empty($item['separator']) && (string)$item['label'] === $label && $menu->rowShown($i)) {                return [$menu->x + $menu->width / 2, $menu->rowTop($i) + $menu->row / 2];            }        }        return null;    }    /** A finger down on a touch menu: its row lights (nothing runs before the finger is lifted). */    public function pressAt(float $x, float $y): void    {        $menu = $this->active();        $menu->hover = $menu->rowAt($x, $y);    }    /** The room of a row's label (skin pixels): from its left margin to its arrow / the edge. */    public function labelRoom(array $item): float    {        return $this->width - ($this->touch ? 22 : 20) - (!empty($item['sub']) ? 16 : 6);    }    /** The size of a row's letters: a finger's menu draws a label too long for its row a little smaller (never under 10). */    public function labelFont(Canvas $c, array $item): float    {        $font = $this->font;        if ($this->touch) {            $label = (string)$item['label'];            while ($font > 10 && $c->textWidth($label, $font) > $this->labelRoom($item)) {                $font -= 0.5;            }        }        return $font;    }    /** false: not its submenu (the desktop version gives it a window of its own) */    public bool $withChild = true;    /** false: no shadow (a window of the computer has no room for it) */    public bool $shadow = true;    public function render(Canvas $c): void    {        if ($this->touch && $this->child !== null) {            $this->child->render($c);      // in its place            return;        }        $h = $this->height();        if ($this->shadow) {            $c->fill($this->x + 2, $this->y + 2, $this->width, $h, [0, 0, 0, 60]);        }        $c->bevel($this->x, $this->y, $this->width, $h);        if ($this->scrolls()) {            // the arrows (grey when it cannot go further that way)            foreach ([[$this->y + 3, -1, $this->scroll > 0], [$this->y + $h - 3 - $this->row, 1, $this->scroll < $this->maxScroll()]] as [$ay, $dir, $on]) {                $color = $on ? '#000000' : '#808080';                for ($k = 0; $k < 4; $k++) {                    $w = $dir < 0 ? 2 * $k + 1 : 7 - 2 * $k;                    $c->fill($this->x + $this->width / 2 - $w / 2, $ay + $this->row / 2 - 2 + $k, $w, 1, $color);                }            }        }        foreach ($this->items as $i => $item) {            if (!$this->rowShown($i)) {                continue;            }            $top = $this->rowTop($i);            if (!empty($item['separator'])) {                $c->fill($this->x + 3, $top + 3, $this->width - 6, 1, '#808080');                $c->fill($this->x + 3, $top + 4, $this->width - 6, 1, '#ffffff');                continue;            }            $enabled = $item['enabled'] ?? true;            $lit = $i === $this->hover && $enabled;            if ($lit) {                $c->fill($this->x + 3, $top, $this->width - 6, $this->row, '#000080');            }            $color = !$enabled ? '#808080' : ($lit ? '#ffffff' : '#000000');            $oy = ($this->row - self::ROW) / 2;      // a finger's rows: their marks in the middle            if (!empty($item['checked'])) {                // the check mark of Windows 98, pixel by pixel                foreach ([[0, 2], [1, 3], [2, 4], [3, 3], [4, 2], [5, 1], [6, 0]] as [$dx, $dy]) {                    $c->fill($this->x + 7 + $dx, $top + $oy + 6 + $dy, 1, 3, $color);                }            }            if (!empty($item['back'])) {                // back to the menu above: an arrow to the left, the name of the submenu in bold                for ($k = 0; $k < 5; $k++) {                    $c->fill($this->x + 7 + $k, $top + $oy + 8.5 - $k, 1, 2 * $k + 1, $color);                }            }            $font = $this->labelFont($c, $item);            $c->text((string)$item['label'], $this->x + ($this->touch ? 22 : 20), $top + ($this->row - $font * 1.3) / 2, $font, $color);            if (!empty($item['sub'])) {                for ($k = 0; $k < 4; $k++) {                    $c->fill($this->x + $this->width - 12 + $k, $top + $oy + 4.5 + $k, 1, 8 - 2 * $k, $color);                }            }        }        if ($this->withChild) {            $this->child?->render($c);        }    }}