By using this site, you agree to have cookies stored on your device, strictly for functional purposes, such as storing your session and preferences.

Dismiss

 __init__.py

View raw Download
text/plain • 8.31 kiB
Python script, ASCII text executable
        
            
1
"""
2
MATE-like app menu applet for the Panorama panel.
3
Copyright 2025, roundabout-host.com <vlad@roundabout-host.com>
4
5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public Licence as published by
7
the Free Software Foundation, either version 3 of the Licence, or
8
(at your option) any later version.
9
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public Licence for more details.
14
15
You should have received a copy of the GNU General Public Licence
16
along with this program. If not, see <https://www.gnu.org/licenses/>.
17
"""
18
19
import os
20
from pathlib import Path
21
import locale
22
import panorama_panel
23
24
import gi
25
gi.require_version("Gtk", "4.0")
26
27
from gi.repository import Gtk, GLib, Gio, Gdk
28
29
30
module_directory = Path(__file__).resolve().parent
31
32
33
CATEGORY_MAPPINGS = {
34
"Utility": {"menu_name": "Accessories", "icon": "applications-accessories"},
35
"Development": {"menu_name": "Programming", "icon": "applications-development"},
36
"Game": {"menu_name": "Games", "icon": "applications-games"},
37
"Graphics": {"menu_name": "Graphics", "icon": "applications-graphics"},
38
"Network": {"menu_name": "Network", "icon": "applications-internet"},
39
"AudioVideo": {"menu_name": "Multimedia", "icon": "applications-multimedia"},
40
"Office": {"menu_name": "Office", "icon": "applications-office"},
41
"Science": {"menu_name": "Science", "icon": "applications-science"},
42
"Education": {"menu_name": "Education", "icon": "applications-education"},
43
"System": {"menu_name": "System", "icon": "applications-system"},
44
"Settings": {"menu_name": "Settings", "icon": "preferences-desktop"},
45
"Other": {"menu_name": "Other", "icon": "applications-other"},
46
}
47
48
custom_css = """
49
.no-menu-item-padding:dir(ltr) {
50
padding-left: 0;
51
}
52
53
.no-menu-item-padding:dir(rtl) {
54
padding-right: 0;
55
}
56
"""
57
58
css_provider = Gtk.CssProvider()
59
css_provider.load_from_data(custom_css)
60
Gtk.StyleContext.add_provider_for_display(
61
Gdk.Display.get_default(),
62
css_provider,
63
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
64
)
65
66
67
locale.bindtextdomain("panorama-app-menu", module_directory / "locale")
68
_ = lambda x: locale.dgettext("panorama-app-menu", x)
69
70
71
class AppMenu(panorama_panel.Applet):
72
name = _("App menu")
73
description = _("Show apps installed on your system, grouped by category")
74
75
def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None):
76
super().__init__(orientation=orientation, config=config)
77
locale.bindtextdomain("panorama-app-menu", module_directory / "locale")
78
_ = lambda x: locale.dgettext("panorama-app-menu", x)
79
if config is None:
80
config = {}
81
82
self.auto_refresh = config.get("auto_refresh", True)
83
self.category_mappings = config.get("category_mappings", CATEGORY_MAPPINGS)
84
self.trigger_name = config.get("trigger_name", "app-menu")
85
self.icon_name = config.get("icon_name", "start-here-symbolic")
86
87
self.button = Gtk.MenuButton()
88
self.button.set_has_frame(False) # flat look
89
self.icon = Gtk.Image.new_from_icon_name(self.icon_name)
90
self.icon.set_pixel_size(config.get("icon_size", 24))
91
self.button.set_child(self.icon)
92
self.apps_by_id: dict[int, Gio.AppInfo] = {}
93
# Wait for the widget to be in a layer-shell window before doing this
94
self.connect("realize", lambda *args: self.add_trigger_to_app())
95
96
self.menu = Gio.Menu()
97
self.popover = Gtk.PopoverMenu.new_from_model_full(self.menu, Gtk.PopoverMenuFlags.NESTED)
98
self.popover.set_has_arrow(False)
99
panorama_panel.track_popover(self.popover)
100
self.button.set_popover(self.popover)
101
102
self.generate_app_menu()
103
104
self.append(self.button)
105
106
self.context_menu = self.make_context_menu()
107
panorama_panel.track_popover(self.context_menu)
108
109
right_click_controller = Gtk.GestureClick()
110
right_click_controller.set_button(3)
111
right_click_controller.connect("pressed", self.show_context_menu)
112
113
self.add_controller(right_click_controller)
114
115
action_group = Gio.SimpleActionGroup()
116
options_action = Gio.SimpleAction.new("options", None)
117
options_action.connect("activate", self.show_options)
118
action_group.add_action(options_action)
119
options_action = Gio.SimpleAction.new("launch-app", GLib.VariantType.new("s"))
120
options_action.connect("activate", self.launch_app)
121
action_group.add_action(options_action)
122
self.insert_action_group("applet", action_group)
123
124
if self.auto_refresh:
125
self.app_info_monitor = Gio.AppInfoMonitor.get()
126
self.app_info_monitor.connect("changed", self.generate_app_menu)
127
128
self.options_window = None
129
130
def add_trigger_to_app(self):
131
app: Gtk.Application = self.get_root().get_application()
132
action = Gio.SimpleAction.new(self.trigger_name, None)
133
action.connect("activate", lambda *args: self.button.popup())
134
app.add_action(action)
135
136
def launch_app(self, action, id: GLib.Variant):
137
app = self.apps_by_id[int(id.get_string())]
138
app.launch()
139
140
def make_context_menu(self):
141
menu = Gio.Menu()
142
menu.append(_("Menu _options"), "applet.options")
143
context_menu = Gtk.PopoverMenu.new_from_model(menu)
144
context_menu.set_has_arrow(False)
145
context_menu.set_parent(self)
146
context_menu.set_halign(Gtk.Align.START)
147
context_menu.set_flags(Gtk.PopoverMenuFlags.NESTED)
148
return context_menu
149
150
def show_context_menu(self, gesture, n_presses, x, y):
151
rect = Gdk.Rectangle()
152
rect.x = int(x)
153
rect.y = int(y)
154
rect.width = 1
155
rect.height = 1
156
157
self.context_menu.set_pointing_to(rect)
158
self.context_menu.popup()
159
160
def show_options(self, _0=None, _1=None):
161
...
162
163
def shutdown(self, app: Gtk.Application):
164
app.remove_action(self.trigger_name)
165
166
def get_config(self):
167
return {
168
"category_mappings": self.category_mappings,
169
"trigger_name": self.trigger_name,
170
"icon_name": self.icon_name,
171
"icon_size": self.icon.get_pixel_size(),
172
}
173
174
def set_panel_position(self, position):
175
self.popover.set_position(panorama_panel.OPPOSITE_POSITION[position])
176
self.button.set_direction(panorama_panel.POSITION_TO_ARROW[panorama_panel.OPPOSITE_POSITION[position]])
177
178
def generate_app_menu(self, app_info_monitor=None):
179
self.menu.remove_all()
180
self.apps_by_id = {}
181
182
all_apps = Gio.AppInfo.get_all()
183
apps_by_category: dict[str, list[Gio.AppInfo]] = {}
184
for category, info in self.category_mappings.items():
185
apps_by_category[category] = []
186
187
for app in all_apps:
188
category_found = False
189
if isinstance(app, Gio.DesktopAppInfo):
190
if app.get_categories() is not None:
191
categories = app.get_categories().split(";")
192
for category in categories:
193
if category in apps_by_category:
194
apps_by_category[category].append(app)
195
category_found = True
196
197
if not category_found:
198
apps_by_category["Other"].append(app)
199
200
for apps in apps_by_category.values():
201
apps.sort(key=lambda app: app.get_display_name())
202
203
for category, info in self.category_mappings.items():
204
if apps_by_category[category]:
205
item = Gio.MenuItem.new(_(info["menu_name"]))
206
item.set_icon(Gio.ThemedIcon.new(info["icon"]))
207
submenu = Gio.Menu()
208
209
for app in apps_by_category[category]:
210
if isinstance(app, Gio.DesktopAppInfo) and (app.get_is_hidden() or app.get_nodisplay()):
211
continue
212
213
subitem = Gio.MenuItem.new(app.get_display_name(), f"applet.launch-app::{id(app)}")
214
subitem.set_icon(app.get_icon() or Gio.ThemedIcon.new("image-missing"))
215
self.apps_by_id[id(app)] = app
216
submenu.append_item(subitem)
217
218
item.set_submenu(submenu)
219
self.menu.append_item(item)
220
221
panorama_panel.add_icons_to_menu(self.popover)
222