Skip to main content

walkit/classic/
pl.rs

1// SPDX-License-Identifier: GPL-3.0-or-later
2//! The classic playlist editor's frame, pixel for pixel as Winamp 5.666 paints it: the title bar
3//! of corners, tiles and the title, the sides tiled down, and the bottom bar with its buttons,
4//! the list's background between them.
5
6use crate::classic::font;
7use crate::classic::listfont::{self, ListInk};
8use crate::classic::main::Painter;
9use crate::classic::marks::{MarkPlace, RowMark, RowMarks, MARK_GAP};
10use crate::classic::style::ClassicStyle;
11use crate::classic::touch::Control;
12use crate::draw::{Align, DrawCmd, Rect};
13use crate::gui::ObjId;
14use crate::player::Status;
15use crate::runtime::SkinRuntime;
16
17/// `seconds` as Winamp writes a playlist time: m:ss, or h:mm:ss from an hour.
18fn time_text(seconds: i32) -> String {
19    if seconds < 3600 {
20        format!("{}:{:02}", seconds / 60, seconds % 60)
21    } else {
22        format!(
23            "{}:{:02}:{:02}",
24            seconds / 3600,
25            (seconds / 60) % 60,
26            seconds % 60
27        )
28    }
29}
30
31/// `seconds` as a row or the shade bar writes it: m:ss, the minutes unbounded ("75:00").
32fn minutes_text(seconds: i32) -> String {
33    format!("{}:{:02}", seconds / 60, seconds % 60)
34}
35
36/// Entry `n` (1 the first) of `count` as the list numbers it, padded with spaces to the
37/// count's digits (" 1. " in a list of 10 or more).
38fn numbered(n: usize, count: usize, title: &str) -> String {
39    let width = count.max(1).to_string().len();
40    format!("{n:>width$}. {title}")
41}
42
43/// Where the first row starts (row `i` at 22 + row height x `i`).
44pub(crate) const ROW_TOP: i32 = 22;
45/// In the skin's letters: the room left of a row's number and right of its length, and the
46/// least between the title and the length, as Andamp's playlist (a phone's) sets them.
47const LIST_LEFT: i32 = 2;
48const LIST_RIGHT: i32 = 3;
49const LIST_GAP: i32 = 4;
50
51impl SkinRuntime {
52    /// The playlist's font as its rows draw in it, or none for the host's font.
53    fn classic_pl_font(face: &str) -> Option<crate::draw::FontDef> {
54        (!face.is_empty()).then(|| crate::draw::FontDef::new(&face.to_lowercase(), ""))
55    }
56
57    /// `text` as it fits in `width` px of the playlist's font: whole, or cut on a whole letter
58    /// with "..." after it.
59    fn classic_pl_fitted(
60        &mut self,
61        text: &str,
62        font: Option<&crate::draw::FontDef>,
63        width: i32,
64    ) -> String {
65        let size = self.classic_row_font();
66        let fits = |rt: &mut Self, s: &str| rt.host.text_width(s, font, size, false) <= width;
67        if fits(self, text) {
68            return text.to_string();
69        }
70        let chars: Vec<char> = text.chars().collect();
71        let cut = |n: usize| format!("{}...", chars[..n].iter().collect::<String>());
72        // the most letters that fit with the ellipsis
73        let (mut lo, mut hi) = (0, chars.len());
74        while lo < hi {
75            let mid = (lo + hi).div_ceil(2);
76            if fits(self, &cut(mid)) {
77                lo = mid;
78            } else {
79                hi = mid - 1;
80            }
81        }
82        cut(lo)
83    }
84
85    /// The playlist's shade bar (14 high): the left piece, tiles, a stretched
86    /// remainder, and the right piece with the bar's buttons; active y 42, inactive 57.
87    pub(crate) fn draw_classic_pl_shade(&mut self, o: ObjId, at: Rect, out: &mut Vec<DrawCmd>) {
88        let w = at.width;
89        let active = self
90            .layout_object(o)
91            .is_some_and(|l| self.has_focus(Some(l)));
92        let y = if active { 42 } else { 57 };
93        let mut p = Painter {
94            at,
95            out,
96            sheets: self.classic_sheets.clone(),
97        };
98        p.blit("pledit.bmp", 72, y, 25, 14, 0, 0);
99        let mut x = 25;
100        for _ in 1..(w - 50) / 25 {
101            p.blit("pledit.bmp", 72, 57, 25, 14, x, 0);
102            x += 25;
103        }
104        let rest = (w - 50) % 25;
105        if rest != 0 {
106            p.stretch("pledit.bmp", 72, 57, 24, 14, x, 0, rest, 14);
107            x += rest;
108        }
109        p.blit("pledit.bmp", 99, y, 50, 14, x, 0);
110        // the song's line in text.bmp cells (with the bitmap font): "n. TITLE" from
111        // x 4 at y 4, its length ending at w-29, blank cells between
112        let index = self.player.track_index();
113        let (title, length) = usize::try_from(index)
114            .ok()
115            .and_then(|i| self.player.tracks().into_iter().nth(i))
116            .map_or((String::new(), 0), |t| (t.title, t.length_ms / 1000));
117        if self.classic_style == ClassicStyle::Winamp291 {
118            let count = self.player.tracks().len();
119            let line = numbered(index as usize + 1, count, &title);
120            Self::shade_line_291(&mut p, w, &line, length);
121            return;
122        }
123        let time = minutes_text(length);
124        let time_cells = time.chars().count() as i32;
125        let room = (w - 29 - 4) / 5 - time_cells - 1;
126        let mut x = 4;
127        let line: Vec<char> = numbered(index as usize + 1, self.player.tracks().len(), &title)
128            .chars()
129            .collect();
130        let room = room.max(0) as usize;
131        for (i, &c) in line.iter().take(room).enumerate() {
132            // a title cut short ends on the ellipsis cell
133            let c = if i + 1 == room && line.len() > room {
134                '\u{1}'
135            } else {
136                c
137            };
138            let (cx, cy) = font::cell(c);
139            p.blit("text.bmp", cx, cy, 5, 6, x, 4);
140            x += 5;
141        }
142        // blank cells on the grid up to where the length starts: the first cell at or past
143        // w-29 less the length's cells (confirmed on the VM: the length sits on the grid)
144        let time_x = w - 29 - time_cells * 5;
145        while x < time_x {
146            let (cx, cy) = font::cell(' ');
147            p.blit("text.bmp", cx, cy, 5, 6, x, 4);
148            x += 5;
149        }
150        for c in time.chars() {
151            let (cx, cy) = font::cell(c);
152            p.blit("text.bmp", cx, cy, 5, 6, x, 4);
153            x += 5;
154        }
155        if x < w - 30 {
156            let (cx, cy) = font::cell(' ');
157            p.blit("text.bmp", cx, cy, (w - 30 - x).min(5), 6, x, 4);
158        }
159    }
160
161    /// 2.91's song line, in cells: `(w - 40) / 5` cells from x 5 less the
162    /// length's and one, a cut title ending on the ellipsis; when the title fits, blanks fill
163    /// those cells and one more; then a blank, and "m:ss" from `w - 29` less its cells.
164    fn shade_line_291(p: &mut Painter, w: i32, line: &str, secs: i32) {
165        let time = format!("{}:{:02}", secs / 60, secs % 60);
166        let time_cells = time.chars().count() as i32;
167        let cells = ((w - 40) / 5 - time_cells - 1).max(0) as usize;
168        let chars: Vec<char> = line.chars().collect();
169        let cell = |p: &mut Painter, c: char, x: i32| {
170            let (cx, cy) = font::cell(c);
171            p.blit("text.bmp", cx, cy, 5, 6, x, 4);
172        };
173        let mut x = 5;
174        for (i, &c) in chars.iter().take(cells).enumerate() {
175            let cut = i + 1 == cells && i + 1 < chars.len();
176            cell(p, if cut { '\u{1}' } else { c }, x);
177            x += 5;
178        }
179        if chars.len() <= cells {
180            for _ in chars.len()..=cells {
181                cell(p, ' ', x);
182                x += 5;
183            }
184        }
185        cell(p, ' ', x);
186        x = w - 29 - time_cells * 5;
187        for c in time.chars() {
188            cell(p, c, x);
189            x += 5;
190        }
191        if x < w - 30 {
192            let (cx, cy) = font::cell(' ');
193            p.blit("text.bmp", cx, cy, w - 30 - x, 6, x, 4);
194        }
195    }
196
197    /// The rows a playlist window `h` tall shows : `(h - 60) / row`.
198    pub(crate) fn classic_pl_rows(&self, h: i32) -> i32 {
199        ((h - 60) / self.classic_row()).max(0)
200    }
201
202    /// A playlist row's height: the font's full height as Windows lays it out, where the font
203    /// file says it for the size, else Arial's 5/4 of the em (15 at Winamp's 12, where 3 rows
204    /// fill a playlist 116 tall), and what the host adds (`Presentation::list_row_extra`).
205    pub(crate) fn classic_row(&self) -> i32 {
206        self.classic_row_height
207            .unwrap_or((self.classic_list_font * 5 + 2) / 4)
208            + self.list_row_extra
209    }
210
211    /// Reads the playlist font's height at its size from its file, for [`Self::classic_row`].
212    pub(crate) fn measure_classic_row(&mut self) {
213        let face = self.classic_pledit.font.clone();
214        let font = Self::classic_pl_font(&face);
215        self.classic_row_height = self.gdi_measured_height(font.as_ref(), self.classic_list_font);
216    }
217
218    /// The playlist font's size as the host reads it, in points: 4/3 of the em in pixels.
219    pub(crate) fn classic_row_font(&self) -> i32 {
220        self.classic_list_font * 4 / 3
221    }
222
223    /// Sets the playlist font's size, pixels to the em, as Winamp's "playlist font size"
224    /// preference does (`pe_fontsize`, the em in pixels); its rows grow and shrink with it.
225    pub fn set_classic_list_font_size(&mut self, px: i32) {
226        let px = px.clamp(6, 40);
227        if px != self.classic_list_font {
228            self.classic_list_font = px;
229            if self.classic_skin {
230                self.measure_classic_row();
231            }
232            self.repaint();
233        }
234    }
235
236    /// The first row shown, clamped to the rows that fit under the list's end.
237    pub(crate) fn classic_pl_scroll(&self, h: i32) -> i32 {
238        let past = self.player.tracks().len() as i32 - self.classic_pl_rows(h);
239        self.classic_pl_scroll.clamp(0, past.max(0))
240    }
241
242    /// How far down the track the thumb sits for `scroll` of `count` rows: the track is
243    /// `h - 76` long past the thumb, shared out over the rows past the window.
244    pub(crate) fn classic_pl_thumb(&self, h: i32, count: i32, scroll: i32) -> i32 {
245        let past = count - self.classic_pl_rows(h);
246        if past <= 0 {
247            return 0;
248        }
249        ((h - 76) * scroll.min(past) / past).max(0)
250    }
251
252    /// The ink of the skin's `text.bmp` for the playlist's rows, read from the host once.
253    fn classic_list_ink(&mut self) -> Option<std::rc::Rc<ListInk>> {
254        if !self.classic_list_ink_read {
255            self.classic_list_ink_read = true;
256            let sheet = self.classic_sheet("text.bmp");
257            self.classic_list_ink = self
258                .image_size(&sheet)
259                .and_then(|(w, h)| {
260                    let px = self.host.pixels(&sheet, 0, 0, w, h)?;
261                    ListInk::of(&px, w, h)
262                })
263                .map(std::rc::Rc::new);
264        }
265        self.classic_list_ink.clone()
266    }
267
268    /// One row in the skin's letters, its letters' tops at `top` of `block` (`width` wide):
269    /// "n. " and the title from the left, the length against the right, a title too long for
270    /// the room between ending on the ellipsis, and the row's `mark` in a cell `cell` wide where
271    /// `place` puts it.
272    #[allow(clippy::too_many_arguments)]
273    fn letter_row(
274        ink: &ListInk,
275        block: &mut [u32],
276        width: i32,
277        top: i32,
278        number: usize,
279        title: &str,
280        time: &str,
281        rgb: u32,
282        mark: Option<&RowMark>,
283        cell: i32,
284        place: MarkPlace,
285    ) {
286        let number = format!("{number}. ");
287        ink.write(&number, (LIST_LEFT, top), rgb, block, width, width);
288        let mut left = LIST_LEFT + number.chars().count() as i32 * listfont::CELL_W;
289        let time_x = width - LIST_RIGHT - time.chars().count() as i32 * listfont::CELL_W;
290        ink.write(time, (time_x, top), rgb, block, width, width);
291        let mut right = time_x - LIST_GAP;
292        if cell > 0 {
293            let x = if place == MarkPlace::Before {
294                left += cell + MARK_GAP;
295                left - cell - MARK_GAP
296            } else {
297                right -= cell + MARK_GAP;
298                right + MARK_GAP
299            };
300            if let Some(m) = mark {
301                Self::letter_mark(ink, block, width, (x, top), cell, m, rgb);
302            }
303        }
304        let room = (right - left) / listfont::CELL_W;
305        ink.write(
306            &listfont::fitted(title, room),
307            (left, top),
308            rgb,
309            block,
310            width,
311            right.max(left),
312        );
313    }
314
315    /// `mark` into `block`, centered in a cell `cell` wide from `x`, on letters whose tops are
316    /// at `top`: its picture a pixel to the pixel, or its text in the skin's letters.
317    fn letter_mark(
318        ink: &ListInk,
319        block: &mut [u32],
320        width: i32,
321        (x, top): (i32, i32),
322        cell: i32,
323        mark: &RowMark,
324        rgb: u32,
325    ) {
326        let w = mark.pixel_width();
327        if w == 0 {
328            let tw = mark.text.chars().count() as i32 * listfont::CELL_W;
329            ink.write(
330                &mark.text,
331                (x + (cell - tw) / 2, top),
332                rgb,
333                block,
334                width,
335                x + cell,
336            );
337            return;
338        }
339        let rows = block.len() as i32 / width.max(1);
340        let x0 = x + (cell - w) / 2;
341        // centered on the letters' ink, which leaves a cell's last row empty
342        let y0 = top + (listfont::CELL_H - 1 - mark.pixel_height()) / 2;
343        let picture = mark.picture(0xff00_0000 | (rgb & 0xff_ffff));
344        for py in 0..mark.pixel_height() {
345            for px in 0..w {
346                let (dx, dy) = (x0 + px, y0 + py);
347                let a = picture[(py * w + px) as usize];
348                if a != 0 && (0..width).contains(&dx) && (0..rows).contains(&dy) {
349                    block[(dy * width + dx) as usize] = a;
350                }
351            }
352        }
353    }
354
355    /// `mark` in the outline font's row at `x`, `y` of the window, centered in a cell `cell`
356    /// wide and `row` tall: its picture a pixel to the skin's pixel, or its text in that font.
357    fn outline_mark(
358        p: &mut Painter,
359        (x, y): (i32, i32),
360        (cell, row): (i32, i32),
361        mark: &RowMark,
362        color: crate::draw::Rgb,
363        face: Option<&String>,
364        size: i32,
365    ) {
366        let (w, h) = (mark.pixel_width(), mark.pixel_height());
367        if w == 0 {
368            p.text(
369                &mark.text,
370                face,
371                size,
372                (x, y, cell, row),
373                color,
374                Align::Center,
375            );
376            return;
377        }
378        let rect = Rect::new(
379            p.at.left + x + (cell - w) / 2,
380            p.at.top + y + (row - h) / 2,
381            w,
382            h,
383        );
384        p.out.push(DrawCmd::Pixels {
385            rect,
386            w,
387            h,
388            argb: std::rc::Rc::from(
389                mark.picture(0xff00_0000 | (color.0 & 0xff_ffff))
390                    .into_boxed_slice(),
391            ),
392            alpha: 255,
393            stable: false,
394        });
395    }
396
397    /// How wide a mark's cell is: the widest of the marks set, as `text_w` measures a mark's text.
398    fn mark_cell(marks: &RowMarks, mut text_w: impl FnMut(&str) -> i32) -> i32 {
399        marks
400            .marks
401            .iter()
402            .map(|m| match m.pixel_width() {
403                0 => text_w(&m.text),
404                w => w,
405            })
406            .max()
407            .unwrap_or(0)
408    }
409
410    pub(crate) fn draw_classic_pl(&mut self, o: ObjId, at: Rect, out: &mut Vec<DrawCmd>) {
411        let (w, h) = (at.width, at.height);
412        let active = self
413            .layout_object(o)
414            .is_some_and(|l| self.has_focus(Some(l)));
415        let style = self.classic_pledit.clone();
416        let tracks = self.player.tracks();
417        let current = self.player.track_index();
418        let mut p = Painter {
419            at,
420            out,
421            sheets: self.classic_sheets.clone(),
422        };
423        // the list's background, under the frame, and the rows on it: "n. Title" with the
424        // length at the right, the current song in the current color
425        p.fill(12, 20, w - 32, h - 58, style.normal_bg);
426        let rows = self.classic_pl_rows(h);
427        let row = self.classic_row();
428        let font_size = self.classic_row_font();
429        // in the skin's letters, the rows go into one block of pixels over the list
430        let ink = if self.skin_list_font {
431            self.classic_list_ink()
432        } else {
433            None
434        };
435        let block_w = w - 32;
436        let mut block = ink
437            .as_ref()
438            .map(|_| vec![0u32; (block_w.max(0) * rows.max(0) * row) as usize]);
439        let mut lettered = false;
440        let marks = self.classic_row_marks.clone();
441        let face = Some(&style.font);
442        let def = Self::classic_pl_font(&style.font);
443        // a mark's cell in the skin's letters, and in the outline font
444        let letter_cell = Self::mark_cell(&marks, |t| t.chars().count() as i32 * listfont::CELL_W);
445        let outline_cell = Self::mark_cell(&marks, |t| {
446            self.host.text_width(t, def.as_ref(), font_size, false)
447        });
448        let selected = self.classic_selected_row;
449        let scroll = self.classic_pl_scroll(h).max(0) as usize;
450        for (i, t) in tracks
451            .iter()
452            .enumerate()
453            .skip(scroll)
454            .take(rows.max(0) as usize)
455        {
456            let y = ROW_TOP + (i - scroll) as i32 * row;
457            if selected == Some(i as i32) || self.classic_selected_more.contains(&(i as i32)) {
458                p.fill(12, y, w - 32, row, style.selected_bg);
459            }
460            let color = if i as i32 == current {
461                style.current
462            } else {
463                style.normal
464            };
465            let time = time_text(t.length_ms / 1000);
466            if let (Some(ink), Some(block)) = (ink.as_deref(), block.as_mut()) {
467                if listfont::covers(&t.title) {
468                    let top = (i - scroll) as i32 * row + (row - listfont::CELL_H) / 2;
469                    Self::letter_row(
470                        ink,
471                        block,
472                        block_w,
473                        top,
474                        i + 1,
475                        &t.title,
476                        &time,
477                        color.0,
478                        marks.of(i),
479                        letter_cell,
480                        marks.place,
481                    );
482                    lettered = true;
483                    continue;
484                }
485            }
486            // " m:ss " right-aligned at the row's end, and the title up to it, cut short on "..."
487            let time = format!(" {} ", minutes_text(t.length_ms / 1000));
488            let time_w = self.host.text_width(&time, def.as_ref(), font_size, false);
489            let mut left = 0;
490            let mut right = block_w - time_w;
491            let line = if outline_cell > 0 {
492                let number = numbered(i + 1, tracks.len(), "");
493                let x = if marks.place == MarkPlace::Before {
494                    p.text(
495                        &number,
496                        face,
497                        font_size,
498                        (12, y, w - 32, row),
499                        color,
500                        Align::Left,
501                    );
502                    let at = self
503                        .host
504                        .text_width(&number, def.as_ref(), font_size, false);
505                    left = at + outline_cell + MARK_GAP;
506                    at
507                } else {
508                    right -= outline_cell + MARK_GAP;
509                    right + MARK_GAP
510                };
511                if let Some(m) = marks.of(i) {
512                    Self::outline_mark(
513                        &mut p,
514                        (12 + x, y),
515                        (outline_cell, row),
516                        m,
517                        color,
518                        face,
519                        font_size,
520                    );
521                }
522                if marks.place == MarkPlace::Before {
523                    t.title.clone()
524                } else {
525                    numbered(i + 1, tracks.len(), &t.title)
526                }
527            } else {
528                numbered(i + 1, tracks.len(), &t.title)
529            };
530            let title = self.classic_pl_fitted(&line, def.as_ref(), right - left);
531            p.text(
532                &title,
533                face,
534                font_size,
535                if outline_cell > 0 {
536                    (12 + left, y, right - left, row)
537                } else {
538                    (12, y, w - 32, row)
539                },
540                color,
541                Align::Left,
542            );
543            p.text(
544                &time,
545                face,
546                font_size,
547                (12, y, w - 32, row),
548                color,
549                Align::Right,
550            );
551        }
552        if let Some(block) = block.filter(|_| lettered) {
553            p.out.push(DrawCmd::Pixels {
554                rect: Rect::new(at.left + 12, at.top + ROW_TOP, block_w, rows * row),
555                w: block_w,
556                h: rows * row,
557                argb: std::rc::Rc::from(block.into_boxed_slice()),
558                alpha: 255,
559                stable: false,
560            });
561        }
562        // the title bar: the left corner, tiles either side of the title, the right corner
563        let y1 = if active { 0 } else { 21 };
564        p.blit("pledit.bmp", 0, y1, 25, 20, 0, 0);
565        let tiles = (w - 150) / 25;
566        let mut x = 25;
567        if tiles != 0 {
568            if tiles % 2 != 0 {
569                p.blit("pledit.bmp", 127, y1, 12, 20, x, 0);
570                x += 12;
571            }
572            for _ in 0..tiles / 2 {
573                p.blit("pledit.bmp", 127, y1, 25, 20, x, 0);
574                x += 25;
575            }
576        }
577        p.blit("pledit.bmp", 26, y1, 100, 20, x, 0);
578        x += 100;
579        if tiles != 0 {
580            if tiles % 2 != 0 {
581                p.blit("pledit.bmp", 127, y1, 13, 20, x, 0);
582                x += 13;
583            }
584            for _ in 0..tiles / 2 {
585                p.blit("pledit.bmp", 127, y1, 25, 20, x, 0);
586                x += 25;
587            }
588        }
589        let rest = (w - 150) % 25;
590        if rest != 0 {
591            p.stretch("pledit.bmp", 127, y1, 25, 20, x, 0, rest, 20);
592            x += rest;
593        }
594        p.blit("pledit.bmp", 153, y1, 25, 20, x, 0);
595        // the sides, 29 rows at a time, the last run stretched, the scrollbar's track between
596        // the right side's two pieces
597        let mut y = 20;
598        for _ in 0..(h - 58) / 29 {
599            p.blit("pledit.bmp", 0, 42, 12, 29, 0, y);
600            p.blit("pledit.bmp", 31, 42, 5, 29, w - 20, y);
601            p.blit("pledit.bmp", 44, 42, 7, 29, w - 7, y);
602            p.blit("pledit.bmp", 36, 42, 8, 29, w - 15, y);
603            y += 29;
604        }
605        let rest = (h - 58) % 29;
606        if rest != 0 {
607            p.stretch("pledit.bmp", 0, 42, 12, 29, 0, y, 12, rest);
608            p.stretch("pledit.bmp", 31, 42, 5, 29, w - 20, y, 5, rest);
609            p.stretch("pledit.bmp", 44, 42, 7, 29, w - 7, y, 7, rest);
610            p.blit("pledit.bmp", 36, 42, 8, rest, w - 15, y);
611            y += rest;
612        }
613        // the scrollbar's thumb: down the track by the scroll's share of the
614        // rows past the window, pressed while a finger holds it
615        let thumb = self.classic_pl_thumb(h, tracks.len() as i32, scroll as i32);
616        let held = self
617            .classic_press_on(o)
618            .is_some_and(|p| p.control == Control::PlScroll);
619        p.blit(
620            "pledit.bmp",
621            if held { 61 } else { 52 },
622            53,
623            8,
624            18,
625            w - 15,
626            20 + thumb,
627        );
628        // the bottom: the left part with its buttons, tiles, the visualizer part when there is
629        // room for it, and the right part
630        p.blit("pledit.bmp", 0, 72, 125, 38, 0, y);
631        let mut tiles = (w - 275) / 25;
632        let vis = tiles > 2;
633        if vis {
634            tiles -= 3;
635        }
636        let mut x = 125;
637        for _ in 0..tiles {
638            p.blit("pledit.bmp", 179, 0, 25, 38, x, y);
639            x += 25;
640        }
641        let rest = (w - 275) % 25;
642        if rest != 0 {
643            p.stretch("pledit.bmp", 179, 0, 25, 38, x, y, rest, 38);
644            x += rest;
645        }
646        if vis {
647            p.blit("pledit.bmp", 205, 0, 75, 38, x, y);
648            x += 75;
649        }
650        p.blit("pledit.bmp", 126, 72, 150, 38, x, y);
651        // a bottom button held, or its menu a tap left open: the pictures stacked over it, the one under the finger
652        // from the pressed column, the bar beside them
653        let held = self
654            .classic_press_on(o)
655            .and_then(|p| match p.control {
656                Control::PlButton(b) => Some((b, p.hover)),
657                _ => None,
658            })
659            .or(self
660                .classic_open_menu
661                .filter(|(menu_on, _)| *menu_on == o)
662                .map(|(_, b)| (b, None)));
663        if let Some((b, hover)) = held {
664            let left = b.menu_left(w);
665            let items = b.items();
666            let top = h - 12 - 18 * items;
667            p.blit("pledit.bmp", b.bar_source(), 111, 3, 18 * items, left, top);
668            for i in 0..items {
669                let sy = match i {
670                    3 => 168,
671                    2 => 111,
672                    1 => 130,
673                    _ => 149,
674                };
675                let sx = b.picture_source() + if hover == Some(i) { 23 } else { 0 };
676                p.blit("pledit.bmp", sx, sy, 22, 18, left + 3, h - 30 - 18 * i);
677            }
678        }
679        // the bottom bar's two lines in text.bmp cells: the
680        // selected songs' time over the list's, 18 cells from (w-143, h-28); and the song's
681        // time at (w-86, h-15), blank while nothing plays
682        let total: i32 = tracks.iter().map(|t| t.length_ms / 1000).sum();
683        let picked: i32 = selected
684            .and_then(|i| tracks.get(i.max(0) as usize))
685            .map_or(0, |t| t.length_ms / 1000);
686        let line = format!("{}/{}", time_text(picked), time_text(total));
687        let mut x = w - 143;
688        for c in line.chars().chain(std::iter::repeat(' ')).take(18) {
689            let (cx, cy) = font::cell(c);
690            p.blit("text.bmp", cx, cy, 5, 6, x, h - 28);
691            x += 5;
692        }
693        let y = h - 15;
694        if self.player.status() == Status::Stopped {
695            p.blit("text.bmp", 142, 0, 4, 6, w - 86, y);
696            for x in [w - 82, w - 77, w - 72, w - 64, w - 59] {
697                p.blit("text.bmp", 142, 0, 5, 6, x, y);
698            }
699        } else {
700            // the time played, or the time left with a minus before it (the minus drawn as 3
701            // columns of text.bmp's "-" where the blank goes)
702            let length = self.player.length_ms();
703            let remaining = self.classic_remaining && length > 0;
704            let ms = if remaining {
705                (length - self.player.position_ms()).max(0)
706            } else {
707                self.player.position_ms().max(0)
708            };
709            let seconds = ms / 1000;
710            let (minutes, seconds) = (seconds / 60, seconds % 60);
711            let digit =
712                |p: &mut Painter, n: i32, x: i32| p.blit("text.bmp", (n % 10) * 5, 6, 5, 6, x, y);
713            let (sx, sy) = if remaining { (75, 6) } else { (142, 0) };
714            if minutes / 100 == 0 {
715                p.blit("text.bmp", 142, 0, 4, 6, w - 86, y);
716                p.blit("text.bmp", sx, sy, 3, 6, w - 82, y);
717            } else {
718                p.blit("text.bmp", sx, sy, 3, 6, w - 86, y);
719                digit(&mut p, minutes / 100, w - 82);
720            }
721            digit(&mut p, minutes / 10, w - 77);
722            digit(&mut p, minutes, w - 72);
723            digit(&mut p, seconds / 10, w - 64);
724            digit(&mut p, seconds, w - 59);
725        }
726    }
727}
728
729#[cfg(test)]
730mod tests {
731    use crate::classic::main::Painter;
732    use crate::draw::{DrawCmd, Rect};
733    use crate::runtime::SkinRuntime;
734
735    #[test]
736    fn the_2_91_shade_line_starts_at_5_and_leaves_a_column_before_the_length() {
737        let mut out = Vec::new();
738        let mut p = Painter {
739            at: Rect::new(0, 0, 275, 14),
740            out: &mut out,
741            sheets: std::rc::Rc::default(),
742        };
743        SkinRuntime::shade_line_291(&mut p, 275, "1. Andamp", 600);
744        let xs: Vec<i32> = out
745            .iter()
746            .filter_map(|c| match c {
747                DrawCmd::Image { dst, .. } => Some(dst.left),
748                _ => None,
749            })
750            .collect();
751        // 41 cells of title and blanks, one more blank, then "10:00" from 275 - 29 - 25
752        let mut want: Vec<i32> = (0..43).map(|i| 5 + 5 * i).collect();
753        want.extend([221, 226, 231, 236, 241]);
754        assert_eq!(xs, want);
755    }
756
757    #[test]
758    fn a_cut_2_91_shade_line_ends_on_the_ellipsis() {
759        let mut out = Vec::new();
760        let mut p = Painter {
761            at: Rect::new(0, 0, 275, 14),
762            out: &mut out,
763            sheets: std::rc::Rc::default(),
764        };
765        SkinRuntime::shade_line_291(&mut p, 275, &"x".repeat(60), 61);
766        let cells: Vec<(i32, i32)> = out
767            .iter()
768            .filter_map(|c| match c {
769                DrawCmd::Image { bitmap, dst, .. } => Some((dst.left, bitmap.x)),
770                _ => None,
771            })
772            .collect();
773        // 47 cells less "1:01" and one leave 42: the last of them is the ellipsis
774        assert_eq!(cells[41], (210, crate::classic::font::cell('\u{1}').0));
775        assert_eq!(cells[42].0, 215, "the blank after it");
776        assert_eq!(cells[43].0, 275 - 29 - 20, "the length");
777    }
778}