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 • 6.51 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 = WindowState(False, False, False, False)
44
print(values)
45
for value in values:
46
match value:
47
case 0:
48
instance.maximised = True
49
case 1:
50
instance.minimised = True
51
case 2:
52
instance.focused = True
53
case 3:
54
instance.fullscreen = True
55
56
return instance
57
58
59
class WindowButton(Gtk.ToggleButton):
60
def __init__(self, window_id, window_title, **kwargs):
61
super().__init__(**kwargs)
62
63
self.window_id = window_id
64
self.set_has_frame(False)
65
self.label = Gtk.Label()
66
box = Gtk.Box()
67
box.append(self.label)
68
self.set_child(box)
69
70
self.window_title = window_title
71
72
self.last_state = False
73
74
@property
75
def window_title(self):
76
return self.label.get_text()
77
78
@window_title.setter
79
def window_title(self, value):
80
self.label.set_text(value)
81
82
83
class WFWindowList(panorama_panel.Applet):
84
name = "Wayfire window list"
85
description = "Traditional window list (for Wayfire)"
86
87
def __init__(self, orientation=Gtk.Orientation.HORIZONTAL, config=None):
88
super().__init__(orientation=orientation, config=config)
89
if config is None:
90
config = {}
91
92
self.toplevel_buttons: dict[ZwlrForeignToplevelHandleV1, WindowButton] = {}
93
# This button doesn't belong to any window but is used for the button group and to be
94
# selected when no window is focused
95
self.initial_button = Gtk.ToggleButton()
96
97
self.display = Display()
98
self.display.connect()
99
self.registry = self.display.get_registry()
100
self.registry.dispatcher["global"] = self.on_global
101
self.display.roundtrip()
102
fd = self.display.get_fd()
103
GLib.io_add_watch(fd, GLib.IO_IN, self.on_display_event)
104
105
self.context_menu = self.make_context_menu()
106
panorama_panel.track_popover(self.context_menu)
107
108
right_click_controller = Gtk.GestureClick()
109
right_click_controller.set_button(3)
110
right_click_controller.connect("pressed", self.show_context_menu)
111
112
self.add_controller(right_click_controller)
113
114
action_group = Gio.SimpleActionGroup()
115
options_action = Gio.SimpleAction.new("options", None)
116
options_action.connect("activate", self.show_options)
117
action_group.add_action(options_action)
118
self.insert_action_group("applet", action_group)
119
120
self.options_window = None
121
122
def on_display_event(self, source, condition):
123
if condition == GLib.IO_IN:
124
self.display.dispatch(block=True)
125
return True
126
127
def on_global(self, registry, name, interface, version):
128
print(f"Global: {interface} (v{version})")
129
if interface == "zwlr_foreign_toplevel_manager_v1":
130
print("Interface registered")
131
self.manager = registry.bind(name, ZwlrForeignToplevelManagerV1, version)
132
self.manager.dispatcher["toplevel"] = self.on_new_toplevel
133
self.manager.dispatcher["finished"] = lambda *a: print("Toplevel manager finished")
134
self.display.roundtrip()
135
self.display.flush()
136
137
def on_new_toplevel(self, manager: ZwlrForeignToplevelManagerV1,
138
handle: ZwlrForeignToplevelHandleV1):
139
print("Toplevel received")
140
handle.dispatcher["title"] = lambda h, title: self.on_title_changed(h, title)
141
#handle.dispatcher["app_id"] = lambda h, app_id: self.on_app_id_changed(h, app_id)
142
handle.dispatcher["state"] = lambda h, states: self.on_state_changed(h, states)
143
handle.dispatcher["closed"] = lambda h: self.on_closed(h)
144
145
def on_title_changed(self, handle, title):
146
print(f"Window title: {title}")
147
if handle not in self.toplevel_buttons:
148
button = WindowButton(handle, title)
149
button.set_group(self.initial_button)
150
button.connect("clicked", lambda *a: self.on_button_click(handle))
151
self.toplevel_buttons[handle] = button
152
self.append(button)
153
else:
154
button = self.toplevel_buttons[handle]
155
button.window_title = title
156
157
def on_state_changed(self, handle, states):
158
if handle in self.toplevel_buttons:
159
state_info = WindowState.from_state_array(states)
160
print(state_info)
161
button = self.toplevel_buttons[handle]
162
if state_info.focused:
163
print("focused")
164
button.set_active(True)
165
else:
166
self.initial_button.set_active(True)
167
168
def on_closed(self, handle):
169
if handle in self.toplevel_buttons:
170
self.remove(self.toplevel_buttons[handle])
171
self.toplevel_buttons.pop(handle)
172
173
def make_context_menu(self):
174
menu = Gio.Menu()
175
menu.append("Window list _options", "applet.options")
176
context_menu = Gtk.PopoverMenu.new_from_model(menu)
177
context_menu.set_has_arrow(False)
178
context_menu.set_parent(self)
179
context_menu.set_halign(Gtk.Align.START)
180
context_menu.set_flags(Gtk.PopoverMenuFlags.NESTED)
181
return context_menu
182
183
def show_context_menu(self, gesture, n_presses, x, y):
184
rect = Gdk.Rectangle()
185
rect.x = int(x)
186
rect.y = int(y)
187
rect.width = 1
188
rect.height = 1
189
190
self.context_menu.set_pointing_to(rect)
191
self.context_menu.popup()
192
193
def show_options(self, _0=None, _1=None):
194
pass
195
196
def get_config(self):
197
return {}
198