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