Skip to content

Commit

Permalink
Handle native quit and file-open events on macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
amolenaar committed Aug 11, 2020
1 parent 3c29e02 commit c9e5214
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
3 changes: 2 additions & 1 deletion gaphor/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ def quit(self):
event_manager.handle(SessionShutdownRequested(self))
if self.active_session == session:
logger.info("Window not closed, abort quit operation")
return
return False
self.shutdown()
return True

def all(self, base: Type[T]) -> Iterator[Tuple[str, T]]:
return (
Expand Down
4 changes: 4 additions & 0 deletions gaphor/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
gi.require_version("Gtk", "3.0") # noqa: isort:skip
gi.require_version("Gdk", "3.0") # noqa: isort:skip
from gi.repository import GLib, Gdk, Gio, Gtk # noqa: isort:skip
from gaphor.ui.macosshim import macos_init

# fmt: on


Expand Down Expand Up @@ -101,6 +103,8 @@ def app_startup(gtk_app):
try:
application = Application()
apply_application_actions(application, gtk_app)
if macos_init:
macos_init(application, gtk_app)
except Exception:
gtk_app.quit()
raise
Expand Down
28 changes: 28 additions & 0 deletions gaphor/ui/macosshim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
try:
import gi

gi.require_version("GtkosxApplication", "1.0")
except ValueError:
macos_init = None
else:
from gi.repository import Gio, GtkosxApplication

macos_app = GtkosxApplication.Application.get()

def open_file(macos_app, path, gtk_app):
if path == __file__:
return False

gtk_app.open([Gio.File.new_for_path(path)], "")

return True

def block_termination(macos_app, application):
quit = application.quit()
return not quit

def macos_init(application, gtk_app):
macos_app.connect("NSApplicationOpenFile", open_file, gtk_app)
macos_app.connect(
"NSApplicationBlockTermination", block_termination, application
)
11 changes: 4 additions & 7 deletions macos-dmg/__boot__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import pathlib
import pprint
import sys

PYVER = "3.7"
Expand All @@ -11,6 +10,10 @@
print(f"App contents folder is {resources}, cache is {cache}")


while sys.path[0] == resources:
del sys.path[0]


os.environ["DYLD_LIBRARY_PATH"] = f"{resources}/lib"
os.environ["FONTCONFIG_SYSROOT"] = f"{resources}"
os.environ["GTK_DATA_PREFIX"] = f"{resources}"
Expand All @@ -28,12 +31,6 @@

os.environ["GTK_IM_MODULE_FILE"] = f"{cache}/immodules.cache"

while sys.path[0] == resources:
del sys.path[0]


pprint.pprint(sys.path)

os.system(f'"{resources}/bin/gdk-pixbuf-query-loaders" --update-cache')
os.system(f'"{resources}/bin/gtk-query-immodules-3.0" --update-cache')

Expand Down
1 change: 1 addition & 0 deletions macos-dmg/package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ function rel_path {
brew deps gobject-introspection
echo adwaita-icon-theme
brew deps adwaita-icon-theme
echo gtk-mac-integration
} | sort -u |\
while read dep
do
Expand Down

0 comments on commit c9e5214

Please sign in to comment.