refactor: reogranize code structure for better styling

This commit is contained in:
sankalp committed 2026-07-21 14:40:23 +05:45
1 parent e095e88715
commit 0e805d24ec
2 files changed
+104 -94

No files matched your search

+59 -55
View File
@@ -1,10 +1,7 @@
import atexit
import os import os
# App-capture typelib path (built via meson in src/window/switcher/app-capture/builddir) # App-capture typelib path (built via meson in src/window/switcher/app-capture/builddir)
# Must be set before gi is imported so the typelib search path includes it. # Must be set before gi is imported so the typelib search path includes it.
# Note: LD_LIBRARY_PATH does not take effect in the current process, so the
# .so is preloaded via ctypes in window/switcher/main.py instead.
_app_capture_builddir = os.path.join( _app_capture_builddir = os.path.join(
os.path.dirname(__file__), "window", "switcher", "app-capture", "builddir" os.path.dirname(__file__), "window", "switcher", "app-capture", "builddir"
) )
@@ -18,18 +15,11 @@ if os.path.isdir(_app_capture_builddir):
from fabric import Application from fabric import Application
from fabric.utils import get_relative_path, logger, monitor_file from fabric.utils import get_relative_path, logger, monitor_file
for log in [ from services.config import config
"fabric",
"services",
"window",
"utils",
"window.globalmenu",
]:
logger.disable(log)
from services.keyboard_layout import KeyboardLayout from services.keyboard_layout import KeyboardLayout
from shared.data import APP_NAME, load_config from shared.data import APP_NAME, load_config
from utils.functions import set_process_name from utils.functions import set_process_name
from utils.gtk_utils import generate_colors_from_wallpaper
from window.desktop.widget import Deskwidgets from window.desktop.widget import Deskwidgets
from window.dock import Dock from window.dock import Dock
from window.lock import LockScreenWrapper from window.lock import LockScreenWrapper
@@ -42,25 +32,19 @@ from window.spotlight.main import SpotlightWindow
from window.switcher import ApplicationSwitcher from window.switcher import ApplicationSwitcher
def main(): def setup_logger():
for log in [
# Generate colors.css if it doesn't exist "fabric",
colors_css_path = get_relative_path("styles/colors.css") "services",
if not os.path.exists(colors_css_path): "window",
from utils.gtk_utils import generate_colors_from_wallpaper "utils",
"window.globalmenu",
default_wallpaper = get_relative_path("assets/wallpaper_example/example-1.png") "LIBDBUSMENU-GLIB",
if os.path.exists(default_wallpaper): "libdbusmenu-glib",
generate_colors_from_wallpaper(default_wallpaper) ]:
logger.disable(log)
set_process_name(APP_NAME)
load_config()
from services.config import config
_debug = config().get("debug", False) _debug = config().get("debug", False)
if not _debug: if not _debug:
logger.disable("modus_plugin_builtin_emoji") logger.disable("modus_plugin_builtin_emoji")
else: else:
@@ -70,6 +54,48 @@ def main():
logger.enable("utils") logger.enable("utils")
logger.enable("window.globalmenu") logger.enable("window.globalmenu")
def setup_css(app):
# Generate colors.css if it doesn't exist
colors_css_path = get_relative_path("styles/colors.css")
if not os.path.exists(colors_css_path):
default_wallpaper = get_relative_path("assets/wallpaper_example/example-1.png")
if os.path.exists(default_wallpaper):
generate_colors_from_wallpaper(default_wallpaper)
def set_css():
app.set_stylesheet_from_file(get_relative_path("styles/main.css"))
app.set_css = set_css
app.set_css()
css_monitors = []
for root, dirs, files in os.walk(get_relative_path("styles/")):
monitor = monitor_file(root)
monitor.connect("changed", lambda *_: set_css())
css_monitors.append(monitor)
def cleanup_css_monitors(*args):
for m in css_monitors:
try:
m.cancel()
except Exception as e:
logger.warning(f"[main] m.cancel() failed: {e}")
app.connect("shutdown", cleanup_css_monitors)
def main():
set_process_name(APP_NAME)
load_config()
setup_logger()
# Initialize the Application first so it can manage styles globally
app = Application(f"{APP_NAME}")
# Set up styling before creating windows to prevent unstyled flashes
setup_css(app)
switcher = ApplicationSwitcher() switcher = ApplicationSwitcher()
panel = Panel() panel = Panel()
modusnoti = ModusNoti() modusnoti = ModusNoti()
@@ -81,15 +107,8 @@ def main():
screencapture = ScreenCaptureWindow() screencapture = ScreenCaptureWindow()
settings = get_settings_window() settings = get_settings_window()
# Monitor CSS files and subdirectories for changes # Register windows with the application
css_monitors = [] for w in [
for root, dirs, files in os.walk(get_relative_path("styles/")):
monitor = monitor_file(root)
monitor.connect("changed", lambda *_: set_css())
css_monitors.append(monitor)
app = Application(
f"{APP_NAME}",
panel, panel,
modusnoti, modusnoti,
deskwidget.top_left, deskwidget.top_left,
@@ -99,25 +118,10 @@ def main():
dock, dock,
screencapture, screencapture,
spotlight, spotlight,
) ]:
app.add_window(w)
def set_css():
app.set_stylesheet_from_file(get_relative_path("styles/main.css"))
app.set_css = set_css
app.set_css()
def cleanup_css_monitors():
for m in css_monitors:
try:
m.cancel()
except Exception as e:
logger.warning(f"[main] m.cancel() failed: {e}")
atexit.register(cleanup_css_monitors)
# Inject into the executing module's namespace (__main__) # Inject into the executing module's namespace (__main__)
# to emulate what happened when this file was run directly.
# This allows fabric-cli exec to execute commands flawlessly. # This allows fabric-cli exec to execute commands flawlessly.
import __main__ import __main__
+45 -39
View File
@@ -1,66 +1,72 @@
import os import os
import sys import sys
# Ensure the 'src' directory is in the python path
# This allows 'import main' and other src-relative imports to work from the root
src_path = os.path.join(os.path.dirname(__file__), "src")
if src_path not in sys.path:
sys.path.insert(0, src_path)
# App-capture library paths (built via meson in src/window/switcher/app-capture/builddir) def setup_environment():
_app_capture_builddir = os.path.join( """Ensure the src directory is in sys.path and configure C library paths."""
src_path, "window", "switcher", "app-capture", "builddir" src_path = os.path.join(os.path.dirname(__file__), "src")
) if src_path not in sys.path:
if os.path.isdir(_app_capture_builddir): sys.path.insert(0, src_path)
for env_var in ("GI_TYPELIB_PATH", "LD_LIBRARY_PATH"):
current = os.environ.get(env_var, "") # App-capture library paths (built via meson in src/window/switcher/app-capture/builddir)
if _app_capture_builddir not in current.split(os.pathsep): app_capture_builddir = os.path.join(
os.environ[env_var] = ( src_path, "window", "switcher", "app-capture", "builddir"
f"{_app_capture_builddir}{os.pathsep}{current}" )
if current
else _app_capture_builddir if os.path.isdir(app_capture_builddir):
) for env_var in ("GI_TYPELIB_PATH", "LD_LIBRARY_PATH"):
current = os.environ.get(env_var, "")
if app_capture_builddir not in current.split(os.pathsep):
os.environ[env_var] = (
f"{app_capture_builddir}{os.pathsep}{current}"
if current
else app_capture_builddir
)
# Apply monkey patch for hyprland lua dispatcher
def run_app(): def run_app():
"""Start the main Modus application."""
from main import main as app_main from main import main as app_main
app_main() app_main()
def run_lock(): def run_lock():
"""Start the lock screen."""
from window.lock import main as lock_main from window.lock import main as lock_main
lock_main() lock_main()
def run_spotlight(argv): def run_spotlight(argv):
"""Start the spotlight search interface."""
from window.spotlight.app import main as spotlight_main from window.spotlight.app import main as spotlight_main
command = "" external = "--external" in argv
text = "" positional = [arg for arg in argv if arg != "--external"]
external = False
positional = [] command = positional[0] if positional else ""
for arg in argv: text = " ".join(positional[1:]) if len(positional) > 1 else ""
if arg == "--external":
external = True
else:
positional.append(arg)
if positional:
command = positional[0]
if len(positional) > 1:
text = " ".join(positional[1:])
spotlight_main(command=command, text=text, external=external) spotlight_main(command=command, text=text, external=external)
def main():
setup_environment()
# Route the command based on arguments
if len(sys.argv) > 1:
command = sys.argv[1]
if command == "lock":
run_lock()
return
elif command == "spotlight":
run_spotlight(sys.argv[2:])
return
# Default to running the main app
run_app()
if __name__ == "__main__": if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "lock": main()
run_lock()
elif len(sys.argv) > 1 and sys.argv[1] == "spotlight":
run_spotlight(sys.argv[2:])
else:
run_app()