mate-xapp-status-applet.py: Use new properties to sync button toggle state with the
menu state.
Michael Webster
2 years ago
1 | 1 | |
2 | 2 | import locale |
3 | 3 | import gettext |
4 | import json | |
4 | 5 | import os |
5 | 6 | import sys |
6 | 7 | import setproctitle |
55 | 56 | |
56 | 57 | class StatusWidget(Gtk.ToggleButton): |
57 | 58 | def __init__(self, icon, orientation, size): |
58 | super(Gtk.Button, self).__init__() | |
59 | super(Gtk.ToggleButton, self).__init__() | |
59 | 60 | self.theme = Gtk.IconTheme.get_default() |
60 | 61 | self.orientation = orientation |
61 | 62 | self.size = size |
92 | 93 | self.proxy.bind_property("tooltip-text", self, "tooltip-markup", flags) |
93 | 94 | self.proxy.bind_property("visible", self, "visible", flags) |
94 | 95 | |
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 | ||
95 | 109 | self.proxy.connect("notify::icon-name", self._on_icon_name_changed) |
96 | 110 | |
97 | 111 | self.in_widget = False |
98 | 112 | self.plain_surface = None |
99 | 113 | self.saturated_surface = None |
114 | ||
115 | self.menu_opened = False | |
100 | 116 | |
101 | 117 | self.connect("button-press-event", self.on_button_press) |
102 | 118 | self.connect("button-release-event", self.on_button_release) |
174 | 190 | self.image.set_pixel_size(self.size - ICON_SIZE_REDUCTION) |
175 | 191 | self.image.set_from_icon_name("image-missing", Gtk.IconSize.MENU) |
176 | 192 | |
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 | ||
177 | 206 | # TODO? |
178 | 207 | def on_enter_notify(self, widget, event): |
179 | 208 | self.in_widget = True |
186 | 215 | return Gdk.EVENT_PROPAGATE |
187 | 216 | # /TODO |
188 | 217 | |
189 | def after_release_idle(self, data=None): | |
190 | self.set_active(False) | |
191 | ||
192 | return GLib.SOURCE_REMOVE | |
193 | ||
194 | 218 | def on_button_press(self, widget, event): |
219 | self.menu_opened = False | |
220 | ||
195 | 221 | # If the user does ctrl->right-click, open the applet's about menu |
196 | 222 | # instead of sending to the app. |
197 | 223 | if event.state & Gdk.ModifierType.CONTROL_MASK and event.button == Gdk.BUTTON_SECONDARY: |
202 | 228 | x, y = self.calc_menu_origin(widget, orientation) |
203 | 229 | self.proxy.call_button_press(x, y, event.button, event.time, orientation, None, None) |
204 | 230 | |
205 | if event.button != Gdk.BUTTON_PRIMARY: | |
206 | self.set_active(True) | |
207 | ||
208 | 231 | if event.button in (Gdk.BUTTON_MIDDLE, Gdk.BUTTON_SECONDARY): |
209 | 232 | # Block the 'remove from panel' menu, and the middle-click drag. |
210 | 233 | # They can still accomplish these things along the edges of the applet |
211 | 234 | return Gdk.EVENT_STOP |
212 | 235 | |
213 | return Gdk.EVENT_PROPAGATE | |
236 | return Gdk.EVENT_STOP | |
214 | 237 | |
215 | 238 | def on_button_release(self, widget, event): |
216 | 239 | orientation = translate_applet_orientation_to_xapp(self.orientation) |
217 | 240 | |
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 | ||
218 | 246 | x, y = self.calc_menu_origin(widget, orientation) |
219 | 247 | self.proxy.call_button_release(x, y, event.button, event.time, orientation, None, None) |
220 | ||
221 | GObject.timeout_add(200, self.after_release_idle) | |
222 | 248 | |
223 | 249 | return Gdk.EVENT_PROPAGATE |
224 | 250 |