This commit is contained in:
sankalp committed 2024-10-18 10:21:37 +05:45
1 parent 2095c1f071
commit 1b7ec29d26
52 files changed
+18934

No files matched your search

@@ -0,0 +1,40 @@
from __init__ import *
class BatteryLabel(Box):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.battery_icon = Label(
label="󰁹",
style="font-size: 16px; margin-right: 4px;",
)
self.battery_label = Label(
label="N/A",
style="font-size: 12px; margin-left: 4px;",
)
invoke_repeater(1000, self.update_battery_status, initial_call=True)
self.children = self.battery_icon, self.battery_label
def update_battery_status(self):
battery_percent = round(
sensor.percent if (sensor := psutil.sensors_battery()) else 0.0
)
self.battery_label.set_label(f"{(battery_percent or 'N/A')}%")
is_charging = bool(
psutil.sensors_battery().power_plugged
if psutil.sensors_battery() is not None
else False
)
if is_charging:
icons = ["󰢟", "󰂆", "󰂇", "󰂈", "󰢝", "󰂉", "󰢞", "󰂊", "󰂋", "󰂅"]
else:
icons = ["󱃍", "󰁻", "󰁼", "󰁽", "󰁾", "󰁿", "󰂀", "󰂁", "󰂂", "󰁹"]
index = min(max(battery_percent // 10, 0), 9)
self.battery_icon.set_label(icons[index])
return True
@@ -0,0 +1,25 @@
from __init__ import *
class Bluetooth(Gtk.Box):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.bluetooth_client = BluetoothClient()
self.bluetooth_icon = Label(
label="󰂲",
style="font-size: 16px;",
)
self.bluetooth_client.connect("changed", self.update_bluetooth_status)
self.update_bluetooth_status()
self.add(self.bluetooth_icon)
def update_bluetooth_status(self, *args):
if self.bluetooth_client.enabled:
self.bluetooth_icon.set_label("󰂯")
else:
self.bluetooth_icon.set_label("󰂲")
def on_destroy(self):
self.bluetooth_client.disconnect("changed", self.update_bluetooth_status)
@@ -0,0 +1,30 @@
from __init__ import *
class MicrophoneIndicator(Box):
def __init__(self, **kwargs):
super().__init__(**kwargs)
audio_service = Audio()
self.audio_service = audio_service
self.microphone_icon = Label(
label="󰍭",
style="font-size: 16px; margin-left:4px;",
)
self.audio_service.connect("microphone_changed", self.update_microphone_status)
self.update_microphone_status()
self.children = (self.microphone_icon,)
def update_microphone_status(self, *args):
current_microphone = self.audio_service.microphone
if current_microphone:
is_muted = current_microphone.muted
if is_muted:
self.microphone_icon.set_visible(True)
else:
self.microphone_icon.set_visible(False)
return True
@@ -0,0 +1,144 @@
from __init__ import *
class PowerMenu(EventBox):
def __init__(self):
super().__init__(name="power-event")
icons_map = {
"power-lock": "",
"power-suspend": "",
"power-logout": "",
"power-reboot": "",
"power-shutdown": "",
}
self.buttons = {
name: Button(name=name, child=Label(label=f"{icon}"))
for name, icon in icons_map.items()
}
self.power_box = Box(
name="power-box",
orientation="h",
children=[
self.buttons[name]
for name in [
"power-lock",
"power-suspend",
"power-logout",
"power-reboot",
]
],
)
self.accept_button = Button(name="accept", child=Label(label="✔"))
self.cancel_button = Button(name="cancel", child=Label(label="✘"))
self.revealer_buttons_box = Box(
name="confirm-box",
orientation="h",
children=[self.accept_button, self.cancel_button],
)
self.revealer = Revealer(
name="power-revealer",
transition_type="slide-right",
transition_duration=300,
child=self.power_box,
reveal_child=False,
)
self.shutdown_box = Box(
name="shutdown-box",
orientation="h",
children=[self.buttons["power-shutdown"]],
)
self.super_box = Box(
name="super-box",
orientation="h",
children=[self.revealer, self.shutdown_box],
)
self.confirm_revealer = Revealer(
name="confirm-revealer",
transition_type="slide-right",
transition_duration=300,
child=self.revealer_buttons_box,
reveal_child=False,
)
self.full_power = Box(
name="full-power",
orientation="h",
children=[self.super_box, self.confirm_revealer],
)
self.add(self.full_power)
self.buttons["self"] = self
self.current_command = None
self.confirmation_active = False
for btn in self.buttons.values():
bulk_connect(
btn,
{
"button-press-event": self.on_button_press,
"enter-notify-event": self.on_button_hover,
"leave-notify-event": self.on_button_unhover,
},
)
self.accept_button.connect("button-press-event", self.on_accept_press)
self.cancel_button.connect("button-press-event", self.on_cancel_press)
def on_button_hover(self, button, event):
if button == self and not self.confirmation_active:
self.revealer.set_reveal_child(True)
def on_button_unhover(self, button, event):
if button == self and not self.confirmation_active:
self.revealer.set_reveal_child(False)
def on_button_press(self, button, event):
commands = {
self.buttons["power-lock"]: "hyprlock",
self.buttons[
"power-suspend"
]: "hyprctl dispatch exec hyprlock && systemctl suspend",
self.buttons["power-logout"]: "hyprctl dispatch exit",
self.buttons["power-reboot"]: "systemctl reboot",
self.buttons["power-shutdown"]: "systemctl poweroff",
}
if button in commands:
if button != self.buttons["power-lock"]:
self.current_command = commands[button]
self.revealer.set_reveal_child(False)
self.confirm_revealer.set_reveal_child(True)
self.shutdown_box.hide()
self.confirmation_active = True
else:
exec_shell_command_async(commands[button], lambda *_: None)
return True
def on_accept_press(self, button, event):
if self.current_command:
exec_shell_command_async(self.current_command, lambda *_: None)
self.current_command = None
self.confirm_revealer.set_reveal_child(False)
self.revealer.set_reveal_child(True)
self.shutdown_box.show()
self.confirmation_active = False
return True
def on_cancel_press(self, button, event):
self.current_command = None
self.confirm_revealer.set_reveal_child(False)
self.revealer.set_reveal_child(True)
self.shutdown_box.show()
self.confirmation_active = False
return True
@@ -0,0 +1,70 @@
from __init__ import *
class RAMUsage(Box):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.ram_icon = Label(
label="󰍛",
style="font-size: 16px; margin-right: 4px;",
)
self.ram_label = Label(
label="N/A%",
style="font-size: 12px; margin-left: 4px;",
)
invoke_repeater(1000, self.update_ram_status, initial_call=True)
self.children = self.ram_icon, self.ram_label
def update_ram_status(self):
ram_percent = int(psutil.virtual_memory().percent)
self.ram_label.set_label(f"{ram_percent}%")
return True
class CPUUsage(Box):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cpu_icon = Label(
label="󰻠",
style="font-size: 16px; margin-right: 4px;",
)
self.cpu_label = Label(
label="N/A%",
style="font-size: 12px; margin-left: 4px;",
)
invoke_repeater(1000, self.update_cpu_status, initial_call=True)
self.children = self.cpu_icon, self.cpu_label
def update_cpu_status(self):
cpu_percent = int(psutil.cpu_percent())
self.cpu_label.set_label(f"{cpu_percent}%")
return True
class SwapMemoryUsage(Box):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.swap_icon = Label(
label="󰓡",
style="font-size: 16px; margin-right: 4px;",
)
self.swap_label = Label(
label="N/A%", style="font-size: 12px; margin-left: 4px;"
)
invoke_repeater(1000, self.update_swap_memory_status, initial_call=True)
self.children = self.swap_icon, self.swap_label
def update_swap_memory_status(self):
swap_info = psutil.swap_memory()
swap_percent = int(swap_info.percent)
self.swap_label.set_label(f"{swap_percent}%")
return True
@@ -0,0 +1,118 @@
from __init__ import *
class PagerClient(TypedDict):
title: str
initialClass: str
mapped: bool
hidden: bool
address: str
class TaskBar(Box):
def __init__(self, icon_size: int = 16, **kwargs):
super().__init__(orientation="h", spacing=6, **kwargs)
self.connection = get_hyprland_connection()
self.icon_size = icon_size
self.icon_theme = Gtk.IconTheme.get_default()
self.displayed_classes = set()
self.set_visible(False)
if self.connection.ready:
self.render_with_delay()
else:
self.connection.connect("event::ready", lambda *_: self.render_with_delay())
for event in (
"activewindow",
"openwindow",
"closewindow",
"changefloatingmode",
):
self.connection.connect("event::" + event, lambda *_: self.render(None))
def render_with_delay(self, *_):
GLib.timeout_add(100, self.render)
def render(self, *_):
self.children = []
self.displayed_classes.clear()
clients = self.fetch_clients()
visible_clients = [
client for client in clients if client["mapped"] and not client["hidden"]
]
if visible_clients:
for client in visible_clients:
window_class = client["initialClass"].lower()
if window_class not in self.displayed_classes:
icon = self.bake_window_icon(window_class)
button = Button(image=icon, tooltip_text=client["title"])
button.connect(
"button-press-event", self.on_icon_click, client["address"]
)
self.add(button)
self.displayed_classes.add(window_class)
self.set_visible(True)
self.show_all()
else:
self.set_visible(False)
def on_icon_click(self, widget, event, address):
command = ["hyprctl", "dispatch", "focuswindow", f"address:{address}"]
subprocess.run(command, check=True)
def fetch_clients(self) -> list[PagerClient]:
return json.loads(self.connection.send_command("j/clients").reply.decode())
def bake_window_icon(
self, window_class: str, fallback_icon: str = "image-missing"
) -> Image:
# First, try to get the icon from the desktop entry
icon_name = self.get_icon_from_desktop_entry(window_class)
if icon_name:
pixbuf = self.load_icon(icon_name)
else:
# Fallback to the icon theme if not found
pixbuf = self.load_icon(window_class, fallback_icon)
return Image(pixbuf=pixbuf, size=self.icon_size)
def get_icon_from_desktop_entry(self, window_class: str) -> str:
desktop_files = glob.glob("/usr/share/applications/*.desktop")
for desktop_file in desktop_files:
with open(desktop_file, "r") as f:
for line in f:
if line.startswith("Name="):
app_name = line.split("=", 1)[1].strip().lower()
if window_class in app_name:
# If a matching app name is found, return the Icon
for line in f:
if line.startswith("Icon="):
return line.split("=", 1)[1].strip()
return None
def load_icon(
self, icon_name: str, fallback_icon: str = "image-missing"
) -> GdkPixbuf.Pixbuf:
try:
pixbuf = self.icon_theme.load_icon(
icon_name,
self.icon_size,
Gtk.IconLookupFlags.FORCE_SIZE,
)
except Exception:
pixbuf = self.icon_theme.load_icon(
fallback_icon,
self.icon_size,
Gtk.IconLookupFlags.FORCE_SIZE,
)
return pixbuf
@@ -0,0 +1,61 @@
from __init__ import *
class VolumeIndicator(Box):
def __init__(self, **kwargs):
super().__init__(**kwargs)
audio_service = Audio()
self.audio_service = audio_service
self.volume_icon = Button(
label="󰖁",
style="font-size: 16px;",
)
# Connect mouse button events to handle clicks
self.volume_icon.connect("button-press-event", self.on_button_press)
self.audio_service.connect("changed", self.update_volume_status)
invoke_repeater(1000, self.update_volume_status, initial_call=True)
self.children = (self.volume_icon,)
def on_button_press(self, widget, event):
if event.button == 1:
self.on_clicked()
elif event.button == 3:
self.on_right_clicked()
elif event.button == 2:
self.on_middle_clicked()
def on_clicked(self):
exec_shell_command("pavucontrol")
def on_right_clicked(self):
current_stream = self.audio_service.speaker
if current_stream:
current_stream.muted = not current_stream.muted
self.update_volume_status()
def on_middle_clicked(self):
exec_shell_command("pavucontrol")
def update_volume_status(self, *args):
current_stream = self.audio_service.speaker
if current_stream:
volume_level = current_stream.volume
is_muted = current_stream.muted
if is_muted:
self.volume_icon.set_label("󰖁")
else:
if volume_level == 0:
self.volume_icon.set_label("󰝟")
elif volume_level <= 33:
self.volume_icon.set_label("󰕿")
elif volume_level <= 66:
self.volume_icon.set_label("󰖀")
else:
self.volume_icon.set_label("󰕾")
return True
@@ -0,0 +1,52 @@
from __init__ import *
class Wifi(Box):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.wifi_icon = Label(
label="󰤭",
style="font-size: 16px; ",
)
self.wifi_icon.set_tooltip_text("Unknown")
invoke_repeater(1000, self.update_wifi_status, initial_call=True)
self.children = (self.wifi_icon,)
def update_wifi_status(self):
wifi_name = self.get_connected_wifi_name()
wifi_on = self.check_wifi_status()
if wifi_on:
self.wifi_icon.set_label("󰤥")
self.wifi_icon.set_tooltip_text(wifi_name)
else:
self.wifi_icon.set_label("󰤭")
self.wifi_icon.set_tooltip_text("Unknown")
return True
def check_wifi_status(self):
for interface, addrs in psutil.net_if_addrs().items():
if "wlan" in interface or "wifi" in interface:
if (
interface in psutil.net_if_stats()
and psutil.net_if_stats()[interface].isup
):
return True
return False
def get_connected_wifi_name(self):
try:
output = exec_shell_command(
"nmcli -t -f active,ssid dev wifi | grep '^yes' | cut -d: -f2"
)
for line in output.splitlines():
if line.startswith("yes:"):
return line.split(":")[1]
except Exception as e:
print(f"Error retrieving Wi-Fi name: {e}")
return None
@@ -0,0 +1,36 @@
from __init__ import *
class Wifi(Box):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.wifi_icon = Label(
label="󰤭",
style="font-size: 16px; margin-right: 6px;",
)
invoke_repeater(1000, self.update_wifi_status, initial_call=True)
self.children = (self.wifi_icon,)
def update_wifi_status(self):
wifi_on = self.check_wifi_status()
if wifi_on:
self.wifi_icon.set_label("󰤥")
else:
self.wifi_icon.set_label("󰤭")
return True
def check_wifi_status(self):
for interface, addrs in psutil.net_if_addrs().items():
if "wlan" in interface or "wifi" in interface:
if (
interface in psutil.net_if_stats()
and psutil.net_if_stats()[interface].isup
):
for addr in addrs:
if addr.family in (socket.AF_INET, socket.AF_INET6):
return True
return False