2024-02-29 18:58:03 +00:00
|
|
|
import logging
|
2024-02-29 15:46:09 +00:00
|
|
|
import threading
|
2024-02-21 09:16:58 +00:00
|
|
|
|
2024-01-20 09:11:52 +00:00
|
|
|
import gi
|
2024-05-21 13:57:27 +00:00
|
|
|
from clan_cli.api import API
|
2024-02-29 15:46:09 +00:00
|
|
|
from clan_cli.history.list import list_history
|
2024-01-20 09:11:52 +00:00
|
|
|
|
2024-06-05 09:23:12 +00:00
|
|
|
from clan_app.components.interfaces import ClanConfig
|
|
|
|
from clan_app.singletons.toast import ToastOverlay
|
|
|
|
from clan_app.singletons.use_views import ViewStack
|
|
|
|
from clan_app.singletons.use_vms import ClanStore
|
|
|
|
from clan_app.views.details import Details
|
|
|
|
from clan_app.views.list import ClanList
|
|
|
|
from clan_app.views.logs import Logs
|
2024-06-08 15:04:51 +00:00
|
|
|
from clan_app.views.webview import WebView, open_file
|
2024-01-20 09:11:52 +00:00
|
|
|
|
|
|
|
gi.require_version("Adw", "1")
|
|
|
|
|
2024-06-05 10:10:26 +00:00
|
|
|
from gi.repository import Adw, Gio, GLib
|
2024-05-21 13:57:27 +00:00
|
|
|
|
2024-06-05 09:23:12 +00:00
|
|
|
from clan_app.components.trayicon import TrayIcon
|
2024-01-20 09:11:52 +00:00
|
|
|
|
2024-02-29 18:58:03 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2024-01-20 09:11:52 +00:00
|
|
|
|
|
|
|
class MainWindow(Adw.ApplicationWindow):
|
|
|
|
def __init__(self, config: ClanConfig) -> None:
|
|
|
|
super().__init__()
|
2024-05-27 13:54:17 +00:00
|
|
|
self.set_title("Clan Manager")
|
2024-03-22 18:08:35 +00:00
|
|
|
self.set_default_size(980, 850)
|
2024-03-10 12:18:01 +00:00
|
|
|
|
2024-06-05 10:10:26 +00:00
|
|
|
# Overlay for GTK side exclusive toasts
|
2024-03-09 15:45:54 +00:00
|
|
|
overlay = ToastOverlay.use().overlay
|
2024-01-20 09:11:52 +00:00
|
|
|
view = Adw.ToolbarView()
|
2024-03-09 15:45:54 +00:00
|
|
|
overlay.set_child(view)
|
|
|
|
|
|
|
|
self.set_content(overlay)
|
2024-01-20 09:11:52 +00:00
|
|
|
|
|
|
|
header = Adw.HeaderBar()
|
|
|
|
view.add_top_bar(header)
|
|
|
|
|
2024-02-21 09:16:58 +00:00
|
|
|
app = Gio.Application.get_default()
|
2024-03-09 16:15:32 +00:00
|
|
|
assert app is not None
|
2024-02-21 09:16:58 +00:00
|
|
|
self.tray_icon: TrayIcon = TrayIcon(app)
|
|
|
|
|
2024-03-03 09:47:38 +00:00
|
|
|
# Initialize all ClanStore
|
2024-04-24 12:41:29 +00:00
|
|
|
threading.Thread(target=self._populate_vms).start()
|
2024-02-29 15:46:09 +00:00
|
|
|
|
2024-03-03 09:51:34 +00:00
|
|
|
stack_view = ViewStack.use().view
|
2024-03-17 13:08:39 +00:00
|
|
|
stack_view.add_named(ClanList(config), "list")
|
2024-01-21 11:45:45 +00:00
|
|
|
stack_view.add_named(Details(), "details")
|
2024-03-10 12:18:01 +00:00
|
|
|
stack_view.add_named(Logs(), "logs")
|
2024-05-18 13:55:59 +00:00
|
|
|
|
2024-06-08 15:04:51 +00:00
|
|
|
# Override platform specific functions
|
|
|
|
API.register(open_file)
|
|
|
|
|
2024-05-20 17:34:27 +00:00
|
|
|
webview = WebView(methods=API._registry)
|
2024-06-08 15:04:51 +00:00
|
|
|
|
2024-05-23 07:33:57 +00:00
|
|
|
stack_view.add_named(webview.get_webview(), "webview")
|
2024-01-20 09:11:52 +00:00
|
|
|
stack_view.set_visible_child_name(config.initial_view)
|
|
|
|
|
2024-06-05 10:10:26 +00:00
|
|
|
view.set_content(stack_view)
|
2024-02-21 09:16:58 +00:00
|
|
|
|
|
|
|
self.connect("destroy", self.on_destroy)
|
|
|
|
|
2024-04-24 12:41:29 +00:00
|
|
|
def _set_clan_store_ready(self) -> bool:
|
|
|
|
ClanStore.use().emit("is_ready")
|
|
|
|
return GLib.SOURCE_REMOVE
|
2024-04-23 14:16:48 +00:00
|
|
|
|
2024-04-24 12:41:29 +00:00
|
|
|
def _populate_vms(self) -> None:
|
2024-02-29 15:46:09 +00:00
|
|
|
# Execute `clan flakes add <path>` to democlan for this to work
|
|
|
|
# TODO: Make list_history a generator function
|
|
|
|
for entry in list_history():
|
2024-03-03 09:47:38 +00:00
|
|
|
GLib.idle_add(ClanStore.use().create_vm_task, entry)
|
2024-02-29 15:46:09 +00:00
|
|
|
|
2024-04-24 12:41:29 +00:00
|
|
|
GLib.idle_add(self._set_clan_store_ready)
|
2024-04-23 14:16:48 +00:00
|
|
|
|
2024-03-05 16:10:30 +00:00
|
|
|
def kill_vms(self) -> None:
|
|
|
|
log.debug("Killing all VMs")
|
|
|
|
ClanStore.use().kill_all()
|
|
|
|
|
2024-03-06 08:05:10 +00:00
|
|
|
def on_destroy(self, source: "Adw.ApplicationWindow") -> None:
|
2024-03-05 16:10:30 +00:00
|
|
|
log.info("====Destroying Adw.ApplicationWindow===")
|
2024-03-03 09:47:38 +00:00
|
|
|
ClanStore.use().kill_all()
|
2024-03-05 16:10:30 +00:00
|
|
|
self.tray_icon.destroy()
|