all_windows_count.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 typing import Any, List, Tuple
  21. from libqtile import bar, hook
  22. from libqtile.widget import base
  23. from libqtile.command.base import expose_command
  24. class WindowCount(base._TextBox):
  25. """A simple widget to show the number of ALL opened windows."""
  26. orientations = base.ORIENTATION_HORIZONTAL
  27. defaults = [
  28. ("font", "sans", "Text font"),
  29. ("fontsize", None, "Font pixel size. Calculated if None."),
  30. ("fontshadow", None, "font shadow color, default is None(no shadow)"),
  31. ("padding", None, "Padding left and right. Calculated if None."),
  32. ("foreground", "#ffffff", "Foreground colour."),
  33. ("text_format", "{num}", "Format for message"),
  34. ("show_zero", False, "Show window count when no windows")
  35. ] # type: List[Tuple[str, Any, str]]
  36. def __init__(self, text=" ", width=bar.CALCULATED, **config):
  37. base._TextBox.__init__(self, text=text, width=width, **config)
  38. self.add_defaults(WindowCount.defaults)
  39. self._count = 0
  40. def _configure(self, qtile, bar):
  41. base._TextBox._configure(self, qtile, bar)
  42. self._setup_hooks()
  43. def _setup_hooks(self):
  44. hook.subscribe.client_killed(self._decrease_count)
  45. hook.subscribe.client_new(self._increase_count)
  46. def _increase_count(self, *args):
  47. self._count += 1
  48. self.update()
  49. def _decrease_count(self, window):
  50. self._count -= 1
  51. self.update()
  52. def calculate_length(self):
  53. if self.text and (self._count or self.show_zero):
  54. return min(
  55. self.layout.width,
  56. self.bar.width
  57. ) + self.actual_padding * 2
  58. else:
  59. return 0
  60. def update(self):
  61. self.text = self.text_format.format(num=self._count)
  62. self.bar.draw()
  63. @expose_command
  64. def cmd_get(self):
  65. """Retrieve the current text."""
  66. return self.text