__init__.py
Python script, ASCII text executable
1import dataclasses 2import os 3from pathlib import Path 4from pywayland.client import Display 5from pywayland.protocol.wayland import WlRegistry 6from pywayland.protocol.wlr_foreign_toplevel_management_unstable_v1 import ( 7ZwlrForeignToplevelManagerV1, 8ZwlrForeignToplevelHandleV1 9) 10import panorama_panel 11 12import gi 13 14gi.require_version("Gtk", "4.0") 15 16from gi.repository import Gtk, GLib, Gio, Gdk 17 18 19module_directory = Path(__file__).resolve().parent 20 21 22def split_bytes_into_ints(array: bytes, size: int = 4) -> list[int]: 23if len(array) % size: 24raise ValueError(f"The byte string's length must be a multiple of {size}") 25 26values: list[int] = [] 27for i in range(0, len(array), size): 28values.append(int.from_bytes(array[i : i+size], byteorder="little")) 29 30return values 31 32 33@dataclasses.dataclass 34class WindowState: 35minimised: bool 36maximised: bool 37fullscreen: bool 38focused: bool 39 40@classmethod 41def from_state_array(cls, array: bytes): 42values = split_bytes_into_ints(array) 43instance = cls(False, False, False, False) 44for value in values: 45match value: 46case 0: 47instance.maximised = True 48case 1: 49instance.minimised = True 50case 2: 51instance.focused = True 52case 3: 53instance.fullscreen = True 54 55return instance 56 57 58class WindowButton(Gtk.ToggleButton): 59def __init__(self, window_id, window_title, **kwargs): 60super().__init__(**kwargs) 61 62self.window_id = window_id 63self.set_has_frame(False) 64self.label = Gtk.Label() 65self.icon = Gtk.Image.new_from_icon_name("application-x-executable") 66box = Gtk.Box() 67box.append(self.icon) 68box.append(self.label) 69self.set_child(box) 70 71self.window_title = window_title 72 73self.last_state = False 74 75@property 76def window_title(self): 77return self.label.get_text() 78 79@window_title.setter 80def window_title(self, value): 81self.label.set_text(value) 82 83def set_icon_from_app_id(self, app_id): 84# Try getting an icon from the correct theme 85app_ids = app_id.split() 86icon_theme = Gtk.IconTheme() 87 88for app_id in app_ids: 89self.get_parent().print_log(app_id) 90if icon_theme.has_icon(app_id): 91self.icon.set_from_icon_name(app_id) 92return 93 94# If that doesn't work, try getting one from .desktop files 95for app_id in app_ids: 96 97desktop_file = Gio.DesktopAppInfo.new(app_id + ".desktop") 98if desktop_file: 99self.icon.set_from_gicon(desktop_file.get_icon()) 100return 101 102 103class WFWindowList(panorama_panel.Applet): 104name = "Wayfire window list" 105description = "Traditional window list (for Wayfire)" 106 107def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None): 108super().__init__(orientation=orientation, config=config) 109if config is None: 110config = {} 111 112self.toplevel_buttons: dict[ZwlrForeignToplevelHandleV1, WindowButton] = {} 113# This button doesn't belong to any window but is used for the button group and to be 114# selected when no window is focused 115self.initial_button = Gtk.ToggleButton() 116 117self.display = Display() 118self.display.connect() 119self.registry = self.display.get_registry() 120self.registry.dispatcher["global"] = self.on_global 121self.display.roundtrip() 122fd = self.display.get_fd() 123GLib.io_add_watch(fd, GLib.IO_IN, self.on_display_event) 124 125self.context_menu = self.make_context_menu() 126panorama_panel.track_popover(self.context_menu) 127 128right_click_controller = Gtk.GestureClick() 129right_click_controller.set_button(3) 130right_click_controller.connect("pressed", self.show_context_menu) 131 132self.add_controller(right_click_controller) 133 134action_group = Gio.SimpleActionGroup() 135options_action = Gio.SimpleAction.new("options", None) 136options_action.connect("activate", self.show_options) 137action_group.add_action(options_action) 138self.insert_action_group("applet", action_group) 139 140self.options_window = None 141 142def on_display_event(self, source, condition): 143if condition == GLib.IO_IN: 144self.display.dispatch(block=True) 145return True 146 147def on_global(self, registry, name, interface, version): 148if interface == "zwlr_foreign_toplevel_manager_v1": 149self.print_log("WFWindowList: Interface registered") 150self.manager = registry.bind(name, ZwlrForeignToplevelManagerV1, version) 151self.manager.dispatcher["toplevel"] = self.on_new_toplevel 152self.manager.dispatcher["finished"] = lambda *a: print("Toplevel manager finished") 153self.display.roundtrip() 154self.display.flush() 155 156def on_new_toplevel(self, manager: ZwlrForeignToplevelManagerV1, 157handle: ZwlrForeignToplevelHandleV1): 158handle.dispatcher["title"] = lambda h, title: self.on_title_changed(h, title) 159handle.dispatcher["app_id"] = lambda h, app_id: self.on_app_id_changed(h, app_id) 160handle.dispatcher["state"] = lambda h, states: self.on_state_changed(h, states) 161handle.dispatcher["closed"] = lambda h: self.on_closed(h) 162 163def on_title_changed(self, handle, title): 164if handle not in self.toplevel_buttons: 165button = WindowButton(handle, title) 166button.set_group(self.initial_button) 167button.connect("clicked", lambda *a: self.on_button_click(handle)) 168self.toplevel_buttons[handle] = button 169self.append(button) 170else: 171button = self.toplevel_buttons[handle] 172button.window_title = title 173 174def on_state_changed(self, handle, states): 175if handle in self.toplevel_buttons: 176state_info = WindowState.from_state_array(states) 177button = self.toplevel_buttons[handle] 178if state_info.focused: 179button.set_active(True) 180else: 181self.initial_button.set_active(True) 182 183def on_app_id_changed(self, handle, app_id): 184if handle in self.toplevel_buttons: 185button = self.toplevel_buttons[handle] 186button.set_icon_from_app_id(app_id) 187 188def on_closed(self, handle): 189if handle in self.toplevel_buttons: 190self.remove(self.toplevel_buttons[handle]) 191self.toplevel_buttons.pop(handle) 192 193def make_context_menu(self): 194menu = Gio.Menu() 195menu.append("Window list _options", "applet.options") 196context_menu = Gtk.PopoverMenu.new_from_model(menu) 197context_menu.set_has_arrow(False) 198context_menu.set_parent(self) 199context_menu.set_halign(Gtk.Align.START) 200context_menu.set_flags(Gtk.PopoverMenuFlags.NESTED) 201return context_menu 202 203def show_context_menu(self, gesture, n_presses, x, y): 204rect = Gdk.Rectangle() 205rect.x = int(x) 206rect.y = int(y) 207rect.width = 1 208rect.height = 1 209 210self.context_menu.set_pointing_to(rect) 211self.context_menu.popup() 212 213def show_options(self, _0=None, _1=None): 214pass 215 216def get_config(self): 217return {} 218