-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathoverview.py
389 lines (327 loc) · 14.2 KB
/
overview.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# Thanks to https://github.com/muhchaudhary for the original code. You are a legend.
import json
import cairo
import gi
from loguru import logger
from fabric.hyprland.service import Hyprland
from fabric.widgets.box import Box
from fabric.widgets.button import Button
from fabric.widgets.eventbox import EventBox
from fabric.widgets.image import Image
from fabric.widgets.label import Label
from fabric.widgets.overlay import Overlay
import modules.icons as icons
# WIP icon resolver (app_id to guessing the icon name)
from utils.icon_resolver import IconResolver
from fabric.utils.helpers import get_desktop_applications
gi.require_version("Gtk", "3.0")
from gi.repository import Gdk, Gtk
screen = Gdk.Screen.get_default()
CURRENT_WIDTH = screen.get_width()
CURRENT_HEIGHT = screen.get_height()
icon_resolver = IconResolver()
connection = Hyprland()
SCALE = 0.1
# Credit to Aylur for the drag and drop code
TARGET = [Gtk.TargetEntry.new("text/plain", Gtk.TargetFlags.SAME_APP, 0)]
# Credit to Aylur for the createSurfaceFromWidget code
def createSurfaceFromWidget(widget: Gtk.Widget) -> cairo.ImageSurface:
alloc = widget.get_allocation()
surface = cairo.ImageSurface(
cairo.Format.ARGB32,
alloc.width,
alloc.height,
)
cr = cairo.Context(surface)
cr.set_source_rgba(255, 255, 255, 0)
cr.rectangle(0, 0, alloc.width, alloc.height)
cr.fill()
widget.draw(cr)
return surface
class HyprlandWindowButton(Button):
def __init__(
self,
window: Box,
title: str,
address: str,
app_id: str,
size,
transform: int = 0,
):
self.transform = transform % 4
self.size = size if transform in [0, 2] else (size[1], size[0])
self.address = address
self.app_id = app_id
self.title = title
self.window: Box = window
# Compute dynamic icon sizes based on the button size.
# Using the minimum dimension of the button for scaling.
icon_size_main = int(min(self.size) * 0.5) # adjust factor as needed
# Enhanced icon resolution using desktop apps
desktop_app = window.find_app(app_id)
# Get icon using improved method with fallbacks
icon_pixbuf = None
if desktop_app:
icon_pixbuf = desktop_app.get_icon_pixbuf(size=icon_size_main)
if not icon_pixbuf:
# Fallback to IconResolver
icon_pixbuf = icon_resolver.get_icon_pixbuf(app_id, icon_size_main)
if not icon_pixbuf:
# Additional fallbacks for common apps
icon_pixbuf = icon_resolver.get_icon_pixbuf("application-x-executable-symbolic", icon_size_main)
if not icon_pixbuf:
icon_pixbuf = icon_resolver.get_icon_pixbuf("image-missing", icon_size_main)
# Ensure icon is scaled to the correct size
if icon_pixbuf and (icon_pixbuf.get_width() != icon_size_main or icon_pixbuf.get_height() != icon_size_main):
icon_pixbuf = icon_pixbuf.scale_simple(
icon_size_main,
icon_size_main,
gi.repository.GdkPixbuf.InterpType.BILINEAR
)
super().__init__(
name="overview-client-box",
image=Image(pixbuf=icon_pixbuf),
tooltip_text=title,
size=size,
on_clicked=self.on_button_click,
on_button_press_event=lambda _, event: connection.send_command(
f"/dispatch closewindow address:{address}"
)
if event.button == 3
else None,
on_drag_data_get=lambda _s, _c, data, *_: data.set_text(
address, len(address)
),
on_drag_begin=lambda _, context: Gtk.drag_set_icon_surface(
context, createSurfaceFromWidget(self)
),
)
# Store the desktop_app for later use
self.desktop_app = desktop_app
self.drag_source_set(
start_button_mask=Gdk.ModifierType.BUTTON1_MASK,
targets=TARGET,
actions=Gdk.DragAction.COPY,
)
self.connect("key_press_event", self.on_key_press_event)
def on_key_press_event(self, widget, event):
if event.get_state() & Gdk.ModifierType.SHIFT_MASK:
if event.keyval in (Gdk.KEY_Return, Gdk.KEY_KP_Enter, Gdk.KEY_space):
connection.send_command(f"/dispatch closewindow address:{self.address}")
return True
return False
def update_image(self, image):
# Compute overlay icon size dynamically.
icon_size_overlay = int(min(self.size) * 0.5) # adjust factor as needed
# Enhanced icon resolution for overlay
icon_pixbuf = None
if hasattr(self, 'desktop_app') and self.desktop_app:
icon_pixbuf = self.desktop_app.get_icon_pixbuf(size=icon_size_overlay)
if not icon_pixbuf:
icon_pixbuf = icon_resolver.get_icon_pixbuf(self.app_id, icon_size_overlay)
if not icon_pixbuf:
icon_pixbuf = icon_resolver.get_icon_pixbuf("application-x-executable-symbolic", icon_size_overlay)
if not icon_pixbuf:
icon_pixbuf = icon_resolver.get_icon_pixbuf("image-missing", icon_size_overlay)
# Ensure icon is scaled to the correct size
if icon_pixbuf and (icon_pixbuf.get_width() != icon_size_overlay or icon_pixbuf.get_height() != icon_size_overlay):
icon_pixbuf = icon_pixbuf.scale_simple(
icon_size_overlay,
icon_size_overlay,
gi.repository.GdkPixbuf.InterpType.BILINEAR
)
self.set_image(
Overlay(
child=image,
overlays=Image(
name="overview-icon",
pixbuf=icon_pixbuf,
h_align="center",
v_align="end",
tooltip_text=self.title,
),
)
)
def on_button_click(self, *_):
connection.send_command(f"/dispatch focuswindow address:{self.address}")
class WorkspaceEventBox(EventBox):
def __init__(self, workspace_id: int, fixed: Gtk.Fixed | None = None):
self.fixed = fixed
super().__init__(
name="overview-workspace-bg",
h_expand=True,
v_expand=True,
size=(int(CURRENT_WIDTH * SCALE), int(CURRENT_HEIGHT * SCALE)),
child=fixed
if fixed
else Label(
name="overview-add-label",
h_expand=True,
v_expand=True,
markup=icons.circle_plus,
),
on_drag_data_received=lambda _w, _c, _x, _y, data, *_: connection.send_command(
f"/dispatch movetoworkspacesilent {workspace_id},address:{data.get_data().decode()}"
),
)
self.drag_dest_set(
Gtk.DestDefaults.ALL,
TARGET,
Gdk.DragAction.COPY,
)
if fixed:
fixed.show_all()
class Overview(Box):
def __init__(self, **kwargs):
# Initialize as a Box instead of a PopupWindow.
super().__init__(name="overview", orientation="v", spacing=8, **kwargs)
self.workspace_boxes: dict[int, Box] = {}
self.clients: dict[str, HyprlandWindowButton] = {}
# Initialize app registry for better icon resolution
self._all_apps = get_desktop_applications()
self.app_identifiers = self._build_app_identifiers_map()
# Remove the window_class_aliases dictionary completely
connection.connect("event::openwindow", self.do_update)
connection.connect("event::closewindow", self.do_update)
connection.connect("event::movewindow", self.do_update)
self.update()
def _normalize_window_class(self, class_name):
"""Normalize window class by removing common suffixes and lowercase."""
if not class_name:
return ""
normalized = class_name.lower()
# Remove common suffixes
suffixes = [".bin", ".exe", ".so", "-bin", "-gtk"]
for suffix in suffixes:
if normalized.endswith(suffix):
normalized = normalized[:-len(suffix)]
return normalized
def _classes_match(self, class1, class2):
"""Check if two window class names match with stricter comparison."""
if not class1 or not class2:
return False
# Normalize both classes
norm1 = self._normalize_window_class(class1)
norm2 = self._normalize_window_class(class2)
# Direct match after normalization
if norm1 == norm2:
return True
# Don't do substring matching as it's too error-prone
# This avoids incorrectly matching flatpak apps and others
return False
def _build_app_identifiers_map(self):
"""Build a mapping of app identifiers (class names, executables, names) to DesktopApp objects"""
identifiers = {}
for app in self._all_apps:
# Map by name (lowercase)
if app.name:
identifiers[app.name.lower()] = app
# Map by display name
if app.display_name:
identifiers[app.display_name.lower()] = app
# Map by window class if available
if app.window_class:
identifiers[app.window_class.lower()] = app
# Map by executable name if available
if app.executable:
exe_basename = app.executable.split('/')[-1].lower()
identifiers[exe_basename] = app
# Map by command line if available (without parameters)
if app.command_line:
cmd_base = app.command_line.split()[0].split('/')[-1].lower()
identifiers[cmd_base] = app
return identifiers
def find_app(self, app_identifier):
"""Return the DesktopApp object by matching any app identifier."""
if not app_identifier:
return None
# Try direct lookup in our identifiers map
normalized_id = str(app_identifier).lower()
if normalized_id in self.app_identifiers:
return self.app_identifiers[normalized_id]
# Try with normalized class name
norm_id = self._normalize_window_class(normalized_id)
if norm_id in self.app_identifiers:
return self.app_identifiers[norm_id]
# More targeted matching with exact names only
for app in self._all_apps:
if app.name and app.name.lower() == normalized_id:
return app
if app.window_class and app.window_class.lower() == normalized_id:
return app
if app.display_name and app.display_name.lower() == normalized_id:
return app
# Try with executable basename
if app.executable:
exe_base = app.executable.split('/')[-1].lower()
if exe_base == normalized_id:
return app
# Try with command basename
if app.command_line:
cmd_base = app.command_line.split()[0].split('/')[-1].lower()
if cmd_base == normalized_id:
return app
return None
def update(self, signal_update=False):
# Refresh app registry when updating to ensure latest data
self._all_apps = get_desktop_applications()
self.app_identifiers = self._build_app_identifiers_map()
# Remove old clients and workspaces.
for client in self.clients.values():
client.destroy()
self.clients.clear()
for workspace in self.workspace_boxes.values():
workspace.destroy()
self.workspace_boxes.clear()
# Create two rows in this Box.
self.children = [Box(spacing=8), Box(spacing=8)]
monitors = {
monitor["id"]: (monitor["x"], monitor["y"], monitor["transform"])
for monitor in json.loads(
connection.send_command("j/monitors").reply.decode()
)
}
for client in json.loads(
str(connection.send_command("j/clients").reply.decode())
):
# Exclude special workspaces.
if client["workspace"]["id"] > 0:
self.clients[client["address"]] = HyprlandWindowButton(
window=self,
title=client["title"],
address=client["address"],
app_id=client["initialClass"],
size=(client["size"][0] * SCALE, client["size"][1] * SCALE),
transform=monitors[client["monitor"]][2],
)
if client["workspace"]["id"] not in self.workspace_boxes:
self.workspace_boxes[client["workspace"]["id"]] = Gtk.Fixed.new()
self.workspace_boxes[client["workspace"]["id"]].put(
self.clients[client["address"]],
abs(client["at"][0] - monitors[client["monitor"]][0]) * SCALE,
abs(client["at"][1] - monitors[client["monitor"]][1]) * SCALE,
)
# Lay out workspaces into two rows.
for w_id in range(1, 11):
if w_id <= 5:
overview_row = self.children[0]
else:
overview_row = self.children[1]
overview_row.add(
Box(
name="overview-workspace-box",
orientation="vertical",
children=[
Label(name="overview-workspace-label", label=f"Workspace {w_id}"),
WorkspaceEventBox(
w_id,
self.workspace_boxes[w_id]
if w_id in self.workspace_boxes
else None,
),
],
)
)
def do_update(self, *_):
logger.info(f"[Overview] Updating for :{_[1].name}")
self.update(signal_update=True)