Codebase list xapp / 56641c6
mate-xapp-status-applet.py: Make improvements to event handling and widget appearance during clicks. Michael Webster 4 years ago
1 changed file(s) with 53 addition(s) and 33 deletion(s). Raw diff Collapse all Expand all
1919 locale.bindtextdomain("xapp", "@locale@")
2020 locale.textdomain("xapp")
2121
22 INDICATOR_BOX_BORDER = 1
23 INDICATOR_BOX_BORDER_COMP = INDICATOR_BOX_BORDER + 1
24 ICON_SIZE_REDUCTION = (INDICATOR_BOX_BORDER * 2)
25 ICON_SPACING = 5
22 ICON_SIZE_REDUCTION = 2
2623 VISIBLE_LABEL_MARGIN = 5 # When an icon has a label, add a margin between icon and label
2724
2825 statusicon_css_string = """
29 .statuswidget {
26 .statuswidget-horizontal {
3027 border: none;
3128 padding-top: 0;
32 padding-left: 4px;
29 padding-left: 2px;
3330 padding-bottom: 0;
34 padding-right: 4px;
31 padding-right: 2px;
32 }
33
34 .statuswidget-vertical {
35 border: none;
36 padding-top: 2px;
37 padding-left: 0;
38 padding-bottom: 2px;
39 padding-right: 0;
3540 }
3641 """
3742
4651 elif mate_applet_orientation == MatePanelApplet.AppletOrient.RIGHT:
4752 return Gtk.PositionType.LEFT
4853
49 class StatusWidget(Gtk.Button):
54 class StatusWidget(Gtk.ToggleButton):
5055 def __init__(self, icon, orientation, size):
5156 super(Gtk.Button, self).__init__()
5257 self.theme = Gtk.IconTheme.get_default()
5358 self.orientation = orientation
5459 self.size = size
5560
56 self.get_style_context().add_class("statuswidget")
57
5861 self.proxy = icon
5962
6063 # this is the bus owned name
6871 self.image = Gtk.Image(hexpand=True)
6972 self.image.show()
7073 self.label = Gtk.Label(no_show_all=True)
71 self.box.pack_start(self.image, False, False, 0)
74 self.box.pack_start(self.image, True, False, 0)
7275 self.box.pack_start(self.label, False, False, 0)
7376 self.add(self.box)
77
78 self.set_can_default(False)
79 self.set_can_focus(False)
80 self.set_relief(Gtk.ReliefStyle.NONE)
81 self.set_focus_on_click(False)
7482
7583 flags = GObject.BindingFlags.DEFAULT | GObject.BindingFlags.SYNC_CREATE
7684
102110
103111 self.set_icon(string)
104112
113 def update_style(self, orientation):
114 ctx = self.get_style_context()
115
116 if orientation == Gtk.Orientation.HORIZONTAL:
117 ctx.remove_class("statuswidget-vertical")
118 ctx.add_class("statuswidget-horizontal")
119 else:
120 ctx.remove_class("statuswidget-horizontal")
121 ctx.add_class("statuswidget-vertical")
122
105123 def update_orientation(self):
106 box_orientation = Gtk.Orientation.HORIZONTAL
107
108 if self.orientation in (MatePanelApplet.AppletOrient.LEFT, MatePanelApplet.AppletOrient.RIGHT):
124 if self.orientation in (MatePanelApplet.AppletOrient.UP, MatePanelApplet.AppletOrient.DOWN):
125 box_orientation = Gtk.Orientation.HORIZONTAL
126 else:
109127 box_orientation = Gtk.Orientation.VERTICAL
110128
129 self.update_style(box_orientation)
111130 self.box.set_orientation(box_orientation)
112131
113132 if len(self.label.props.label) > 0 and box_orientation == Gtk.Orientation.HORIZONTAL:
119138
120139 def set_icon(self, string):
121140 size = self.size - ICON_SIZE_REDUCTION
122
123 # round down to even
124 if size % 2 != 0:
125 size -= 1
126141
127142 self.image.set_pixel_size(size)
128143
157172 return Gdk.EVENT_PROPAGATE
158173 # /TODO
159174
175 def after_release_idle(self, data=None):
176 self.set_active(False)
177
178 return GLib.SOURCE_REMOVE
179
160180 def on_button_press(self, widget, event):
161181 orientation = translate_applet_orientation_to_xapp(self.orientation)
162182
163183 x, y = self.calc_menu_origin(widget, orientation)
164184 self.proxy.call_button_press(x, y, event.button, event.time, orientation, None, None)
165185
166 self.set_state_flags(Gtk.StateFlags.SELECTED, False)
167
168 if event.button == 3:
186 if event.button != Gdk.BUTTON_PRIMARY:
187 self.set_active(True)
188
189 if event.button in (Gdk.BUTTON_MIDDLE, Gdk.BUTTON_SECONDARY):
190 # Block the 'remove from panel' menu, and the middle-click drag.
191 # They can still accomplish these things along the edges of the applet
169192 return Gdk.EVENT_STOP
170193
171194 return Gdk.EVENT_PROPAGATE
175198
176199 x, y = self.calc_menu_origin(widget, orientation)
177200 self.proxy.call_button_release(x, y, event.button, event.time, orientation, None, None)
178 self.set_state_flags(Gtk.StateFlags.SELECTED, False)
179
180 if event.button == 3:
181 return Gdk.EVENT_STOP
201
202 GObject.timeout_add(200, self.after_release_idle)
182203
183204 return Gdk.EVENT_PROPAGATE
184205
190211
191212 if orientation == Gtk.PositionType.TOP:
192213 rx = x + alloc.x
193 ry = y + alloc.y + alloc.height + INDICATOR_BOX_BORDER_COMP
214 ry = y + alloc.y + alloc.height
194215 elif orientation == Gtk.PositionType.BOTTOM:
195216 rx = x + alloc.x
196 ry = y + alloc.y - INDICATOR_BOX_BORDER_COMP
217 ry = y + alloc.y
197218 elif orientation == Gtk.PositionType.LEFT:
198 rx = x + alloc.x + alloc.width + INDICATOR_BOX_BORDER_COMP
219 rx = x + alloc.x + alloc.width
199220 ry = y + alloc.y
200221 elif orientation == Gtk.PositionType.RIGHT:
201 rx = x + alloc.x - INDICATOR_BOX_BORDER_COMP
222 rx = x + alloc.x
202223 ry = y + alloc.y
203224 else:
204225 rx = x
209230 class MateXAppStatusApplet(object):
210231 def __init__(self, applet, iid):
211232 self.applet = applet
212 self.applet.set_flags(MatePanelApplet.AppletFlags.EXPAND_MINOR)
233 self.applet.set_flags(MatePanelApplet.AppletFlags.EXPAND_MINOR |
234 MatePanelApplet.AppletFlags.HAS_HANDLE)
213235 self.applet.set_can_focus(False)
214236 self.applet.set_background_widget(self.applet)
215237
225247 self.monitor = None
226248
227249 def on_applet_realized(self, widget, data=None):
228 self.indicator_box = Gtk.Box(spacing=ICON_SPACING,
229 visible=True,
230 border_width=INDICATOR_BOX_BORDER)
250 self.indicator_box = Gtk.Box(visible=True)
231251
232252 self.applet.add(self.indicator_box)
233253 self.applet.connect("change-size", self.on_applet_size_changed)