window switcher

This commit is contained in:
Johannes Kirschbauer
2023-12-16 12:52:10 +01:00
parent 590d39a29b
commit d60cfbc0a6
5 changed files with 94 additions and 35 deletions

View File

@@ -1,21 +1,56 @@
#!/usr/bin/env python3
import argparse
from dataclasses import dataclass
import gi
from typing import Callable, Optional, Type
gi.require_version("Gtk", "3.0")
from gi.repository import Gio, Gtk
from clan_cli.clan_uri import ClanURI
from .constants import constants
from .windows.join import JoinWindow
from .windows.overview import OverviewWindow
from .interfaces import Callbacks, InitialJoinValues
@dataclass
class ClanWindows():
join: type[JoinWindow]
overview: type[OverviewWindow]
@dataclass
class ClanConfig():
initial_window: str
url: Optional[ClanURI]
class Application(Gtk.Application):
def __init__(self, window: Gtk.ApplicationWindow) -> None:
def __init__(self, windows: ClanWindows, config: ClanConfig) -> None:
super().__init__(
application_id=constants["APPID"], flags=Gio.ApplicationFlags.FLAGS_NONE
)
self.init_style()
self.window = window
self.windows = windows
initial = windows.__dict__[config.initial_window]
if(issubclass(initial,JoinWindow)):
# see JoinWindow constructor
self.window = initial(initial_values=InitialJoinValues(url=config.url or ""), cbs=Callbacks(show_list=self.show_list, show_join=self.show_join))
if(issubclass(initial,OverviewWindow)):
# see OverviewWindow constructor
self.window = initial()
def show_list(self) -> None:
prev = self.window
self.window = self.windows.__dict__["overview"]()
self.do_activate()
prev.hide()
def show_join(self,initial_values: InitialJoinValues) -> None:
prev = self.window
self.window = self.windows.__dict__["join"]()
self.do_activate()
prev.hide()
def do_startup(self) -> None:
Gtk.Application.do_startup(self)
@@ -36,3 +71,23 @@ class Application(Gtk.Application):
# screen = Gdk.Screen.get_default()
# style_context = Gtk.StyleContext()
# style_context.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
def show_join(args: argparse.Namespace) -> None:
print(f"Joining clan {args.clan_uri}")
app = Application(windows=ClanWindows(join=JoinWindow, overview=OverviewWindow), config=ClanConfig(url=args.clan_uri, initial_window="join") )
return app.run()
def register_join_parser(parser: argparse.ArgumentParser) -> None:
parser.add_argument("clan_uri", type=ClanURI, help="clan URI to join")
parser.set_defaults(func=show_join)
def show_overview(args: argparse.Namespace) -> None:
app = Application(windows=ClanWindows(join=JoinWindow, overview=OverviewWindow), config=ClanConfig(url=None, initial_window="overview") )
return app.run()
def register_overview_parser(parser: argparse.ArgumentParser) -> None:
parser.set_defaults(func=show_overview)