Codebase list xapp / c1e2b64 status-applets / mate / mate-xapp-status-applet.py
c1e2b64

Tree @c1e2b64 (Download .tar.gz)

mate-xapp-status-applet.py @c1e2b64raw · history · blame

  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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#!/usr/bin/python3

import locale
import gettext
import json
import os
import sys
import setproctitle

import gi
gi.require_version("Gtk", "3.0")
gi.require_version("XApp", "1.0")
gi.require_version('MatePanelApplet', '4.0')
from gi.repository import Gtk, GdkPixbuf, Gdk, GObject, Gio, XApp, GLib, MatePanelApplet

import applet_constants

# Rename the process
setproctitle.setproctitle('mate-xapp-status-applet')

# i18n
gettext.install("xapp", applet_constants.LOCALEDIR)
locale.bindtextdomain("xapp", applet_constants.LOCALEDIR)
locale.textdomain("xapp")

ICON_SIZE_REDUCTION = 2
VISIBLE_LABEL_MARGIN = 5 # When an icon has a label, add a margin between icon and label
SYMBOLIC_ICON_SIZE = 22

statusicon_css_string = """
.statuswidget-horizontal {
    border: none;
    padding-top: 0;
    padding-left: 2px;
    padding-bottom: 0;
    padding-right: 2px;
}
.statuswidget-vertical {
    border: none;
    padding-top: 2px;
    padding-left: 0;
    padding-bottom: 2px;
    padding-right: 0;
}
"""

def translate_applet_orientation_to_xapp(mate_applet_orientation):
    # wtf...mate panel's orientation is.. the direction to center of monitor?
    if mate_applet_orientation == MatePanelApplet.AppletOrient.UP:
        return Gtk.PositionType.BOTTOM
    elif mate_applet_orientation == MatePanelApplet.AppletOrient.DOWN:
        return Gtk.PositionType.TOP
    elif mate_applet_orientation == MatePanelApplet.AppletOrient.LEFT:
        return Gtk.PositionType.RIGHT
    elif mate_applet_orientation == MatePanelApplet.AppletOrient.RIGHT:
        return Gtk.PositionType.LEFT

