RU
apps/winzik / src / Form / WinZikDialog.php · commit 6576d6f
<?phpnamespace App\Form;use App\Core\WinZik;use JumPHP\Gui\Button;use JumPHP\Gui\Form;use JumPHP\Gui\Pointer;use JumPHP\Gui\TextBox;use JumPHP\Kernel\Platform;/** * A dialog of WinZik over the desktop (the small windows of 2001: Form3, Form6, Form9, Form11, Form17, Form20...). * The music goes on while it is open: only the scene on top is updated, so it ticks WinZik itself. * * Keys of every dialog: Entrée = the button marked `default: true` (from a text field too, when the field's * `submit` event is wired to onFieldSubmit), Échap = the button marked `cancel: true`, even from a text field * (a text field of the engine keeps Échap for itself). * * On a phone with WinZik original (the windows of 2001, MainForm::originalOnPhone()) a dialog is the whole screen, at the * phone's width (no box in the middle of the windows): the phone's back button (Échap) closes it. */abstract class WinZikDialog extends Form{    public function __construct(protected WinZik $wz)    {        parent::__construct();    }    public function enter(array $params = []): void    {        $this->wz->attach($this->engine);        parent::enter($params);    }    /** tests: a phone simulated in a column at the left of the view (its width / its height, MainForm's param phone), 0 = none */    public static float $phoneAspect = 0.0;    /** The phone with WinZik original: the screen the dialog fills (view pixels); null: a dialog over the windows. @return array{0: float, 1: float}|null */    public function phoneScreen(): ?array    {        if (!isset($this->engine)) {            return null;        }        [$w, $h] = [(float)$this->width(), (float)$this->height()];        if (self::$phoneAspect > 0 && !(($this->scenes()->stack()[0] ?? null) instanceof MobileForm)) {            return [min($w, round($h * self::$phoneAspect)), $h];        }        return Platform::isPhoneApp() && MainForm::originalOnPhone($this->wz) ? [$w, $h] : null;    }    /** the phone with WinZik original (the dialog is a screen of it) */    protected function onPhone(): bool    {        return $this->phoneScreen() !== null;    }    /**     * The phone: these widgets are hidden (what only means something on a computer: the task bar, starting with it,     * yt-dlp to download, the joystick...).     */    protected function hideOnPhone(string ...$ids): void    {        if (!$this->onPhone()) {            return;        }        foreach ($ids as $id) {            $widget = $this->widget($id);            if ($widget !== null) {                $widget->props['visible'] = false;            }        }    }    /** on the phone (WinZik original) the whole screen, not a box over the windows */    public function isOverlay(): bool    {        return parent::isOverlay() && $this->phoneScreen() === null;    }    /** the width of a phone held upright, in dp (the mobile interface's too: MobileView) */    public const PHONE_DP = 392.0;    public function update(float $dt): void    {        $this->phoneZoom();        parent::update($dt);    }    /**     * The phone gives its screen in real pixels (1080 wide for ~400 dp) and says nothing of its density: the dialog     * would be drawn one pixel per dp, tiny. On the phone it is drawn as the phone's dp (its width / 392), the same     * dialog at the size of a finger; elsewhere the zoom of the form or of the options, as before.     */    private function phoneZoom(): void    {        $phone = $this->phoneScreen();        if ($phone === null) {            if ($this->phoneZoomed) {                unset($this->settings['zoom']);                $this->phoneZoomed = false;            }            return;        }        $zoom = (float)($this->settings['zoom'] ?? $this->engine->options->get('gui_zoom', 1.0));        $density = $this->theme->scale > 0 && $zoom > 0 ? $this->theme->scale / $zoom : 1.0;        $target = max(1.0, $phone[0] / self::PHONE_DP);        $this->settings['zoom'] = $target / max(0.5, $density);        $this->phoneZoomed = true;    }    private bool $phoneZoomed = false;    public function layoutTo(float $width, float $height): void    {        $phone = $this->phoneScreen();        if ($phone !== null) {            // the phone's narrow screen: what goes past its bottom is reached by sliding the dialog (the editors too)            if ($this->root !== null) {                $this->root->props['scroll'] = true;                // its margins narrower: the width of a phone for the texts of a dialog of the computer                $this->root->props['padding'] = min(8, (float)($this->root->props['padding'] ?? 16));            }            // what stands side by side goes to the next line when there is no room            foreach ($this->root?->walk() ?? [] as $widget) {                if ($widget instanceof \JumPHP\Gui\Container && ($widget->props['layout'] ?? '') === 'row' && !is_numeric($widget->props['height'] ?? null)) {                    $widget->props['wrap'] = true;                }            }        }        parent::layoutTo($phone !== null ? min($width, $phone[0]) : $width, $phone !== null ? min($height, $phone[1]) : $height);    }    protected function tick(float $dt): void    {        $this->wz->tick($dt);    }    public function handle(Pointer $p): void    {        // Échap in a text field (no menu of its own open): the dialog is cancelled, as from anywhere else        if ($p->pressed(Pointer::ESCAPE) && $this->focused() instanceof TextBox && $this->overlay === null) {            $this->setFocus(null);        }        if ($p->pressed(Pointer::ESCAPE) && $this->overlay === null && parent::isOverlay() && $this->phoneScreen() !== null && !$this->hasCancel()) {            $this->close();      // the phone's back button: a dialog shown as a screen closes too            return;        }        parent::handle($p);    }    /** Entrée in a text field (`events: {submit: onFieldSubmit}`): the default button of the dialog. */    public function onFieldSubmit(TextBox $sender): void    {        $this->pressDefault();    }    private function hasCancel(): bool    {        foreach ($this->root?->walk() ?? [] as $widget) {            if ($widget instanceof Button && !empty($widget->props['cancel']) && $widget->isEnabled() && $widget->isShown($this->breakpoint())) {                return true;            }        }        return false;    }    /** The button marked `default: true`, when it can be pressed now. */    protected function pressDefault(): void    {        foreach ($this->root?->walk() ?? [] as $widget) {            if ($widget instanceof Button && !empty($widget->props['default']) && $widget->isEnabled() && $widget->isShown($this->breakpoint())) {                $widget->emit('click');                return;            }        }    }}