"""Screen layout, drawn with cairo and Pango in panel pixels. Sizes are absolute pixels, not points. Gray edges smear on the panel at 2 bpp, so text is drawn without antialiasing and with full hinting, in fonts whose hinting holds up in plain black and white at 9-11 px: Verdana and Andale Mono (Microsoft core fonts), with Liberation as the fallback. """ import cairo import gi gi.require_version("Pango", "1.0") gi.require_version("PangoCairo", "1.0") from gi.repository import Pango, PangoCairo # noqa: E402 from . import state as st # noqa: E402 FONT_TITLE = "DejaVu Sans 11px" FONT_STATUS = "DejaVu Sans Mono 10px" FONT_SMALL = "Verdana, Liberation Sans 9px" FONT_LOG = "Andale Mono, Liberation Mono 9px" TITLE_Y = 0 TITLE_LINES = 3 STATUS_Y = 40 ICON_SIZE = 12 BAR_Y = 54 OPTIONS_Y = 60 LINK_Y = 71 LOG_Y = 85 LOG_LINE_H = 10 # 9 px text: room for Polish accents TEXT_ANTIALIAS = cairo.ANTIALIAS_NONE # playlist screen: a row is the text plus a line above and below for the # selection marker FONT_LIST = FONT_SMALL LIST_ROW_H = 12 LIST_TEXT_DY = 1 LIST_SEP = " - " def gray(level): """Source color for panel level 0 (background) .. 3 (black), at 2 bpp""" v = 1 - level / 3.0 return v, v, v _fonts = {} def _font(desc): d = _fonts.get(desc) if d is None: d = _fonts[desc] = Pango.FontDescription.from_string(desc) return d def _layout(cr, font): layout = PangoCairo.create_layout(cr) fo = cairo.FontOptions() fo.set_antialias(TEXT_ANTIALIAS) fo.set_hint_style(cairo.HINT_STYLE_FULL) fo.set_hint_metrics(cairo.HINT_METRICS_ON) PangoCairo.context_set_font_options(layout.get_context(), fo) layout.context_changed() layout.set_font_description(_font(font)) return layout def _text(cr, font, x, y, w, align, text): layout = _layout(cr, font) layout.set_width(w * Pango.SCALE) layout.set_alignment(align) layout.set_ellipsize(Pango.EllipsizeMode.END) layout.set_text(text, -1) cr.move_to(x, y) PangoCairo.show_layout(cr, layout) def _title(cr, w, text): layout = _layout(cr, FONT_TITLE) layout.set_width(w * Pango.SCALE) layout.set_wrap(Pango.WrapMode.WORD_CHAR) layout.set_ellipsize(Pango.EllipsizeMode.END) layout.set_height(-TITLE_LINES) layout.set_text(text, -1) cr.move_to(1, TITLE_Y) PangoCairo.show_layout(cr, layout) def _icon(cr, state, x, y): s = ICON_SIZE if state == st.PLAY_PLAYING: cr.move_to(x + 2, y) cr.line_to(x + s, y + s / 2.0) cr.line_to(x + 2, y + s) cr.close_path() cr.fill() elif state == st.PLAY_PAUSED: cr.rectangle(x + 2, y, 3, s) cr.rectangle(x + 7, y, 3, s) cr.fill() elif state == st.PLAY_STOPPED: cr.rectangle(x + 1, y + 1, s - 2, s - 2) cr.fill() def format_time(ms): t = ms // 1000 if t >= 3600: return "%d:%02d:%02d" % (t // 3600, t // 60 % 60, t % 60) return "%d:%02d" % (t // 60, t % 60) def _status(cr, s, w): _icon(cr, s.state, 2, STATUS_Y) if s.state == st.PLAY_STOPPED or s.song_pos < 0: return _text(cr, FONT_STATUS, 20, STATUS_Y, 90, Pango.Alignment.LEFT, "#%d/%d" % (s.song_pos + 1, s.queue_len)) elapsed = s.current_elapsed_ms() text = format_time(elapsed) if s.total_ms: text += "/" + format_time(s.total_ms) _text(cr, FONT_STATUS, 110, STATUS_Y, w - 112, Pango.Alignment.RIGHT, text) if s.total_ms: bar_w = w - 22 fill = elapsed * bar_w // s.total_ms cr.set_line_width(1) cr.rectangle(20.5, BAR_Y + 0.5, bar_w - 1, 3) cr.stroke() cr.rectangle(20, BAR_Y, fill, 4) cr.fill() def _options(cr, s, w): if not s.connected: return text = "Włączone:" + (" repeat" if s.repeat else "") + (" random" if s.random else "") _text(cr, FONT_SMALL, 1, OPTIONS_Y, 170, Pango.Alignment.LEFT, text) text = "Głośność %d%%" % s.volume if s.volume >= 0 else "Głośność –" _text(cr, FONT_SMALL, 150, OPTIONS_Y, w - 151, Pango.Alignment.RIGHT, text) def _link(cr, s, w): if s.connected: return cr.rectangle(0, LINK_Y, w, 12) cr.fill() cr.set_source_rgb(1, 1, 1) _text(cr, FONT_SMALL, 2, LINK_Y, w - 4, Pango.Alignment.LEFT, "Brak połączenia z %s" % s.server) cr.set_source_rgb(0, 0, 0) def _log(cr, s, w, h): cr.set_line_width(1) cr.move_to(0, LOG_Y - 1.5) cr.line_to(w, LOG_Y - 1.5) cr.stroke() for i, row in enumerate(s.log): y = LOG_Y + i * LOG_LINE_H if y + LOG_LINE_H > h: break if row: _text(cr, FONT_LOG, 1, y - 1, w - 1, Pango.Alignment.LEFT, row) def list_rows(height): return height // LIST_ROW_H def list_first(sel, count, rows): """First row shown: the selection centered, without empty rows at the end""" return max(0, min(sel - rows // 2, count - rows)) def _measure(cr, text): layout = _layout(cr, FONT_LIST) layout.set_text(text, -1) return layout.get_pixel_size()[0] def _fit(cr, text, w): """text, or its longest prefix plus an ellipsis that fits in w pixels. Not Pango's ellipsizing: that pads the line out to the full width, which would leave a gap before whatever follows.""" if _measure(cr, text) <= w: return text lo, hi = 0, len(text) # text[:lo] + "…" fits; text[:hi] + "…" does not while hi - lo > 1: mid = (lo + hi) // 2 if _measure(cr, text[:mid].rstrip() + "…") <= w: lo = mid else: hi = mid return text[:lo].rstrip() + "…" def _piece(cr, x, y, text): """Draws text; returns its width""" layout = _layout(cr, FONT_LIST) layout.set_text(text, -1) cr.move_to(x, y) PangoCairo.show_layout(cr, layout) return layout.get_pixel_size()[0] def split_width(avail, artist_w, title_w): """Widths for artist and title sharing avail: the shorter one whole if it fits in half, the other truncated; otherwise half each""" if artist_w + title_w <= avail: return artist_w, title_w half = avail // 2 if artist_w <= half: return artist_w, avail - artist_w if title_w <= half: return avail - title_w, title_w return half, avail - half def _list_line(cr, pos, e, y, w): """N. artist - title [m:ss], artist and/or title truncated to fit""" x = 1 right = w - 1 prefix = "%d. " % (pos + 1) suffix = " [%s]" % format_time(e.duration_s * 1000) if e.duration_s >= 0 else "" avail = right - x - _measure(cr, prefix) - _measure(cr, suffix) if e.artist is None: text = _fit(cr, e.fallback, avail) else: sep_w = _measure(cr, LIST_SEP) aw, tw = split_width(avail - sep_w, _measure(cr, e.artist), _measure(cr, e.title)) text = _fit(cr, e.artist, aw) + LIST_SEP + _fit(cr, e.title, tw) _piece(cr, x, y, prefix + text + suffix) def _playlist(cr, s, w, h, grayscale): if s.playlist is None or not s.playlist: if not s.connected: text = "Brak połączenia z %s" % s.server elif s.playlist is None: text = "Wczytywanie listy…" else: text = "Lista odtwarzania jest pusta" _text(cr, FONT_LIST, 1, (h - LIST_ROW_H) // 2, w - 2, Pango.Alignment.CENTER, text) return rows = list_rows(h) count = len(s.playlist) sel = min(max(s.list_sel, 0), count - 1) first = list_first(sel, count, rows) top = (h - rows * LIST_ROW_H) // 2 for i in range(first, min(first + rows, count)): y = top + (i - first) * LIST_ROW_H if i == sel: if grayscale: cr.set_source_rgb(*gray(1)) cr.rectangle(0, y, w, LIST_ROW_H) cr.fill() cr.set_source_rgb(*gray(2)) cr.rectangle(0, y, w, 1) cr.rectangle(0, y + LIST_ROW_H - 1, w, 1) cr.fill() cr.set_source_rgb(*gray(3)) else: cr.rectangle(0, y, w, LIST_ROW_H) cr.fill() cr.set_source_rgb(1, 1, 1) _list_line(cr, i, s.playlist[i], y + LIST_TEXT_DY, w) cr.set_source_rgb(0, 0, 0) def render(cr, s, width, height, grayscale=True): cr.set_antialias(cairo.ANTIALIAS_NONE) cr.set_source_rgb(1, 1, 1) cr.paint() cr.set_source_rgb(0, 0, 0) if s.list_open: _playlist(cr, s, width, height, grayscale) return if s.title: _title(cr, width - 2, s.title) _status(cr, s, width) _options(cr, s, width) _link(cr, s, width) _log(cr, s, width, height)