ES
apps/caramail-chat / src / Gui / AwtText.php · commit 6eebcdb
<?phpnamespace App\Gui;use JumPHP\Gui\TextBox;use JumPHP\Gui\Theme;use JumPHP\Renderer\Renderer;/** java.awt.TextField: white, in a sunken bevel (the engine's text box does the typing, its frame is drawn over). */final class AwtText extends TextBox{    private float $time = 0;    public function update(float $dt): void    {        parent::update($dt);        $this->time += $dt;    }    protected function content(Theme $t, float $maxWidth, string $breakpoint): array    {        [$w] = parent::content($t, $maxWidth, $breakpoint);        return [$w, $t->px(22)];    }    public function render(Renderer $r, Theme $t): void    {        // the engine's own caret (a short bar in its taller fields) would land on the first letter here: kept hidden,        // this field draws the caret of Windows itself (below)        \Closure::bind(function (): void {            $this->blink = 0.9;        }, $this, TextBox::class)();        parent::render($r, $t);        // the engine's focus halo and rounded frame are covered by the bevel of Windows        $px = Awt::px($t);        $r->fillRect($this->x - $t->px(3), $this->y - $t->px(3), $this->w + $t->px(6), $t->px(3), Awt::$face);        $r->fillRect($this->x - $t->px(3), $this->y + $this->h, $this->w + $t->px(6), $t->px(3), Awt::$face);        $r->fillRect($this->x - $t->px(3), $this->y, $t->px(3), $this->h, Awt::$face);        $r->fillRect($this->x + $this->w, $this->y, $t->px(3), $this->h, Awt::$face);        Awt::sunken($r, $this->x, $this->y, $this->w, $this->h, $px);        // the caret of Windows: one black line the height of the text, blinking every half second (the engine's is        // made for its own, taller fields)        if ($this->focused && $this->caret !== null && fmod($this->time, 1.06) < 0.53) {            $font = $t->font();            // one pixel before the letter that follows it, never over it            $r->fillRect($this->caret[0] - $px, $this->y + ($this->h - $font->height()) / 2, $px, $font->height(), '#000000');        }    }}