This repository was archived by the owner on Jan 29, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathbar.py
More file actions
631 lines (549 loc) · 20.9 KB
/
bar.py
File metadata and controls
631 lines (549 loc) · 20.9 KB
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
import json
import os
from fabric.hyprland.service import HyprlandEvent
from fabric.hyprland.widgets import HyprlandLanguage as Language
from fabric.hyprland.widgets import HyprlandWorkspaces as Workspaces
from fabric.hyprland.widgets import WorkspaceButton, get_hyprland_connection
from fabric.utils.helpers import exec_shell_command_async
from fabric.widgets.box import Box
from fabric.widgets.button import Button
from fabric.widgets.centerbox import CenterBox
from fabric.widgets.datetime import DateTime
from fabric.widgets.label import Label
from fabric.widgets.revealer import Revealer
from gi.repository import Gdk, GLib, Gtk
import config.data as data
import modules.icons as icons
from modules.controls import ControlSmall
from modules.dock import Dock
from modules.metrics import Battery, MetricsSmall, NetworkApplet
from modules.systemprofiles import Systemprofiles
from modules.systemtray import SystemTray
from modules.weather import Weather
from widgets.wayland import WaylandWindow as Window
CHINESE_NUMERALS = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "〇"]
# Tooltips
tooltip_apps = f"""<b><u>Launcher</u></b>
<b>• Apps:</b> Type to search.
<b>• Calculator [Prefix "="]:</b> Solve a math expression.
e.g. "=2+2"
<b>• Converter [Prefix ";"]:</b> Convert between units.
e.g. ";100 USD to EUR", ";10 km to miles"
<b>• Special Commands [Prefix ":"]:</b>
:update - Open {data.APP_NAME_CAP}'s updater.
:d - Open Dashboard.
:w - Open Wallpapers."""
tooltip_power = """<b>Power Menu</b>"""
tooltip_tools = """<b>Toolbox</b>"""
tooltip_overview = """<b>Overview</b>"""
class Bar(Window):
def __init__(self, monitor_id: int = 0, **kwargs):
self.monitor_id = monitor_id
super().__init__(
name="bar",
layer="top",
exclusivity="auto",
visible=True,
all_visible=True,
monitor=monitor_id,
)
self.anchor_var = ""
self.margin_var = ""
match data.BAR_POSITION:
case "Top":
self.anchor_var = "left top right"
case "Bottom":
self.anchor_var = "left bottom right"
case "Left":
self.anchor_var = "left" if data.CENTERED_BAR else "left top bottom"
case "Right":
self.anchor_var = "right" if data.CENTERED_BAR else "top right bottom"
case _:
self.anchor_var = "left top right"
if data.VERTICAL:
match data.BAR_THEME:
case "Edge":
self.margin_var = "-8px -8px -8px -8px"
case _:
self.margin_var = "-4px -8px -4px -4px"
else:
match data.BAR_THEME:
case "Edge":
self.margin_var = "-8px -8px -8px -8px"
case _:
if data.BAR_POSITION == "Bottom":
self.margin_var = "-8px -4px -4px -4px"
else:
self.margin_var = "-4px -4px -8px -4px"
self.set_anchor(self.anchor_var)
self.set_margin(self.margin_var)
self.notch = kwargs.get("notch", None)
self.component_visibility = data.BAR_COMPONENTS_VISIBILITY
self.dock_instance = None
self.integrated_dock_widget = None
# Calculate workspace range based on monitor_id
# Monitor 0: workspaces 1-10, Monitor 1: workspaces 11-20, etc.
start_workspace = self.monitor_id * 10 + 1
end_workspace = start_workspace + 10
workspace_range = range(start_workspace, end_workspace)
self.workspaces = Workspaces(
name="workspaces",
invert_scroll=True,
empty_scroll=True,
v_align="fill",
orientation="h" if not data.VERTICAL else "v",
spacing=8,
buttons=[
WorkspaceButton(
h_expand=False,
v_expand=False,
h_align="center",
v_align="center",
id=i,
label=None,
style_classes=["vertical"] if data.VERTICAL else None,
)
for i in workspace_range
],
buttons_factory=(
None
if data.BAR_HIDE_SPECIAL_WORKSPACE
else Workspaces.default_buttons_factory
),
)
self.workspaces_num = Workspaces(
name="workspaces-num",
invert_scroll=True,
empty_scroll=True,
v_align="fill",
orientation="h" if not data.VERTICAL else "v",
spacing=0 if not data.BAR_WORKSPACE_USE_CHINESE_NUMERALS else 4,
buttons=[
WorkspaceButton(
h_expand=False,
v_expand=False,
h_align="center",
v_align="center",
id=i,
label=(
CHINESE_NUMERALS[(i - start_workspace)]
if data.BAR_WORKSPACE_USE_CHINESE_NUMERALS
and 0 <= (i - start_workspace) < len(CHINESE_NUMERALS)
else str(i)
),
)
for i in workspace_range
],
buttons_factory=(
None
if data.BAR_HIDE_SPECIAL_WORKSPACE
else Workspaces.default_buttons_factory
),
)
self.ws_container = Box(
name="workspaces-container",
children=(
self.workspaces
if not data.BAR_WORKSPACE_SHOW_NUMBER
else self.workspaces_num
),
)
self.button_tools = Button(
name="button-bar",
tooltip_markup=tooltip_tools,
on_clicked=lambda *_: self.tools_menu(),
child=Label(name="button-bar-label", markup=icons.toolbox),
)
self.connection = get_hyprland_connection()
self.button_tools.connect("enter_notify_event", self.on_button_enter)
self.button_tools.connect("leave_notify_event", self.on_button_leave)
self.systray = SystemTray()
self.weather = Weather()
self.sysprofiles = Systemprofiles()
self.network = NetworkApplet()
self.lang_label = Label(name="lang-label")
self.language = Button(
name="language",
h_align="center",
v_align="center",
child=self.lang_label,
on_clicked=lambda *_: exec_shell_command_async("hyprctl switchxkblayout all next"),
)
self.on_language_switch()
self.connection.connect("event::activelayout", self.on_language_switch)
# Determine date-time format based on the new setting
if data.DATETIME_12H_FORMAT:
time_format_horizontal = "%I:%M %p"
time_format_vertical = "%I\n%M\n%p"
else:
time_format_horizontal = "%H:%M"
time_format_vertical = "%H\n%M"
self.date_time = DateTime(
name="date-time",
formatters=(
[time_format_horizontal]
if not data.VERTICAL
else [time_format_vertical]
),
h_align="center" if not data.VERTICAL else "fill",
v_align="center",
h_expand=True,
v_expand=True,
style_classes=["vertical"] if data.VERTICAL else [],
)
self.button_apps = Button(
name="button-bar",
tooltip_markup=tooltip_apps,
on_clicked=lambda *_: self.search_apps(),
child=Label(name="button-bar-label", markup=icons.apps),
)
self.button_apps.connect("enter_notify_event", self.on_button_enter)
self.button_apps.connect("leave_notify_event", self.on_button_leave)
self.button_power = Button(
name="button-bar",
tooltip_markup=tooltip_power,
on_clicked=lambda *_: self.power_menu(),
child=Label(name="button-bar-label", markup=icons.shutdown),
)
self.button_power.connect("enter_notify_event", self.on_button_enter)
self.button_power.connect("leave_notify_event", self.on_button_leave)
self.button_overview = Button(
name="button-bar",
tooltip_markup=tooltip_overview,
on_clicked=lambda *_: self.overview(),
child=Label(name="button-bar-label", markup=icons.windows),
)
self.button_overview.connect("enter_notify_event", self.on_button_enter)
self.button_overview.connect("leave_notify_event", self.on_button_leave)
self.control = ControlSmall()
self.metrics = MetricsSmall()
self.battery = Battery()
self.apply_component_props()
self.rev_right = [
self.metrics,
self.control,
]
self.revealer_right = Revealer(
name="bar-revealer",
transition_type="slide-left",
child_revealed=True,
child=Box(
name="bar-revealer-box",
orientation="h",
spacing=4,
children=self.rev_right if not data.VERTICAL else None,
),
)
self.boxed_revealer_right = Box(
name="boxed-revealer",
children=[
self.revealer_right,
],
)
self.rev_left = [
self.weather,
self.sysprofiles,
self.network,
]
self.revealer_left = Revealer(
name="bar-revealer",
transition_type="slide-right",
child_revealed=True,
child=Box(
name="bar-revealer-box",
orientation="h",
spacing=4,
children=self.rev_left if not data.VERTICAL else None,
),
)
self.boxed_revealer_left = Box(
name="boxed-revealer",
children=[
self.revealer_left,
],
)
self.h_start_children = [
self.button_apps,
self.ws_container,
self.button_overview,
self.boxed_revealer_left,
]
self.h_end_children = [
self.boxed_revealer_right,
self.battery,
self.systray,
self.button_tools,
self.language,
self.date_time,
self.button_power,
]
self.v_start_children = [
self.button_apps,
self.systray,
self.control,
self.sysprofiles,
self.network,
self.button_tools,
]
self.v_center_children = [
self.button_overview,
self.ws_container,
self.weather,
]
self.v_end_children = [
self.battery,
self.metrics,
self.language,
self.date_time,
self.button_power,
]
self.v_all_children = []
self.v_all_children.extend(self.v_start_children)
self.v_all_children.extend(self.v_center_children)
self.v_all_children.extend(self.v_end_children)
# Create embedded dock when bar is in center position (regardless of DOCK_ENABLED setting)
should_embed_dock = (
data.BAR_POSITION == "Bottom"
or (data.PANEL_THEME == "Panel" and data.BAR_POSITION in ["Top", "Bottom"])
)
if should_embed_dock:
if not data.VERTICAL:
self.dock_instance = Dock(integrated_mode=True)
self.integrated_dock_widget = self.dock_instance.wrapper
is_centered_bar = data.VERTICAL and getattr(data, "CENTERED_BAR", False)
bar_center_actual_children = None
if self.integrated_dock_widget is not None:
bar_center_actual_children = self.integrated_dock_widget
elif data.VERTICAL:
bar_center_actual_children = Box(
orientation=Gtk.Orientation.VERTICAL,
spacing=4,
children=(
self.v_all_children if is_centered_bar else self.v_center_children
),
)
self.bar_inner = CenterBox(
name="bar-inner",
orientation=(
Gtk.Orientation.HORIZONTAL
if not data.VERTICAL
else Gtk.Orientation.VERTICAL
),
h_align="fill",
v_align="fill",
start_children=(
None
if is_centered_bar
else Box(
name="start-container",
spacing=4,
orientation=(
Gtk.Orientation.HORIZONTAL
if not data.VERTICAL
else Gtk.Orientation.VERTICAL
),
children=(
self.h_start_children
if not data.VERTICAL
else self.v_start_children
),
)
),
center_children=bar_center_actual_children,
end_children=(
None
if is_centered_bar
else Box(
name="end-container",
spacing=4,
orientation=(
Gtk.Orientation.HORIZONTAL
if not data.VERTICAL
else Gtk.Orientation.VERTICAL
),
children=(
self.h_end_children
if not data.VERTICAL
else self.v_end_children
),
)
),
)
self.children = self.bar_inner
self.hidden = False
self.themed_children = [
self.button_apps,
self.button_overview,
self.button_power,
self.button_tools,
self.language,
self.date_time,
self.ws_container,
self.weather,
self.network,
self.battery,
self.metrics,
self.systray,
self.control,
]
if self.integrated_dock_widget:
self.themed_children.append(self.integrated_dock_widget)
current_theme = data.BAR_THEME
theme_classes = ["pills", "dense", "edge", "edgecenter"]
for tc in theme_classes:
self.bar_inner.remove_style_class(tc)
self.style = None
match current_theme:
case "Pills":
self.style = "pills"
case "Dense":
self.style = "dense"
case "Edge":
if data.VERTICAL and data.CENTERED_BAR:
self.style = "edgecenter"
else:
self.style = "edge"
case _:
self.style = "pills"
self.bar_inner.add_style_class(self.style)
if self.integrated_dock_widget and hasattr(
self.integrated_dock_widget, "add_style_class"
):
for theme_class_to_remove in ["pills", "dense", "edge"]:
style_context = self.integrated_dock_widget.get_style_context()
if style_context.has_class(theme_class_to_remove):
self.integrated_dock_widget.remove_style_class(
theme_class_to_remove
)
self.integrated_dock_widget.add_style_class(self.style)
if data.BAR_THEME == "Dense" or data.BAR_THEME == "Edge":
for child in self.themed_children:
if hasattr(child, "add_style_class"):
child.add_style_class("invert")
match data.BAR_POSITION:
case "Top":
self.bar_inner.add_style_class("top")
case "Bottom":
self.bar_inner.add_style_class("bottom")
case "Left":
self.bar_inner.add_style_class("left")
case "Right":
self.bar_inner.add_style_class("right")
case _:
self.bar_inner.add_style_class("top")
if data.VERTICAL:
self.bar_inner.add_style_class("vertical")
self.systray._update_visibility()
self.chinese_numbers()
def apply_component_props(self):
components = {
"button_apps": self.button_apps,
"systray": self.systray,
"control": self.control,
"network": self.network,
"button_tools": self.button_tools,
"button_overview": self.button_overview,
"ws_container": self.ws_container,
"weather": self.weather,
"battery": self.battery,
"metrics": self.metrics,
"language": self.language,
"date_time": self.date_time,
"button_power": self.button_power,
"sysprofiles": self.sysprofiles,
}
for component_name, widget in components.items():
if component_name in self.component_visibility:
widget.set_visible(self.component_visibility[component_name])
def toggle_component_visibility(self, component_name):
components = {
"button_apps": self.button_apps,
"systray": self.systray,
"control": self.control,
"network": self.network,
"button_tools": self.button_tools,
"button_overview": self.button_overview,
"ws_container": self.ws_container,
"weather": self.weather,
"battery": self.battery,
"metrics": self.metrics,
"language": self.language,
"date_time": self.date_time,
"button_power": self.button_power,
"sysprofiles": self.sysprofiles,
}
if component_name in components and component_name in self.component_visibility:
self.component_visibility[component_name] = not self.component_visibility[
component_name
]
components[component_name].set_visible(
self.component_visibility[component_name]
)
config_file = os.path.expanduser(
f"~/.config/{data.APP_NAME}/config/config.json"
)
if os.path.exists(config_file):
try:
with open(config_file, "r") as f:
config = json.load(f)
config[f"bar_{component_name}_visible"] = self.component_visibility[
component_name
]
with open(config_file, "w") as f:
json.dump(config, f, indent=4)
except Exception as e:
print(f"Error updating config file: {e}")
return self.component_visibility[component_name]
return None
def on_button_enter(self, widget, event):
window = widget.get_window()
if window:
window.set_cursor(Gdk.Cursor.new_from_name(widget.get_display(), "hand2"))
def on_button_leave(self, widget, event):
window = widget.get_window()
if window:
window.set_cursor(None)
def on_button_clicked(self, *args):
exec_shell_command_async("notify-send 'Botón presionado' '¡Funciona!'")
def search_apps(self):
if self.notch:
self.notch.open_notch("launcher")
def overview(self):
if self.notch:
self.notch.open_notch("overview")
def power_menu(self):
if self.notch:
self.notch.open_notch("power")
def tools_menu(self):
if self.notch:
self.notch.open_notch("tools")
def on_language_switch(self, _=None, event: HyprlandEvent = None):
try:
lang_data = (
event.data[1]
if event and event.data and len(event.data) > 1
else Language().get_label()
)
except json.JSONDecodeError:
lang_data = "UNK" # Fallback to default language label
self.language.set_tooltip_text(lang_data)
if not data.VERTICAL:
self.lang_label.set_label(lang_data[:3].upper())
else:
self.lang_label.add_style_class("icon")
self.lang_label.set_markup(icons.keyboard)
def toggle_hidden(self):
self.hidden = not self.hidden
if self.hidden:
self.bar_inner.add_style_class("hidden")
else:
self.bar_inner.remove_style_class("hidden")
# Ensure notch is above bar when bar is shown
if self.notch:
# Focus the notch window to bring it to front
GLib.idle_add(lambda: exec_shell_command_async("hyprctl dispatch focuswindow class:notch") if self.notch else None)
def chinese_numbers(self):
if data.BAR_WORKSPACE_USE_CHINESE_NUMERALS:
self.workspaces_num.add_style_class("chinese")
else:
self.workspaces_num.remove_style_class("chinese")