-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathbar.py
340 lines (292 loc) · 11.7 KB
/
bar.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
from fabric.widgets.box import Box
from fabric.widgets.label import Label
from fabric.widgets.datetime import DateTime
from fabric.widgets.centerbox import CenterBox
from fabric.widgets.button import Button
from fabric.widgets.revealer import Revealer
from fabric.widgets.wayland import WaylandWindow as Window
from fabric.hyprland.widgets import Workspaces, WorkspaceButton, Language, get_hyprland_connection
from fabric.hyprland.service import HyprlandEvent
from fabric.utils.helpers import exec_shell_command_async
from gi.repository import Gdk
from modules.systemtray import SystemTray
import modules.icons as icons
import config.data as data
from modules.metrics import MetricsSmall, Battery, NetworkApplet
from modules.controls import ControlSmall
from modules.weather import Weather
class Bar(Window):
def __init__(self, **kwargs):
super().__init__(
name="bar",
layer="top",
anchor="left top right" if not data.VERTICAL else "top left bottom",
margin="-4px -4px -8px -4px" if not data.VERTICAL else "-4px -8px -4px -4px",
exclusivity="auto",
visible=True,
all_visible=True,
)
self.notch = kwargs.get("notch", None)
self.component_visibility = data.BAR_COMPONENTS_VISIBILITY
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(id=i, label="") for i in range(1, 11)],
)
self.ws_container = Box(
name="workspaces-container",
children=self.workspaces,
)
self.button_tools = Button(
name="button-bar",
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.network = NetworkApplet()
self.lang_label = Label(name="lang-label")
self.language = Button(name="language", h_align="center", v_align="center", child=self.lang_label)
self.on_language_switch()
self.connection.connect("event::activelayout", self.on_language_switch)
self.date_time = DateTime(name="date-time", formatters=["%H:%M"] if not data.VERTICAL else ["%H\n%M"], h_align="center" if not data.VERTICAL else "fill", v_align="center", h_expand=True, v_expand=True)
self.button_apps = Button(
name="button-bar",
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",
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",
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()
# Apply visibility settings to components
self.apply_component_visibility()
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.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.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)
# Use centered layout when both vertical and centered_bar are enabled
is_centered_bar = data.VERTICAL and getattr(data, 'CENTERED_BAR', False)
self.bar_inner = CenterBox(
name="bar-inner",
orientation="h" if not data.VERTICAL else "v",
h_align="fill",
v_align="fill",
start_children=None if is_centered_bar else Box(
name="start-container",
spacing=4,
orientation="h" if not data.VERTICAL else "v",
children=self.h_start_children if not data.VERTICAL else self.v_start_children,
),
center_children=Box(
orientation="v",
spacing=4,
children=self.v_all_children if is_centered_bar else self.v_center_children
) if data.VERTICAL else None,
end_children=None if is_centered_bar else Box(
name="end-container",
spacing=4,
orientation="h" if not data.VERTICAL else "v",
children=self.h_end_children if not data.VERTICAL else self.v_end_children,
),
)
self.children = self.bar_inner
self.hidden = False
# self.show_all()
self.systray._update_visibility()
def apply_component_visibility(self):
"""Apply saved visibility settings to all components"""
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,
}
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):
"""Toggle visibility for a specific component"""
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,
}
if component_name in components and component_name in self.component_visibility:
# Toggle the visibility state
self.component_visibility[component_name] = not self.component_visibility[component_name]
# Apply the new state
components[component_name].set_visible(self.component_visibility[component_name])
# Update the configuration
config_file = os.path.expanduser(f"~/.config/{data.APP_NAME}/config/config.json")
if os.path.exists(config_file):
with open(config_file, 'r') as f:
config = json.load(f)
# Update the config with the new visibility state
config[f'bar_{component_name}_visible'] = self.component_visibility[component_name]
with open(config_file, 'w') as f:
json.dump(config, f)
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(Gdk.CursorType.HAND2))
def on_button_leave(self, widget, event):
window = widget.get_window()
if window:
window.set_cursor(None)
def on_button_clicked(self, *args):
# Ejecuta notify-send cuando se hace clic en el botón
exec_shell_command_async("notify-send 'Botón presionado' '¡Funciona!'")
def search_apps(self):
self.notch.open_notch("launcher")
def overview(self):
self.notch.open_notch("overview")
def power_menu(self):
self.notch.open_notch("power")
def tools_menu(self):
self.notch.open_notch("tools")
def on_language_switch(self, _=None, event: HyprlandEvent=None):
lang = event.data[1] if event else Language().get_label()
self.language.set_tooltip_text(lang)
if not data.VERTICAL:
self.lang_label.set_label(lang[: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")