| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- """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
- _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 render(cr, s, width, height):
- cr.set_antialias(cairo.ANTIALIAS_NONE)
- cr.set_source_rgb(1, 1, 1)
- cr.paint()
- cr.set_source_rgb(0, 0, 0)
- 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)
|