refactor! : drop subprocess usage and centralize brightness service initialization
This commit is contained in:
21 files changed
+834
-828
No files matched your search
@@ -0,0 +1,163 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from fabric.utils import GLib, logger, random
|
||||
from fabric.widgets.box import Box
|
||||
from fabric.widgets.eventbox import EventBox
|
||||
from fabric.widgets.revealer import Revealer
|
||||
from fabric.widgets.wayland import WaylandWindow as Window
|
||||
|
||||
from services.config import config, on_config_change
|
||||
from utils.occlusion import check_occlusion
|
||||
|
||||
from .canvas import DockCanvas
|
||||
|
||||
|
||||
class Dock(Window):
|
||||
def __init__(self):
|
||||
super().__init__(
|
||||
layer="top",
|
||||
anchor="bottom center",
|
||||
exclusivity="none",
|
||||
title="modus-dock",
|
||||
name="dock",
|
||||
)
|
||||
|
||||
screen = self.get_screen()
|
||||
if screen:
|
||||
visual = screen.get_rgba_visual()
|
||||
if visual:
|
||||
self.set_visual(visual)
|
||||
self.set_app_paintable(True)
|
||||
|
||||
self.canvas = DockCanvas(self)
|
||||
|
||||
canvas_box = Box(
|
||||
name="dock-canvas-box",
|
||||
children=[self.canvas],
|
||||
)
|
||||
|
||||
self.revealer = Revealer(
|
||||
child=canvas_box,
|
||||
transition_duration=200,
|
||||
transition_type="slide-up",
|
||||
)
|
||||
|
||||
hover_strip = Box(
|
||||
name="dock-hover-strip",
|
||||
)
|
||||
|
||||
outer_box = Box(
|
||||
name="dock-outer-box",
|
||||
orientation="v",
|
||||
children=[self.revealer, hover_strip],
|
||||
)
|
||||
|
||||
self.children = EventBox(
|
||||
events=["enter-notify", "leave-notify"],
|
||||
child=outer_box,
|
||||
on_enter_notify_event=lambda *_: self.on_hover_enter(),
|
||||
on_leave_notify_event=lambda *_: self.on_hover_leave(),
|
||||
)
|
||||
|
||||
self.dock_height = 100
|
||||
self.is_hovered = False
|
||||
self.hide_ticket = 0
|
||||
self._startup_grace = True
|
||||
|
||||
self.revealer.set_reveal_child(True)
|
||||
self.show_all()
|
||||
|
||||
on_config_change(self._on_config_change)
|
||||
|
||||
if config().get("dock_auto_hide", True):
|
||||
GLib.timeout_add(2000, self._start_occlusion_monitoring)
|
||||
else:
|
||||
self._occlusion_timer_id = None
|
||||
|
||||
def _start_occlusion_monitoring(self) -> bool:
|
||||
self._startup_grace = False
|
||||
self.setup_occlusion_monitoring()
|
||||
return False
|
||||
|
||||
def _on_config_change(self, new_config, old_config) -> None:
|
||||
if config().has_changed("dock_enabled", old_config):
|
||||
self._update_visibility()
|
||||
|
||||
if config().has_changed("dock_icon_size", old_config):
|
||||
self.canvas.update_icon_size()
|
||||
|
||||
if config().has_changed("dock_auto_hide", old_config):
|
||||
if new_config.get("dock_auto_hide", True):
|
||||
self.setup_occlusion_monitoring()
|
||||
else:
|
||||
if hasattr(self, "_occlusion_timer_id") and self._occlusion_timer_id:
|
||||
GLib.source_remove(self._occlusion_timer_id)
|
||||
self._occlusion_timer_id = None
|
||||
self.revealer.set_reveal_child(True)
|
||||
|
||||
if config().has_changed("dock_hide_special_workspace_apps", old_config):
|
||||
self.canvas._rebuild_model()
|
||||
|
||||
def _update_visibility(self) -> None:
|
||||
if config().get("dock_enabled", True):
|
||||
self.show()
|
||||
self.revealer.set_reveal_child(True)
|
||||
else:
|
||||
self.hide()
|
||||
|
||||
def on_hover_enter(self) -> None:
|
||||
self.is_hovered = True
|
||||
self.hide_ticket = random.getrandbits(32)
|
||||
self.revealer.set_reveal_child(True)
|
||||
|
||||
def on_hover_leave(self) -> None:
|
||||
self.is_hovered = False
|
||||
ticket = random.getrandbits(32)
|
||||
self.hide_ticket = ticket
|
||||
|
||||
def delayed_hide(t):
|
||||
if t == self.hide_ticket and not self.is_hovered:
|
||||
if config().get("dock_auto_hide", True):
|
||||
is_occ = config().get(
|
||||
"dock_always_occluded", False
|
||||
) or check_occlusion(("bottom", self.dock_height))
|
||||
if is_occ:
|
||||
self.revealer.set_reveal_child(False)
|
||||
return False
|
||||
|
||||
GLib.timeout_add(500, delayed_hide, ticket)
|
||||
|
||||
def setup_occlusion_monitoring(self) -> None:
|
||||
if hasattr(self, "_occlusion_timer_id") and self._occlusion_timer_id:
|
||||
GLib.source_remove(self._occlusion_timer_id)
|
||||
self._occlusion_timer_id = None
|
||||
|
||||
def check_dock_occlusion() -> bool:
|
||||
try:
|
||||
if config().get("dock_always_occluded", False):
|
||||
is_occ = True
|
||||
else:
|
||||
is_occ = check_occlusion(("bottom", self.dock_height))
|
||||
|
||||
if is_occ and not self.is_hovered and self.revealer.get_reveal_child():
|
||||
self.revealer.set_reveal_child(False)
|
||||
elif not is_occ and not self.revealer.get_reveal_child():
|
||||
self.revealer.set_reveal_child(True)
|
||||
elif is_occ and self.is_hovered:
|
||||
if not self.revealer.get_reveal_child():
|
||||
self.revealer.set_reveal_child(True)
|
||||
except Exception as e:
|
||||
logger.error(f"[Dock] Occlusion check error: {e}")
|
||||
return True
|
||||
|
||||
self._occlusion_timer_id = GLib.timeout_add(300, check_dock_occlusion)
|
||||
|
||||
def destroy(self) -> None:
|
||||
if hasattr(self, "_occlusion_timer_id") and self._occlusion_timer_id:
|
||||
GLib.source_remove(self._occlusion_timer_id)
|
||||
self._occlusion_timer_id = None
|
||||
|
||||
if hasattr(self, "canvas") and self.canvas:
|
||||
self.canvas.destroy()
|
||||
|
||||
super().destroy()
|
||||
Reference in new issue
Block a user