widgetbox.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. # Copyright (c) 2020 elParaguayo
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. from collections import namedtuple
  21. from libqtile import bar
  22. from libqtile.log_utils import logger
  23. from libqtile.widget import base
  24. BoxedWidget = namedtuple("BoxedWidget", ["widget", "draw"])
  25. def _no_draw(*args, **kwargs):
  26. pass
  27. class WidgetBox(base._Widget):
  28. """A widget to declutter your bar.
  29. WidgetBox is a widget that hides widgets by default but shows them when
  30. the box is opened.
  31. Widgets that are hidden will still update etc. as if they were on the main
  32. bar.
  33. Button clicks are passed to widgets when they are visible so callbacks will
  34. work.
  35. Widgets in the box also remain accessible via command interfaces.
  36. Widgets can only be added to the box via the configuration file. The widget
  37. is configured by adding widgets to the "widgets" parameter as follows::
  38. widget.WidgetBox(widgets=[
  39. widget.TextBox(text="This widget is in the box"),
  40. widget.Memory()
  41. ]
  42. ),
  43. """
  44. orientations = base.ORIENTATION_HORIZONTAL
  45. defaults = [
  46. (
  47. "font",
  48. "sans",
  49. "Text font"
  50. ),
  51. (
  52. "fontsize",
  53. None,
  54. "Font pixel size. Calculated if None."
  55. ),
  56. (
  57. "fontshadow",
  58. None,
  59. "font shadow color, default is None(no shadow)"
  60. ),
  61. (
  62. "foreground",
  63. "#ffffff",
  64. "Foreground colour."
  65. ),
  66. (
  67. "close_button_location",
  68. "left",
  69. "Location of close button when box open ('left' or 'right')"
  70. ),
  71. (
  72. "text_closed",
  73. "[<]",
  74. "Text when box is closed"
  75. ),
  76. (
  77. "text_open",
  78. "[>]",
  79. "Text when box is open"
  80. ),
  81. ]
  82. def __init__(self, widgets=list(), **config):
  83. base._Widget.__init__(self, bar.CALCULATED, **config)
  84. self.add_defaults(WidgetBox.defaults)
  85. self.box_is_open = False
  86. self._widgets = widgets
  87. # self.add_callbacks({"Button1": self.cmd_toggle})
  88. if self.close_button_location not in ["left", "right"]:
  89. val = self.close_button_location
  90. msg = "Invalid value for 'close_button_location': {}".format(val)
  91. logger.warning(msg)
  92. self.close_button_location = "left"
  93. def _configure(self, qtile, bar):
  94. base._Widget._configure(self, qtile, bar)
  95. self.layout = self.drawer.textlayout(
  96. self.text_closed,
  97. self.foreground,
  98. self.font,
  99. self.fontsize,
  100. self.fontshadow,
  101. markup=False,
  102. )
  103. for idx, w in enumerate(self._widgets):
  104. if w.configured:
  105. w = w.create_mirror()
  106. self._widgets[idx] = w
  107. self.qtile.register_widget(w)
  108. w._configure(self.qtile, self.bar)
  109. # In case the widget is mirrored, we need to draw it once so the
  110. # mirror can copy the surface but draw it off screen
  111. w.offsetx = self.bar.width
  112. self.qtile.call_soon(w.draw)
  113. # We need to stop hidden widgets from drawing while hidden
  114. # (e.g. draw could be triggered by a timer) so we take a reference to
  115. # the widget's drawer.draw method
  116. self.widgets = [BoxedWidget(w, w.drawer.draw) for w in self._widgets]
  117. # # Overwrite the current drawer.draw method with a no-op
  118. for w in self.widgets:
  119. w.widget.drawer.draw = _no_draw
  120. def calculate_length(self):
  121. return self.layout.width
  122. def set_box_label(self):
  123. self.layout.text = (self.text_open if self.box_is_open
  124. else self.text_closed)
  125. def toggle_widgets(self):
  126. for item in self.widgets:
  127. try:
  128. self.bar.widgets.remove(item.widget)
  129. # Override drawer.drawer with a no-op
  130. item.widget.drawer.draw = _no_draw
  131. except ValueError:
  132. continue
  133. index = self.bar.widgets.index(self)
  134. if self.close_button_location == "left":
  135. index += 1
  136. if self.box_is_open:
  137. # Need to reverse list as widgets get added in front of eachother.
  138. for item in self.widgets[::-1]:
  139. # Restore the original drawer.draw method
  140. item.widget.drawer.draw = item.draw
  141. self.bar.widgets.insert(index, item.widget)
  142. def draw(self):
  143. self.drawer.clear(self.background or self.bar.background)
  144. self.layout.draw(0,
  145. int(self.bar.height / 2.0 -
  146. self.layout.height / 2.0) + 1)
  147. self.drawer.draw(offsetx=self.offsetx, width=self.width)
  148. def button_press(self, x, y, button):
  149. name = "Button{}".format(button)
  150. if name in self.mouse_callbacks:
  151. self.mouse_callbacks[name]()
  152. def cmd_toggle(self):
  153. """Toggle box state"""
  154. self.box_is_open = not self.box_is_open
  155. self.toggle_widgets()
  156. self.set_box_label()
  157. self.bar.draw()