-
Notifications
You must be signed in to change notification settings - Fork 1
/
zendisplay_gtk.py
143 lines (125 loc) · 5.22 KB
/
zendisplay_gtk.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""Small script to adjust display brightness according to ambient lighting"""
# pylint: disable=wrong-import-position,wrong-import-order
import sys
import gi
from dbus.mainloop.glib import DBusGMainLoop
from zendisplay_base import ZenDisplay as ZenDisplayBase
from zendisplay_config import Config
gi.require_version('Gtk', '3.0')
gi.require_version('AyatanaAppIndicator3', '0.1')
from gi.repository import AyatanaAppIndicator3 as appindicator
from gi.repository import Gtk as gtk
from gi.repository import GLib
class ZenDisplay(ZenDisplayBase):
"""System tray icon class"""
def __init__(self):
ZenDisplayBase.__init__(self)
self.menu = self.construct_menu()
self.indicator.set_menu(self.menu)
self.indicator.connect("scroll_event", self._scroll_event)
self._brightness_updated(self.displays.get_brightness())
def run(self):
"""Run main loop"""
gtk.main()
def _init_framework(self):
"""Initialize the main loop"""
self.indicator = self._create_indicator()
DBusGMainLoop(set_as_default=True)
GLib.timeout_add(1000, self.main_control)
def _brightness_updated(self, brightness):
"""Run when brightness is updated"""
self.indicator.set_title('Brightness: ' + str(brightness) + '%')
@staticmethod
def _create_indicator():
indicator = appindicator.Indicator.new(
"ZenDisplay",
"video-display-symbolic",
appindicator.IndicatorCategory.APPLICATION_STATUS,
)
indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
return indicator
def construct_menu(self):
"""Construct a menu from the sitemap"""
menu = gtk.Menu()
self.construct_menu_displays(menu)
self.construct_menu_sensors(menu)
self.construct_menu_brightness(menu)
self._construct_menu_separator(menu)
self._construct_menu_save(menu)
self._construct_menu_quit(menu)
menu.show()
return menu
@classmethod
def _construct_menu_separator(cls, parent):
"""Create a separator menu item"""
separator = gtk.SeparatorMenuItem()
separator.show()
parent.append(separator)
@classmethod
def _construct_menu_save(cls, parent):
"""Create quit button"""
action = gtk.MenuItem(label="Save settings")
action.connect("activate", lambda _: Config().save())
action.show()
parent.append(action)
@classmethod
def _construct_menu_quit(cls, parent):
"""Create quit button"""
action = gtk.MenuItem(label="Quit")
action.connect("activate", lambda _: sys.exit())
action.show()
parent.append(action)
def construct_menu_displays(self, parent):
"""Create submenu for displays"""
display_menu = gtk.Menu()
display_menu_action = gtk.MenuItem(label="Displays")
display_menu_action.set_submenu(display_menu)
display_menu_action.show()
parent.append(display_menu_action)
for display in self.displays:
action = gtk.CheckMenuItem(label=display.name + " (" + display.path + ")")
action.set_active(display.enabled)
did = display.uid
action.connect(
'toggled',
lambda item, did=did: self.displays.set_active(did, item.get_active())
)
action.show()
display_menu.append(action)
def construct_menu_sensors(self, parent):
"""Create submenu for sensors"""
sensor_menu = gtk.Menu()
sensor_menu_action = gtk.MenuItem(label="Sensors")
sensor_menu_action.set_submenu(sensor_menu)
sensor_menu_action.show()
parent.append(sensor_menu_action)
for sensor in self.sensors:
action = gtk.RadioMenuItem(label=sensor.name + " (" + sensor.path + ")")
action.join_group((sensor_menu.get_children()[:1] or [None])[0])
if sensor.uid == self.sensors.get_active():
action.set_active(True)
sid = sensor.uid
action.connect('activate', lambda _, sid=sid: self.sensors.activate(sid))
action.show()
sensor_menu.append(action)
def construct_menu_brightness(self, parent):
"""Create submenu for the brightness setting"""
brightness_menu = gtk.Menu()
brightness_menu_action = gtk.MenuItem(label='Brightness')
brightness_menu_action.set_submenu(brightness_menu)
brightness_menu_action.show()
parent.append(brightness_menu_action)
for value in range(0, 101, 10):
action = gtk.RadioMenuItem(label=str(value) + "%")
action.join_group((brightness_menu.get_children()[:1] or [None])[0])
if value == Config().get('brightness', 'base_value'):
action.set_active(True)
action.connect('activate', lambda _, value=value: self.controller.set_intercept(value))
action.show()
brightness_menu.append(action)
def _scroll_event(self, _1, _2, direction):
"""Event handler for the scroll_event signal"""
if direction == 0:
self.controller.increase_intercept()
elif direction == 1:
self.controller.decrease_intercept()