class StatusWidget(Gtk.ToggleButton):
    __gsignals__ = {
        "re-sort": (GObject.SignalFlags.RUN_LAST, None, ())
    }

    def __init__(self, icon, orientation, size):
        super(Gtk.ToggleButton, self).__init__()
        self.theme = Gtk.IconTheme.get_default()
        self.orientation = orientation
        self.size = size

        self.proxy = icon
        self.proxy.props.icon_size = size

        # this is the bus owned name
        self.name = self.proxy.get_name()

        self.add_events(Gdk.EventMask.SCROLL_MASK)

        # this is (usually) the name of the remote process
        self.proc_name = self.proxy.props.name

        self.box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL)

        self.image = Gtk.Image(hexpand=True)
        self.label = Gtk.Label(no_show_all=True)
        self.box.pack_start(self.image, True, False, 0)
        self.box.pack_start(self.label, False, False, 0)
        self.add(self.box)

        self.set_can_default(False)
        self.set_can_focus(False)
        self.set_relief(Gtk.ReliefStyle.NONE)
        self.set_focus_on_click(False)

        self.show_all()

        flags = GObject.BindingFlags.DEFAULT | GObject.BindingFlags.SYNC_CREATE

        self.proxy.bind_property("label", self.label, "label", flags)
        self.proxy.bind_property("tooltip-text", self, "tooltip-markup", flags)
        self.proxy.bind_property("visible", self, "visible", flags)

        self.proxy.connect("notify::primary-menu-is-open", self.menu_state_changed)
        self.proxy.connect("notify::secondary-menu-is-open", self.menu_state_changed)

        self.highlight_both_menus = False

        if self.proxy.props.metadata not in ("", None):
            try:
                meta = json.loads(self.proxy.props.metadata)
                if meta["highlight-both-menus"]:
                    self.highlight_both_menus = True
            except json.JSONDecodeError as e:
                print("Could not read metadata: %s" % e)

        self.proxy.connect("notify::icon-name", self._on_icon_name_changed)
        self.proxy.connect("notify::name", self._on_name_changed)

        self.in_widget = False
        self.plain_surface = None
        self.saturated_surface = None

        self.menu_opened = False

        self.connect("button-press-event", self.on_button_press)
        self.connect("button-release-event", self.on_button_release)
        self.connect("scroll-event", self.on_scroll)
        self.connect("enter-notify-event", self.on_enter_notify)
        self.connect("leave-notify-event", self.on_leave_notify)

        self.update_orientation()
        self.update_icon()

    def _on_icon_name_changed(self, proxy, gparamspec, data=None):
        self.update_icon()

    def _on_name_changed(self, proxy, gparamspec, data=None):
        self.emit("re-sort")

    def update_icon(self):
        string = self.proxy.props.icon_name
        self.proxy.props.icon_size = self.size

        self.set_icon(string)

    def update_style(self, orientation):
        ctx = self.get_style_context()

        if orientation == Gtk.Orientation.HORIZONTAL:
            ctx.remove_class("statuswidget-vertical")
            ctx.add_class("statuswidget-horizontal")
        else:
            ctx.remove_class("statuswidget-horizontal")
            ctx.add_class("statuswidget-vertical")

    def update_orientation(self):
        if self.orientation in (MatePanelApplet.AppletOrient.UP, MatePanelApplet.AppletOrient.DOWN):
            box_orientation = Gtk.Orientation.HORIZONTAL
        else:
            box_orientation = Gtk.Orientation.VERTICAL

        self.update_style(box_orientation)
        self.box.set_orientation(box_orientation)

        if len(self.label.props.label) > 0 and box_orientation == Gtk.Orientation.HORIZONTAL:
            self.label.set_visible(True)
            self.label.set_margin_start(VISIBLE_LABEL_MARGIN)
        else:
            self.label.set_visible(False)
            self.label.set_margin_start(0)

    def set_icon(self, string):
        fallback = True

        if string:
            if "symbolic" in string:
                size = SYMBOLIC_ICON_SIZE
            else:
                size = self.size - ICON_SIZE_REDUCTION

            self.image.set_pixel_size(size)

            try:
                if os.path.exists(string):
                    icon_file = Gio.File.new_for_path(string)
                    icon = Gio.FileIcon.new(icon_file)
                    self.image.set_from_gicon(icon, Gtk.IconSize.MENU)
                else:
                    if self.theme.has_icon(string):
                        icon = Gio.ThemedIcon.new(string)
                        self.image.set_from_gicon(icon, Gtk.IconSize.MENU)

                fallback = False
            except GLib.Error as e:
                print("MateXAppStatusApplet: Could not load icon '%s' for '%s': %s" % (string, self.proc_name, e.message))
            except TypeError as e:
                print("MateXAppStatusApplet: Could not load icon '%s' for '%s': %s" % (string, self.proc_name, str(e)))

        #fallback
        if fallback:
            self.image.set_pixel_size(self.size - ICON_SIZE_REDUCTION)
            self.image.set_from_icon_name("image-missing", Gtk.IconSize.MENU)

    def menu_state_changed(self, proxy, pspec, data=None):
        if pspec.name == "primary-menu-is-open":
            prop = proxy.props.primary_menu_is_open
        else:
            prop = proxy.props.secondary_menu_is_open

        if not self.menu_opened or prop == False:
            self.set_active(False)
            return

        self.set_active(prop)
        self.menu_opened = False

    # TODO?
    def on_enter_notify(self, widget, event):
        self.in_widget = True

        return Gdk.EVENT_PROPAGATE

    def on_leave_notify(self, widget, event):
        self.in_widget = False

        return Gdk.EVENT_PROPAGATE
    # /TODO

    def on_button_press(self, widget, event):
        self.menu_opened = False

        # If the user does ctrl->right-click, open the applet's about menu
        # instead of sending to the app.
        if event.state & Gdk.ModifierType.CONTROL_MASK and event.button == Gdk.BUTTON_SECONDARY:
           return Gdk.EVENT_PROPAGATE

        orientation = translate_applet_orientation_to_xapp(self.orientation)

        x, y = self.calc_menu_origin(widget, orientation)
        self.proxy.call_button_press(x, y, event.button, event.time, orientation, None, None)

        if event.button in (Gdk.BUTTON_MIDDLE, Gdk.BUTTON_SECONDARY):
            # Block the 'remove from panel' menu, and the middle-click drag.
            # They can still accomplish these things along the edges of the applet
            return Gdk.EVENT_STOP

        return Gdk.EVENT_STOP

    def on_button_release(self, widget, event):
        orientation = translate_applet_orientation_to_xapp(self.orientation)

        if event.button == Gdk.BUTTON_PRIMARY:
            self.menu_opened = True
        elif event.button == Gdk.BUTTON_SECONDARY and self.highlight_both_menus:
            self.menu_opened = True

        x, y = self.calc_menu_origin(widget, orientation)
        self.proxy.call_button_release(x, y, event.button, event.time, orientation, None, None)

        return Gdk.EVENT_PROPAGATE

    def on_scroll(self, widget, event):
        has, direction = event.get_scroll_direction()

        x_dir = XApp.ScrollDirection.UP
        delta = 0

        if direction != Gdk.ScrollDirection.SMOOTH:
            x_dir = XApp.ScrollDirection(int(direction))

            if direction == Gdk.ScrollDirection.UP:
                delta = -1
            elif direction == Gdk.ScrollDirection.DOWN:
                delta = 1
            elif direction == Gdk.ScrollDirection.LEFT:
                delta = -1
            elif direction == Gdk.ScrollDirection.RIGHT:
                delta = 1

        self.proxy.call_scroll_sync(delta, x_dir, event.time, None)

    def calc_menu_origin(self, widget, orientation):
        alloc = widget.get_allocation()
        ignore, x, y = widget.get_window().get_origin()
        rx = 0
        ry = 0

        if orientation == Gtk.PositionType.TOP:
            rx = x + alloc.x
            ry = y + alloc.y + alloc.height
        elif orientation == Gtk.PositionType.BOTTOM:
            rx = x + alloc.x
            ry = y + alloc.y
        elif orientation == Gtk.PositionType.LEFT:
            rx = x + alloc.x + alloc.width
            ry = y + alloc.y
        elif orientation == Gtk.PositionType.RIGHT:
            rx = x + alloc.x
            ry = y + alloc.y
        else:
            rx = x
            ry = y

        return rx, ry

