Merge pull request 'window switcher' (#647) from hsjobeki-main into main
This commit is contained in:
commit
a12fdabf8f
@ -1,7 +1,6 @@
|
||||
import argparse
|
||||
|
||||
from .windows.join import register_join_parser
|
||||
from .windows.overview import register_overview_parser, show_overview
|
||||
from .app import register_join_parser, register_overview_parser, show_overview
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
@ -1,21 +1,62 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
from dataclasses import dataclass
|
||||
|
||||
import gi
|
||||
|
||||
gi.require_version("Gtk", "3.0")
|
||||
from clan_cli.clan_uri import ClanURI
|
||||
from gi.repository import Gio, Gtk
|
||||
|
||||
from .constants import constants
|
||||
from .interfaces import Callbacks, InitialJoinValues
|
||||
from .windows.join import JoinWindow
|
||||
from .windows.overview import OverviewWindow
|
||||
|
||||
|
||||
@dataclass
|
||||
class ClanWindows:
|
||||
join: type[JoinWindow]
|
||||
overview: type[OverviewWindow]
|
||||
|
||||
|
||||
@dataclass
|
||||
class ClanConfig:
|
||||
initial_window: str
|
||||
url: ClanURI | None
|
||||
|
||||
|
||||
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 +77,29 @@ 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)
|
||||
|
15
pkgs/clan-vm-manager/clan_vm_manager/interfaces.py
Normal file
15
pkgs/clan-vm-manager/clan_vm_manager/interfaces.py
Normal file
@ -0,0 +1,15 @@
|
||||
from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
|
||||
from clan_cli.clan_uri import ClanURI
|
||||
|
||||
|
||||
@dataclass
|
||||
class InitialJoinValues:
|
||||
url: ClanURI
|
||||
|
||||
|
||||
@dataclass
|
||||
class Callbacks:
|
||||
show_list: Callable[[], None]
|
||||
show_join: Callable[[InitialJoinValues], None]
|
@ -1,19 +1,19 @@
|
||||
import argparse
|
||||
from typing import Any
|
||||
|
||||
import gi
|
||||
from clan_cli.clan_uri import ClanURI
|
||||
|
||||
from ..app import Application
|
||||
from ..interfaces import Callbacks, InitialJoinValues
|
||||
|
||||
gi.require_version("Gtk", "3.0")
|
||||
|
||||
from gi.repository import Gio, Gtk
|
||||
|
||||
|
||||
class JoinWindow(Gtk.ApplicationWindow):
|
||||
def __init__(self) -> None:
|
||||
def __init__(self, initial_values: InitialJoinValues, cbs: Callbacks) -> None:
|
||||
super().__init__()
|
||||
# Initialize the main window
|
||||
# Initialize the main wincbsdow
|
||||
self.cbs = cbs
|
||||
self.set_title("cLAN Manager")
|
||||
self.connect("delete-event", self.on_quit)
|
||||
self.set_default_size(800, 600)
|
||||
@ -21,28 +21,24 @@ class JoinWindow(Gtk.ApplicationWindow):
|
||||
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6, expand=True)
|
||||
self.add(vbox)
|
||||
|
||||
# Add a notebook layout
|
||||
# https://python-gtk-3-tutorial.readthedocs.io/en/latest/layout.html#notebook
|
||||
self.notebook = Gtk.Notebook()
|
||||
self.stack = Gtk.Stack()
|
||||
|
||||
self.stack.add_titled(Gtk.Label("Join cLan"), "join", "Join")
|
||||
self.stack.add_titled(Gtk.Label(str(initial_values.url)), "join", "Join")
|
||||
|
||||
vbox.add(self.stack)
|
||||
vbox.add(Gtk.Entry(text=str(initial_values.url)))
|
||||
|
||||
button = Gtk.Button(
|
||||
label="To List",
|
||||
)
|
||||
button.connect("clicked", self.switch)
|
||||
vbox.add(button)
|
||||
|
||||
# Must be called AFTER all components were added
|
||||
self.show_all()
|
||||
|
||||
def switch(self, widget: Gtk.Widget) -> None:
|
||||
self.cbs.show_list()
|
||||
|
||||
def on_quit(self, *args: Any) -> None:
|
||||
Gio.Application.quit(self.get_application())
|
||||
|
||||
|
||||
def show_join(args: argparse.Namespace) -> None:
|
||||
print(f"Joining clan {args.clan_uri}")
|
||||
app = Application(JoinWindow())
|
||||
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)
|
||||
|
@ -5,11 +5,9 @@ import gi
|
||||
from ..models import VMBase
|
||||
|
||||
gi.require_version("Gtk", "3.0")
|
||||
import argparse
|
||||
|
||||
from gi.repository import Gio, Gtk
|
||||
|
||||
from ..app import Application
|
||||
from ..ui.clan_join_page import ClanJoinPage
|
||||
from ..ui.clan_select_list import ClanEdit, ClanList
|
||||
|
||||
@ -86,12 +84,3 @@ class OverviewWindow(Gtk.ApplicationWindow):
|
||||
|
||||
def on_quit(self, *args: Any) -> None:
|
||||
Gio.Application.quit(self.get_application())
|
||||
|
||||
|
||||
def show_overview(args: argparse.Namespace) -> None:
|
||||
app = Application(OverviewWindow())
|
||||
return app.run()
|
||||
|
||||
|
||||
def register_overview_parser(parser: argparse.ArgumentParser) -> None:
|
||||
parser.set_defaults(func=show_overview)
|
||||
|
Loading…
Reference in New Issue
Block a user