__init__.py
Python script, ASCII text executable
1""" 2The MIT License (MIT) 3 4Copyright (c) 2025 Scott Moreau <oreaus@gmail.com>, modified by <root@roundabout-host.com> 5 6Permission is hereby granted, free of charge, to any person obtaining a copy 7of this software and associated documentation files (the "Software"), to deal 8in the Software without restriction, including without limitation the rights 9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10copies of the Software, and to permit persons to whom the Software is 11furnished to do so, subject to the following conditions: 12 13The above copyright notice and this permission notice shall be included in 14all copies or substantial portions of the Software. 15 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22SOFTWARE. 23""" 24 25import os 26from pathlib import Path 27import locale 28import panorama_panel 29 30import gi 31gi.require_version("Gtk", "4.0") 32 33from gi.repository import Gtk, GLib, Gio, Gdk, Pango 34import subprocess 35 36 37LOGOUT_BUTTON_SIZE = 128 38LOGOUT_BUTTON_MARGIN = 12 39 40def create_logout_ui_button(icon_name, label_text): 41layout = Gtk.Box() 42image = Gtk.Image() 43label = Gtk.Label() 44button = Gtk.Button() 45button.set_size_request(LOGOUT_BUTTON_SIZE, LOGOUT_BUTTON_SIZE) 46image.set_from_icon_name(icon_name) 47label.set_text(label_text) 48layout.set_orientation(Gtk.Orientation.VERTICAL) 49layout.set_halign(Gtk.Align.CENTER) 50layout.append(image) 51image.set_icon_size(Gtk.IconSize.LARGE) 52image.set_vexpand(True) 53layout.append(label) 54button.set_child(layout) 55return button 56 57class WayfireLogoutUI(Gtk.Window): 58 59def __init__(self): 60super().__init__() 61 62self.set_application(Gtk.Application(application_id="com.roundabout_host.panorama.logout")) 63hbox = Gtk.CenterBox() 64main_layout = Gtk.Grid() 65suspend = create_logout_ui_button("emblem-synchronizing", "Suspend") 66suspend.connect("clicked", self.on_suspend_click) 67main_layout.attach(suspend, 0, 0, 1, 1) 68 69hibernate = create_logout_ui_button("weather-clear-night", "Hibernate") 70hibernate.connect("clicked", self.on_hibernate_click) 71main_layout.attach(hibernate, 1, 0, 1, 1) 72 73switchuser = create_logout_ui_button("system-users", "Switch User") 74switchuser.connect("clicked", self.on_switchuser_click) 75main_layout.attach(switchuser, 2, 0, 1, 1) 76 77logout = create_logout_ui_button("system-log-out", "Log Out") 78logout.connect("clicked", self.on_logout_click) 79main_layout.attach(logout, 0, 1, 1, 1) 80 81reboot = create_logout_ui_button("system-reboot", "Reboot") 82reboot.connect("clicked", self.on_reboot_click) 83main_layout.attach(reboot, 1, 1, 1, 1) 84 85shutdown = create_logout_ui_button("system-shutdown", "Shut Down") 86shutdown.connect("clicked", self.on_shutdown_click) 87main_layout.attach(shutdown, 2, 1, 1, 1) 88 89cancel_button = Gtk.Button() 90cancel_button.set_size_request(100, 50) 91cancel_button.set_label("Cancel") 92main_layout.attach(cancel_button, 1, 2, 1, 1) 93cancel_button.connect("clicked", self.on_cancel_click) 94 95main_layout.set_row_spacing(LOGOUT_BUTTON_MARGIN) 96main_layout.set_column_spacing(LOGOUT_BUTTON_MARGIN) 97key_controller = Gtk.EventControllerKey() 98key_controller.connect("key-pressed", self.on_key_press) 99self.add_controller(key_controller) 100main_layout.set_valign(Gtk.Align.CENTER) 101hbox.set_center_widget(main_layout) 102hbox.set_hexpand(True) 103hbox.set_vexpand(True) 104self.set_child(hbox) 105self.get_style_context().add_class("logout") 106display = self.get_display() 107css_provider = Gtk.CssProvider() 108css_provider.load_from_data("window.logout { background-color: rgba(0, 0, 0, 0.5); }") 109Gtk.StyleContext.add_provider_for_display(display, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER) 110self.fullscreen() 111 112def on_key_press(self, controller, keyval, keycode, state): 113if keyval == Gdk.KEY_Escape: 114self.hide() 115return True 116return False 117 118def on_suspend_click(self, button): 119GLib.spawn_command_line_async("systemctl suspend") 120 121def on_hibernate_click(self, button): 122GLib.spawn_command_line_async("systemctl hibernate") 123 124def on_switchuser_click(self, button): 125GLib.spawn_command_line_async("dm-tool switch-to-greeter") 126 127def on_logout_click(self, button): 128GLib.spawn_command_line_async("wayland-logout") 129 130def on_reboot_click(self, button): 131GLib.spawn_command_line_async("systemctl reboot") 132 133def on_shutdown_click(self, button): 134GLib.spawn_command_line_async("systemctl poweroff") 135 136def on_cancel_click(self, button): 137self.hide() 138 139class MenuItemButton(Gtk.Button): 140 141def __init__(self, name, desc, exe): 142super().__init__() 143self.name = name 144self.desc = desc 145self.exe = exe 146 147class SearchMenu(panorama_panel.Applet): 148name = "Searchable menu" 149description = "Flowbox app menu" 150 151def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None): 152super().__init__() 153if config is None: 154config = {} 155 156self.button = Gtk.MenuButton() 157 158self.menu_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) 159self.image = Gtk.Image.new_from_icon_name(config.get("icon_name", "wayfire")) 160self.image.set_pixel_size(config.get("icon_size", 24)) 161self.append(self.button) 162self.button.set_child(self.image) 163self.popover = Gtk.Popover() 164self.popover.set_parent(self) 165self.flowbox = Gtk.FlowBox() 166self.flowbox.set_valign(Gtk.Align.START) 167self.flowbox.set_homogeneous(True) 168self.flowbox.set_max_children_per_line(config.get("icons_per_line", 3)) 169self.flowbox.set_min_children_per_line(config.get("icons_per_line", 3)) 170self.flowbox_item_focus_signal = self.flowbox.connect("selected-children-changed", self.on_flowbox_item_focus) 171self.scrolled_window = Gtk.ScrolledWindow() 172self.scrolled_window.set_size_request(-1, config.get("height", 360)) 173self.scrolled_window.set_child(self.flowbox) 174self.logout_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL) 175self.logout_button = Gtk.Button() 176self.logout_button.set_margin_top(6) 177self.logout_button.set_margin_bottom(6) 178self.logout_button.set_margin_start(6) 179self.logout_button.set_margin_end(6) 180self.logout_button.set_child(Gtk.Image.new_from_icon_name("system-shutdown")) 181self.logout_button.connect("clicked", self.on_logout_button_clicked) 182self.logout_box.set_halign(Gtk.Align.END) 183self.logout_box.append(self.logout_button) 184self.search_entry = Gtk.SearchEntry() 185self.search_entry.connect("search-changed", self.on_search_changed) 186self.search_entry.connect("activate", self.click_first_button) 187self.menu_box.append(self.search_entry) 188self.menu_box.append(self.scrolled_window) 189self.menu_box.append(self.logout_box) 190self.popover.set_child(self.menu_box) 191self.popover.set_size_request(config.get("width", 400), -1) 192self.popover.connect("show", self.on_popover_popup) 193self.button.set_popover(self.popover) 194self.logout_ui = WayfireLogoutUI() 195self.populate_menu_entries() 196self.app_info_monitor = Gio.AppInfoMonitor.get() 197self.app_info_monitor.connect("changed", self.populate_menu_entries) 198 199def click_first_button(self, search_entry): 200flowbox_child = self.flowbox.get_first_child() 201if flowbox_child is not None: 202button = flowbox_child.get_first_child() 203button.emit("clicked") 204 205def on_search_changed(self, search_entry): 206self.flowbox.remove_all() 207text = self.search_entry.get_text().lower() 208for button in self.cached_buttons: 209if (button.name and text in button.name.lower()) or \ 210(button.desc and text in button.desc.lower()) or \ 211(button.exe and text in button.exe.lower()): 212self.flowbox.append(button) 213 214def on_logout_button_clicked(self, button): 215self.logout_ui.present() 216self.popover.popdown() 217 218def on_flowbox_item_focus(self, flowbox): 219selected_children = flowbox.get_selected_children() 220if len(selected_children) >= 1: 221selected_children[0].get_child().grab_focus() 222 223def get_config(self): 224return { 225"width": self.popover.get_size_request()[0], 226"height": self.scrolled_window.get_size_request()[1], 227"icon_name": self.image.get_icon_name(), 228"icon_size": self.image.get_pixel_size(), 229"icons_per_line": self.flowbox.get_max_children_per_line() 230} 231 232def on_popover_popup(self, parent): 233for child in self.flowbox.get_selected_children(): 234self.flowbox.unselect_child(child) 235self.search_entry.set_text("") 236self.popover.popup() 237 238def app_button_clicked(self, app_button): 239app_button.execute() 240self.popover.popdown() 241 242def populate_menu_entries(self, app_info_monitor=None): 243app_infos = Gio.AppInfo.get_all() 244app_infos.sort(key=lambda app_info: app_info.get_display_name().lower()) 245self.cached_buttons = [] 246self.flowbox.remove_all() 247 248for app_info in app_infos: 249app_categories = app_info.get_categories() 250if app_categories == None: 251continue 252app_name = app_info.get_display_name() 253command = app_info.get_executable() 254app_button = MenuItemButton(app_name, app_info.get_description(), command) 255app_button.execute = app_info.launch 256app_button.set_tooltip_text(app_name) 257app_label = Gtk.Label(label=app_name) 258app_label.set_ellipsize(Pango.EllipsizeMode.END) 259app_button_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) 260app_button_box.append(app_label) 261app_button.set_child(app_button_box) 262app_button.set_has_frame(False) 263 264image = Gtk.Image() 265image.set_icon_size(Gtk.IconSize.LARGE) 266icon = app_info.get_icon() 267if icon: 268icon_name = icon.to_string() 269if not Gtk.IconTheme.get_for_display(Gdk.Display.get_default()).has_icon(icon_name): 270continue 271if icon_name[0] == '/': 272image.set_from_file(icon_name) 273else: 274image.set_from_icon_name(icon_name) 275else: 276if not Gtk.IconTheme.get_for_display(Gdk.Display.get_default()).has_icon(app_name.lower()): 277continue 278image.set_from_icon_name(app_name.lower()) 279 280self.flowbox.append(app_button) 281app_button.connect("clicked", self.app_button_clicked) 282app_button_box.prepend(image) 283self.cached_buttons.append(app_button) 284