IT
apps/winzik / src / Gui / CardWindow.php · commit 6576d6f
<?phpnamespace App\Gui;use App\Model\Text;/** * The card of a line of the play list: the pointer still on it for 2 seconds, it shows near the pointer the cover of * its album (or the artist's picture) and what is known of the song: title, artist, album, release date, genre, * length, bitrate. It goes as soon as the pointer moves. Dressed with the skin's colours. */final class CardWindow extends SkinWindow{    public const WIDTH = 250;    public const HEIGHT = 104;    /** the line shown (an index of the play list), -1: none */    public int $index = -1;    public function id(): string    {        return 'card';    }    public function width(): float    {        return self::WIDTH;    }    public function height(): float    {        return self::HEIGHT;    }    public function visible(): bool    {        return $this->index >= 0 && isset($this->wz->playlist->items[$this->index]);    }    /** Shows the card of a line near a point (desktop pixels), on the screen. */    public function showAt(int $index, float $dx, float $dy): void    {        $this->index = $index;        [$mx, $my, $mw, $mh] = $this->desk->monitorAt($dx, $dy);        $this->x = min($dx + 14, $mx + $mw - self::WIDTH - 2);        $this->y = $dy + 16 + self::HEIGHT > $my + $mh ? $dy - self::HEIGHT - 8 : $dy + 16;    }    public function hide(): void    {        $this->index = -1;    }    public function render(Canvas $c): void    {        $file = $this->wz->playlist->items[$this->index] ?? null;        if ($file === null) {            return;        }        $skin = $this->wz->skin;        $pal = $skin->palette();        $colors = $skin->colors;        $c->fill(0, 0, self::WIDTH, self::HEIGHT, $pal['list']);        $c->frame(0, 0, self::WIDTH, self::HEIGHT, $colors['barGreenLeft']);        [$tag, $info] = $this->wz->describeLater($file);        $path = $this->wz->path($file);        $cover = $this->wz->covers()->picture($path, $tag);        if ($cover === null || !$c->file($cover, 6, 6, 92, 92)) {            // no picture (yet): a note on the skin's face            $c->fill(6, 6, 92, 92, $pal['face']);            // a note, drawn (the fonts have no ♪)            $c->fill(58, 28, 3, 38, $pal['ink']);            $c->fill(58, 28, 14, 4, $pal['ink']);            $c->fill(68, 28, 4, 12, $pal['ink']);            $c->fill(46, 60, 14, 10, $pal['ink']);        }        $more = $this->wz->covers()->info($path);        $name = Text::baseName(Text::withoutExtension($file));        $title = $tag->title !== '' ? $tag->title : $name;        $artist = $tag->artist !== '' ? $tag->artist : (string)($more['artist'] ?? '');        $album = $tag->album !== '' ? $tag->album : (string)($more['album'] ?? '');        $date = (string)($more['date'] ?? '') ?: $tag->year;        $s = (int)round($info->duration);        $stats = $this->wz->stats();        $rating = $stats->rating($path);        $plays = $stats->plays($path);        $mark = ($rating > 0 ? 'Note : ' . $rating . '/5  -  ' : '') . ($plays > 0 ? 'écouté ' . $plays . ' fois  -  note d\'écoute ' . $stats->score($path) : 'jamais écouté en entier');        $lines = [            [$title, 9, $colors['active']],            [$artist, 8, $colors['title']],            [$album !== '' ? 'Album : ' . $album : '', 7.5, $colors['volume']],            [$date !== '' ? 'Sortie : ' . $date : '', 7.5, $colors['volume']],            [$tag->present && $tag->genre !== \App\Model\Id3Tag::OTHER ? 'Genre : ' . $tag->genreName() : '', 7.5, $colors['volume']],            [$mark, 7, '#ffd040'],            [trim(($s > 0 ? sprintf('%d:%02d', intdiv($s, 60), $s % 60) : '') . ($info->kbps > 0 ? '  -  ' . $info->kbps . ' kbps' : '') . '  -  ' . strtoupper(Text::extension($file)), ' -'), 7, '#a0a0b0'],        ];        $y = 6;        foreach ($lines as [$text, $size, $color]) {            if ($text === '') {                continue;            }            $c->text($c->fit($text, self::WIDTH - 112, $size), 106, $y, $size, $color);            $y += $size + 5;        }    }}