No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tray.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python
  2. import brightness
  3. import threading
  4. import time
  5. import pystray
  6. from io import BytesIO
  7. import cairosvg
  8. from PIL import Image
  9. #icon_path = "/usr/share/icons/breeze-dark/devices/22/video-display-brightness.svg"
  10. #icon_path = "/usr/share/icons/breeze-dark/devices/32/video-display-brightness-symbolic.svg"
  11. #icon_path = "/usr/share/icons/breeze-dark/status/32/input-keyboard-brightness.svg"
  12. icon_path = "/usr/share/icons/breeze-dark/actions/24/brightness-high.svg"
  13. prev_pause = None
  14. prev_active = None
  15. prev_last = None
  16. prev_disp = None
  17. def is_running_name():
  18. #print("check 1")
  19. return "Running" if (brightness.is_active and brightness.is_unpaused) else "Stopped"
  20. def is_running_checked():
  21. #print("check 2")
  22. return True if (brightness.is_active and brightness.is_unpaused) else False
  23. def get_value():
  24. #print("check 3")
  25. return f"Brightness: {brightness.last_brightness}"
  26. def is_paused_checked():
  27. #print("check 4")
  28. return False if brightness.is_unpaused else True
  29. def is_paused_name():
  30. #print("check 5")
  31. return "Unpaused" if brightness.is_unpaused else "Paused"
  32. def toggle_pause():
  33. brightness.is_unpaused = False if brightness.is_unpaused else True
  34. def quit(icon):
  35. print("stop brightness")
  36. brightness.running = False
  37. print("stop tray")
  38. icon.stop()
  39. def poll(icon):
  40. global prev_pause, prev_active, prev_last, prev_disp
  41. while brightness.running:
  42. time.sleep(1.0)
  43. if (prev_pause == brightness.is_unpaused) and (prev_active == brightness.is_active) and (prev_last == brightness.last_brightness) and (prev_disp == brightness.disps):
  44. #print("skip")
  45. continue
  46. prev_pause = brightness.is_unpaused
  47. prev_active = brightness.is_active
  48. prev_last = brightness.last_brightness
  49. prev_disp = brightness.disps
  50. #print("update")
  51. icon.update_menu()
  52. def display_menu():
  53. #print("menu")
  54. if (brightness.disps == None) or (len(brightness.disps) <= 0):
  55. return (pystray.MenuItem("No displays", None, enabled=False), )
  56. display_entries = []
  57. for d in brightness.disps:
  58. #s = "{} ({}) @ {}".format(d["name"], d["_id"], d["prev"])
  59. s = f"{d['prev']} @ {d['name']}"
  60. display_entries.append(pystray.MenuItem(s, None, enabled=False))
  61. return display_entries
  62. def main():
  63. out = BytesIO()
  64. cairosvg.svg2png(url=icon_path, write_to=out)
  65. image = Image.open(out)
  66. print("start brightness")
  67. t_b = threading.Thread(target=brightness.main)
  68. t_b.start()
  69. print("prepare tray")
  70. icon = pystray.Icon("AutoBrightness", image, "AutoBrightness",
  71. menu=pystray.Menu(
  72. pystray.MenuItem(
  73. lambda icon=pystray.Icon: get_value(),
  74. None,
  75. enabled=False,
  76. ),
  77. pystray.MenuItem(
  78. "Displays",
  79. pystray.Menu(
  80. lambda icon=pystray.Icon: display_menu(),
  81. )
  82. ),
  83. pystray.MenuItem(
  84. lambda icon=pystray.Icon: is_running_name(),
  85. None,
  86. enabled=False,
  87. checked=lambda icon=pystray.Icon: is_running_checked(),
  88. ),
  89. pystray.MenuItem(
  90. lambda icon=pystray.Icon: is_paused_name(),
  91. lambda icon=pystray.Icon: toggle_pause(),
  92. checked=lambda icon=pystray.Icon: is_paused_checked(),
  93. ),
  94. pystray.MenuItem(
  95. "Quit",
  96. lambda icon=pystray.Icon: quit(icon),
  97. ),
  98. )
  99. )
  100. print("start polling")
  101. t_p = threading.Thread(target=poll, args=(icon,))
  102. t_p.start()
  103. print("start tray")
  104. icon.run()
  105. print("join brightness")
  106. t_b.join()
  107. t_p.join()
  108. print("done")
  109. if __name__ == "__main__":
  110. main()