__init__.py
Python script, ASCII text executable
1""" 2MATE-like app menu applet for the Panorama panel. 3Copyright 2025, roundabout-host.com <vlad@roundabout-host.com> 4 5This program is free software: you can redistribute it and/or modify 6it under the terms of the GNU General Public Licence as published by 7the Free Software Foundation, either version 3 of the Licence, or 8(at your option) any later version. 9 10This program is distributed in the hope that it will be useful, 11but WITHOUT ANY WARRANTY; without even the implied warranty of 12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13GNU General Public Licence for more details. 14 15You should have received a copy of the GNU General Public Licence 16along with this program. If not, see <https://www.gnu.org/licenses/>. 17""" 18 19import os 20from pathlib import Path 21import locale 22import panorama_panel 23 24import gi 25gi.require_version("Gtk", "4.0") 26 27from gi.repository import Gtk, GLib, Gio, Gdk 28 29 30module_directory = Path(__file__).resolve().parent 31 32 33CATEGORY_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 48custom_css = """ 49.no-menu-item-padding:dir(ltr) { 50padding-left: 0; 51} 52 53.no-menu-item-padding:dir(rtl) { 54padding-right: 0; 55} 56""" 57 58css_provider = Gtk.CssProvider() 59css_provider.load_from_data(custom_css) 60Gtk.StyleContext.add_provider_for_display( 61Gdk.Display.get_default(), 62css_provider, 63Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION 64) 65 66 67def force_visible_on_visible_notify(widget, *args): 68if not widget.get_visible(): 69widget.set_visible(True) 70 71 72def add_icons_to_menu(popover: Gtk.PopoverMenu): 73section = popover.get_child().get_first_child().get_first_child().get_first_child() 74while section is not None: 75child = section.get_first_child().get_first_child() 76 77while child is not None: 78gutter_box: Gtk.Box = child.get_first_child() 79if isinstance(gutter_box.get_next_sibling(), Gtk.Image): 80# For some reason GTK creates images but they're hidden? 81image: Gtk.Image = gutter_box.get_next_sibling() 82label: Gtk.Label = image.get_next_sibling() 83 84image.set_icon_size(Gtk.IconSize.LARGE) 85image.set_margin_start(8) 86image.set_margin_end(8) 87 88child.add_css_class("no-menu-item-padding") 89 90# Push the arrow to the left 91label.set_halign(Gtk.Align.FILL) 92label.set_hexpand(True) 93label.set_xalign(0) 94 95# GTK pushes its stance on icons so hard it makes them invisible multiple times; 96# force it visible 97image.set_visible(True) 98image.connect("notify::visible", force_visible_on_visible_notify) 99 100# Find the submenu if there is one 101subchild = child.get_first_child() 102submenu = None 103while subchild is not None: 104if isinstance(subchild, Gtk.PopoverMenu): 105submenu = subchild 106subchild = subchild.get_next_sibling() 107 108# Recursive 109if submenu is not None: 110add_icons_to_menu(submenu) 111 112child = child.get_next_sibling() 113 114section = section.get_next_sibling() 115 116 117locale.bindtextdomain("panorama-app-menu", module_directory / "locale") 118_ = lambda x: locale.dgettext("panorama-app-menu", x) 119 120 121class AppMenu(panorama_panel.Applet): 122name = _("App menu") 123description = _("Show apps installed on your system, grouped by category") 124 125def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None): 126super().__init__(orientation=orientation, config=config) 127locale.bindtextdomain("panorama-app-menu", module_directory / "locale") 128_ = lambda x: locale.dgettext("panorama-app-menu", x) 129if config is None: 130config = {} 131 132self.auto_refresh = config.get("auto_refresh", True) 133self.category_mappings = config.get("category_mappings", CATEGORY_MAPPINGS) 134self.trigger_name = config.get("trigger_name", "app-menu") 135self.icon_name = config.get("icon_name", "start-here-symbolic") 136 137self.button = Gtk.MenuButton() 138self.button.set_has_frame(False) # flat look 139self.icon = Gtk.Image.new_from_icon_name(self.icon_name) 140self.icon.set_pixel_size(config.get("icon_size", 24)) 141self.button.set_child(self.icon) 142self.apps_by_id: dict[int, Gio.AppInfo] = {} 143# Wait for the widget to be in a layer-shell window before doing this 144self.connect("realize", lambda *args: self.add_trigger_to_app()) 145 146self.menu = Gio.Menu() 147self.popover = Gtk.PopoverMenu.new_from_model_full(self.menu, Gtk.PopoverMenuFlags.NESTED) 148self.popover.set_has_arrow(False) 149panorama_panel.track_popover(self.popover) 150self.button.set_popover(self.popover) 151 152self.generate_app_menu() 153 154self.append(self.button) 155 156self.context_menu = self.make_context_menu() 157panorama_panel.track_popover(self.context_menu) 158 159right_click_controller = Gtk.GestureClick() 160right_click_controller.set_button(3) 161right_click_controller.connect("pressed", self.show_context_menu) 162 163self.add_controller(right_click_controller) 164 165action_group = Gio.SimpleActionGroup() 166options_action = Gio.SimpleAction.new("options", None) 167options_action.connect("activate", self.show_options) 168action_group.add_action(options_action) 169options_action = Gio.SimpleAction.new("launch-app", GLib.VariantType.new("s")) 170options_action.connect("activate", self.launch_app) 171action_group.add_action(options_action) 172self.insert_action_group("applet", action_group) 173 174if self.auto_refresh: 175self.app_info_monitor = Gio.AppInfoMonitor.get() 176self.app_info_monitor.connect("changed", self.generate_app_menu) 177 178self.options_window = None 179 180def add_trigger_to_app(self): 181app: Gtk.Application = self.get_root().get_application() 182action = Gio.SimpleAction.new(self.trigger_name, None) 183action.connect("activate", lambda *args: self.button.popup()) 184app.add_action(action) 185 186def launch_app(self, action, id: GLib.Variant): 187app = self.apps_by_id[int(id.get_string())] 188app.launch() 189 190def make_context_menu(self): 191menu = Gio.Menu() 192menu.append(_("Menu _options"), "applet.options") 193context_menu = Gtk.PopoverMenu.new_from_model(menu) 194context_menu.set_has_arrow(False) 195context_menu.set_parent(self) 196context_menu.set_halign(Gtk.Align.START) 197context_menu.set_flags(Gtk.PopoverMenuFlags.NESTED) 198return context_menu 199 200def show_context_menu(self, gesture, n_presses, x, y): 201rect = Gdk.Rectangle() 202rect.x = int(x) 203rect.y = int(y) 204rect.width = 1 205rect.height = 1 206 207self.context_menu.set_pointing_to(rect) 208self.context_menu.popup() 209 210def show_options(self, _0=None, _1=None): 211... 212 213def shutdown(self): 214app: Gtk.Application = self.get_root().get_application() 215app.remove_action(self.trigger_name) 216 217def get_config(self): 218return { 219"category_mappings": self.category_mappings, 220"trigger_name": self.trigger_name, 221"icon_name": self.icon_name, 222"icon_size": self.icon_size, 223} 224 225def set_panel_position(self, position): 226self.popover.set_position(panorama_panel.OPPOSITE_POSITION[position]) 227self.button.set_direction(panorama_panel.POSITION_TO_ARROW[panorama_panel.OPPOSITE_POSITION[position]]) 228 229def generate_app_menu(self, app_info_monitor=None): 230self.menu.remove_all() 231self.apps_by_id = {} 232 233all_apps = Gio.AppInfo.get_all() 234apps_by_category: dict[str, list[Gio.AppInfo]] = {} 235for category, info in self.category_mappings.items(): 236apps_by_category[category] = [] 237 238for app in all_apps: 239category_found = False 240if isinstance(app, Gio.DesktopAppInfo): 241if app.get_categories() is not None: 242categories = app.get_categories().split(";") 243for category in categories: 244if category in apps_by_category: 245apps_by_category[category].append(app) 246category_found = True 247 248if not category_found: 249apps_by_category["Other"].append(app) 250 251for apps in apps_by_category.values(): 252apps.sort(key=lambda app: app.get_display_name()) 253 254for category, info in self.category_mappings.items(): 255if apps_by_category[category]: 256item = Gio.MenuItem.new(_(info["menu_name"])) 257item.set_icon(Gio.ThemedIcon.new(info["icon"])) 258submenu = Gio.Menu() 259 260for app in apps_by_category[category]: 261if isinstance(app, Gio.DesktopAppInfo) and (app.get_is_hidden() or app.get_nodisplay()): 262continue 263 264subitem = Gio.MenuItem.new(app.get_display_name(), f"applet.launch-app::{id(app)}") 265subitem.set_icon(app.get_icon() or Gio.ThemedIcon.new("image-missing")) 266self.apps_by_id[id(app)] = app 267submenu.append_item(subitem) 268 269item.set_submenu(submenu) 270self.menu.append_item(item) 271 272add_icons_to_menu(self.popover) 273