render.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. _fonts = {}
  28. def _font(desc):
  29. d = _fonts.get(desc)
  30. if d is None:
  31. d = _fonts[desc] = Pango.FontDescription.from_string(desc)
  32. return d
  33. def _layout(cr, font):
  34. layout = PangoCairo.create_layout(cr)
  35. fo = cairo.FontOptions()
  36. fo.set_antialias(TEXT_ANTIALIAS)
  37. fo.set_hint_style(cairo.HINT_STYLE_FULL)
  38. fo.set_hint_metrics(cairo.HINT_METRICS_ON)
  39. PangoCairo.context_set_font_options(layout.get_context(), fo)
  40. layout.context_changed()
  41. layout.set_font_description(_font(font))
  42. return layout
  43. def _text(cr, font, x, y, w, align, text):
  44. layout = _layout(cr, font)
  45. layout.set_width(w * Pango.SCALE)
  46. layout.set_alignment(align)
  47. layout.set_ellipsize(Pango.EllipsizeMode.END)
  48. layout.set_text(text, -1)
  49. cr.move_to(x, y)
  50. PangoCairo.show_layout(cr, layout)
  51. def _title(cr, w, text):
  52. layout = _layout(cr, FONT_TITLE)
  53. layout.set_width(w * Pango.SCALE)
  54. layout.set_wrap(Pango.WrapMode.WORD_CHAR)
  55. layout.set_ellipsize(Pango.EllipsizeMode.END)
  56. layout.set_height(-TITLE_LINES)
  57. layout.set_text(text, -1)
  58. cr.move_to(1, TITLE_Y)
  59. PangoCairo.show_layout(cr, layout)
  60. def _icon(cr, state, x, y):
  61. s = ICON_SIZE
  62. if state == st.PLAY_PLAYING:
  63. cr.move_to(x + 2, y)
  64. cr.line_to(x + s, y + s / 2.0)
  65. cr.line_to(x + 2, y + s)
  66. cr.close_path()
  67. cr.fill()
  68. elif state == st.PLAY_PAUSED:
  69. cr.rectangle(x + 2, y, 3, s)
  70. cr.rectangle(x + 7, y, 3, s)
  71. cr.fill()
  72. elif state == st.PLAY_STOPPED:
  73. cr.rectangle(x + 1, y + 1, s - 2, s - 2)
  74. cr.fill()
  75. def format_time(ms):
  76. t = ms // 1000
  77. if t >= 3600:
  78. return "%d:%02d:%02d" % (t // 3600, t // 60 % 60, t % 60)
  79. return "%d:%02d" % (t // 60, t % 60)
  80. def _status(cr, s, w):
  81. _icon(cr, s.state, 2, STATUS_Y)
  82. if s.state == st.PLAY_STOPPED or s.song_pos < 0:
  83. return
  84. _text(cr, FONT_STATUS, 20, STATUS_Y, 90, Pango.Alignment.LEFT,
  85. "#%d/%d" % (s.song_pos + 1, s.queue_len))
  86. elapsed = s.current_elapsed_ms()
  87. text = format_time(elapsed)
  88. if s.total_ms:
  89. text += "/" + format_time(s.total_ms)
  90. _text(cr, FONT_STATUS, 110, STATUS_Y, w - 112, Pango.Alignment.RIGHT, text)
  91. if s.total_ms:
  92. bar_w = w - 22
  93. fill = elapsed * bar_w // s.total_ms
  94. cr.set_line_width(1)
  95. cr.rectangle(20.5, BAR_Y + 0.5, bar_w - 1, 3)
  96. cr.stroke()
  97. cr.rectangle(20, BAR_Y, fill, 4)
  98. cr.fill()
  99. def _options(cr, s, w):
  100. if not s.connected:
  101. return
  102. text = "Włączone:" + (" repeat" if s.repeat else "") + (" random" if s.random else "")
  103. _text(cr, FONT_SMALL, 1, OPTIONS_Y, 170, Pango.Alignment.LEFT, text)
  104. text = "Głośność %d%%" % s.volume if s.volume >= 0 else "Głośność –"
  105. _text(cr, FONT_SMALL, 150, OPTIONS_Y, w - 151, Pango.Alignment.RIGHT, text)
  106. def _link(cr, s, w):
  107. if s.connected:
  108. return
  109. cr.rectangle(0, LINK_Y, w, 12)
  110. cr.fill()
  111. cr.set_source_rgb(1, 1, 1)
  112. _text(cr, FONT_SMALL, 2, LINK_Y, w - 4, Pango.Alignment.LEFT,
  113. "Brak połączenia z %s" % s.server)
  114. cr.set_source_rgb(0, 0, 0)
  115. def _log(cr, s, w, h):
  116. cr.set_line_width(1)
  117. cr.move_to(0, LOG_Y - 1.5)
  118. cr.line_to(w, LOG_Y - 1.5)
  119. cr.stroke()
  120. for i, row in enumerate(s.log):
  121. y = LOG_Y + i * LOG_LINE_H
  122. if y + LOG_LINE_H > h:
  123. break
  124. if row:
  125. _text(cr, FONT_LOG, 1, y - 1, w - 1, Pango.Alignment.LEFT, row)
  126. def render(cr, s, width, height):
  127. cr.set_antialias(cairo.ANTIALIAS_NONE)
  128. cr.set_source_rgb(1, 1, 1)
  129. cr.paint()
  130. cr.set_source_rgb(0, 0, 0)
  131. if s.title:
  132. _title(cr, width - 2, s.title)
  133. _status(cr, s, width)
  134. _options(cr, s, width)
  135. _link(cr, s, width)
  136. _log(cr, s, width, height)