From 0bc0ad4e6623b0ba9ae10daaf9942f0f7ac6f112 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Tue, 26 Nov 2024 03:01:41 +0545 Subject: [PATCH] huge update on launcher --- config/fabric/modules/launcher/launcher.py | 65 +++++++++++++++++-- .../modules/launcher/widgets/__init__.py | 4 +- .../modules/launcher/widgets/bluetooth.py | 36 ++++++++-- .../modules/launcher/widgets/clipboard.py | 6 +- .../fabric/modules/launcher/widgets/emoji.py | 6 +- .../widgets/{powermenu.py => power_menu.py} | 45 +++++++++++-- .../fabric/modules/launcher/widgets/shell.py | 6 +- .../modules/launcher/widgets/wallpaper.py | 37 +++++++++-- .../fabric/modules/launcher/widgets/wifi.py | 49 ++++++++++---- config/fabric/snippets/util.py | 20 +++--- 10 files changed, 219 insertions(+), 55 deletions(-) rename config/fabric/modules/launcher/widgets/{powermenu.py => power_menu.py} (59%) diff --git a/config/fabric/modules/launcher/launcher.py b/config/fabric/modules/launcher/launcher.py index 0717a687..51f717a2 100644 --- a/config/fabric/modules/launcher/launcher.py +++ b/config/fabric/modules/launcher/launcher.py @@ -22,7 +22,13 @@ from modules.launcher.widgets import ( bake_favorite_slot, handle_application_search, ) -from snippets import get_current_uptime, get_profile_picture_path, read_config, username +from snippets import ( + MaterialIcon, + get_current_uptime, + get_profile_picture_path, + read_config, + username, +) class Launcher(Window): @@ -44,6 +50,7 @@ class Launcher(Window): ) 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() @@ -58,15 +65,23 @@ class Launcher(Window): self.network_menu = WifiMenu(self) def setup_ui(self): + self.default_button = Button( + child=MaterialIcon("grid_view"), + h_align="center", + v_align="center", + ) self.scrolled_window = self.create_scrolled_window() self.scrolled_revealer = self.create_revealer() self.search_entry = self.create_search_entry() self.additional_box = self.create_additional_box() - self.search_box = Box(spacing=10, orientation="h", children=[self.search_entry]) + self.search_box = Box( + spacing=10, + orientation="h", + children=[self.search_entry, self.default_button], + ) launcher_box = Box( orientation="v", - size=(580, 0), name="launcher", children=[ self.search_box, @@ -81,11 +96,12 @@ class Launcher(Window): def create_search_entry(self): entry = Entry( - placeholder="Search.....", + placeholder="Type ':' to list subcommands", h_expand=True, notify_text=lambda entry, *_: self.debounce_search(entry.get_text()), ) entry.set_icon_from_icon_name(0, "preferences-system-search-symbolic") + entry.props.xalign = 0.1 return entry def create_scrolled_window(self): @@ -98,6 +114,7 @@ class Launcher(Window): def create_revealer(self): return Revealer( + style=" transition: min-height 10s cubic-bezier(0.42, 0, 0.58, 1);", child=self.scrolled_window, transition_duration=300, transition_type="slide-down", @@ -118,7 +135,7 @@ class Launcher(Window): ) def get_favorites_box(self): - if not hasattr(self, "fav_apps_box"): + if not hasattr(self, "fav_apps_box") or self.fav_apps_box is None: self.fav_apps_box = Box(spacing=10, orientation="h") return self.fav_apps_box @@ -134,6 +151,8 @@ class Launcher(Window): ) def load_commands(self): + if not self.commands: + return {} if not hasattr(self, "_command_map"): self._command_map = { cmd["command"]: cmd["description"] for cmd in self.commands @@ -156,6 +175,7 @@ class Launcher(Window): def arrange_viewport(self, query: str = ""): self.get_viewport().children = [] + dynamic_buttons = [] for command, handler_name in self.command_map.items(): if query.startswith(command): @@ -167,12 +187,25 @@ class Launcher(Window): if query.startswith(":"): self.get_favorites_box().set_visible(False) self.additional_box.set_visible(False) + self.update_dynamic_button([self.default_button]) self.show_help(query[1:]) self.scrolled_revealer.reveal() return + if dynamic_buttons: + self.update_dynamic_button(dynamic_buttons) + handle_application_search(self, query) + def update_dynamic_button(self, new_buttons): + if not isinstance(new_buttons, list): + new_buttons = [new_buttons] + for child in self.search_box.children[1:]: + self.search_box.remove(child) + + for new_button in new_buttons: + self.search_box.add(new_button) + def on_key_press(self, widget, event): from gi.repository import Gdk @@ -188,6 +221,9 @@ class Launcher(Window): if query.lower() in command[1:].lower() ] + if not filtered_help: + return + for command, description in filtered_help: button = Button( child=self.create_help_button_content(command, description), @@ -214,25 +250,37 @@ class Launcher(Window): def show_network(self, *_): self.network_menu.show_wifi_menu(self.viewport) + dynamic_buttons = self.network_menu.get_wifi_buttons() + self.update_dynamic_button(dynamic_buttons) self.scrolled_revealer.reveal() def show_emojis(self, query: str): self.emoji_manager.show_emojis(self.get_viewport(), query) + dynamic_buttons = self.emoji_manager.get_emoji_buttons() + self.update_dynamic_button(dynamic_buttons) self.scrolled_revealer.reveal() def show_clipboard_history(self, query: str): self.clipboard_manager.show_clipboard_history(self.get_viewport(), query) + dynamic_buttons = self.clipboard_manager.get_clipboard_buttons() + self.update_dynamic_button(dynamic_buttons) self.scrolled_revealer.reveal() def show_powermenu(self, query: str = ""): self.power_menu.show_power_menu(self.get_viewport(), query) + dynamic_buttons = self.power_menu.get_power_buttons() + self.update_dynamic_button(dynamic_buttons) self.scrolled_revealer.reveal() def show_shell_commands(self, query: str): if not query: + dynamic_buttons = self.shell_command_manager.get_shell_buttons() + self.update_dynamic_button(dynamic_buttons) self.scrolled_revealer.unreveal() return self.shell_command_manager.show_shell_commands(self.get_viewport(), query) + dynamic_buttons = self.shell_command_manager.get_shell_buttons() + self.update_dynamic_button(dynamic_buttons) self.scrolled_revealer.reveal() def show_wallpapers(self, query: str): @@ -240,11 +288,18 @@ class Launcher(Window): self.wallpaper_manager.apply_wallpaper_random() else: self.wallpaper_manager.show_wallpaper_thumbnails(self.get_viewport(), query) + dynamic_buttons = self.wallpaper_manager.get_wallpaper_buttons() + self.update_dynamic_button(dynamic_buttons) self.scrolled_revealer.reveal() def show_bluetooth_settings(self, *_): self.bluetooth_menu.show_bluetooth_menu(self.viewport) + dynamic_buttons = self.bluetooth_menu.get_bluetooth_buttons() + self.update_dynamic_button(dynamic_buttons) self.scrolled_revealer.reveal() def toggle(self): self.set_visible(not self.is_visible()) + if self.is_visible(): + self.search_entry.set_text("") + self.search_entry.grab_focus() diff --git a/config/fabric/modules/launcher/widgets/__init__.py b/config/fabric/modules/launcher/widgets/__init__.py index 22b583f0..175cdd92 100644 --- a/config/fabric/modules/launcher/widgets/__init__.py +++ b/config/fabric/modules/launcher/widgets/__init__.py @@ -2,7 +2,7 @@ from .app import bake_favorite_slot, handle_application_search from .bluetooth import BluetoothMenu from .clipboard import ClipboardManager from .emoji import EmojiManager -from .powermenu import PowerMenu +from .power_menu import PowerMenu from .shell import ShellCommandManager from .wallpaper import WallpaperManager from .wifi import WifiMenu @@ -15,6 +15,6 @@ __all__ = [ "ShellCommandManager", "WallpaperManager", "WifiMenu", - "PowerMenu", "BluetoothMenu", + "PowerMenu", ] diff --git a/config/fabric/modules/launcher/widgets/bluetooth.py b/config/fabric/modules/launcher/widgets/bluetooth.py index e6190afd..8eb8be9a 100644 --- a/config/fabric/modules/launcher/widgets/bluetooth.py +++ b/config/fabric/modules/launcher/widgets/bluetooth.py @@ -13,6 +13,36 @@ class BluetoothMenu: self.paired_devices = {} self.available_devices = {} + self.bt_button = Button( + child=MaterialIcon("bluetooth"), + h_align="center", + v_align="center", + on_clicked=self.bt_toggle, + ) + self.update_bt_button_style() + + self.scan_button = Button( + child=MaterialIcon("refresh"), + h_align="center", + v_align="center", + on_clicked=lambda *_: self.client.toggle_scan(), + style=" min-height:50px; min-width:50px;", + ) + + def bt_toggle(self, *args): + self.update_bt_button_style() + self.client.toggle_power() + + def update_bt_button_style(self): + if self.client.enabled: + self.bt_button.set_style( + "background-color: transparent; min-height: 50px; min-width: 50px;" + ) + else: + self.bt_button.set_style( + "background-color: @surfaceVariant; min-height: 50px; min-width: 50px;" + ) + def show_bluetooth_menu(self, viewport): viewport.children = [] @@ -85,7 +115,5 @@ class BluetoothMenu: self.show_bluetooth_menu(self.launcher.viewport) - def icon_button(self): - return Button( - child=MaterialIcon("bluetooth"), - ) + def get_bluetooth_buttons(self): + return [self.bt_button, self.scan_button] diff --git a/config/fabric/modules/launcher/widgets/clipboard.py b/config/fabric/modules/launcher/widgets/clipboard.py index dc43da6d..5fc691cf 100644 --- a/config/fabric/modules/launcher/widgets/clipboard.py +++ b/config/fabric/modules/launcher/widgets/clipboard.py @@ -133,7 +133,5 @@ class ClipboardManager: self.copy_by_id(clip_id) self.launcher.set_visible(False) - def icon_button(self): - return Button( - child=MaterialIcon("content_paste"), - ) + def get_clipboard_buttons(self): + return MaterialIcon("content_paste") diff --git a/config/fabric/modules/launcher/widgets/emoji.py b/config/fabric/modules/launcher/widgets/emoji.py index c8208731..4b907152 100644 --- a/config/fabric/modules/launcher/widgets/emoji.py +++ b/config/fabric/modules/launcher/widgets/emoji.py @@ -88,7 +88,5 @@ class EmojiManager: name="emoji-item", ) - def icon_button(self): - return Button( - child=MaterialIcon("mood"), - ) + def get_emoji_buttons(self): + return MaterialIcon("mood") diff --git a/config/fabric/modules/launcher/widgets/powermenu.py b/config/fabric/modules/launcher/widgets/power_menu.py similarity index 59% rename from config/fabric/modules/launcher/widgets/powermenu.py rename to config/fabric/modules/launcher/widgets/power_menu.py index b9d2a94a..a07af40a 100644 --- a/config/fabric/modules/launcher/widgets/powermenu.py +++ b/config/fabric/modules/launcher/widgets/power_menu.py @@ -1,13 +1,50 @@ -from fabric.utils import exec_shell_command +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 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( + "background-color: transparent;min-width:50px; min-height:50px;" + ) + + def toggle_idle_inhibitor(self, *args): + 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( + "background-color: @surfaceVariant; min-width:50px; min-height:50px; border-radius:100px;" + ) + else: + exec_shell_command("pkill -f wayland-idle-inhibitor.py") + self.idle_button.set_style( + "background-color: transparent;min-width:50px; min-height:50px;" + ) + + def check_idle_state(self): + return "wayland-idle-inhibitor.py" in exec_shell_command( + "pidof wayland-idle-inhibitor.py" + ) def show_power_menu(self, viewport, query: str = ""): viewport.children = [] @@ -76,7 +113,5 @@ class PowerMenu: def lock(self): exec_shell_command("hyprlock --immediate") - def icon_button(self): - return Button( - child=MaterialIcon("power_settings_new"), - ) + def get_power_buttons(self): + return self.idle_button diff --git a/config/fabric/modules/launcher/widgets/shell.py b/config/fabric/modules/launcher/widgets/shell.py index 87d7b2ad..9609de2e 100644 --- a/config/fabric/modules/launcher/widgets/shell.py +++ b/config/fabric/modules/launcher/widgets/shell.py @@ -77,7 +77,5 @@ class ShellCommandManager: finally: self.launcher.set_visible(False) - def icon_button(self): - return Button( - child=MaterialIcon("terminal"), - ) + def get_shell_buttons(self): + return MaterialIcon("terminal") diff --git a/config/fabric/modules/launcher/widgets/wallpaper.py b/config/fabric/modules/launcher/widgets/wallpaper.py index 672b0c9b..ac4302a2 100644 --- a/config/fabric/modules/launcher/widgets/wallpaper.py +++ b/config/fabric/modules/launcher/widgets/wallpaper.py @@ -1,7 +1,7 @@ import os from pathlib import Path -from fabric.utils import exec_shell_command_async +from fabric.utils import exec_shell_command, exec_shell_command_async from fabric.widgets.box import Box from fabric.widgets.button import Button from loguru import logger @@ -20,6 +20,35 @@ class WallpaperManager: Path.home() / ".cache" / "fabric" / "thumbnails" / "wallpaper" ) self.thumbnail_dir.mkdir(parents=True, exist_ok=True) + self.theme_toggle_button = Button( + child=MaterialIcon("contrast"), + v_align="center", + on_clicked=self.toggle_dark_mode, + ) + self._update_button_style() + + def toggle_dark_mode(self, *_): + command = os.path.expanduser("~/fabric/assets/scripts/dark-theme.sh --toggle") + exec_shell_command(command) + self._update_button_style() + + def check_dark_mode_state(self): + result = exec_shell_command( + "gsettings get org.gnome.desktop.interface color-scheme" + ) + current_mode = result.strip().replace("'", "") + return current_mode == "prefer-dark" + + def _update_button_style(self): + """Update the style of the theme toggle button based on current theme""" + if self.check_dark_mode_state(): # Dark theme + self.theme_toggle_button.set_style( + "background-color: @surfaceVariant; border-radius:100px; min-height:50px; min-width:50px;" + ) + else: # Light theme (white) + self.theme_toggle_button.set_style( + "background-color: transparent; border-radius:100px; min-height:50px; min-width:50px;" + ) def show_wallpaper_thumbnails(self, viewport, search_term: str = ""): wallpapers = self._get_wallpapers(search_term) @@ -117,7 +146,5 @@ class WallpaperManager: except Exception: pass - def icon_button(self): - return Button( - child=MaterialIcon("image"), - ) + def get_wallpaper_buttons(self): + return self.theme_toggle_button diff --git a/config/fabric/modules/launcher/widgets/wifi.py b/config/fabric/modules/launcher/widgets/wifi.py index 27b4272a..c985a56e 100644 --- a/config/fabric/modules/launcher/widgets/wifi.py +++ b/config/fabric/modules/launcher/widgets/wifi.py @@ -13,21 +13,42 @@ class WifiMenu: self.paired_devices = {} self.available_devices = {} - # Connect the event when a new device is added + self.wifi_toggle_button = Button( + child=MaterialIcon("wifi"), + h_align="center", + v_align="center", + on_clicked=self.toggle_wifi, + ) + self.update_wifi_button_style() + + self.rescan_button = Button( + child=MaterialIcon("refresh"), + h_align="center", + v_align="center", + on_clicked=self.scan_for_networks, + style=" min-height:50px; min-width:50px;", + ) + self.client.device_ready.connect(self.on_device_added) + def update_wifi_button_style(self): + if self.check_wifi_state(): + self.wifi_toggle_button.set_style( + "background-color: transparent; min-height:50px; min-width:50px;" + ) + else: + self.wifi_toggle_button.set_style( + "background-color: @surfaceVariant; min-height:50px; min-width:50px;" + ) + + def check_wifi_state(self): + return self.client.wifi_device.enabled if self.client.wifi_device else False + def show_wifi_menu(self, viewport): viewport.children = [] devices_box = Box(orientation="v", spacing=5) - scan_button = Button( - child=MaterialIcon("sync"), - on_clicked=self.scan_for_networks, - name="sh-item", - ) - devices_box.add(scan_button) - # Iterate over the list of available access points for ap in self.client.wifi_device.access_points: is_connected = ap["ssid"] == self.client.wifi_device.ssid # Compare SSID @@ -71,7 +92,11 @@ class WifiMenu: self.client.wifi_device.scan() self.show_wifi_menu(self.launcher.viewport) - def icon_button(self): - return Button( - child=MaterialIcon("sync"), - ) + def toggle_wifi(self, button): + self.update_wifi_button_style() + if self.client.wifi_device: + self.client.wifi_device.toggle_wifi() + self.show_wifi_menu(self.launcher.viewport) + + def get_wifi_buttons(self): + return [self.wifi_toggle_button, self.rescan_button] diff --git a/config/fabric/snippets/util.py b/config/fabric/snippets/util.py index d3dbe783..f9606e31 100644 --- a/config/fabric/snippets/util.py +++ b/config/fabric/snippets/util.py @@ -17,6 +17,16 @@ def read_config(): return data +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 get_profile_picture_path() -> str | None: path = os.path.expanduser("~/Pictures/Other/face.jpg") if not os.path.exists(path): @@ -29,15 +39,5 @@ 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()