forked from qtile/qtile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscratchpad.py
410 lines (369 loc) · 15.4 KB
/
scratchpad.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# Copyright (c) 2017, Dirk Hartmann
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import annotations
from typing import TYPE_CHECKING
from libqtile import config, group, hook
from libqtile.backend.base import FloatStates
from libqtile.command.base import expose_command
from libqtile.config import Match
if TYPE_CHECKING:
from libqtile.backend.base import Window
class WindowVisibilityToggler:
"""
WindowVisibilityToggler is a wrapper for a window, used in ScratchPad group
to toggle visibility of a window by toggling the group it belongs to.
The window is either sent to the named ScratchPad, which is by default
invisble, or the current group on the current screen.
With this functionality the window can be shown and hidden by a single
keystroke (bound to command of ScratchPad group).
By default, the window is also hidden if it loses focus.
"""
def __init__(self, scratchpad_name, window: Window, on_focus_lost_hide, warp_pointer):
"""
Initiliaze the WindowVisibilityToggler.
Parameters:
===========
scratchpad_name: string
The name (not label) of the ScratchPad group used to hide the window
window: window
The window to toggle
on_focus_lost_hide: bool
if True the associated window is hidden if it loses focus
warp_pointer: bool
if True the mouse pointer is warped to center of associated window
if shown. Only used if on_focus_lost_hide is True
"""
self.scratchpad_name = scratchpad_name
self.window = window
self.on_focus_lost_hide = on_focus_lost_hide
self.warp_pointer = warp_pointer
# determine current status based on visibility
self.shown = False
self.show()
def info(self):
return dict(
window=self.window.info(),
scratchpad_name=self.scratchpad_name,
visible=self.visible,
on_focus_lost_hide=self.on_focus_lost_hide,
warp_pointer=self.warp_pointer,
)
@property
def visible(self):
"""
Determine if associated window is currently visible.
That is the window is on a group different from the scratchpad
and that group is the current visible group.
"""
if self.window.group is None:
return False
return (
self.window.group.name != self.scratchpad_name
and self.window.group is self.window.qtile.current_group
)
def toggle(self):
"""
Toggle the visibility of associated window. Either show() or hide().
"""
if not self.visible or not self.shown:
self.show()
else:
self.hide()
def show(self):
"""
Show the associated window on top of current screen.
The window is moved to the current group as floating window.
If 'warp_pointer' is True the mouse pointer is warped to center of the
window if 'on_focus_lost_hide' is True.
Otherwise, if pointer is moved manually to window by the user
the window might be hidden again before actually reaching it.
"""
if (not self.visible) or (not self.shown):
win = self.window
# always set the floating state before changing group
# to avoid disturbance of tiling layout
win._float_state = FloatStates.TOP
# add to group and bring it to front.
win.togroup()
win.bring_to_front()
# toggle internal flag of visibility
self.shown = True
# add hooks to determine if focus get lost
if self.on_focus_lost_hide:
if self.warp_pointer:
win.focus(warp=True)
hook.subscribe.client_focus(self.on_focus_change)
hook.subscribe.setgroup(self.on_focus_change)
def hide(self):
"""
Hide the associated window. That is, send it to the scratchpad group.
"""
if self.visible or self.shown:
# unsubscribe the hook methods, since the window is not shown
if self.on_focus_lost_hide:
hook.unsubscribe.client_focus(self.on_focus_change)
hook.unsubscribe.setgroup(self.on_focus_change)
self.window.togroup(self.scratchpad_name)
self.shown = False
def unsubscribe(self):
"""unsubscribe all hooks"""
if self.on_focus_lost_hide and (self.visible or self.shown):
hook.unsubscribe.client_focus(self.on_focus_change)
hook.unsubscribe.setgroup(self.on_focus_change)
def on_focus_change(self, *args, **kwargs):
"""
hook method which is called on window focus change and group change.
Depending on 'on_focus_lost_xxx' arguments, the associated window may
get hidden (by call to hide) or even killed.
"""
if self.shown:
current_group = self.window.qtile.current_group
if (
self.window.group is not current_group
or self.window is not current_group.current_window
):
if self.on_focus_lost_hide:
self.hide()
class DropDownToggler(WindowVisibilityToggler):
"""
Specialized WindowVisibilityToggler which places the associatd window
each time it is shown at desired location.
For example this can be used to create a quake-like terminal.
"""
def __init__(self, window, scratchpad_name, ddconfig):
self.name = ddconfig.name
self.x = ddconfig.x
self.y = ddconfig.y
self.width = ddconfig.width
self.height = ddconfig.height
# Let's add the window to the scratchpad group.
window.togroup(scratchpad_name)
window.opacity = ddconfig.opacity
WindowVisibilityToggler.__init__(
self, scratchpad_name, window, ddconfig.on_focus_lost_hide, ddconfig.warp_pointer
)
def info(self):
info = WindowVisibilityToggler.info(self)
info.update(
dict(name=self.name, x=self.x, y=self.y, width=self.width, height=self.height)
)
return info
def show(self):
"""
Like WindowVisibilityToggler.show, but before showing the window,
its floating x, y, width and height is set.
"""
if (not self.visible) or (not self.shown):
# SET GEOMETRY
win = self.window
screen = win.qtile.current_screen
# calculate windows floating position and width/height
# these may differ for screens, and thus always recalculated.
x = int(screen.dx + self.x * screen.dwidth)
y = int(screen.dy + self.y * screen.dheight)
win.float_x = x
win.float_y = y
width = int(screen.dwidth * self.width)
height = int(screen.dheight * self.height)
win.place(x, y, width, height, win.borderwidth, win.bordercolor, respect_hints=True)
# Toggle the dropdown
WindowVisibilityToggler.show(self)
class ScratchPad(group._Group):
"""
Specialized group which is by default invisible and can be configured, to
spawn windows and toggle its visibility (in the current group) by command.
The ScratchPad group acts as a container for windows which are currently
not visible but associated to a DropDownToggler and can toggle their
group by command (of ScratchPad group).
The ScratchPad, by default, has no label and thus is not shown in
GroupBox widget.
"""
def __init__(
self,
name="scratchpad",
dropdowns: list[config.DropDown] | None = None,
label="",
single=False,
):
group._Group.__init__(self, name, label=label)
self._dropdownconfig = {dd.name: dd for dd in dropdowns} if dropdowns is not None else {}
self.dropdowns: dict[str, DropDownToggler] = {}
self._spawned: dict[str, Match] = {}
self._to_hide: list[str] = []
self._single = single
def _check_unsubscribe(self):
if not self.dropdowns:
hook.unsubscribe.client_killed(self.on_client_killed)
hook.unsubscribe.float_change(self.on_float_change)
def _spawn(self, ddconfig):
"""
Spawn a process by defined command.
Method is only called if no window is associated. This is either on the
first call to show or if the window was killed.
The process id of spawned process is saved and compared to new windows.
In case of a match the window gets associated to this DropDown object.
"""
name = ddconfig.name
if name not in self._spawned:
if not self._spawned:
hook.subscribe.client_new(self.on_client_new)
pid = self.qtile.spawn(ddconfig.command)
self._spawned[name] = ddconfig.match or Match(net_wm_pid=pid)
def on_client_new(self, client, *args, **kwargs):
"""
hook method which is called on new windows.
This method is subscribed if the given command is spawned
and unsubscribed immediately if the associated window is detected.
"""
name = None
for n, match in self._spawned.items():
if match.compare(client):
name = n
break
if name is not None:
self._spawned.pop(name)
if not self._spawned:
hook.unsubscribe.client_new(self.on_client_new)
self.dropdowns[name] = DropDownToggler(client, self.name, self._dropdownconfig[name])
if self._single:
for n, d in self.dropdowns.items():
if n != name:
d.hide()
if name in self._to_hide:
self.dropdowns[name].hide()
self._to_hide.remove(name)
if len(self.dropdowns) == 1:
hook.subscribe.client_killed(self.on_client_killed)
hook.subscribe.float_change(self.on_float_change)
def on_client_killed(self, client, *args, **kwargs):
"""
hook method which is called if a client is killed.
If the associated window is killed, reset internal state.
"""
name = None
for name, dd in self.dropdowns.items():
if dd.window is client:
del self.dropdowns[name]
break
self._check_unsubscribe()
def on_float_change(self, *args, **kwargs):
"""
hook method which is called if window float state is changed.
If the current associated window is not floated (any more) the window
and process is detached from DRopDown, thus the next call to Show
will spawn a new process.
"""
name = None
for name, dd in self.dropdowns.items():
if not dd.window.floating:
if dd.window.group is not self:
dd.unsubscribe()
del self.dropdowns[name]
break
self._check_unsubscribe()
@expose_command()
def dropdown_toggle(self, name):
"""
Toggle visibility of named DropDown.
"""
if self._single:
for n, d in self.dropdowns.items():
if n != name:
d.hide()
if name in self.dropdowns:
self.dropdowns[name].toggle()
else:
if name in self._dropdownconfig:
self._spawn(self._dropdownconfig[name])
@expose_command()
def hide_all(self):
"""
Hide all scratchpads.
"""
for d in self.dropdowns.values():
d.hide()
@expose_command()
def dropdown_reconfigure(self, name, **kwargs):
"""
reconfigure the named DropDown configuration.
Note that changed attributes only have an effect on spawning the window.
"""
if name not in self._dropdownconfig:
return
dd = self._dropdownconfig[name]
for attr, value in kwargs.items():
if hasattr(dd, attr):
setattr(dd, attr, value)
@expose_command()
def dropdown_info(self, name=None):
"""
Get information on configured or currently active DropDowns.
If name is None, a list of all dropdown names is returned.
"""
if name is None:
return {"dropdowns": [ddname for ddname in self._dropdownconfig]}
elif name in self.dropdowns:
return self.dropdowns[name].info()
elif name in self._dropdownconfig:
return self._dropdownconfig[name].info()
else:
raise ValueError('No DropDown named "%s".' % name)
def get_state(self):
"""
Get the state of existing dropdown windows. Used for restoring state across
Qtile restarts (`restart` == True) or config reloads (`restart` == False).
"""
state = []
for name, dd in self.dropdowns.items():
client_wid = dd.window.wid
state.append((name, client_wid, dd.visible))
return state
def restore_state(self, state, restart: bool) -> list[int]:
"""
Restore the state of existing dropdown windows. Used for restoring state across
Qtile restarts (`restart` == True) or config reloads (`restart` == False).
"""
orphans = []
for name, wid, visible in state:
if name in self._dropdownconfig:
if restart:
self._spawned[name] = Match(wid=wid)
if not visible:
self._to_hide.append(name)
else:
# We are reloading the config; manage the clients now
self.dropdowns[name] = DropDownToggler(
self.qtile.windows_map[wid],
self.name,
self._dropdownconfig[name],
)
if not visible:
self.dropdowns[name].hide()
else:
orphans.append(wid)
if self._spawned:
# Handle re-managed clients after restarting
assert restart
hook.subscribe.client_new(self.on_client_new)
if not restart and self.dropdowns:
# We're only reloading so don't have these hooked via self.on_client_new
hook.subscribe.client_killed(self.on_client_killed)
hook.subscribe.float_change(self.on_float_change)
return orphans