-
Notifications
You must be signed in to change notification settings - Fork 0
/
bar.py
executable file
·222 lines (196 loc) · 7.35 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
#!/usr/bin/python3 -u
"""
Description: Main GTK bar class that spawns the bar
Author: thnikk
"""
import gi
import os
from subprocess import run, CalledProcessError
import json
import time
import common as c
import module
gi.require_version('Gtk', '3.0')
gi.require_version('GtkLayerShell', '0.1')
from gi.repository import Gtk, Gdk, GtkLayerShell, GLib # noqa
class Display:
""" Display class """
def __init__(self, config):
self.display = Gdk.Display.get_default()
self.display.connect("monitor-added", self.added)
self.display.connect("monitor-removed", self.removed)
self.wm = self.get_wm()
self.config = config
self.bars = {}
self.monitors = self.get_monitors()
self.plugs = self.get_plugs()
def get_wm(self):
try:
run(['swaymsg', '-q'], check=True)
return 'sway'
except CalledProcessError:
return 'hyprland'
def get_plugs(self):
""" Get plugs from swaymsg """
if self.wm == 'sway':
return [
output['name'] for output in
json.loads(run(
["swaymsg", "-t", "get_outputs"],
check=True, capture_output=True
).stdout.decode('utf-8'))
if output['active']
]
else:
return [
output['name'] for output in
json.loads(run(
['hyprctl', '-j', 'monitors'],
check=True, capture_output=True
).stdout.decode('utf-8'))
if not output['disabled']
]
def get_monitors(self):
""" Get monitor objects from gdk """
return [
self.display.get_monitor(n)
for n in range(self.display.get_n_monitors())
]
def removed(self, display, monitor):
""" Remove bar from bar list when a monitor is removed """
index = self.monitors.index(monitor)
plug = self.plugs[index]
if 'outputs' in list(self.config):
if plug not in self.config['outputs']:
return
self.bars[plug].window.destroy()
self.bars.pop(plug)
def added(self, display, monitor):
""" Draw a new bar when a monitor is added """
self.monitors = self.get_monitors()
self.plugs = self.get_plugs()
self.draw_bar(monitor)
def draw_bar(self, monitor):
""" Draw a bar on a monitor """
try_count = 0
while True:
try:
index = self.monitors.index(monitor)
plug = self.plugs[index]
break
except IndexError:
if try_count >= 3:
return
time.sleep(1)
try_count += 1
if 'outputs' in list(self.config):
if plug not in self.config['outputs']:
return
bar = Bar(self, monitor)
bar.populate()
css_path = "/".join(__file__.split('/')[:-1]) + '/style.css'
bar.css(css_path)
try:
bar.css(self.config['style'])
except KeyError:
pass
bar.start()
self.bars[plug] = bar
def draw_all(self):
""" Initialize all monitors """
for monitor in self.monitors:
self.draw_bar(monitor)
class Bar:
""" Bar class"""
def __init__(self, display, monitor):
self.window = Gtk.Window()
self.display = display
self.config = display.config
self.spacing = self.config['spacing'] if 'spacing' in self.config \
else 5
try:
self.position = display.config['position']
except KeyError:
self.position = 'bottom'
self.bar = c.box('h', style='bar', spacing=self.spacing)
self.left = c.box('h', style='modules-left', spacing=self.spacing)
self.center = c.box('h', style='modules-center', spacing=self.spacing)
self.right = c.box('h', style='modules-right', spacing=self.spacing)
self.bar.pack_start(self.left, 0, 0, 0)
self.bar.set_center_widget(self.center)
self.bar.pack_end(self.right, 0, 0, 0)
self.window.add(self.bar)
self.monitor = monitor
def populate(self):
""" Populate bar with modules """
for section_name, section in {
"modules-left": self.left,
"modules-center": self.center,
"modules-right": self.right,
}.items():
for name in self.config[section_name]:
loaded_module = module.module(self, name, self.config)
if loaded_module:
section.add(loaded_module)
def css(self, file):
""" Load CSS from file """
try:
css_provider = Gtk.CssProvider()
css_provider.load_from_path(os.path.expanduser(file))
screen = Gdk.Screen.get_default()
style_context = Gtk.StyleContext()
style_context.add_provider_for_screen(
screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
except GLib.GError as e:
filename = f"/{'/'.join(e.message.split('/')[1:]).split(':')[0]}"
if '.config/pybar' not in filename:
c.print_debug(
f"Failed to load CSS from {filename}",
name='pybar', color="red")
pass
def modules(self, modules):
""" Add modules to bar """
main = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=0)
main.get_style_context().add_class("bar")
for index, position in enumerate(["left", "center", "right"]):
section = Gtk.Box(
orientation=Gtk.Orientation.HORIZONTAL, spacing=2)
section.get_style_context().add_class(position)
for item in modules[index]:
section.pack_start(item(), False, False, 0)
main.pack_start(section, position == "center", False, 0)
self.window.add(main)
def start(self):
""" Start bar """
GtkLayerShell.init_for_window(self.window)
pos = {
"bottom": GtkLayerShell.Edge.BOTTOM,
"top": GtkLayerShell.Edge.TOP
}
try:
position = pos[self.position]
except KeyError:
position = pos['bottom']
# Anchor and stretch to bottom of the screen
GtkLayerShell.set_anchor(self.window, position, 1)
GtkLayerShell.set_anchor(self.window, GtkLayerShell.Edge.LEFT, 1)
GtkLayerShell.set_anchor(self.window, GtkLayerShell.Edge.RIGHT, 1)
# Set margin to make bar more readable for testing
try:
margin = self.config['margin']
except KeyError:
margin = 10
GtkLayerShell.set_margin(
self.window, GtkLayerShell.Edge.LEFT, margin)
GtkLayerShell.set_margin(
self.window, GtkLayerShell.Edge.RIGHT, margin)
GtkLayerShell.set_margin(self.window, position, margin)
# Set namespace based on config
if 'namespace' in list(self.config):
GtkLayerShell.set_namespace(self.window, self.config['namespace'])
else:
GtkLayerShell.set_namespace(self.window, 'pybar')
GtkLayerShell.set_monitor(self.window, self.monitor)
# Reserve part of screen
GtkLayerShell.auto_exclusive_zone_enable(self.window)
self.window.show_all()