render.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. """Screen layout, drawn with cairo and Pango in panel pixels.
  2. Sizes are absolute pixels, not points. Gray edges smear on the panel at 2 bpp,
  3. so text is drawn without antialiasing and with full hinting, in fonts whose
  4. hinting holds up in plain black and white at 9-11 px: Verdana and Andale Mono
  5. (Microsoft core fonts), with Liberation as the fallback.
  6. """
  7. import cairo
  8. import gi
  9. gi.require_version("Pango", "1.0")
  10. gi.require_version("PangoCairo", "1.0")
  11. from gi.repository import Pango, PangoCairo # noqa: E402
  12. from . import state as st # noqa: E402
  13. FONT_TITLE = "DejaVu Sans 11px"
  14. FONT_STATUS = "DejaVu Sans Mono 10px"
  15. FONT_SMALL = "Verdana, Liberation Sans 9px"
  16. FONT_LOG = "Andale Mono, Liberation Mono 9px"
  17. TITLE_Y = 0
  18. TITLE_LINES = 3
  19. STATUS_Y = 40
  20. ICON_SIZE = 12
  21. BAR_Y = 54
  22. OPTIONS_Y = 60
  23. LINK_Y = 71
  24. LOG_Y = 85
  25. LOG_LINE_H = 10 # 9 px text: room for Polish accents
  26. TEXT_ANTIALIAS = cairo.ANTIALIAS_NONE
  27. # playlist screen: a row is the text plus a line above and below for the
  28. # selection marker
  29. FONT_LIST = FONT_SMALL
  30. LIST_ROW_H = 12
  31. LIST_TEXT_DY = 1
  32. LIST_SEP = " - "
  33. def gray(level):
  34. """Source color for panel level 0 (background) .. 3 (black), at 2 bpp"""
  35. v = 1 - level / 3.0
  36. return v, v, v
  37. _fonts = {}
  38. def _font(desc):
  39. d = _fonts.get(desc)
  40. if d is None:
  41. d = _fonts[desc] = Pango.FontDescription.from_string(desc)
  42. return d
  43. def _layout(cr, font):
  44. layout = PangoCairo.create_layout(cr)
  45. fo = cairo.FontOptions()
  46. fo.set_antialias(TEXT_ANTIALIAS)
  47. fo.set_hint_style(cairo.HINT_STYLE_FULL)
  48. fo.set_hint_metrics(cairo.HINT_METRICS_ON)
  49. PangoCairo.context_set_font_options(layout.get_context(), fo)
  50. layout.context_changed()
  51. layout.set_font_description(_font(font))
  52. return layout
  53. def _text(cr, font, x, y, w, align, text):
  54. layout = _layout(cr, font)
  55. layout.set_width(w * Pango.SCALE)
  56. layout.set_alignment(align)
  57. layout.set_ellipsize(Pango.EllipsizeMode.END)
  58. layout.set_text(text, -1)
  59. cr.move_to(x, y)
  60. PangoCairo.show_layout(cr, layout)
  61. def _title(cr, w, text):
  62. layout = _layout(cr, FONT_TITLE)
  63. layout.set_width(w * Pango.SCALE)
  64. layout.set_wrap(Pango.WrapMode.WORD_CHAR)
  65. layout.set_ellipsize(Pango.EllipsizeMode.END)
  66. layout.set_height(-TITLE_LINES)
  67. layout.set_text(text, -1)
  68. cr.move_to(1, TITLE_Y)
  69. PangoCairo.show_layout(cr, layout)
  70. def _icon(cr, state, x, y):
  71. s = ICON_SIZE
  72. if state == st.PLAY_PLAYING:
  73. cr.move_to(x + 2, y)
  74. cr.line_to(x + s, y + s / 2.0)
  75. cr.line_to(x + 2, y + s)
  76. cr.close_path()
  77. cr.fill()
  78. elif state == st.PLAY_PAUSED:
  79. cr.rectangle(x + 2, y, 3, s)
  80. cr.rectangle(x + 7, y, 3, s)
  81. cr.fill()
  82. elif state == st.PLAY_STOPPED:
  83. cr.rectangle(x + 1, y + 1, s - 2, s - 2)
  84. cr.fill()
  85. def format_time(ms):
  86. t = ms // 1000
  87. if t >= 3600:
  88. return "%d:%02d:%02d" % (t // 3600, t // 60 % 60, t % 60)
  89. return "%d:%02d" % (t // 60, t % 60)
  90. def _status(cr, s, w):
  91. _icon(cr, s.state, 2, STATUS_Y)
  92. if s.state == st.PLAY_STOPPED or s.song_pos < 0:
  93. return
  94. _text(cr, FONT_STATUS, 20, STATUS_Y, 90, Pango.Alignment.LEFT,
  95. "#%d/%d" % (s.song_pos + 1, s.queue_len))
  96. elapsed = s.current_elapsed_ms()
  97. text = format_time(elapsed)
  98. if s.total_ms:
  99. text += "/" + format_time(s.total_ms)
  100. _text(cr, FONT_STATUS, 110, STATUS_Y, w - 112, Pango.Alignment.RIGHT, text)
  101. if s.total_ms:
  102. bar_w = w - 22
  103. fill = elapsed * bar_w // s.total_ms
  104. cr.set_line_width(1)
  105. cr.rectangle(20.5, BAR_Y + 0.5, bar_w - 1, 3)
  106. cr.stroke()
  107. cr.rectangle(20, BAR_Y, fill, 4)
  108. cr.fill()
  109. def _options(cr, s, w):
  110. if not s.connected:
  111. return
  112. text = "Włączone:" + (" repeat" if s.repeat else "") + (" random" if s.random else "")
  113. _text(cr, FONT_SMALL, 1, OPTIONS_Y, 170, Pango.Alignment.LEFT, text)
  114. text = "Głośność %d%%" % s.volume if s.volume >= 0 else "Głośność –"
  115. _text(cr, FONT_SMALL, 150, OPTIONS_Y, w - 151, Pango.Alignment.RIGHT, text)
  116. def _link(cr, s, w):
  117. if s.connected:
  118. return
  119. cr.rectangle(0, LINK_Y, w, 12)
  120. cr.fill()
  121. cr.set_source_rgb(1, 1, 1)
  122. _text(cr, FONT_SMALL, 2, LINK_Y, w - 4, Pango.Alignment.LEFT,
  123. "Brak połączenia z %s" % s.server)
  124. cr.set_source_rgb(0, 0, 0)
  125. def _log(cr, s, w, h):
  126. cr.set_line_width(1)
  127. cr.move_to(0, LOG_Y - 1.5)
  128. cr.line_to(w, LOG_Y - 1.5)
  129. cr.stroke()
  130. for i, row in enumerate(s.log):
  131. y = LOG_Y + i * LOG_LINE_H
  132. if y + LOG_LINE_H > h:
  133. break
  134. if row:
  135. _text(cr, FONT_LOG, 1, y - 1, w - 1, Pango.Alignment.LEFT, row)
  136. def list_rows(height):
  137. return height // LIST_ROW_H
  138. def list_first(sel, count, rows):
  139. """First row shown: the selection centered, without empty rows at the end"""
  140. return max(0, min(sel - rows // 2, count - rows))
  141. def _measure(cr, text):
  142. layout = _layout(cr, FONT_LIST)
  143. layout.set_text(text, -1)
  144. return layout.get_pixel_size()[0]
  145. def _fit(cr, text, w):
  146. """text, or its longest prefix plus an ellipsis that fits in w pixels.
  147. Not Pango's ellipsizing: that pads the line out to the full width, which
  148. would leave a gap before whatever follows."""
  149. if _measure(cr, text) <= w:
  150. return text
  151. lo, hi = 0, len(text) # text[:lo] + "…" fits; text[:hi] + "…" does not
  152. while hi - lo > 1:
  153. mid = (lo + hi) // 2
  154. if _measure(cr, text[:mid].rstrip() + "…") <= w:
  155. lo = mid
  156. else:
  157. hi = mid
  158. return text[:lo].rstrip() + "…"
  159. def _piece(cr, x, y, text):
  160. """Draws text; returns its width"""
  161. layout = _layout(cr, FONT_LIST)
  162. layout.set_text(text, -1)
  163. cr.move_to(x, y)
  164. PangoCairo.show_layout(cr, layout)
  165. return layout.get_pixel_size()[0]
  166. def split_width(avail, artist_w, title_w):
  167. """Widths for artist and title sharing avail: the shorter one whole if it
  168. fits in half, the other truncated; otherwise half each"""
  169. if artist_w + title_w <= avail:
  170. return artist_w, title_w
  171. half = avail // 2
  172. if artist_w <= half:
  173. return artist_w, avail - artist_w
  174. if title_w <= half:
  175. return avail - title_w, title_w
  176. return half, avail - half
  177. def _list_line(cr, pos, e, y, w):
  178. """N. artist - title [m:ss], artist and/or title truncated to fit"""
  179. x = 1
  180. right = w - 1
  181. prefix = "%d. " % (pos + 1)
  182. suffix = " [%s]" % format_time(e.duration_s * 1000) if e.duration_s >= 0 else ""
  183. avail = right - x - _measure(cr, prefix) - _measure(cr, suffix)
  184. if e.artist is None:
  185. text = _fit(cr, e.fallback, avail)
  186. else:
  187. sep_w = _measure(cr, LIST_SEP)
  188. aw, tw = split_width(avail - sep_w, _measure(cr, e.artist), _measure(cr, e.title))
  189. text = _fit(cr, e.artist, aw) + LIST_SEP + _fit(cr, e.title, tw)
  190. _piece(cr, x, y, prefix + text + suffix)
  191. def _playlist(cr, s, w, h, grayscale):
  192. if s.playlist is None or not s.playlist:
  193. if not s.connected:
  194. text = "Brak połączenia z %s" % s.server
  195. elif s.playlist is None:
  196. text = "Wczytywanie listy…"
  197. else:
  198. text = "Lista odtwarzania jest pusta"
  199. _text(cr, FONT_LIST, 1, (h - LIST_ROW_H) // 2, w - 2, Pango.Alignment.CENTER, text)
  200. return
  201. rows = list_rows(h)
  202. count = len(s.playlist)
  203. sel = min(max(s.list_sel, 0), count - 1)
  204. first = list_first(sel, count, rows)
  205. top = (h - rows * LIST_ROW_H) // 2
  206. for i in range(first, min(first + rows, count)):
  207. y = top + (i - first) * LIST_ROW_H
  208. if i == sel:
  209. if grayscale:
  210. cr.set_source_rgb(*gray(1))
  211. cr.rectangle(0, y, w, LIST_ROW_H)
  212. cr.fill()
  213. cr.set_source_rgb(*gray(2))
  214. cr.rectangle(0, y, w, 1)
  215. cr.rectangle(0, y + LIST_ROW_H - 1, w, 1)
  216. cr.fill()
  217. cr.set_source_rgb(*gray(3))
  218. else:
  219. cr.rectangle(0, y, w, LIST_ROW_H)
  220. cr.fill()
  221. cr.set_source_rgb(1, 1, 1)
  222. _list_line(cr, i, s.playlist[i], y + LIST_TEXT_DY, w)
  223. cr.set_source_rgb(0, 0, 0)
  224. def render(cr, s, width, height, grayscale=True):
  225. cr.set_antialias(cairo.ANTIALIAS_NONE)
  226. cr.set_source_rgb(1, 1, 1)
  227. cr.paint()
  228. cr.set_source_rgb(0, 0, 0)
  229. if s.list_open:
  230. _playlist(cr, s, width, height, grayscale)
  231. return
  232. if s.title:
  233. _title(cr, width - 2, s.title)
  234. _status(cr, s, width)
  235. _options(cr, s, width)
  236. _link(cr, s, width)
  237. _log(cr, s, width, height)