Keep active session in sync with active window
This commit is contained in:
parent
54e456451c
commit
6cd64043d8
@ -9,9 +9,11 @@ All important services are present in the application object:
|
||||
- action sets
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import inspect
|
||||
import logging
|
||||
from typing import Dict, Type
|
||||
from typing import Dict, Optional, Set, Type
|
||||
|
||||
import importlib_metadata
|
||||
|
||||
@ -41,8 +43,8 @@ class _Application:
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.active_session = None
|
||||
self.sessions = set()
|
||||
self.active_session: Optional[Session] = None
|
||||
self.sessions: Set[Session] = set()
|
||||
|
||||
def init(self):
|
||||
uninitialized_services = load_services("gaphor.appservices")
|
||||
|
@ -11,12 +11,16 @@ from gaphor.core import action
|
||||
|
||||
|
||||
class HelpService(Service, ActionProvider):
|
||||
# def __init__(self, main_window):
|
||||
# self.main_window = main_window
|
||||
def __init__(self, session):
|
||||
self.session = session
|
||||
|
||||
def shutdown(self):
|
||||
pass
|
||||
|
||||
@property
|
||||
def window(self):
|
||||
return self.session.get_service("main_window").window
|
||||
|
||||
@action(name="app.about")
|
||||
def about(self):
|
||||
builder = Gtk.Builder()
|
||||
@ -29,7 +33,7 @@ class HelpService(Service, ActionProvider):
|
||||
|
||||
about.set_version(str(__version__))
|
||||
|
||||
# about.set_transient_for(self.main_window.window)
|
||||
about.set_transient_for(self.window)
|
||||
|
||||
about.show_all()
|
||||
about.run()
|
||||
@ -45,7 +49,7 @@ class HelpService(Service, ActionProvider):
|
||||
|
||||
shortcuts = builder.get_object("shortcuts-gaphor")
|
||||
shortcuts.set_modal(True)
|
||||
# shortcuts.set_transient_for(self.main_window.window)
|
||||
shortcuts.set_transient_for(self.window)
|
||||
|
||||
shortcuts.show_all()
|
||||
return shortcuts
|
||||
|
15
gaphor/services/session.py
Normal file
15
gaphor/services/session.py
Normal file
@ -0,0 +1,15 @@
|
||||
from gaphor.abc import Service
|
||||
from gaphor.application import Application
|
||||
|
||||
|
||||
class Session(Service):
|
||||
"""
|
||||
Application service. Get the active session.
|
||||
"""
|
||||
|
||||
def shutdown(self):
|
||||
pass
|
||||
|
||||
def get_service(self, name):
|
||||
assert Application.active_session
|
||||
return Application.active_session.get_service(name)
|
@ -65,28 +65,29 @@ def main(argv=sys.argv):
|
||||
|
||||
|
||||
def run(args):
|
||||
def app_startup(app):
|
||||
Application.init()
|
||||
apply_application_actions(Application, app)
|
||||
def on_active_window(window, prop, session):
|
||||
Application.active_session = session
|
||||
|
||||
def app_activate(app):
|
||||
# Make sure gui is loaded ASAP.
|
||||
# This prevents menu items from appearing at unwanted places.
|
||||
# main_window = application.get_service("main_window")
|
||||
# main_window.open(app)
|
||||
# app.add_window(main_window.window)
|
||||
|
||||
if not Application.has_sessions():
|
||||
def new_session(app):
|
||||
session = Application.new_session()
|
||||
|
||||
# Only at application level (startup())?
|
||||
# component_registry = session.get_service("component_registry")
|
||||
# apply_application_actions(component_registry, app)
|
||||
|
||||
main_window = session.get_service("main_window")
|
||||
main_window.open(app)
|
||||
app.add_window(main_window.window)
|
||||
main_window.window.connect("notify::is-active", on_active_window, session)
|
||||
return session
|
||||
|
||||
def app_startup(app):
|
||||
try:
|
||||
Application.init()
|
||||
apply_application_actions(Application, app)
|
||||
except Exception:
|
||||
app.quit()
|
||||
raise
|
||||
|
||||
def app_activate(app):
|
||||
if not Application.has_sessions():
|
||||
session = new_session(app)
|
||||
file_manager = session.get_service("file_manager")
|
||||
file_manager.action_new()
|
||||
|
||||
@ -94,14 +95,7 @@ def run(args):
|
||||
print(f"Open files {files} with '{hint}'.")
|
||||
assert n_files == 1
|
||||
for file in files:
|
||||
session = Application.new_session()
|
||||
# component_registry = session.get_service("component_registry")
|
||||
# apply_application_actions(component_registry, app)
|
||||
|
||||
main_window = session.get_service("main_window")
|
||||
main_window.open(app)
|
||||
app.add_window(main_window.window)
|
||||
|
||||
session = new_session(app)
|
||||
file_manager = Application.get_service("file_manager")
|
||||
file_manager.load(file.get_path())
|
||||
|
||||
@ -120,6 +114,11 @@ def run(args):
|
||||
|
||||
|
||||
def quit():
|
||||
session = Application.active_session
|
||||
assert session
|
||||
session.shutdown()
|
||||
Application.sessions.discard(session)
|
||||
if not Application.sessions:
|
||||
Gtk.Application.get_default().quit()
|
||||
|
||||
|
||||
|
@ -337,7 +337,7 @@ class FileManager(Service, ActionProvider):
|
||||
|
||||
return False
|
||||
|
||||
@action(name="app.quit", shortcut="<Primary>q")
|
||||
@action(name="quit", shortcut="<Primary>q")
|
||||
def file_quit(self):
|
||||
"""
|
||||
Ask user to close window if the model has changed.
|
||||
|
@ -62,6 +62,7 @@ gaphor = 'gaphor.ui:main'
|
||||
gaphorconvert = 'gaphor.tools.gaphorconvert:main'
|
||||
|
||||
[tool.poetry.plugins."gaphor.appservices"]
|
||||
"session" = "gaphor.services.session:Session"
|
||||
"help" = "gaphor.services.helpservice:HelpService"
|
||||
|
||||
[tool.poetry.plugins."gaphor.services"]
|
||||
|
Loading…
x
Reference in New Issue
Block a user