Skip to content

armgit / BUI

Public Imported from GitHub
0

Blender's GPU Accelerated UI, Decoupled from it's original codebase. Supports Rust, Python, JS, React, C & C++. Twice as good as GPUI

fix(macos): route quit to bui windows when tk owns nsapp

Krish Kalaria308370a854bf
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f3e5765..f4624c4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -205,6 +205,7 @@ The newest work. Details for some items follow in the sections below.
- **tkinter on HiDPI:** a window fitting its content (and `simpledialog` windows) was scaled twice, so it came out twice too large on Retina displays and other scaled screens.
- **tkinter on macOS:** `Menu` defaults to `tearoff=0`, like Tk on Aqua.
- **`geometry()` round trip:** `root.geometry(root.geometry())` doubled the window on Retina displays and other scaled screens, and on macOS moved it down by the title bar. The getter now reports logical pixels, and on macOS `wm("pos")` sets the content's position, the one `wm_get("pos")` reports.
+- **macOS, Python:** Quit (Cmd+Q, the Dock) did nothing while Tk owned `NSApp`, the default when tkinter is installed. It now asks every BUI window to close, as without Tk.
### Changed
- **Text looks like Blender's:** Inter, Blender's UI font, is embedded and is the default face on every platform. The portable stb_truetype engine is now the default on Windows too. `-DBUI_TEXT_GDI=ON` (Rust: the `gdi-text` feature) restores GDI and system fonts. `BUI_TEXT_STB` and `portable-text` are now no-ops.
diff --git a/bindings/python/tests/test_tk_compat.py b/bindings/python/tests/test_tk_compat.py
index d45318d..1f1722c 100644
--- a/bindings/python/tests/test_tk_compat.py
+++ b/bindings/python/tests/test_tk_compat.py
@@ -596,8 +596,44 @@ def test_window_fits_content_at_ui_scale():
subprocess.run([sys.executable, "-c", _FIT_AT_SCALE], env=env, check=True, timeout=60)
+def test_quit_asks_windows_to_close():
+ """macOS: Quit in Tk's menu (Cmd+Q) runs the windows' close handlers, as it does
+ on BUI's own NSApp."""
+ if sys.platform != "darwin":
+ return
+ import ctypes
+ import ctypes.util
+ from bui.tksyntax import Tk
+ objc = ctypes.CDLL(ctypes.util.find_library("objc"))
+ objc.objc_getClass.restype = objc.sel_registerName.restype = ctypes.c_void_p
+
+ def msg(obj, sel, *args):
+ # PYFUNCTYPE keeps the GIL: Quit runs the Python close handler inside the call
+ call = ctypes.PYFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p,
+ *[ctypes.c_long] * len(args))(("objc_msgSend", objc))
+ return call(obj, objc.sel_registerName(sel), *args)
+ root = Tk()
+ closed = []
+ root.protocol("WM_DELETE_WINDOW", lambda: (closed.append(1), root.destroy()))
+
+ def choose_quit():
+ app = msg(objc.objc_getClass(b"NSApplication"), b"sharedApplication")
+ menu = msg(msg(msg(app, b"mainMenu"), b"itemAtIndex:", 0), b"submenu")
+ for i in range(msg(menu, b"numberOfItems") or 0):
+ key = msg(msg(menu, b"itemAtIndex:", i), b"keyEquivalent")
+ if ctypes.cast(msg(key, b"UTF8String"), ctypes.c_char_p).value == b"q":
+ msg(menu, b"performActionForItemAtIndex:", i)
+ return
+ raise AssertionError("no Quit item in Tk's application menu")
+ root.after(50, choose_quit)
+ root.after(3000, root.destroy) # the failure case: Quit did nothing
+ root.mainloop()
+ assert closed, "Quit didn't reach the window"
+
+
def main() -> int:
test_hidden_root_is_not_default_root()
+ test_quit_asks_windows_to_close()
test_window_fits_content_at_ui_scale()
test_programs_match_stdlib()
test_install_runs_unchanged_scripts()
diff --git a/kit/intern/kit_window_cocoa.m b/kit/intern/kit_window_cocoa.m
index cae873b..0300b5a 100644
--- a/kit/intern/kit_window_cocoa.m
+++ b/kit/intern/kit_window_cocoa.m
@@ -649,6 +649,23 @@ static CGColorSpaceRef srgb(void)
return NSTerminateCancel;
}
+/* Quit on an NSApp someone else made (see route_quit): with no window of ours
+ * left, the quit is theirs. */
+- (void)quit:(id)sender
+{
+ if (g_open == 0) {
+ [NSApp terminate:sender];
+ }
+ else {
+ [self applicationShouldTerminate:NSApp];
+ }
+}
+
+- (void)quitEvent:(NSAppleEventDescriptor *)event withReply:(NSAppleEventDescriptor *)reply
+{
+ [self quit:nil];
+}
+
- (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app
{
return YES;
@@ -666,6 +683,26 @@ static NSMenuItem *menu_add(NSMenu *menu, NSString *title, SEL action, NSString
return item;
}
+/* An NSApp someone else made quits on its own terms. Tk's terminate: only
+ * queues a Tcl event, which our loop doesn't service (and Tk would then exit
+ * without asking the windows). So its Quit menu item and the Quit Apple Event
+ * (the Dock's Quit, logout) come to our delegate. */
+static void route_quit(void)
+{
+ NSMenu *app_menu = [[[[NSApp mainMenu] itemArray] firstObject] submenu];
+ for (NSMenuItem *item in [app_menu itemArray]) {
+ if ([item action] == @selector(terminate:)) {
+ [item setTarget:g_app_delegate];
+ [item setAction:@selector(quit:)];
+ }
+ }
+ [[NSAppleEventManager sharedAppleEventManager]
+ setEventHandler:g_app_delegate
+ andSelector:@selector(quitEvent:withReply:)
+ forEventClass:'aevt'
+ andEventID:'quit'];
+}
+
static int app_init(void)
{
if (g_app_ready) {
@@ -720,10 +757,13 @@ static int app_init(void)
[window_menu release];
[bar release];
}
+ g_app_delegate = [[BUIKitAppDelegate alloc] init]; /* kept: NSApp doesn't retain it */
if ([NSApp delegate] == nil) {
- g_app_delegate = [[BUIKitAppDelegate alloc] init]; /* kept: NSApp doesn't retain it */
[NSApp setDelegate:g_app_delegate];
}
+ if (!created) {
+ route_quit();
+ }
/* GHOST: no automatic window tabbing */
[NSWindow setAllowsAutomaticWindowTabbing:NO];
if (created) {