Codebase list xapp / dfb1757
mate-xapp-status-applet.py: Use new properties to sync button toggle state with the menu state. Michael Webster 2 years ago
1 changed file(s) with 38 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
11
22 import locale
33 import gettext
4 import json
45 import os
56 import sys
67 import setproctitle
5556
5657 class StatusWidget(Gtk.ToggleButton):
5758 def __init__(self, icon, orientation, size):
58 super(Gtk.Button, self).__init__()
59 super(Gtk.ToggleButton, self).__init__()
5960 self.theme = Gtk.IconTheme.get_default()
6061 self.orientation = orientation
6162 self.size = size
9293 self.proxy.bind_property("tooltip-text", self, "tooltip-markup", flags)
9394 self.proxy.bind_property("visible", self, "visible", flags)
9495
96 self.proxy.connect("notify::primary-menu-is-open", self.menu_state_changed)
97 self.proxy.connect("notify::secondary-menu-is-open", self.menu_state_changed)
98
99 self.highlight_both_menus = False
100
101 if self.proxy.props.metadata not in ("", None):
102 try:
103 meta = json.loads(self.proxy.props.metadata)
104 if meta["highlight-both-menus"]:
105 self.highlight_both_menus = True
106 except json.JSONDecodeError as e:
107 print("Could not read metadata: %s" % e)
108
95109 self.proxy.connect("notify::icon-name", self._on_icon_name_changed)
96110
97111 self.in_widget = False
98112 self.plain_surface = None
99113 self.saturated_surface = None
114
115 self.menu_opened = False
100116
101117 self.connect("button-press-event", self.on_button_press)
102118 self.connect("button-release-event", self.on_button_release)
174190 self.image.set_pixel_size(self.size - ICON_SIZE_REDUCTION)
175191 self.image.set_from_icon_name("image-missing", Gtk.IconSize.MENU)
176192
193 def menu_state_changed(self, proxy, pspec, data=None):
194 if pspec.name == "primary-menu-is-open":
195 prop = proxy.props.primary_menu_is_open
196 else:
197 prop = proxy.props.secondary_menu_is_open
198
199 if not self.menu_opened or prop == False:
200 self.set_active(False)
201 return
202
203 self.set_active(prop)
204 self.menu_opened = False
205
177206 # TODO?
178207 def on_enter_notify(self, widget, event):
179208 self.in_widget = True
186215 return Gdk.EVENT_PROPAGATE
187216 # /TODO
188217
189 def after_release_idle(self, data=None):
190 self.set_active(False)
191
192 return GLib.SOURCE_REMOVE
193
194218 def on_button_press(self, widget, event):
219 self.menu_opened = False
220
195221 # If the user does ctrl->right-click, open the applet's about menu
196222 # instead of sending to the app.
197223 if event.state & Gdk.ModifierType.CONTROL_MASK and event.button == Gdk.BUTTON_SECONDARY:
202228 x, y = self.calc_menu_origin(widget, orientation)
203229 self.proxy.call_button_press(x, y, event.button, event.time, orientation, None, None)
204230
205 if event.button != Gdk.BUTTON_PRIMARY:
206 self.set_active(True)
207
208231 if event.button in (Gdk.BUTTON_MIDDLE, Gdk.BUTTON_SECONDARY):
209232 # Block the 'remove from panel' menu, and the middle-click drag.
210233 # They can still accomplish these things along the edges of the applet
211234 return Gdk.EVENT_STOP
212235
213 return Gdk.EVENT_PROPAGATE
236 return Gdk.EVENT_STOP
214237
215238 def on_button_release(self, widget, event):
216239 orientation = translate_applet_orientation_to_xapp(self.orientation)
217240
241 if event.button == Gdk.BUTTON_PRIMARY:
242 self.menu_opened = True
243 elif event.button == Gdk.BUTTON_SECONDARY and self.highlight_both_menus:
244 self.menu_opened = True
245
218246 x, y = self.calc_menu_origin(widget, orientation)
219247 self.proxy.call_button_release(x, y, event.button, event.time, orientation, None, None)
220
221 GObject.timeout_add(200, self.after_release_idle)
222248
223249 return Gdk.EVENT_PROPAGATE
224250