151 lines
4.4 KiB
Python
151 lines
4.4 KiB
Python
from . import instance
|
|
import os
|
|
import gi
|
|
gi.require_version("Gtk", "3.0")
|
|
from gi.repository import Gtk, Gio
|
|
import asyncio
|
|
|
|
|
|
# Templates
|
|
_template_path = os.path.join(os.path.split(__file__)[0], "templates")
|
|
|
|
|
|
@Gtk.Template(filename=os.path.join(_template_path, "StartWidget.glade"))
|
|
class _StartWidget(Gtk.Box):
|
|
__gtype_name__ = "StartWidget"
|
|
|
|
__image = Gtk.Template.Child(name="start_image")
|
|
__name: Gtk.Label = Gtk.Template.Child("start_name")
|
|
__settings = Gtk.Template.Child("start_settings")
|
|
__run = Gtk.Template.Child("start_run")
|
|
__add_snapshot = Gtk.Template.Child("start_add_snapshot")
|
|
__snapshots = Gtk.Template.Child("start_snapshots")
|
|
|
|
__instance:instance.Instance
|
|
__aplication = None
|
|
|
|
def __init__(self, application, instance:instance.Instance, *args, **kargs):
|
|
# Init template
|
|
super().__init__(*args, **kargs)
|
|
self.__instance = instance
|
|
self.__aplication = application
|
|
|
|
# Set values
|
|
self.__name.set_label(instance.get_instance())
|
|
|
|
# Run callback
|
|
self.__run.connect("clicked", self.__run_clicked)
|
|
|
|
def __run_clicked(self, button):
|
|
self.__aplication.run_async(self.__instance.run(), wait=False)
|
|
|
|
|
|
|
|
@Gtk.Template(filename=os.path.join(_template_path, "AboutDialog.glade"))
|
|
class _AboutDialog(Gtk.AboutDialog):
|
|
__gtype_name__ = "AboutDialog"
|
|
template_type = "AboutDialog"
|
|
|
|
|
|
# Application
|
|
class _Application(Gtk.Application):
|
|
_builder: Gtk.Builder
|
|
_window: Gtk.ApplicationWindow
|
|
_stack: Gtk.Stack
|
|
_configsSource: list
|
|
_instance: str
|
|
_configs: dict
|
|
_configs_box:dict
|
|
_inited = None
|
|
_asyncloop = None
|
|
|
|
def __init__(self, asyncloop, inited, configs: list = [], instance: str = None, *args, **kargs):
|
|
super().__init__(*args, application_id="de.marko10_000.WineStarter", **kargs)
|
|
self._configsSource = list(configs)
|
|
self._instance = str(instance)
|
|
self._inited = inited
|
|
self._asyncloop = asyncloop
|
|
|
|
def do_startup(self):
|
|
# Load configs
|
|
configs = self._configs = {}
|
|
for i in self._configsSource:
|
|
tmp = instance.gen_instance(i)
|
|
configs[tmp.get_instance()] = tmp
|
|
|
|
# Start gtk application
|
|
Gtk.Application.do_startup(self)
|
|
|
|
def do_activate(self):
|
|
# Load gui
|
|
self._builder = builder = Gtk.Builder()
|
|
builder.add_from_file(os.path.join(
|
|
os.path.split(__file__)[0], "gui.glade"))
|
|
|
|
# Gen gui
|
|
self._window = builder.get_object("main_window")
|
|
self._stack = builder.get_object("main_content")
|
|
self._configs_box = instance_box = {}
|
|
if len(self._configs) == 1:
|
|
self._window.remove(self._window.get_child())
|
|
tmp = Gtk.Box()
|
|
tmp.show()
|
|
self._window.add(tmp)
|
|
instance_box[list(self._configs.keys())[0]] = tmp
|
|
else:
|
|
for i in self._stack.get_children():
|
|
self._stack.remove(i)
|
|
raise NotImplementedError()
|
|
|
|
# Add main window callbacks
|
|
def show_about(menu_entry):
|
|
_AboutDialog(parent=self._window).show_all()
|
|
builder.get_object("mainMenuAbout").connect("activate", show_about)
|
|
|
|
# Show main window
|
|
self._window.set_application(self)
|
|
self._window.show_all()
|
|
|
|
# Notify init end
|
|
self._inited()
|
|
|
|
def _set_instance_ui(self, instance:str, gui):
|
|
# Clean box
|
|
box = self._configs_box[instance]
|
|
for i in box.get_children():
|
|
box.remove(i)
|
|
|
|
# Set item
|
|
box.pack_start(gui, True, True, 0)
|
|
gui.show_all()
|
|
|
|
async def update_instances(self, start:str=None):
|
|
# Load gui for instances
|
|
for iID, i in self._configs.items():
|
|
tmp = _StartWidget(self, i)
|
|
self._set_instance_ui(iID, tmp)
|
|
tmp.show_all()
|
|
|
|
# Start
|
|
if start is not None:
|
|
raise NotImplementedError() # TODO
|
|
|
|
def run_async(self, command, wait:bool=True):
|
|
|
|
# Wait
|
|
if wait:
|
|
asyncio.run_coroutine_threadsafe(command, self._asyncloop)
|
|
else:
|
|
async def runner():
|
|
self._asyncloop.create_task(command)
|
|
asyncio.run_coroutine_threadsafe(runner(), self._asyncloop)
|
|
|
|
|
|
app: _Application
|
|
|
|
|
|
def run_gui(asyncloop, inited=lambda: None, configs: list = None):
|
|
global app
|
|
app = _Application(inited=inited, configs=configs, asyncloop=asyncloop)
|
|
app.run()
|