2023-11-25 00:55:01 +00:00
|
|
|
#!/usr/bin/env python3
|
2023-12-16 11:52:10 +00:00
|
|
|
import argparse
|
|
|
|
from dataclasses import dataclass
|
2023-12-26 17:02:43 +00:00
|
|
|
from pathlib import Path
|
2023-12-16 12:03:10 +00:00
|
|
|
|
2023-11-25 00:55:01 +00:00
|
|
|
import gi
|
2023-12-16 12:03:10 +00:00
|
|
|
|
2024-01-17 12:11:49 +00:00
|
|
|
from clan_vm_manager.interfaces import InitialJoinValues
|
|
|
|
from clan_vm_manager.model.use_views import Views
|
|
|
|
from clan_vm_manager.views.list import ClanList
|
|
|
|
from clan_vm_manager.views.trust_join import Trust
|
2023-12-30 10:14:45 +00:00
|
|
|
|
2024-01-17 12:11:49 +00:00
|
|
|
gi.require_version("Gtk", "4.0")
|
|
|
|
gi.require_version("Adw", "1")
|
2024-01-02 04:54:19 +00:00
|
|
|
|
2023-12-16 11:52:10 +00:00
|
|
|
from clan_cli.clan_uri import ClanURI
|
2024-01-17 12:11:49 +00:00
|
|
|
from gi.repository import Adw, Gdk, Gio, Gtk
|
2023-12-16 12:03:10 +00:00
|
|
|
|
2023-11-28 17:19:01 +00:00
|
|
|
from .constants import constants
|
2024-01-17 12:11:49 +00:00
|
|
|
from .model.use_vms import VMS
|
2023-12-16 12:05:17 +00:00
|
|
|
|
2023-11-28 17:19:01 +00:00
|
|
|
|
2023-12-16 11:52:10 +00:00
|
|
|
@dataclass
|
2023-12-16 12:03:10 +00:00
|
|
|
class ClanConfig:
|
2024-01-17 12:11:49 +00:00
|
|
|
initial_view: str
|
2023-12-16 12:04:50 +00:00
|
|
|
url: ClanURI | None
|
2023-12-16 12:05:17 +00:00
|
|
|
|
2023-11-28 18:55:40 +00:00
|
|
|
|
2024-01-17 12:11:49 +00:00
|
|
|
class MainWindow(Adw.ApplicationWindow):
|
|
|
|
def __init__(self, config: ClanConfig) -> None:
|
|
|
|
super().__init__()
|
|
|
|
self.set_title("cLAN Manager")
|
|
|
|
self.set_default_size(980, 650)
|
2024-01-02 04:54:19 +00:00
|
|
|
|
2024-01-17 12:11:49 +00:00
|
|
|
view = Adw.ToolbarView()
|
|
|
|
self.set_content(view)
|
2024-01-02 04:54:19 +00:00
|
|
|
|
2024-01-17 12:11:49 +00:00
|
|
|
header = Adw.HeaderBar()
|
|
|
|
view.add_top_bar(header)
|
2023-12-26 17:02:43 +00:00
|
|
|
|
2024-01-17 12:11:49 +00:00
|
|
|
# Initialize all views
|
|
|
|
stack_view = Views.use().view
|
|
|
|
stack_view.add_named(ClanList(), "list")
|
|
|
|
stack_view.add_named(
|
|
|
|
Trust(initial_values=InitialJoinValues(url=config.url)), "join.trust"
|
2023-12-26 17:02:43 +00:00
|
|
|
)
|
|
|
|
|
2024-01-17 12:11:49 +00:00
|
|
|
stack_view.set_visible_child_name(config.initial_view)
|
2023-12-26 17:02:43 +00:00
|
|
|
|
2024-01-17 12:11:49 +00:00
|
|
|
clamp = Adw.Clamp()
|
|
|
|
clamp.set_child(stack_view)
|
|
|
|
clamp.set_maximum_size(1000)
|
2023-12-26 17:02:43 +00:00
|
|
|
|
2024-01-17 12:11:49 +00:00
|
|
|
view.set_content(clamp)
|
|
|
|
|
|
|
|
# Push the first page to the navigation view
|
2023-12-16 11:52:10 +00:00
|
|
|
|
2023-11-28 17:19:01 +00:00
|
|
|
|
2024-01-17 12:11:49 +00:00
|
|
|
class Application(Adw.Application):
|
|
|
|
def __init__(self, config: ClanConfig) -> None:
|
|
|
|
super().__init__(
|
|
|
|
application_id=constants["APPID"], flags=Gio.ApplicationFlags.FLAGS_NONE
|
2023-12-30 10:14:45 +00:00
|
|
|
)
|
2024-01-17 12:11:49 +00:00
|
|
|
self.config = config
|
|
|
|
self.connect("shutdown", self.on_shutdown)
|
2023-12-30 10:14:45 +00:00
|
|
|
|
2024-01-17 12:11:49 +00:00
|
|
|
def on_shutdown(self, app: Gtk.Application) -> None:
|
|
|
|
print("Shutting down")
|
|
|
|
VMS.use().kill_all()
|
2023-11-28 17:19:01 +00:00
|
|
|
|
2023-11-30 12:42:15 +00:00
|
|
|
def do_activate(self) -> None:
|
2024-01-17 12:11:49 +00:00
|
|
|
self.init_style()
|
|
|
|
window = MainWindow(config=self.config)
|
|
|
|
window.set_application(self)
|
|
|
|
window.present()
|
2023-11-28 17:19:01 +00:00
|
|
|
|
|
|
|
# TODO: For css styling
|
2023-11-30 12:42:15 +00:00
|
|
|
def init_style(self) -> None:
|
2024-01-17 12:11:49 +00:00
|
|
|
resource_path = Path(__file__).parent / "style.css"
|
|
|
|
css_provider = Gtk.CssProvider()
|
|
|
|
css_provider.load_from_path(str(resource_path))
|
|
|
|
Gtk.StyleContext.add_provider_for_display(
|
|
|
|
Gdk.Display.get_default(),
|
|
|
|
css_provider,
|
|
|
|
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION,
|
|
|
|
)
|
2023-12-16 11:52:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
def show_join(args: argparse.Namespace) -> None:
|
2023-12-16 12:05:17 +00:00
|
|
|
app = Application(
|
2024-01-17 12:11:49 +00:00
|
|
|
config=ClanConfig(url=args.clan_uri, initial_view="join.trust"),
|
2023-12-16 12:05:17 +00:00
|
|
|
)
|
2023-12-16 11:52:10 +00:00
|
|
|
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:
|
2023-12-16 12:05:17 +00:00
|
|
|
app = Application(
|
2024-01-17 12:11:49 +00:00
|
|
|
config=ClanConfig(url=None, initial_view="list"),
|
2023-12-16 12:05:17 +00:00
|
|
|
)
|
2023-12-16 11:52:10 +00:00
|
|
|
return app.run()
|
|
|
|
|
|
|
|
|
|
|
|
def register_overview_parser(parser: argparse.ArgumentParser) -> None:
|
|
|
|
parser.set_defaults(func=show_overview)
|