chore(initial):
This commit is contained in:
42 files changed
+21627
No files matched your search
@@ -0,0 +1 @@
|
||||
# Bar components package
|
||||
@@ -0,0 +1,202 @@
|
||||
import json
|
||||
from gi.repository import GLib, Gdk
|
||||
from fabric.hyprland.widgets import get_hyprland_connection
|
||||
from utils.occlusion import check_occlusion
|
||||
|
||||
|
||||
class AutoHideBarController:
|
||||
"""Controller for bar auto-hide behavior."""
|
||||
|
||||
# Constants for configuration
|
||||
OCCLUSION_CHECK_INTERVAL = 250 # ms
|
||||
HIDE_DELAY = 300 # ms
|
||||
|
||||
def __init__(self, bar):
|
||||
self.bar = bar
|
||||
self.is_hidden = False
|
||||
self.hide_id = None
|
||||
self.is_hovered = False
|
||||
self.conn = get_hyprland_connection()
|
||||
self.last_occlusion_state = False
|
||||
|
||||
self._setup_event_handlers()
|
||||
self._start_occlusion_checking()
|
||||
|
||||
def _setup_event_handlers(self):
|
||||
"""Initialize event handlers for window management"""
|
||||
# Initial state check
|
||||
if self.conn.ready:
|
||||
self.check_hide()
|
||||
else:
|
||||
self.conn.connect("event::ready", lambda *_: self.check_hide())
|
||||
|
||||
# Monitor window events
|
||||
window_events = ("activewindow", "openwindow", "closewindow", "changefloatingmode")
|
||||
for event in window_events:
|
||||
self.conn.connect(f"event::{event}", lambda *_: self.check_hide())
|
||||
self.conn.connect("event::workspace", lambda *_: GLib.timeout_add(50, self.check_hide))
|
||||
|
||||
def _start_occlusion_checking(self):
|
||||
"""Start periodic occlusion check"""
|
||||
GLib.timeout_add(self.OCCLUSION_CHECK_INTERVAL, self.check_occlusion_state)
|
||||
|
||||
def _get_bar_height(self):
|
||||
"""Get the height of the bar with fallback"""
|
||||
try:
|
||||
height = self.bar.get_allocated_height()
|
||||
return height if height > 0 else 30
|
||||
except Exception:
|
||||
return 30 # Default fallback
|
||||
|
||||
def _get_occlusion_region(self):
|
||||
"""Get the region to check for occlusion"""
|
||||
return ("top", self._get_bar_height())
|
||||
|
||||
def check_occlusion_state(self):
|
||||
"""Periodic occlusion check"""
|
||||
# Skip occlusion check if hovered
|
||||
if self.is_hovered:
|
||||
self.bar.main_box.remove_style_class("occluded")
|
||||
return True
|
||||
|
||||
# Check occlusion using the top region
|
||||
is_occluded = check_occlusion(self._get_occlusion_region())
|
||||
|
||||
# Only apply changes if state has changed to avoid unnecessary CSS operations
|
||||
if is_occluded != self.last_occlusion_state:
|
||||
self.last_occlusion_state = is_occluded
|
||||
if is_occluded:
|
||||
self.bar.main_box.add_style_class("occluded")
|
||||
else:
|
||||
self.bar.main_box.remove_style_class("occluded")
|
||||
if not self.is_hovered and self.is_hidden:
|
||||
# Debounce showing the bar to prevent flickering
|
||||
GLib.timeout_add(100, lambda: self.toggle_bar(not self._should_stay_hidden()))
|
||||
|
||||
return True
|
||||
|
||||
def _should_stay_hidden(self):
|
||||
"""Determine if the bar should stay hidden based on window state"""
|
||||
clients = self.get_clients()
|
||||
current_ws = self.get_workspace()
|
||||
ws_clients = [w for w in clients if w["workspace"]["id"] == current_ws]
|
||||
|
||||
# If we have normal windows and are not being hovered, stay hidden
|
||||
return self._has_normal_windows(ws_clients) and not self.is_hovered
|
||||
|
||||
def on_bar_enter(self, widget, event):
|
||||
"""Handle hover over bar content."""
|
||||
self.is_hovered = True
|
||||
self.bar.main_box.remove_style_class("occluded")
|
||||
|
||||
# Cancel any pending hide operations
|
||||
self._cancel_hide_timer()
|
||||
self.toggle_bar(show=True)
|
||||
return True # Stop event propagation
|
||||
|
||||
def on_bar_leave(self, widget, event):
|
||||
"""Handle leave from bar content."""
|
||||
# Only trigger if mouse actually left the entire bar area
|
||||
if event.detail == Gdk.NotifyType.INFERIOR:
|
||||
return False # Ignore child-to-child mouse movements
|
||||
|
||||
self.is_hovered = False
|
||||
|
||||
# Schedule hide if needed
|
||||
if self._should_stay_hidden() or check_occlusion(self._get_occlusion_region()):
|
||||
self.delay_hide()
|
||||
|
||||
# Update occlusion state
|
||||
if check_occlusion(self._get_occlusion_region()):
|
||||
self.last_occlusion_state = True
|
||||
self.bar.main_box.add_style_class("occluded")
|
||||
|
||||
return True # Stop event propagation
|
||||
|
||||
def on_hover_enter(self, *args):
|
||||
"""Handle hover over top activation area."""
|
||||
self.bar.main_box.remove_style_class("occluded")
|
||||
self.toggle_bar(show=True)
|
||||
return False
|
||||
|
||||
def _cancel_hide_timer(self):
|
||||
"""Cancel any pending hide timer"""
|
||||
if self.hide_id:
|
||||
GLib.source_remove(self.hide_id)
|
||||
self.hide_id = None
|
||||
|
||||
def toggle_bar(self, show):
|
||||
"""Show or hide the bar immediately."""
|
||||
if show:
|
||||
if self.is_hidden:
|
||||
self.is_hidden = False
|
||||
self.bar.main_box.remove_style_class("hide-bar")
|
||||
self.bar.main_box.add_style_class("show-bar")
|
||||
self._cancel_hide_timer()
|
||||
else:
|
||||
if not self.is_hidden:
|
||||
self.is_hidden = True
|
||||
self.bar.main_box.remove_style_class("show-bar")
|
||||
self.bar.main_box.add_style_class("hide-bar")
|
||||
|
||||
def delay_hide(self):
|
||||
"""Schedule hiding after short delay."""
|
||||
self._cancel_hide_timer()
|
||||
self.hide_id = GLib.timeout_add(self.HIDE_DELAY, self.hide_bar)
|
||||
|
||||
def hide_bar(self):
|
||||
"""Finalize hiding procedure."""
|
||||
if not self.is_hovered:
|
||||
self.toggle_bar(show=False)
|
||||
self.hide_id = None
|
||||
return False
|
||||
|
||||
def get_clients(self):
|
||||
"""Get current client list."""
|
||||
try:
|
||||
return json.loads(self.conn.send_command("j/clients").reply.decode())
|
||||
except (json.JSONDecodeError, AttributeError):
|
||||
return []
|
||||
|
||||
def get_workspace(self):
|
||||
"""Get current workspace ID."""
|
||||
try:
|
||||
return json.loads(
|
||||
self.conn.send_command("j/activeworkspace").reply.decode()
|
||||
).get("id", 0)
|
||||
except (json.JSONDecodeError, AttributeError):
|
||||
return 0
|
||||
|
||||
def _has_normal_windows(self, clients):
|
||||
"""Check if any non-floating, non-fullscreen windows exist"""
|
||||
return any(not w.get("floating") and not w.get("fullscreen") for w in clients)
|
||||
|
||||
def check_hide(self, *args):
|
||||
"""Determine if bar should auto-hide based on window state."""
|
||||
# Get window data
|
||||
clients = self.get_clients()
|
||||
current_ws = self.get_workspace()
|
||||
ws_clients = [w for w in clients if w["workspace"]["id"] == current_ws]
|
||||
|
||||
# Check for occlusion
|
||||
is_occluded = check_occlusion(self._get_occlusion_region())
|
||||
self.last_occlusion_state = is_occluded
|
||||
|
||||
# Determine visibility state
|
||||
if not ws_clients:
|
||||
# No windows in current workspace, show the bar
|
||||
self.bar.main_box.remove_style_class("occluded")
|
||||
self.toggle_bar(show=True)
|
||||
elif self._has_normal_windows(ws_clients):
|
||||
# Normal windows exist, handle occlusion and hide if not hovered
|
||||
if is_occluded:
|
||||
self.bar.main_box.add_style_class("occluded")
|
||||
|
||||
if not self.is_hovered:
|
||||
self.toggle_bar(show=False)
|
||||
else:
|
||||
# Only floating/fullscreen windows, show the bar
|
||||
self.bar.main_box.remove_style_class("occluded")
|
||||
self.toggle_bar(show=True)
|
||||
|
||||
return False
|
||||
@@ -0,0 +1,24 @@
|
||||
from fabric.widgets.button import Button
|
||||
from fabric.widgets.label import Label
|
||||
from fabric.hyprland.widgets import Language, HyprlandEvent, get_hyprland_connection
|
||||
|
||||
|
||||
class LanguageWidget(Button):
|
||||
"""Widget that displays the current keyboard layout."""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.lang_label = Label(name="lang-label")
|
||||
super().__init__(
|
||||
name="language", h_align="center", v_align="center",
|
||||
child=self.lang_label, **kwargs
|
||||
)
|
||||
|
||||
self.conn = get_hyprland_connection()
|
||||
self.update_language()
|
||||
self.conn.connect("event::activelayout", self.update_language)
|
||||
|
||||
def update_language(self, _=None, event: HyprlandEvent = None):
|
||||
"""Update the language indicator based on Hyprland events."""
|
||||
lang = event.data[1] if event else Language().get_label()
|
||||
self.set_tooltip_text(lang)
|
||||
self.lang_label.set_label(lang[:2].lower())
|
||||
@@ -0,0 +1,327 @@
|
||||
import subprocess
|
||||
import json
|
||||
import psutil
|
||||
from gi.repository import GLib, Gdk
|
||||
|
||||
from fabric.widgets.label import Label
|
||||
from fabric.widgets.box import Box
|
||||
from fabric.widgets.circularprogressbar import CircularProgressBar
|
||||
from fabric.widgets.revealer import Revealer
|
||||
from fabric.core.fabricator import Fabricator
|
||||
from fabric.widgets.button import Button
|
||||
import utils.icons as icons
|
||||
import config.data as data
|
||||
|
||||
|
||||
class MetricsProvider:
|
||||
def __init__(self):
|
||||
self.cpu = 0.0
|
||||
self.mem = 0.0
|
||||
self.swap = 0.0
|
||||
self.bat_percent = 0.0
|
||||
self.bat_charging = None
|
||||
|
||||
GLib.timeout_add_seconds(1, self._update)
|
||||
|
||||
def _update(self):
|
||||
# Get non-blocking usage percentages (interval=0)
|
||||
# The first call may return 0, but subsequent calls will provide consistent values.
|
||||
self.cpu = psutil.cpu_percent(interval=0)
|
||||
mem = psutil.virtual_memory()
|
||||
self.mem = mem.percent
|
||||
swap = psutil.swap_memory()
|
||||
self.swap = swap.percent
|
||||
|
||||
battery = psutil.sensors_battery()
|
||||
if battery is None:
|
||||
self.bat_percent = 0.0
|
||||
self.bat_charging = None
|
||||
else:
|
||||
self.bat_percent = battery.percent
|
||||
self.bat_charging = battery.power_plugged
|
||||
return True
|
||||
|
||||
def get_metrics(self):
|
||||
return (self.cpu, self.mem, self.swap)
|
||||
|
||||
def get_battery(self):
|
||||
return (self.bat_percent, self.bat_charging)
|
||||
|
||||
|
||||
|
||||
shared_provider = MetricsProvider()
|
||||
|
||||
|
||||
class SingularMetric:
|
||||
def __init__(self, id, name, icon):
|
||||
self.is_vertical_layout = data.VERTICAL
|
||||
self.name_markup = name
|
||||
self.icon_markup = icon
|
||||
|
||||
self.icon = Label(name="metrics-icon", markup=icon)
|
||||
self.circle = CircularProgressBar(
|
||||
name="metrics-circle",
|
||||
value=0,
|
||||
size=26,
|
||||
line_width=2,
|
||||
start_angle=150,
|
||||
end_angle=390,
|
||||
style_classes=id,
|
||||
child=self.icon,
|
||||
)
|
||||
|
||||
self.circle_button = Button(
|
||||
name=f"metrics-{id}-button",
|
||||
child=self.circle,
|
||||
style="padding: 0; margin: 0; background: none;",
|
||||
)
|
||||
|
||||
self.circle_button.add_events(
|
||||
Gdk.EventMask.ENTER_NOTIFY_MASK | Gdk.EventMask.LEAVE_NOTIFY_MASK
|
||||
)
|
||||
self.circle_button.connect("enter-notify-event", self.on_button_enter)
|
||||
self.circle_button.connect("leave-notify-event", self.on_button_leave)
|
||||
|
||||
self.circle_button.connect("clicked", self.on_circle_clicked)
|
||||
|
||||
self.level = Label(
|
||||
name="metrics-level",
|
||||
style_classes=id,
|
||||
label="0%",
|
||||
style="margin: 4px 0px;" if self.is_vertical_layout else "margin: 0 4px;",
|
||||
)
|
||||
self.revealer = Revealer(
|
||||
name=f"metrics-{id}-revealer",
|
||||
transition_duration=250,
|
||||
transition_type="slide-up" if self.is_vertical_layout else "slide-left",
|
||||
child=self.level,
|
||||
child_revealed=False,
|
||||
)
|
||||
|
||||
self.box = Box(
|
||||
name=f"metrics-{id}-box",
|
||||
orientation="v" if self.is_vertical_layout else "h",
|
||||
spacing=0,
|
||||
children=[self.circle_button, self.revealer],
|
||||
)
|
||||
|
||||
# Connect events for cursor changes to the box as well
|
||||
self.box.connect("enter-notify-event", self.on_button_enter)
|
||||
self.box.connect("leave-notify-event", self.on_button_leave)
|
||||
|
||||
# Set initial tooltip - moved after box is created
|
||||
self.update_tooltip("0%")
|
||||
|
||||
def update_tooltip(self, value_text):
|
||||
tooltip = f"{self.icon_markup} {self.name_markup}: {value_text}"
|
||||
# self.box.set_tooltip_markup(tooltip)
|
||||
self.circle_button.set_tooltip_markup(tooltip)
|
||||
self.circle.set_tooltip_markup(tooltip)
|
||||
self.icon.set_tooltip_markup(tooltip)
|
||||
|
||||
def markup(self):
|
||||
"""Get formatted text for tooltip display"""
|
||||
return f"{self.icon_markup} {self.name_markup}: {self.level.get_label()}"
|
||||
|
||||
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_circle_clicked(self, button):
|
||||
# Toggle the revealed state of the label
|
||||
current_state = self.revealer.get_child_revealed()
|
||||
self.revealer.set_reveal_child(not current_state)
|
||||
|
||||
|
||||
class Metrics(Box):
|
||||
def __init__(self, **kwargs):
|
||||
# Determine if we should use vertical layout for components
|
||||
self.is_vertical_layout = data.VERTICAL
|
||||
|
||||
super().__init__(
|
||||
name="metrics",
|
||||
spacing=0,
|
||||
orientation="h" if not self.is_vertical_layout else "v",
|
||||
visible=True,
|
||||
all_visible=True,
|
||||
style="padding:4px 0px" if self.is_vertical_layout else "padding: 0px 4px;",
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
self.batt_fabricator = Fabricator(
|
||||
poll_from=lambda v: shared_provider.get_battery(),
|
||||
on_changed=lambda f, v: self.update_battery,
|
||||
interval=1000,
|
||||
stream=False,
|
||||
default_value=0,
|
||||
)
|
||||
self.batt_fabricator.changed.connect(self.update_battery)
|
||||
GLib.idle_add(self.update_battery, None, shared_provider.get_battery())
|
||||
|
||||
|
||||
self.cpu = (
|
||||
SingularMetric("cpu", "CPU", icons.cpu)
|
||||
)
|
||||
self.ram = (
|
||||
SingularMetric("ram", "RAM", icons.memory)
|
||||
)
|
||||
self.swap = (
|
||||
SingularMetric("swap", "SWAP", icons.swap)
|
||||
)
|
||||
self.bat = (
|
||||
SingularMetric("bat", "BATTERY", icons.battery)
|
||||
)
|
||||
|
||||
|
||||
# Add only enabled metrics
|
||||
if self.ram:
|
||||
self.add(self.ram.box)
|
||||
self.add(Box(name="metrics-sep"))
|
||||
if self.cpu:
|
||||
self.add(self.cpu.box)
|
||||
if self.swap:
|
||||
self.add(Box(name="metrics-sep"))
|
||||
self.add(self.swap.box)
|
||||
if self.bat:
|
||||
self.add(Box(name="metrics-sep"))
|
||||
self.add(self.bat.box)
|
||||
|
||||
# Connect events directly to the button
|
||||
self.connect("enter-notify-event", self.on_mouse_enter)
|
||||
self.connect("leave-notify-event", self.on_mouse_leave)
|
||||
|
||||
GLib.timeout_add_seconds(1, self.update_metrics)
|
||||
|
||||
self.hide_timer = None
|
||||
self.hover_counter = 0
|
||||
|
||||
def _format_percentage(self, value: int) -> str:
|
||||
return f"{value}%"
|
||||
|
||||
def on_mouse_enter(self, widget, event):
|
||||
window = widget.get_window()
|
||||
if window:
|
||||
window.set_cursor(Gdk.Cursor(Gdk.CursorType.HAND2))
|
||||
|
||||
if not data.VERTICAL:
|
||||
self.hover_counter += 1
|
||||
if self.hide_timer is not None:
|
||||
GLib.source_remove(self.hide_timer)
|
||||
self.hide_timer = None
|
||||
if self.cpu:
|
||||
self.cpu.revealer.set_reveal_child(True)
|
||||
if self.ram:
|
||||
self.ram.revealer.set_reveal_child(True)
|
||||
if self.swap:
|
||||
self.swap.revealer.set_reveal_child(True)
|
||||
if self.bat:
|
||||
self.bat.revealer.set_reveal_child(True)
|
||||
return False
|
||||
|
||||
def on_mouse_leave(self, widget, event):
|
||||
window = widget.get_window()
|
||||
if window:
|
||||
window.set_cursor(None)
|
||||
|
||||
if not data.VERTICAL:
|
||||
if self.hover_counter > 0:
|
||||
self.hover_counter -= 1
|
||||
if self.hover_counter == 0:
|
||||
if self.hide_timer is not None:
|
||||
GLib.source_remove(self.hide_timer)
|
||||
self.hide_timer = GLib.timeout_add(500, self.hide_revealer)
|
||||
return False
|
||||
|
||||
def hide_revealer(self):
|
||||
if not data.VERTICAL:
|
||||
if self.cpu:
|
||||
self.cpu.revealer.set_reveal_child(False)
|
||||
if self.ram:
|
||||
self.ram.revealer.set_reveal_child(False)
|
||||
self.hide_timer = None
|
||||
return False
|
||||
|
||||
def update_metrics(self):
|
||||
cpu, mem, swap = shared_provider.get_metrics()
|
||||
if self.cpu:
|
||||
self.cpu.circle.set_value(cpu / 100.0)
|
||||
value_text = self._format_percentage(int(cpu))
|
||||
self.cpu.level.set_label(value_text)
|
||||
self.cpu.update_tooltip(value_text)
|
||||
if self.ram:
|
||||
self.ram.circle.set_value(mem / 100.0)
|
||||
value_text = self._format_percentage(int(mem))
|
||||
self.ram.level.set_label(value_text)
|
||||
self.ram.update_tooltip(value_text)
|
||||
if self.swap:
|
||||
self.swap.circle.set_value(swap / 100.0)
|
||||
value_text = self._format_percentage(int(swap))
|
||||
self.swap.level.set_label(value_text)
|
||||
self.swap.update_tooltip(value_text)
|
||||
if self.bat:
|
||||
bat_percent, _ = shared_provider.get_battery()
|
||||
self.bat.circle.set_value(bat_percent / 100.0)
|
||||
value_text = self._format_percentage(int(bat_percent))
|
||||
self.bat.level.set_label(value_text)
|
||||
self.bat.update_tooltip(value_text)
|
||||
|
||||
# Tooltip: only show enabled metrics
|
||||
tooltip_metrics = []
|
||||
if self.swap: tooltip_metrics.append(self.swap)
|
||||
if self.bat: tooltip_metrics.append(self.bat)
|
||||
if self.ram: tooltip_metrics.append(self.ram)
|
||||
if self.cpu: tooltip_metrics.append(self.cpu)
|
||||
|
||||
return True
|
||||
|
||||
def update_battery(self, sender, battery_data):
|
||||
if self.bat is None:
|
||||
return
|
||||
|
||||
value, charging = battery_data
|
||||
if value == 0:
|
||||
if self.bat:
|
||||
self.bat.box.set_visible(False)
|
||||
else:
|
||||
if self.bat:
|
||||
self.bat.box.set_visible(True)
|
||||
self.bat.circle.set_value(value / 100)
|
||||
percentage = int(value)
|
||||
self.bat.level.set_label(self._format_percentage(percentage))
|
||||
|
||||
# Apply alert styling if battery is low AND not charging
|
||||
if percentage <= 15 and charging == False:
|
||||
self.bat.icon.add_style_class("alert")
|
||||
self.bat.circle.add_style_class("alert")
|
||||
else:
|
||||
self.bat.icon.remove_style_class("alert")
|
||||
self.bat.circle.remove_style_class("alert")
|
||||
|
||||
# Choose the icon based on charging state first, then battery level
|
||||
if percentage == 100:
|
||||
self.bat.icon.set_markup(icons.battery)
|
||||
charging_status = f"{icons.bat_full} Fully Charged"
|
||||
elif charging == True:
|
||||
self.bat.icon.set_markup(icons.charging)
|
||||
charging_status = f"{icons.bat_charging} Charging"
|
||||
elif percentage <= 15 and charging == False:
|
||||
self.bat.icon.set_markup(icons.alert)
|
||||
charging_status = f"{icons.bat_low} Low Battery"
|
||||
elif charging == False:
|
||||
self.bat.icon.set_markup(icons.discharging)
|
||||
charging_status = f"{icons.bat_discharging} Discharging"
|
||||
else:
|
||||
self.bat.icon.set_markup(icons.battery)
|
||||
charging_status = "Battery"
|
||||
|
||||
# Update individual tooltip for battery
|
||||
self.bat.update_tooltip(f"{charging_status}: {self._format_percentage(percentage)}")
|
||||
|
||||
# Update the combined tooltip
|
||||
self.update_metrics()
|
||||
@@ -0,0 +1,180 @@
|
||||
from fabric.widgets.button import Button
|
||||
from fabric.widgets.box import Box
|
||||
from fabric.widgets.label import Label
|
||||
from fabric.widgets.stack import Stack
|
||||
from fabric.widgets.revealer import Revealer
|
||||
from fabric.utils import exec_shell_command_async
|
||||
from gi.repository import GLib, Gdk, Gtk
|
||||
import utils.icons as icons
|
||||
|
||||
|
||||
class Power(Button):
|
||||
TRANSITION_TYPE = "slide-up-down"
|
||||
TRANSITION_DURATION = 300 # milliseconds
|
||||
LABEL_TRANSITION_TYPE = "slide-left"
|
||||
LONG_PRESS_DURATION = 500 # milliseconds
|
||||
SCROLL_COOLDOWN = 100 # milliseconds
|
||||
|
||||
def __init__(self):
|
||||
self.current_action = "shutdown"
|
||||
self.is_locked = True
|
||||
self.long_press_timeout = None
|
||||
self.scroll_timeout = None
|
||||
self.just_unlocked = False
|
||||
|
||||
# Initialize stacks for icons and labels
|
||||
for name in ["icon_stack", "label_stack"]:
|
||||
setattr(
|
||||
self,
|
||||
name,
|
||||
Stack(
|
||||
transition_type=self.TRANSITION_TYPE,
|
||||
transition_duration=self.TRANSITION_DURATION,
|
||||
),
|
||||
)
|
||||
|
||||
# Define power actions
|
||||
self.power_actions = {
|
||||
"shutdown": {
|
||||
"icon": icons.shutdown,
|
||||
"label": "Shutdown",
|
||||
"command": "systemctl poweroff"
|
||||
},
|
||||
"reboot": {
|
||||
"icon": icons.reboot,
|
||||
"label": "Reboot",
|
||||
"command": "systemctl reboot"
|
||||
},
|
||||
"suspend": {
|
||||
"icon": icons.suspend,
|
||||
"label": "Suspend",
|
||||
"command": "systemctl suspend"
|
||||
},
|
||||
"logout": {
|
||||
"icon": icons.logout,
|
||||
"label": "Logout",
|
||||
"command": "hyprctl dispatch exit"
|
||||
},
|
||||
"lock": {
|
||||
"icon": icons.lock,
|
||||
"label": "Lock",
|
||||
"command": "hyprlock --immediate"
|
||||
}
|
||||
}
|
||||
|
||||
# Add all actions to stacks
|
||||
for action, data in self.power_actions.items():
|
||||
self.icon_stack.add_named(
|
||||
Label(
|
||||
markup=data["icon"],
|
||||
style_classes=["power-icon"],
|
||||
),
|
||||
name=action,
|
||||
)
|
||||
self.label_stack.add_named(
|
||||
Label(
|
||||
label=data["label"],
|
||||
style_classes=["power-label"]
|
||||
),
|
||||
name=action,
|
||||
)
|
||||
|
||||
self.revealer = Revealer(
|
||||
transition_type=self.LABEL_TRANSITION_TYPE,
|
||||
transition_duration=self.TRANSITION_DURATION,
|
||||
child=self.label_stack,
|
||||
)
|
||||
|
||||
super().__init__(
|
||||
name="power-menu",
|
||||
on_enter_notify_event=lambda *args: self.revealer.set_reveal_child(True),
|
||||
on_leave_notify_event=self.on_leave,
|
||||
on_pressed=self.on_pressed,
|
||||
on_released=self.on_released,
|
||||
on_scroll_event=self.on_scroll,
|
||||
child=Box(
|
||||
children=[
|
||||
self.icon_stack,
|
||||
self.revealer,
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
self.add_events(
|
||||
Gdk.EventMask.SCROLL_MASK |
|
||||
Gdk.EventMask.BUTTON_PRESS_MASK |
|
||||
Gdk.EventMask.SMOOTH_SCROLL_MASK
|
||||
)
|
||||
self.add_style_class("locked")
|
||||
|
||||
# Add cursor change on hover
|
||||
self.connect(
|
||||
"state-flags-changed",
|
||||
lambda btn, *_: (
|
||||
btn.set_cursor("pointer")
|
||||
if btn.get_state_flags() & Gtk.StateFlags.PRELIGHT # type: ignore
|
||||
else btn.set_cursor("default"),
|
||||
),
|
||||
)
|
||||
|
||||
def on_pressed(self, widget, event=None):
|
||||
if self.is_locked:
|
||||
self.long_press_timeout = GLib.timeout_add(
|
||||
self.LONG_PRESS_DURATION,
|
||||
self.on_long_press
|
||||
)
|
||||
|
||||
def on_released(self, widget, event=None):
|
||||
timeout = self.long_press_timeout
|
||||
self.long_press_timeout = None
|
||||
if timeout:
|
||||
GLib.source_remove(timeout)
|
||||
|
||||
if not self.is_locked and not self.just_unlocked:
|
||||
exec_shell_command_async(self.power_actions[self.current_action]["command"])
|
||||
self.is_locked = True
|
||||
self.add_style_class("locked")
|
||||
|
||||
self.just_unlocked = False
|
||||
|
||||
def on_long_press(self):
|
||||
if self.is_locked:
|
||||
self.is_locked = False
|
||||
self.just_unlocked = True
|
||||
self.remove_style_class("locked")
|
||||
self.long_press_timeout = None
|
||||
return False
|
||||
|
||||
def _change_action(self, direction):
|
||||
if self.is_locked:
|
||||
return
|
||||
|
||||
actions = list(self.power_actions.keys())
|
||||
current_index = actions.index(self.current_action)
|
||||
new_index = (current_index + direction) % len(actions)
|
||||
|
||||
self.current_action = actions[new_index]
|
||||
self.icon_stack.set_visible_child_name(self.current_action)
|
||||
self.label_stack.set_visible_child_name(self.current_action)
|
||||
|
||||
def on_scroll(self, widget, event):
|
||||
if self.is_locked:
|
||||
return True
|
||||
|
||||
if event.direction == Gdk.ScrollDirection.SMOOTH:
|
||||
if event.delta_y < -0.1:
|
||||
self._change_action(-1)
|
||||
elif event.delta_y > 0.1:
|
||||
self._change_action(1)
|
||||
elif event.direction == Gdk.ScrollDirection.UP:
|
||||
self._change_action(-1)
|
||||
elif event.direction == Gdk.ScrollDirection.DOWN:
|
||||
self._change_action(1)
|
||||
|
||||
self.scroll_timeout = GLib.timeout_add(self.SCROLL_COOLDOWN, lambda: None)
|
||||
return True
|
||||
def on_leave(self, widget, event=None):
|
||||
self.revealer.set_reveal_child(False)
|
||||
if not self.is_locked:
|
||||
self.is_locked = True
|
||||
self.add_style_class("locked")
|
||||
@@ -0,0 +1,214 @@
|
||||
import subprocess
|
||||
from fabric import Fabricator
|
||||
from fabric.widgets.label import Label
|
||||
from fabric.widgets.box import Box
|
||||
from fabric.bluetooth import BluetoothClient
|
||||
from services import network_client, audio
|
||||
import utils.icons as icons
|
||||
from fabric.hyprland.widgets import Language, HyprlandEvent, get_hyprland_connection
|
||||
|
||||
|
||||
class SystemIndicators(Box):
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(
|
||||
orientation="h", spacing=2, name="system-indicator", **kwargs
|
||||
)
|
||||
|
||||
self.bluetooth_icon = Label(name="system-indicator-icon")
|
||||
self.wifi_icon = Label(name="system-indicator-icon")
|
||||
self.volume_icon_button = Label(name="system-indicator-icon")
|
||||
self.language_icon = Label(name="lang-label")
|
||||
self.idle_label = Label(name="system-indicator-icon")
|
||||
self.night_label = Label(name="system-indicator-icon")
|
||||
self.power_label = Label(name="system-indicator-icon")
|
||||
|
||||
|
||||
for widget in [
|
||||
self.language_icon,
|
||||
self.bluetooth_icon,
|
||||
self.wifi_icon,
|
||||
self.volume_icon_button,
|
||||
self.idle_label,
|
||||
self.night_label,
|
||||
self.power_label,
|
||||
]:
|
||||
self.add(widget)
|
||||
|
||||
self.audio_service = audio
|
||||
self.bluetooth_client = BluetoothClient()
|
||||
self.network_client = network_client
|
||||
self.hyprland = get_hyprland_connection()
|
||||
|
||||
# Connect signals
|
||||
self.bluetooth_client.connect("changed", self.update_bluetooth_status)
|
||||
self.audio_service.connect("changed", self.update_volume_status)
|
||||
self.network_client.connect("device_ready", self.update_network_status)
|
||||
self.hyprland.connect("event::activelayout", self.update_language)
|
||||
|
||||
|
||||
if self.network_client.wifi_device:
|
||||
self.network_client.wifi_device.connect(
|
||||
"changed", self.update_network_status
|
||||
)
|
||||
|
||||
if self.network_client.ethernet_device:
|
||||
self.network_client.ethernet_device.connect(
|
||||
"changed", self.update_network_status
|
||||
)
|
||||
|
||||
Fabricator(interval=1000, poll_from=self.update_all_statuses)
|
||||
|
||||
# Initial updates
|
||||
self.update_bluetooth_status()
|
||||
self.update_language()
|
||||
self.update_idle_night_status()
|
||||
|
||||
def update_all_statuses(self, *_args):
|
||||
self.update_idle_night_status()
|
||||
self.update_power_profile()
|
||||
self.update_network_status()
|
||||
|
||||
def update_language(self, _=None, event: HyprlandEvent = None):
|
||||
"""Update the language indicator based on Hyprland events."""
|
||||
lang = event.data[1] if event else Language().get_label()
|
||||
self.language_icon.set_tooltip_text(lang)
|
||||
self.language_icon.set_label(lang[:2].lower())
|
||||
|
||||
def update_idle_night_status(self, *_):
|
||||
# Update idle status
|
||||
has_idle = (
|
||||
subprocess.run(["pgrep", "-x", "wlinhibit"], capture_output=True).returncode
|
||||
== 0
|
||||
)
|
||||
self.idle_label.set_visible(has_idle)
|
||||
self.idle_label.set_markup(icons.coffee if has_idle else "")
|
||||
|
||||
# Update night mode status
|
||||
has_night = (
|
||||
subprocess.run(
|
||||
["pgrep", "-x", "hyprsunset"], capture_output=True
|
||||
).returncode
|
||||
== 0
|
||||
)
|
||||
self.night_label.set_visible(has_night)
|
||||
self.night_label.set_markup(icons.night if has_night else "")
|
||||
|
||||
def update_volume_status(self, *_):
|
||||
stream = self.audio_service.speaker
|
||||
if not stream:
|
||||
self.volume_icon_button.set_visible(False)
|
||||
return
|
||||
|
||||
self.volume_icon_button.set_visible(True)
|
||||
# Normalize volume to integer percentage (0-100)
|
||||
volume_level = stream.volume
|
||||
if volume_level > 1:
|
||||
volume_level = min(int(volume_level), 100)
|
||||
else:
|
||||
volume_level = int(volume_level * 100)
|
||||
|
||||
is_muted = stream.muted
|
||||
|
||||
if is_muted:
|
||||
icon = icons.vol_off
|
||||
elif volume_level > 74:
|
||||
icon = icons.vol_high
|
||||
elif volume_level > 0:
|
||||
icon = icons.vol_medium
|
||||
else:
|
||||
icon = icons.vol_mute
|
||||
|
||||
self.volume_icon_button.set_markup(icon)
|
||||
|
||||
tooltip_text = "Muted" if is_muted else f"Volume: {volume_level}%"
|
||||
self.volume_icon_button.set_tooltip_text(tooltip_text)
|
||||
|
||||
|
||||
def update_bluetooth_status(self, *_):
|
||||
if self.bluetooth_client.enabled:
|
||||
self.bluetooth_icon.set_visible(True)
|
||||
# Get a list of connected devices
|
||||
connected_devices = [
|
||||
device.name
|
||||
for device in self.bluetooth_client.devices
|
||||
if device.connected
|
||||
]
|
||||
|
||||
if connected_devices:
|
||||
self.bluetooth_icon.set_markup(icons.bluetooth_connected)
|
||||
self.bluetooth_icon.set_tooltip_text(f"{', '.join(connected_devices)}")
|
||||
else:
|
||||
self.bluetooth_icon.set_markup(icons.bluetooth)
|
||||
self.bluetooth_icon.set_tooltip_text(
|
||||
"Bluetooth is enabled, no devices connected"
|
||||
)
|
||||
else:
|
||||
self.bluetooth_icon.set_visible(True) # Keep it visible
|
||||
self.bluetooth_icon.set_markup(icons.bluetooth_off)
|
||||
self.bluetooth_icon.set_tooltip_text("Bluetooth is disabled")
|
||||
|
||||
def update_network_status(self, *_):
|
||||
primary_device = self.network_client.primary_device
|
||||
self.wifi_icon.set_visible(True)
|
||||
|
||||
if primary_device == "wifi" and self.network_client.wifi_device:
|
||||
wifi_device = self.network_client.wifi_device
|
||||
|
||||
self.wifi_icon.set_tooltip_text(
|
||||
wifi_device.ssid if wifi_device.ssid else "No WiFi Connection"
|
||||
)
|
||||
|
||||
if wifi_device.enabled:
|
||||
strength = wifi_device.strength
|
||||
|
||||
if strength > 0:
|
||||
if strength < 25:
|
||||
icon_label = icons.wifi_0
|
||||
elif strength < 50:
|
||||
icon_label = icons.wifi_1
|
||||
elif strength < 75:
|
||||
icon_label = icons.wifi_2
|
||||
else:
|
||||
icon_label = icons.wifi_3
|
||||
else:
|
||||
icon_label = icons.wifi_off
|
||||
else:
|
||||
icon_label = icons.wifi_off
|
||||
|
||||
elif primary_device == "wired":
|
||||
icon_label = icons.lan
|
||||
self.wifi_icon.set_tooltip_text("Wired Connection")
|
||||
|
||||
else:
|
||||
icon_label = icons.wifi_off # No network
|
||||
|
||||
self.wifi_icon.set_markup(icon_label)
|
||||
|
||||
def update_power_profile(self, *_):
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["powerprofilesctl", "get"], capture_output=True, text=True
|
||||
)
|
||||
current_profile = result.stdout.strip()
|
||||
|
||||
profile_icons = {
|
||||
"performance": icons.power_performance,
|
||||
"balanced": "",
|
||||
"power-saver": icons.power_saving,
|
||||
}
|
||||
|
||||
icon = profile_icons.get(current_profile)
|
||||
tooltip = (
|
||||
f"Power Profile: {current_profile.capitalize()}"
|
||||
if current_profile
|
||||
else "Unknown Profile"
|
||||
)
|
||||
|
||||
self.power_label.set_visible(bool(icon)) # Only show if there's an icon
|
||||
self.power_label.set_markup(icon)
|
||||
self.power_label.set_tooltip_text(tooltip)
|
||||
|
||||
except Exception as e:
|
||||
self.power_label.set_visible(False)
|
||||
self.power_label.set_tooltip_text(f"Error fetching power profile: {e}")
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import json
|
||||
|
||||
from fabric.hyprland.service import Hyprland
|
||||
from fabric.widgets.label import Label
|
||||
|
||||
connection = Hyprland()
|
||||
|
||||
workspaceData = connection.send_command("j/activeworkspace").reply
|
||||
activeWorkspace = json.loads(workspaceData.decode("utf-8"))["name"]
|
||||
workspace = Label(label=f"{activeWorkspace}")
|
||||
|
||||
|
||||
def on_workspace(obj, signal):
|
||||
global activeWorkspace
|
||||
activeWorkspace = json.loads(signal.data[0])
|
||||
workspace.set_label(f"{activeWorkspace}")
|
||||
|
||||
|
||||
connection.connect("event::workspace", on_workspace)
|
||||
Reference in new issue
Block a user