Files
hyprland/config/fabric/modules/launcher/widgets/power_menu.py
T
2024-12-01 13:19:20 +05:45

103 lines
3.4 KiB
Python

import os
from fabric.utils import exec_shell_command, exec_shell_command_async
from fabric.widgets.box import Box
from fabric.widgets.button import Button
from fabric.widgets.label import Label
from loguru import logger
from snippets import MaterialIcon
IDLE_BUTTON_ACTIVE_STYLE = "background-color: @surfaceVariant; min-width:50px; min-height:50px; border-radius:100px;"
IDLE_BUTTON_INACTIVE_STYLE = "background-color: transparent;"
class PowerMenu:
def __init__(self, launcher):
self.launcher = launcher
self.idle_inhibitor = False
self.idle_button = Button(
child=MaterialIcon("schedule"),
v_align="center",
on_clicked=self.toggle_idle_inhibitor,
)
self.idle_button.set_style(IDLE_BUTTON_INACTIVE_STYLE)
def toggle_idle_inhibitor(self, *_):
self.idle_inhibitor = not self.idle_inhibitor
script_path = os.path.expanduser(
"~/fabric/assets/scripts/wayland-idle-inhibitor.py"
)
if self.idle_inhibitor and not self.check_idle_state():
exec_shell_command_async(
["python3", script_path],
lambda output: logger.info(f"Idle inhibitor output: {output}"),
)
self.idle_button.set_style(IDLE_BUTTON_ACTIVE_STYLE)
else:
exec_shell_command("pkill -f wayland-idle-inhibitor.py")
self.idle_button.set_style(IDLE_BUTTON_INACTIVE_STYLE)
def check_idle_state(self):
pid_check = exec_shell_command("pidof wayland-idle-inhibitor.py")
return "wayland-idle-inhibitor.py" in pid_check
def show_power_menu(self, viewport, query: str = ""):
viewport.children = []
power_options = [
{
"label": "Shutdown",
"icon": "power_settings_new",
"action": self.shutdown,
},
{"label": "Logout", "icon": "logout", "action": self.logout},
{"label": "Reboot", "icon": "restart_alt", "action": self.reboot},
{"label": "Suspend", "icon": "sleep", "action": self.suspend},
{"label": "Lock", "icon": "lock", "action": self.lock},
]
# Filter power options based on the query using a generator
filtered_options = (
(
option
for option in power_options
if query.lower() in option["label"].lower()
)
if query
else power_options
)
for option in filtered_options:
button = Button(
child=Box(
orientation="h",
spacing=10,
children=[
MaterialIcon(option["icon"], size="28px"),
Label(label=option["label"], h_align="start"),
],
),
on_clicked=lambda _, action=option["action"]: action(),
name="launcher-item",
)
viewport.add(button)
def shutdown(self):
exec_shell_command("systemctl poweroff")
def reboot(self):
exec_shell_command("systemctl reboot")
def suspend(self):
exec_shell_command("systemctl suspend")
def logout(self):
exec_shell_command("hyprctl dispatch logout")
def lock(self):
exec_shell_command("hyprlock --immediate")
def get_power_buttons(self):
return self.idle_button