✨ feat(menubar): improve active window detection and About dialog handling

This commit is contained in:
sankalp committed 2025-08-11 02:52:45 +05:45
1 parent 9aa9ab6e90
commit 1e456b7fd0
1 file changed
+78 -25
+78 -25
View File
@@ -1,6 +1,6 @@
import json
import os import os
import subprocess import subprocess
import json
from fabric.hyprland.widgets import HyprlandActiveWindow as ActiveWindow from fabric.hyprland.widgets import HyprlandActiveWindow as ActiveWindow
from fabric.utils import FormattedString from fabric.utils import FormattedString
@@ -8,6 +8,7 @@ from fabric.widgets.box import Box
from fabric.widgets.button import Button from fabric.widgets.button import Button
from fabric.widgets.centerbox import CenterBox from fabric.widgets.centerbox import CenterBox
from fabric.widgets.label import Label from fabric.widgets.label import Label
from modules.about import About, AboutApp from modules.about import About, AboutApp
from utils.roam import modus_service from utils.roam import modus_service
from widgets.dropdown import ModusDropdown, dropdown_divider from widgets.dropdown import ModusDropdown, dropdown_divider
@@ -73,12 +74,20 @@ class AppName:
# Try to get the proper app name from desktop file only if wmclass is not empty # Try to get the proper app name from desktop file only if wmclass is not empty
if wmclass: if wmclass:
name = self.get_app_name(wmclass=wmclass) desktop_name = self.get_app_name(wmclass=wmclass)
# Only use desktop name if it's different from wmclass (meaning we found a proper name)
if desktop_name and desktop_name != wmclass:
name = desktop_name
# Smart title formatting (capitalize first letter) # Handle empty or None names (but not when we already set it to "Finder")
name = str(name).title() if not name or (name.lower() in ["unknown", "none"]):
if "." in name: name = title if title else "Unknown Application"
name = name.split(".")[-1]
# Smart title formatting (capitalize first letter) - but not for "Finder"
if name != "Finder":
name = str(name).title()
if "." in name:
name = name.split(".")[-1]
if update: if update:
modus_service.current_active_app_name = name modus_service.current_active_app_name = name
@@ -96,26 +105,39 @@ def format_window(title, wmclass):
def show_about_app(): def show_about_app():
"""Show about dialog for current active application""" """Show about dialog for current active application"""
try: try:
# Get current window info from Hyprland # Use modus_service's Hyprland connection to get current window info
result = subprocess.run( wmclass = ""
["hyprctl", "activewindow", "-j"], title = ""
capture_output=True,
text=True, if (
check=True hasattr(modus_service, "_hyprland_connection")
) and modus_service._hyprland_connection
import json ):
window_info = json.loads(result.stdout) window_data = modus_service._hyprland_connection.send_command(
wmclass = window_info.get("class", "") "j/activewindow"
title = window_info.get("title", "") ).reply
if window_data:
window_info = json.loads(window_data.decode("utf-8"))
wmclass = window_info.get("class", "")
title = window_info.get("title", "")
# Don't show about dialog if there's no active window (Finder state)
if not wmclass and not title:
return
app_name = modus_service.current_active_app_name or "Unknown Application" app_name = modus_service.current_active_app_name or "Unknown Application"
# Don't show about dialog for Finder
if app_name == "Finder":
return
about_window = AboutApp(app_name=app_name, wmclass=wmclass) about_window = AboutApp(app_name=app_name, wmclass=wmclass)
about_window.toggle(None) about_window.toggle(None)
except Exception as e: except Exception:
# Fallback: show about dialog with just the app name # Fallback: only show if we have a real app name
app_name = modus_service.current_active_app_name or "Unknown Application" app_name = modus_service.current_active_app_name or ""
about_window = AboutApp(app_name=app_name, wmclass="") if app_name and app_name != "Finder":
about_window.toggle(None) about_window = AboutApp(app_name=app_name, wmclass="")
about_window.toggle(None)
def dropdown_option( def dropdown_option(
@@ -213,7 +235,7 @@ class MenuBarDropdowns:
# Global menu dropdowns # Global menu dropdowns
self.global_title_menu_about = dropdown_option( self.global_title_menu_about = dropdown_option(
f"About {modus_service.current_active_app_name}", f"About {modus_service.current_active_app_name}",
on_clicked=lambda _: show_about_app() on_clicked=lambda _: show_about_app(),
) )
self.global_menu_title = DropDownMouseCapture( self.global_menu_title = DropDownMouseCapture(
layer="bottom", layer="bottom",
@@ -325,7 +347,7 @@ class MenuBarDropdowns:
), ),
name="global-title-button", name="global-title-button",
style_classes="button", style_classes="button",
on_clicked=lambda _: self.global_menu_title.toggle_mousecapture(), on_clicked=self._on_title_button_clicked,
) )
self.global_menu_title.child_window.set_pointing_to( self.global_menu_title.child_window.set_pointing_to(
@@ -368,6 +390,37 @@ class MenuBarDropdowns:
modus_service.connect("current-dropdown-changed", self.changed_dropdown) modus_service.connect("current-dropdown-changed", self.changed_dropdown)
modus_service.connect("dropdowns-hide-changed", self.hide_dropdowns) modus_service.connect("dropdowns-hide-changed", self.hide_dropdowns)
def _on_title_button_clicked(self, _):
"""Handle title button click - only show dropdown if there's an active window"""
try:
# Use modus_service's Hyprland connection to get current window info
if (
hasattr(modus_service, "_hyprland_connection")
and modus_service._hyprland_connection
):
window_data = modus_service._hyprland_connection.send_command(
"j/activewindow"
).reply
if window_data:
window_info = json.loads(window_data.decode("utf-8"))
wmclass = window_info.get("class", "")
title = window_info.get("title", "")
# Only show dropdown if there's an active window (not Finder)
if wmclass or title:
self.global_menu_title.toggle_mousecapture()
return
# Fallback: check if current_active_app_name is not "Finder"
if (
modus_service.current_active_app_name
and modus_service.current_active_app_name != "Finder"
):
self.global_menu_title.toggle_mousecapture()
except Exception:
# If we can't get window info, don't show dropdown
pass
def hide_dropdowns(self, *_): def hide_dropdowns(self, *_):
self.menu_button.remove_style_class("active") self.menu_button.remove_style_class("active")
self.global_menu_button_edit.remove_style_class("active") self.global_menu_button_edit.remove_style_class("active")