JA
apps/caramail-chat / src / Gui / AwtCheck.php · commit 6eebcdb
<?phpnamespace App\Gui;use JumPHP\Gui\CheckBox;use JumPHP\Gui\Theme;use JumPHP\Renderer\Renderer;/** java.awt.Checkbox: a white 13 px square in a sunken bevel, a black tick. `background` behind it (the applet used a lighter grey). */final class AwtCheck extends CheckBox{    public const DEFAULTS = ['text' => 'Option', 'checked' => false, 'background' => ''];    protected function content(Theme $t, float $maxWidth, string $breakpoint): array    {        return [$t->px(13 + 6) + $t->font(12)->width($this->tr((string)$this->props['text'])) + $t->px(4), $t->px(20)];    }    public function render(Renderer $r, Theme $t): void    {        if ($this->props['background'] !== '') {            $r->fillRect($this->x, $this->y, $this->w, $this->h, (string)$this->props['background']);        }        self::box($r, $t, $this->x + $t->px(2), $this->y + ($this->h - $t->px(13)) / 2, (bool)$this->props['checked'], $this->isEnabled());        $font = $t->font(12);        $tx = $this->x + $t->px(13 + 6);        $ty = $this->y + ($this->h - $font->height()) / 2;        $text = $this->tr((string)$this->props['text']);        $this->isEnabled() ? $font->draw($r, $text, $tx, $ty, 1, '#000000') : Awt::disabledText($r, $t, $font, $text, $tx, $ty);        if ($this->focused) {            $r->drawRect($tx - $t->px(2), $ty - $t->px(1), $font->width($text) + $t->px(4), $font->height() + $t->px(2), Awt::SHADOW);        }    }    public static function box(Renderer $r, Theme $t, float $x, float $y, bool $on, bool $enabled): void    {        $s = $t->px(13);        $px = Awt::px($t);        $r->fillRect($x, $y, $s, $s, $enabled ? '#ffffff' : Awt::$face);        Awt::sunken($r, $x, $y, $s, $s, $px);        if ($on) {            // the tick of Windows: 7 columns of 3 pixels, going down then up            $c = $enabled ? '#000000' : Awt::SHADOW;            foreach ([4, 5, 6, 5, 4, 3, 2] as $i => $top) {                $r->fillRect($x + $t->px(3 + $i), $y + $t->px($top), $px, $t->px(3), $c);            }        }    }}