class MateXAppStatusApplet(object):
    def __init__(self, applet, iid):
        self.applet = applet
        self.applet.set_flags(MatePanelApplet.AppletFlags.EXPAND_MINOR)
        self.applet.set_can_focus(False)
        self.applet.set_background_widget(self.applet)

        self.add_about()

        button_css = Gtk.CssProvider()

        if button_css.load_from_data(statusicon_css_string.encode()):
            Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), button_css, 600)

        self.applet.connect("realize", self.on_applet_realized)
        self.applet.connect("destroy", self.on_applet_destroy)

        self.indicators = {}
        self.monitor = None

    def add_about(self):
        group = Gtk.ActionGroup(name="xapp-status-applet-group")
        group.set_translation_domain("xapp")

        about_action = Gtk.Action(name="ShowAbout",
                                  icon_name="info",
                                  label=_("About"),
                                  visible=True)

        about_action.connect("activate", self.show_about)
        group.add_action(about_action)

        xml = '\
            <menuitem name="ShowDesktopAboutItem" action="ShowAbout"/> \
        '

        self.applet.setup_menu(xml, group)

    def show_about(self, action, data=None):
        dialog = Gtk.AboutDialog.new()

        dialog.set_program_name("XApp Status Applet")
        dialog.set_version(applet_constants.PKGVERSION)
        dialog.set_license_type(Gtk.License.GPL_3_0)
        dialog.set_website("https://github.com/linuxmint/xapps")
        dialog.set_logo_icon_name("panel-applets")
        dialog.set_comments(_("Area where XApp status icons appear"))

        dialog.run()
        dialog.destroy()

    def on_applet_realized(self, widget, data=None):
        self.indicator_box = Gtk.Box(visible=True)

        self.applet.add(self.indicator_box)
        self.applet.connect("change-size", self.on_applet_size_changed)
        self.applet.connect("change-orient", self.on_applet_orientation_changed)
        self.update_orientation()

        if not self.monitor:
            self.setup_monitor()

    def on_applet_destroy(self, widget, data=None):
        self.destroy_monitor()
        Gtk.main_quit()

    def setup_monitor (self):
        self.monitor = XApp.StatusIconMonitor()
        self.monitor.connect("icon-added", self.on_icon_added)
        self.monitor.connect("icon-removed", self.on_icon_removed)

    def make_key(self, proxy):
        name = proxy.get_name()
        path = proxy.get_object_path()

        print("Key: %s" % (name+path))
        return name + path

    def destroy_monitor (self):
        for key in self.indicators.keys():
            self.indicator_box.remove(self.indicators[key])

        self.monitor = None
        self.indicators = {}

    def on_icon_added(self, monitor, proxy):
        key = self.make_key(proxy)

        self.indicators[key] = StatusWidget(proxy, self.applet.get_orient(), self.applet.get_size())
        self.indicator_box.add(self.indicators[key])
        self.indicators[key].connect("re-sort", self.sort_icons)

        self.sort_icons()

    def on_icon_removed(self, monitor, proxy):
        key = self.make_key(proxy)

        self.indicator_box.remove(self.indicators[key])
        self.indicators[key].disconnect_by_func(self.sort_icons)
        del(self.indicators[key])

        self.sort_icons()

    def update_orientation(self):
        self.on_applet_orientation_changed(self, self.applet.get_orient())

    def on_applet_size_changed(self, applet, size, data=None):
        for key in self.indicators.keys():
            indicator = self.indicators[key]

            indicator.size = applet.get_size()
            indicator.update_icon()

        self.applet.queue_resize()

    def on_applet_orientation_changed(self, applet, applet_orient, data=None):
        orient = self.applet.get_orient()

        for key in self.indicators.keys():
            indicator = self.indicators[key]

            indicator.orientation = orient
            indicator.update_orientation()

        if orient in (MatePanelApplet.AppletOrient.LEFT, MatePanelApplet.AppletOrient.RIGHT):
            self.indicator_box.set_orientation(Gtk.Orientation.VERTICAL)

            self.indicator_box.props.margin_start = 0
            self.indicator_box.props.margin_end = 0
            self.indicator_box.props.margin_top = 2
            self.indicator_box.props.margin_bottom = 2
        else:
            self.indicator_box.set_orientation(Gtk.Orientation.HORIZONTAL)

            self.indicator_box.props.margin_start = 2
            self.indicator_box.props.margin_end = 2
            self.indicator_box.props.margin_top = 0
            self.indicator_box.props.margin_bottom = 0

        self.indicator_box.queue_resize()

    def sort_icons(self, status_widget=None):
        icon_list = list(self.indicators.values())

        # for i in icon_list:
        #     print("before: ", i.proxy.props.icon_name, i.proxy.props.name.lower())

        icon_list.sort(key=lambda icon: icon.proxy.props.name.lower())
        icon_list.sort(key=lambda icon: icon.proxy.props.icon_name.lower().endswith("symbolic"))

        # for i in icon_list:
        #     print("after: ", i.proxy.props.icon_name, i.proxy.props.name.lower())

        icon_list.reverse()

        for icon in icon_list:
            self.indicator_box.reorder_child(icon, 0)

def applet_factory(applet, iid, data):
    MateXAppStatusApplet(applet, iid)
    applet.show()
    return True

def quit_all(widget):
    Gtk.main_quit()
    sys.exit(0)

MatePanelApplet.Applet.factory_main("MateXAppStatusAppletFactory", True,
                                    MatePanelApplet.Applet.__gtype__,
                                    applet_factory, None)