-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.py
615 lines (511 loc) · 19.5 KB
/
config.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
"""qtile wayland config"""
from __future__ import annotations
import os
import random
import subprocess
from enum import Enum
from typing import TYPE_CHECKING
from libqtile import hook, qtile
from libqtile.backend.base import Window
from libqtile.backend.wayland import InputConfig
from libqtile.config import (
Click,
Drag,
DropDown,
Group,
Key,
Match,
ScratchPad,
Screen,
)
from libqtile.group import _Group
from libqtile.layout.floating import Floating
from libqtile.layout.max import Max
from libqtile.layout.tree import TreeTab
from libqtile.layout.xmonad import MonadTall
from libqtile.lazy import lazy
from colors import colors
if TYPE_CHECKING:
from typing import Any
from libqtile.core.manager import Qtile
assert qtile is not None, "This should never be None."
MOD = "mod4"
ALT = "mod1"
TERMINAL = "foot"
BROWSER = "firefox"
LAUNCHER = "fuzzel.sh"
FILE_MANAGER = "pcmanfm"
MUSIC_CTRL = """dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify
/org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player."""
font_setting: tuple[str, int] = ("FiraMono Nerd Font", 13)
group_assignments: dict[str, Any[str, ...]] = {
"1": (BROWSER),
"2": (
"valheim.x86_64",
"battle.net.exe",
"wowclassic.exe",
"ck3",
"paradox launcher",
"steam_app",
),
"3": ("discord", "signal"),
"4": ("spotify"),
"5": ("Steam"),
}
# Inputs configuration
wl_input_rules = {
"type:keyboard": InputConfig(
kb_repeat_rate=50,
kb_repeat_delay=300,
),
"type:touchpad": InputConfig(drag=True, tap=True, natural_scroll=True),
}
home = os.path.expandvars("$HOME")
wallpapers = os.listdir(f"{home}/wallpapers")
random_wallpaper = random.choice(wallpapers)
path_to_wallpaper = os.path.join(f"{home}/wallpapers", random_wallpaper)
startup_items = [
f"waybar -c {home}/.config/waybar/config-qtile &",
"wlsunset -l 59.6 -L 18.1 &",
f"swaybg -m fill -i {path_to_wallpaper} &",
f"magick {path_to_wallpaper} -blur 16x8 /tmp/lock_img.jpg &",
"mako &",
"kanshi &",
"swayidle -w timeout 300 'lock.sh' timeout 600 'wlopm --off '*'' resume 'wlopm --on '*'' timeout 900 'sudo zzz' &",
"gpg-connect-agent --daemon &",
"/usr/libexec/polkit-gnome-authentication-agent-1 &",
"dbus-update-activation-environment DISPLAY",
]
@hook.subscribe.startup_once
def autostart() -> None:
"""Autostart things when qtile starts"""
subprocess.call(["brightnessctl set 20%"], shell=True)
for item in startup_items:
with subprocess.Popen(item, shell=True) as process:
hook.subscribe.shutdown(process.terminate)
# This is just a temporary workaround to make Waybar align nicely on startup
@hook.subscribe.client_managed
def client_managed(_client):
qtile.core._current_output.organise_layers() # type: ignore[attr-defined]
class GroupState(Enum):
EMPTY = 1
OCCUPIED = 2
FOCUSED = 3
@hook.subscribe.focus_change
@hook.subscribe.client_killed
@hook.subscribe.client_managed
def update_groups_waybar(*_args) -> None:
"""Update Waybar of open groups and windows"""
existing_groups = dict.fromkeys(qtile.groups_map.keys(), GroupState.EMPTY) # type: ignore[attr-defined]
existing_groups.pop("scratchpad", None)
current_group: str = qtile.current_screen.group.label # type: ignore[attr-defined]
for window in qtile.windows(): # type: ignore[attr-defined]
if (
window["wm_class"] is not None
and window["group"] is not None
and window["group"] in existing_groups
):
existing_groups[window["group"]] = GroupState.OCCUPIED
existing_groups[current_group] = GroupState.FOCUSED
text: str = ""
for group, status in existing_groups.items():
match status:
case GroupState.OCCUPIED:
text += f"""<span fgcolor='{colors["primary"]}'> {group} </span>"""
case GroupState.EMPTY:
text += f"""<span fgcolor='{colors["secondary"]}'> {group} </span>"""
case GroupState.FOCUSED:
text += f"""<span fgcolor='{colors["background"]}' bgcolor='{colors["primary"]}' line_height='2'> {group} </span>"""
with open("/tmp/qtile-groups.txt", "w", encoding="utf-8") as output:
output.write(text)
output.close()
subprocess.call(["pkill -RTMIN+8 waybar"], shell=True)
@hook.subscribe.client_name_updated
@hook.subscribe.layout_change
@hook.subscribe.focus_change
def update_window_title_waybar(*args) -> None:
"""Update Waybar of focused window title"""
# Unpack a client from args (ignore layouts and groups)
client: Window = args[0] if len(args) == 1 else None
# Ignore any clients that are not the current window
if client and client.wid != qtile.current_window.wid: # type: ignore[attr-defined]
return
window_title: str = (
"" if qtile.current_window is None else qtile.current_window.name # type: ignore[attr-defined]
)
# Count the windows for max layout
if qtile.current_layout.name == layout_names["max"]: # type: ignore[attr-defined]
wincount = len(qtile.current_group.windows) # type: ignore[attr-defined]
window_title = f"({wincount}) {window_title}" if wincount > 1 else window_title
with open("/tmp/qtile-window-title.txt", "w", encoding="utf-8") as output:
output.write(f"<span fgcolor='{colors["text"]}'>{window_title}</span>")
output.close()
subprocess.call(["pkill -RTMIN+9 waybar"], shell=True)
@hook.subscribe.startup_complete
@hook.subscribe.layout_change
def update_layout_waybar(*_args) -> None:
"""Update Waybar of current layout"""
current_layout = qtile.current_layout.name or "" # type: ignore[attr-defined]
with open("/tmp/qtile-layout.txt", "w", encoding="utf-8") as output:
output.write(f"<span fgcolor='{colors["primary"]}'>{current_layout}</span>")
output.close()
subprocess.call(["pkill -RTMIN+7 waybar"], shell=True)
@hook.subscribe.client_urgent_hint_changed
def follow_url(client: Window) -> None:
"""If Firefox is flagged as urgent, focus it"""
wm_class: list | None = client.get_wm_class()
if wm_class is not None:
for item in wm_class:
match item:
case item if item.lower() in BROWSER and client.group is not None:
qtile.current_screen.set_group(client.group) # type: ignore[attr-defined]
client.group.focus(client)
return
@hook.subscribe.float_change
def center_window() -> None:
"""Centers all the floating windows"""
try:
if qtile.current_window is not None: # type: ignore[attr-defined]
qtile.current_window.center() # type: ignore[attr-defined]
except AttributeError:
return
@hook.subscribe.client_new
def assign_app_group(client: Window) -> None:
"""Decides which apps go where when they are launched"""
wm_class: list | None = client.get_wm_class()
if wm_class is None:
return
try:
for group, apps in group_assignments.items():
if any(item.lower().startswith(apps) for item in wm_class):
client.togroup(group)
except IndexError:
return
@hook.subscribe.client_new
def toggle_fullscreen_off(client: Window) -> None:
"""Toggle fullscreen off in case there's any window fullscreened in the group"""
try:
group = client.group
except AttributeError:
return
if group is None:
group = qtile.current_group # type: ignore[attr-defined]
for window in group.windows:
if window.fullscreen:
window.toggle_fullscreen()
@hook.subscribe.client_killed
def fallback_default_layout(client: Window) -> None:
"""Reset a group to default layout when theres is only one window left"""
if (
not isinstance(client, Window)
or client.group is None
or client.group.screen != qtile.current_screen # type: ignore[attr-defined]
or client.floating is True
):
return
try:
win_count = len(client.group.windows)
except AttributeError:
win_count = 0
if win_count > 1:
return
group_name: str = client.group.name
default_layout_index: int = 0
qtile.to_layout_index(default_layout_index, group_name) # type: ignore[attr-defined]
@hook.subscribe.current_screen_change
def warp_cursor() -> None:
"""Warp cursor to focused screen"""
qtile.warp_to_screen() # type: ignore[attr-defined]
def spawn_or_focus(self: Qtile, app: str) -> None:
"""Check if the app being launched is already running, if so focus it"""
window = None
for win in self.windows_map.values():
if isinstance(win, Window):
wm_class: list | None = win.get_wm_class()
if wm_class is None or win.group is None:
return
if any(item.lower() in app.lower() for item in wm_class):
window = win
group = win.group
group.toscreen(toggle=False)
break
if window is None:
self.spawn(app)
elif window == self.current_window:
try:
assert (
self.current_layout.swap_main is not None
), "The current layout should have swap_main"
self.current_layout.swap_main()
except AttributeError:
return
else:
self.current_group.focus(window)
def float_to_front(self: Qtile) -> None:
"""Bring all floating windows of the group to front"""
for window in self.current_group.windows:
if window.floating:
window.bring_to_front()
def toggle_layout(self: Qtile, layout_name: str) -> None:
"""Takes a layout name and tries to set it, or if it's already active back to monadtall"""
assert (
self.current_group.screen is not None
), "The screen should not be none for the current group"
screen_rect = self.current_group.screen.get_rect()
self.current_group.layout.hide()
if self.current_group.layout.name == layout_name:
self.current_group.setlayout(layout_names["monadtall"])
else:
self.current_group.setlayout(layout_name)
self.current_group.layout.show(screen_rect)
def next_window(self: Qtile) -> None:
"""If treetab or max layout, cycle next window"""
if self.current_layout.name in (layout_names["max"], layout_names["treetab"]):
self.current_group.layout.down()
def focus_group(self: Qtile, direction: str) -> None:
"""Go to next/previous group"""
group: _Group = self.current_screen.group
go_to: _Group
match direction:
case "next":
go_to = group.get_next_group(skip_empty=False)
case "previous":
go_to = group.get_previous_group(skip_empty=False)
case _:
return
self.current_screen.set_group(go_to)
def window_to_screen(self: Qtile, direction: str) -> None:
"""Send a window to next/previous screen"""
screen_i: int = self.screens.index(self.current_screen)
if screen_i != 0 and self.current_window is not None:
group: str
match direction:
case "next":
group = self.screens[screen_i + 1].group.name
case "previous":
group = self.screens[screen_i - 1].group.name
self.current_window.togroup(group)
# Layouts
layout_theme: dict[str, int | str] = {
"border_width": 3,
"border_focus": colors["primary"],
"border_normal": colors["secondary"],
}
layout_names: dict[str, str] = {"monadtall": "tall~", "max": "max~", "treetab": "tree~"}
layouts = [
MonadTall(
**layout_theme,
single_border_width=0,
single_margin=0,
margin=10,
new_client_position="top",
name=layout_names["monadtall"],
),
Max(name=layout_names["max"]),
TreeTab(
name=layout_names["treetab"],
font=font_setting[0],
fontsize=font_setting[1],
active_fg=colors["background"],
active_bg=colors["primary"],
bg_color=colors["background"],
border_width=5,
inactive_bg=colors["secondary"],
inactive_fg=colors["text"],
previous_on_rm=True,
urgent_fg=colors["urgent"],
urgent_bg=colors["secondary"],
sections=[""],
section_fg=colors["background"],
padding_left=10,
padding_y=8,
margin_y=0,
),
]
floating_layout = Floating(
float_rules=[
*Floating.default_float_rules,
Match(wm_class="Pinentry-gtk-2"),
Match(wm_class="pavucontrol"),
Match(title="Execute File", wm_class="Pcmanfm"),
Match(title="Confirm File Replacing", wm_class="Pcmanfm"),
Match(title="Steam", wm_class=""),
],
**layout_theme,
)
# Keybinds
keys = [
# Switch focus between windows
Key([MOD], "Down", lazy.layout.down()),
Key([MOD], "Up", lazy.layout.up()),
Key([MOD], "Left", lazy.layout.left().when(layout=layout_names["monadtall"])),
Key([MOD], "Right", lazy.layout.right().when(layout=layout_names["monadtall"])),
# Move windows between left/right columns or move up/down in current stack
Key(
[MOD, "shift"],
"Left",
lazy.layout.swap_left().when(layout=layout_names["monadtall"]),
),
Key(
[MOD, "shift"],
"Right",
lazy.layout.swap_right().when(layout=layout_names["monadtall"]),
),
Key(
[MOD, "shift"],
"Down",
lazy.layout.shuffle_down().when(layout=layout_names["monadtall"]),
lazy.layout.move_down().when(layout=layout_names["treetab"]),
),
Key(
[MOD, "shift"],
"Up",
lazy.layout.shuffle_up().when(layout=layout_names["monadtall"]),
lazy.layout.move_up().when(layout=layout_names["treetab"]),
),
# Grow/shrink windows
Key(
[MOD, ALT],
"Left",
lazy.layout.shrink_main().when(layout=layout_names["monadtall"]),
),
Key(
[MOD, ALT],
"Right",
lazy.layout.grow_main().when(layout=layout_names["monadtall"]),
),
Key(
[MOD, ALT], "Down", lazy.layout.shrink().when(layout=layout_names["monadtall"])
),
Key([MOD, ALT], "Up", lazy.layout.grow().when(layout=layout_names["monadtall"])),
# Move focus/windows between screens
Key([MOD], "Tab", lazy.screen.toggle_group()),
Key([MOD], "period", lazy.next_screen()),
Key([MOD], "comma", lazy.prev_screen()),
Key([MOD, "shift"], "period", lazy.function(window_to_screen, "next")),
Key([MOD, "shift"], "comma", lazy.function(window_to_screen, "previous")),
Key([MOD, "control"], "Right", lazy.function(focus_group, "next")),
Key([MOD, "control"], "Left", lazy.function(focus_group, "previous")),
# Various window controls
Key([MOD, "shift"], "c", lazy.window.kill()),
Key([MOD, "control"], "c", lazy.window.center()),
Key([MOD, "shift"], "space", lazy.layout.reset()),
Key([MOD], "f", lazy.window.toggle_fullscreen()),
Key([MOD, "shift"], "f", lazy.window.toggle_floating()),
Key([MOD], "space", lazy.layout.flip()),
Key([MOD, "shift"], "Tab", lazy.function(float_to_front)),
Key([MOD], "b", lazy.hide_show_bar()),
Key([MOD], "u", lazy.clear_urgent("keybind")),
Key([MOD], "i", lazy.toggle_widget_info()),
# Layout toggles
Key([MOD], "m", lazy.function(toggle_layout, layout_names["max"])),
Key([MOD], "t", lazy.function(toggle_layout, layout_names["treetab"])),
# Notification commands
Key([MOD, "shift"], "b", lazy.spawn("callbacks.sh battery")),
Key([MOD, "shift"], "d", lazy.spawn("callbacks.sh date")),
Key([MOD, "shift"], "w", lazy.spawn("callbacks.sh wifi")),
# Some app shortcuts
Key([MOD], "w", lazy.function(spawn_or_focus, BROWSER)),
Key([MOD], "Return", lazy.spawn(TERMINAL)),
Key([MOD, "control"], "Return", lazy.spawn(FILE_MANAGER)),
Key([MOD], "c", lazy.function(spawn_or_focus, "signal-desktop")),
Key([MOD], "r", lazy.spawn(LAUNCHER)),
Key(
[MOD], "d", lazy.function(spawn_or_focus, "flatpak run com.discordapp.Discord")
),
Key([MOD], "s", lazy.function(spawn_or_focus, "flatpak run com.spotify.Client")),
Key([MOD], "g", lazy.function(spawn_or_focus, "steam-native")),
Key([MOD], "p", lazy.spawn("pass.sh")),
Key([MOD], "o", lazy.spawn("otp.sh")),
Key([MOD, "control"], "m", lazy.spawn("mount.sh")),
Key([MOD], "e", lazy.spawn("emojis.sh")),
Key([MOD, "shift"], "p", lazy.spawn("screenshot.sh")),
# ScratchPads
Key([MOD, "shift"], "Return", lazy.group["scratchpad"].dropdown_toggle("terminal")),
Key([MOD], "n", lazy.group["scratchpad"].dropdown_toggle("newsboat")),
Key([MOD], "Escape", lazy.group["scratchpad"].hide_all()),
# Spotify controls, lacking real media keys on 65% keyboard
Key([MOD], "8", lazy.spawn(f"{MUSIC_CTRL}PlayPause")),
Key([MOD], "9", lazy.spawn(f"{MUSIC_CTRL}Next")),
Key([MOD], "7", lazy.spawn(f"{MUSIC_CTRL}Previous")),
# Media volume keys
Key([], "XF86AudioMute", lazy.spawn("pactl set-sink-mute 0 toggle")),
Key(
[MOD, "shift"], "m", lazy.spawn("pactl set-sink-mute 0 toggle")
), # Extra keybind
Key([], "XF86AudioLowerVolume", lazy.spawn("pactl set-sink-volume 0 -5%")),
Key([], "XF86AudioRaiseVolume", lazy.spawn("pactl set-sink-volume 0 +5%")),
# Brightness controll
Key([], "XF86MonBrightnessDown", lazy.spawn("brightnessctl set 5%-")),
Key([], "XF86MonBrightnessUp", lazy.spawn("brightnessctl set +5%")),
# Microphone toggle muted/unmuted
Key([MOD], "q", lazy.spawn("callbacks.sh mic")),
# System controls
Key([MOD], "l", lazy.spawn("lock.sh")),
Key([MOD, "shift"], "r", lazy.reload_config()),
Key([MOD, "control"], "r", lazy.restart()),
Key([MOD, "shift"], "q", lazy.shutdown()),
]
# Groups
group_settings: list[tuple[str, dict[str, Any]]] = [
("1", {"label": "1", "layout": layout_names["monadtall"]}),
("2", {"label": "2", "layout": layout_names["monadtall"]}),
("3", {"label": "3", "layout": layout_names["monadtall"]}),
("4", {"label": "4", "layout": layout_names["monadtall"]}),
("5", {"label": "5", "layout": layout_names["monadtall"]}),
("6", {"label": "6", "layout": layout_names["monadtall"]}),
]
groups: list[Any] = [Group(name, **kwargs) for name, kwargs in group_settings]
for i in groups:
keys.extend(
[
Key([MOD], i.name, lazy.group[i.name].toscreen(toggle=True)),
Key([MOD, "shift"], i.name, lazy.window.togroup(i.name)),
]
)
# ScratchPads
scratchpad_conf: dict[str, Any] = {
"warp_pointer": False,
"height": 0.6,
"y": 0.2,
"opacity": 1,
}
groups.append(
ScratchPad(
"scratchpad",
[
DropDown("terminal", TERMINAL, **scratchpad_conf),
DropDown(
"newsboat",
f"""{TERMINAL} newsboat -C=~/.config/newsboat/config
-u=~/sync/files/newsboat/urls -c=~/sync/files/newsboat/cache.db""",
**scratchpad_conf,
),
],
)
)
# Mouse
mouse = [
Drag(
[MOD],
"Button1",
lazy.window.set_position_floating(),
start=lazy.window.get_position(),
),
Drag(
[MOD], "Button3", lazy.window.set_size_floating(), start=lazy.window.get_size()
),
Click([MOD], "Button2", lazy.window.toggle_floating()),
]
# Screen and bar
screens = [Screen()]
# Misc
dgroups_key_binder = None # type: ignore[var-annotated]
dgroups_app_rules = [] # type: list
follow_mouse_focus = True
bring_front_click = True
cursor_warp = False
reconfigure_screens = True
auto_fullscreen = True
auto_minimize = True
focus_on_window_activation = "urgent"