From a95553a49b345ea818f9744fdfedde5791217870 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Sat, 23 Nov 2024 12:36:53 +0545 Subject: [PATCH] updates --- config/fabric/assets/config.json | 39 +++- config/fabric/modules/launcher/launcher.py | 198 +++++++++------------ config/fabric/snippets/__init__.py | 3 +- config/fabric/snippets/util.py | 12 ++ 4 files changed, 140 insertions(+), 112 deletions(-) diff --git a/config/fabric/assets/config.json b/config/fabric/assets/config.json index b3c6e031..96184ca7 100644 --- a/config/fabric/assets/config.json +++ b/config/fabric/assets/config.json @@ -1,4 +1,41 @@ { "favorite_apps": ["Firefox", "materialgram", "Vesktop", "Spotify", "kitty"], - "city": "Kohalpur" + "city": "Kohalpur", + "commands": [ + { + "command": ":em", + "description": "Copy an emoji", + "handler": "show_emojis" + }, + { + "command": ":sh", + "description": "Run executables from PATH", + "handler": "show_shell_commands" + }, + { + "command": ":wl", + "description": "Set the wallpaper", + "handler": "show_wallpapers" + }, + { + "command": ":nw", + "description": "List of available wifi networks", + "handler": "show_network" + }, + { + "command": ":bt", + "description": "Bluetooth", + "handler": "show_bluetooth_settings" + }, + { + "command": ":ch", + "description": "Copy a clipboard history entry", + "handler": "show_clipboard_history" + }, + { + "command": ":p", + "description": "Shutdown and go sleepy time", + "handler": "show_powermenu" + } + ] } diff --git a/config/fabric/modules/launcher/launcher.py b/config/fabric/modules/launcher/launcher.py index 0249205b..0717a687 100644 --- a/config/fabric/modules/launcher/launcher.py +++ b/config/fabric/modules/launcher/launcher.py @@ -1,6 +1,3 @@ -import time - -import psutil from fabric.utils import ( get_desktop_applications, idle_add, @@ -25,12 +22,12 @@ from modules.launcher.widgets import ( bake_favorite_slot, handle_application_search, ) -from snippets import get_profile_picture_path, read_config, username +from snippets import get_current_uptime, get_profile_picture_path, read_config, username class Launcher(Window): def __init__(self, **kwargs): - self.FAVORITE_APPS = self.load_favorite_apps() + self.config = read_config() super().__init__( layer="top", anchor="center", @@ -43,16 +40,14 @@ class Launcher(Window): self.all_apps = get_desktop_applications() self.initialize_managers() self.uptime_label = Label( - label=self.get_current_uptime(), style="margin-left: 60px;" + label=get_current_uptime(), style="margin-left: 60px;" ) + self.commands = self.config.get("commands", []) + self.command_map = self.build_command_map() self.setup_ui() self.connect("key-press-event", self.on_key_press) self.update_uptime_label() - def load_favorite_apps(self): - config = read_config() - return config.get("favorite_apps", []) - def initialize_managers(self): self.clipboard_manager = ClipboardManager(self, self.get_viewport()) self.emoji_manager = EmojiManager(self) @@ -84,35 +79,6 @@ class Launcher(Window): self.show_favorites() self.show_all() - def create_additional_box(self): - return Box( - orientation="h", - spacing=10, - children=[ - Box( - name="profile-pic", - style=f"background-image: url(\"file://{get_profile_picture_path() or ''}\")", - ), - Label(label=username()), - self.uptime_label, - ], - ) - - def get_current_uptime(self): - uptime = time.time() - psutil.boot_time() - uptime_hours, remainder = divmod(uptime, 3600) - uptime_minutes, _ = divmod(remainder, 60) - return ( - f"Up: {int(uptime_hours)} {'hours' if uptime_hours != 1 else 'hour'}, " - f"{int(uptime_minutes)} {'minutes' if uptime_minutes != 1 else 'minute'}" - ) - - def update_uptime_label(self): - self.uptime_label.set_label(self.get_current_uptime()) - invoke_repeater( - 60 * 1000, lambda: self.uptime_label.set_label(self.get_current_uptime()) - ) - def create_search_entry(self): entry = Entry( placeholder="Search.....", @@ -122,11 +88,6 @@ class Launcher(Window): entry.set_icon_from_icon_name(0, "preferences-system-search-symbolic") return entry - def get_favorites_box(self): - if not hasattr(self, "fav_apps_box"): - self.fav_apps_box = Box(spacing=10, orientation="h") - return self.fav_apps_box - def create_scrolled_window(self): return ScrolledWindow( name="launcher-scroll", @@ -142,47 +103,66 @@ class Launcher(Window): transition_type="slide-down", ) + def create_additional_box(self): + return Box( + orientation="h", + spacing=10, + children=[ + Box( + name="profile-pic", + style=f"background-image: url(\"file://{get_profile_picture_path() or ''}\")", + ), + Label(label=username()), + self.uptime_label, + ], + ) + + def get_favorites_box(self): + if not hasattr(self, "fav_apps_box"): + self.fav_apps_box = Box(spacing=10, orientation="h") + return self.fav_apps_box + def get_viewport(self): if not hasattr(self, "viewport"): self.viewport = Box(spacing=10, orientation="v") return self.viewport - def on_key_press(self, widget, event): - from gi.repository import Gdk + def update_uptime_label(self): + self.uptime_label.set_label(get_current_uptime()) + invoke_repeater( + 60 * 1000, lambda: self.uptime_label.set_label(get_current_uptime()) + ) - if self.is_visible() and event.keyval == Gdk.KEY_Escape: - self.set_visible(False) - return True - return False + def load_commands(self): + if not hasattr(self, "_command_map"): + self._command_map = { + cmd["command"]: cmd["description"] for cmd in self.commands + } + return self._command_map + + def build_command_map(self): + return {cmd["command"]: cmd["handler"] for cmd in self.commands} + + def show_favorites(self): + for app_name in self.config.get("favorite_apps", []): + app = next((a for a in self.all_apps if a.name == app_name), None) + if app: + self.get_favorites_box().add(bake_favorite_slot(self, app)) def debounce_search(self, query: str): if hasattr(self, "_arranger_handler"): remove_handler(self._arranger_handler) self._arranger_handler = idle_add(self.arrange_viewport, query, pin=True) - def show_favorites(self): - for app_name in self.FAVORITE_APPS: - app = next((a for a in self.all_apps if a.name == app_name), None) - if app: - self.get_favorites_box().add(bake_favorite_slot(self, app)) - def arrange_viewport(self, query: str = ""): self.get_viewport().children = [] - command_handlers = { - ":em": self.show_emojis, - ":ch": self.show_clipboard_history, - ":sh": self.show_shell_commands, - ":wl": self.show_wallpapers, - ":p": self.show_powermenu, - ":bt": self.show_bluetooth_settings, - ":nw": self.show_network, - } - - for command, handler in command_handlers.items(): + for command, handler_name in self.command_map.items(): if query.startswith(command): - handler(query[len(command) :].strip()) - return + handler = getattr(self, handler_name, None) + if handler: + handler(query[len(command) :].strip()) + return if query.startswith(":"): self.get_favorites_box().set_visible(False) @@ -193,6 +173,45 @@ class Launcher(Window): handle_application_search(self, query) + def on_key_press(self, widget, event): + from gi.repository import Gdk + + if self.is_visible() and event.keyval == Gdk.KEY_Escape: + self.set_visible(False) + return True + return False + + def show_help(self, query: str): + filtered_help = [ + (command, description) + for command, description in self.load_commands().items() + if query.lower() in command[1:].lower() + ] + + for command, description in filtered_help: + button = Button( + child=self.create_help_button_content(command, description), + name="help-item", + ) + self.get_viewport().add(button) + + def create_help_button_content(self, command, description): + return Box( + orientation="h", + spacing=10, + children=[ + Label( + label=command, h_expand=True, name="command-label", h_align="start" + ), + Label( + label=description, + h_expand=True, + name="description-label", + h_align="end", + ), + ], + ) + def show_network(self, *_): self.network_menu.show_wifi_menu(self.viewport) self.scrolled_revealer.reveal() @@ -227,46 +246,5 @@ class Launcher(Window): self.bluetooth_menu.show_bluetooth_menu(self.viewport) self.scrolled_revealer.reveal() - def show_help(self, query: str): - help_commands = [ - (":bt", "Bluetooth"), - (":ch", "Copy a clipboard history entry"), - (":em", "Copy an emoji"), - (":p", "Shutdown and go sleepy time"), - (":sh", "Run executables from PATH"), - (":wl", "Set the wallpaper"), - (":nw", "List of available wifi networks"), - ] - - filtered_help = [ - (command, description) - for command, description in help_commands - if query.lower() in command[1:].lower() - ] - - for command, description in filtered_help: - button = Button( - child=self.create_help_button_content(command, description), - name="help-item", - ) - self.get_viewport().add(button) - - def create_help_button_content(self, command, description): - return Box( - orientation="h", - spacing=10, - children=[ - Label( - label=command, h_expand=True, name="command-label", h_align="start" - ), - Label( - label=description, - h_expand=True, - name="description-label", - h_align="end", - ), - ], - ) - def toggle(self): self.set_visible(not self.is_visible()) diff --git a/config/fabric/snippets/__init__.py b/config/fabric/snippets/__init__.py index 71cc21d8..2451c7ee 100644 --- a/config/fabric/snippets/__init__.py +++ b/config/fabric/snippets/__init__.py @@ -1,7 +1,7 @@ from .animator import Animator from .icon import MaterialIcon from .icon_resolver import IconResolver -from .util import get_profile_picture_path, read_config, username +from .util import get_current_uptime, get_profile_picture_path, read_config, username __all__ = [ "Animator", @@ -10,4 +10,5 @@ __all__ = [ "read_config", "get_profile_picture_path", "username", + "get_current_uptime", ] diff --git a/config/fabric/snippets/util.py b/config/fabric/snippets/util.py index db42ed08..d3dbe783 100644 --- a/config/fabric/snippets/util.py +++ b/config/fabric/snippets/util.py @@ -1,6 +1,8 @@ import json import os +import time +import psutil from gi.repository import GLib from loguru import logger @@ -27,5 +29,15 @@ def get_profile_picture_path() -> str | None: return path +def get_current_uptime(): + uptime = time.time() - psutil.boot_time() + uptime_hours, remainder = divmod(uptime, 3600) + uptime_minutes, _ = divmod(remainder, 60) + return ( + f"Up: {int(uptime_hours)} {'hours' if uptime_hours != 1 else 'hour'}, " + f"{int(uptime_minutes)} {'minutes' if uptime_minutes != 1 else 'minute'}" + ) + + def username(): return os.getlogin().title()