-
Notifications
You must be signed in to change notification settings - Fork 0
/
Systray.py
168 lines (143 loc) · 4.74 KB
/
Systray.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import rox
from rox import g, options, app_options
from MenuWindow import MenuWindow
import main
import gobject
import sys
import os.path
systrayEnable = options.Option('systray_enable', True)
systrayHideOnStartup = options.Option('systray_hide_on_startup', False)
systrayWorkaround = options.Option('systray_workaround', False)
# Require gtk version 2.10.x or better for the 'StatusIcon' widget
assert( g.gtk_version >= (2,10,0) )
menuAdditions={
'topActions': [
("/Main Window", "toggle_main", "", ""),
]
}
mainAdditions = { 'topMain': [
(_('Hide Main Window'), "hide", "")
]}
class Systray(g.StatusIcon, MenuWindow):
def __init__(self, showOnStartup = None):
g.StatusIcon.__init__(self)
MenuWindow.__init__(self, attach=False, additions=menuAdditions)
icon = os.path.join(rox.app_dir, '.DirIcon')
self.set_from_file(icon)
self.firstChange = True
self.showOnStartup = showOnStartup
self.was_embedded = None
main.memo_list.connect( "MemoListChanged", self.tooltip_refresh )
self.tooltip_refresh( main.memo_list )
self.connect("popup-menu", self.popup)
self.connect("activate", self.toggle_main)
self.connect("notify::visible", self.visibility)
if g.gtk_version >= (2,12,0):
self.connect("notify::embedded", self.check_embed)
else:
self.connect("size-changed", self.size_change)
self.timeout = gobject.timeout_add(500, self.check_embed)
def tooltip_refresh(self, memo_list):
(all, hidden) = memo_list.count_today()
visible = all-hidden
vplural = "s"
if visible == 1:
vplural = ""
self.set_tooltip("Memo - %d reminder%s today" % (visible, vplural) )
def position_menu(self, menu):
return g.status_icon_position_menu(menu, self)
def popup(self, status_icon, button, activate_time):
# Rox's 'menu' class doesn't actually need a 'gtk.gdk.Event', just an object
# with 'button' and 'time' parameters - Which is good, since gtk.gdk.Event
# won't accept a 'long' value, which is what both 'button' and
# 'activate_time' are (plus activate_time is usually too large to properly
# cast into a signed python 'int' type
class e: pass;
event = e()
event.button = button
event.time = activate_time
self.popup_menu(event, self.position_menu)
def size_change(self, icon = None, size = None):
self.check_embed()
def is_present(self):
return self.is_embedded and self.get_visible()
def visibility(self, object=None, property=None):
if self.get_visible():
self._embedded()
else:
self._unembedded()
def _embedded(self):
# Systray present, allow main window to hide
main.main_window.addAdditions( mainAdditions )
def _unembedded(self):
# Systray no longer present, show the main window, and disallow hiding
main.main_window.removeAdditions( mainAdditions )
if not main.main_window.get_property('visible'):
main.main_window.present()
def check_embed(self, object = None, property = None):
if self.is_embedded() and (not self.was_embedded or self.firstChange):
self._embedded()
# Check for "hide on startup" flag
if self.showOnStartup is not None:
if not self.showOnStartup:
main.main_window.hide()
else:
if not main.main_window.get_property('visible'):
main.main_window.present()
self.was_embedded = True
elif not self.is_embedded() and (self.was_embedded or self.firstChange):
self._unembedded()
self.was_embedded = False
self.firstChange = False
return True
def toggle_main(self, event = None):
if main.main_window.get_property('visible'):
main.main_window.hide()
else:
main.main_window.present()
def quit(self, event = None):
main.main_window.destroy()
class HideableSystray(object):
def __init__(self):
if systrayEnable.int_value:
self.icon = Systray( showOnStartup = not systrayHideOnStartup.int_value )
else:
self.icon = None
main.main_window.present()
app_options.add_notify(self.options_changed)
def options_changed(self):
if systrayEnable.has_changed:
if systrayEnable.int_value:
self.show()
else:
self.hide()
if systrayWorkaround.has_changed:
if systrayEnable.int_value and systrayWorkaround.int_value:
self.hide()
self.show()
def show(self):
if not self.icon:
self.icon = Systray()
else:
self.icon.set_visible(True)
def hide(self):
if self.icon:
self.icon.set_visible(False)
# Workaround for broken systrays (like older fluxbox versions) which
# may not support set_visible(True) - Delete the systray icon (which
# will be re-created at 'show' time.
if systrayWorkaround.int_value:
del self.icon
self.icon = None
def is_embedded(self):
if self.icon:
return self.icon.is_embedded()
return False
def get_visible(self):
if self.icon:
return self.icon.get_visible()
return False
def is_present(self):
if self.icon:
return self.icon.is_present()
return False