refactor: extract reusable utils, remove screencapture facade
- Add ManagedTimer to replace repeated source_remove/timeout_add boilerplate - Add EvdevLockService base class for numlock/capslock (cut 154→28 lines) - Add BaseNotchIndicator for 5 near-identical indicator classes (cut 404→171 lines) - Add load_pixbuf_thumbnail and resolve_monitor to utils - Delete screencapture.py facade; callers use Screenshot/ScreenRecorder directly
This commit is contained in:
16 files changed
+401
-612
No files matched your search
+12
-72
@@ -1,77 +1,17 @@
|
||||
from pathlib import Path
|
||||
|
||||
from fabric.core.service import Property, Service, Signal
|
||||
from fabric.utils import logger
|
||||
|
||||
from utils.gtk_utils import EvdevLEDMonitor
|
||||
|
||||
# Discover CapsLock LED device
|
||||
capslock_leds = list(Path("/sys/class/leds").glob("input*::capslock"))
|
||||
capslock_device = capslock_leds[0] if capslock_leds else None
|
||||
from utils.evdev_lock import EvdevLockService
|
||||
|
||||
|
||||
class CapsLock(Service):
|
||||
instance = None
|
||||
class CapsLock(EvdevLockService):
|
||||
"""Service for monitoring CapsLock LED state via evdev."""
|
||||
|
||||
_LED_GLOB = "input*::capslock"
|
||||
_LED_INDEX = 1 # LED_CAPSL
|
||||
_SERVICE_NAME = "CapsLock"
|
||||
|
||||
_instance = None
|
||||
|
||||
@staticmethod
|
||||
def get_initial():
|
||||
if CapsLock.instance is None:
|
||||
CapsLock.instance = CapsLock()
|
||||
|
||||
return CapsLock.instance
|
||||
|
||||
@Signal
|
||||
def state_changed(self, is_on: bool) -> None:
|
||||
"""Signal emitted when CapsLock state changes."""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
|
||||
self.capslock_led_path = capslock_device
|
||||
self._last_state = None
|
||||
self._monitoring_started = False
|
||||
|
||||
if capslock_device is None:
|
||||
logger.warning("CapsLock device not found, CapsLock service disabled")
|
||||
return
|
||||
|
||||
# LED_CAPSL = 1
|
||||
self._monitor = EvdevLEDMonitor(
|
||||
self.capslock_led_path, 1, self._on_state_changed
|
||||
)
|
||||
|
||||
def _on_state_changed(self, is_on: bool):
|
||||
if is_on != self._last_state:
|
||||
self._last_state = is_on
|
||||
self.emit("state_changed", is_on)
|
||||
|
||||
def stop(self):
|
||||
if hasattr(self, "_monitor"):
|
||||
self._monitor.stop()
|
||||
|
||||
def _ensure_monitoring_started(self):
|
||||
if not self._monitoring_started and self.capslock_led_path:
|
||||
self._monitoring_started = True
|
||||
# Set initial state before starting monitoring
|
||||
self._last_state = self._get_current_state()
|
||||
self._monitor._last_state = self._last_state
|
||||
self._monitor.start()
|
||||
|
||||
def _get_current_state(self) -> bool:
|
||||
if not self.capslock_led_path:
|
||||
return False
|
||||
brightness_path = self.capslock_led_path / "brightness"
|
||||
if brightness_path.exists():
|
||||
try:
|
||||
return bool(int(brightness_path.read_text().strip()))
|
||||
except (ValueError, OSError) as e:
|
||||
logger.warning(
|
||||
f"[capslock] return bool(int(brightness_path.read_text().strip())) failed: {e}"
|
||||
)
|
||||
return False
|
||||
return False
|
||||
|
||||
@Property(bool, "read-write", default_value=False)
|
||||
def is_on(self) -> bool:
|
||||
self._ensure_monitoring_started()
|
||||
return self._get_current_state()
|
||||
if CapsLock._instance is None:
|
||||
CapsLock._instance = CapsLock()
|
||||
return CapsLock._instance
|
||||
Reference in new issue
Block a user