gui/window: keep the window frame across starts, take the first click
- Options.FrameFile: the window's position, size and maximized state in a small JSON file, read at New (a frame on no connected screen is ignored, the window opens centered), written shortly after every move or resize and at Destroy. macOS: NSWindow frame (the normal frame under a zoomed one), checked against NSScreen visible frames; Windows: Get/SetWindowPlacement, checked with MonitorFromRect. - macOS: the web view is a WKWebView subclass that accepts the first mouse click, so a page in an inactive window reacts to it instead of the click only activating the window (yanks' start dialog). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c16b871db5
commit
31dbc347e8
9 files changed
+325
-13
No files matched your search
+50
-3
@@ -18,6 +18,17 @@ extern void ytGoSchemeStop(uintptr_t h, long id);
|
||||
extern void ytGoDispatch(uintptr_t fn);
|
||||
extern void ytGoClosed(uintptr_t h);
|
||||
extern void ytGoSnapshotDone(uintptr_t h, const char *err);
|
||||
extern void ytGoFrame(uintptr_t h, int x, int y, int w, int hgt, int zoomed);
|
||||
|
||||
// YTWebView takes the first mouse click in an inactive window: without
|
||||
// it the click only activates the window, so a page (a dialog shown at
|
||||
// start) looks dead until clicked twice.
|
||||
@interface YTWebView : WKWebView
|
||||
@end
|
||||
|
||||
@implementation YTWebView
|
||||
- (BOOL)acceptsFirstMouse:(NSEvent *)event { return YES; }
|
||||
@end
|
||||
|
||||
@interface YTHost : NSObject <WKScriptMessageHandler, WKURLSchemeHandler, NSWindowDelegate>
|
||||
@property (assign) uintptr_t goHandle;
|
||||
@@ -26,6 +37,7 @@ extern void ytGoSnapshotDone(uintptr_t h, const char *err);
|
||||
@property (strong) NSMutableDictionary<NSNumber *, id<WKURLSchemeTask>> *tasks;
|
||||
@property (assign) long nextTask;
|
||||
@property (assign) BOOL closed;
|
||||
@property (assign) NSRect normalFrame; // the last frame while not zoomed
|
||||
@end
|
||||
|
||||
@implementation YTHost
|
||||
@@ -67,7 +79,21 @@ extern void ytGoSnapshotDone(uintptr_t h, const char *err);
|
||||
ytGoSchemeStop(self.goHandle, key.longValue);
|
||||
}
|
||||
|
||||
// The frame is reported after every move and resize; while zoomed, the
|
||||
// normal frame underneath is kept, so un-zooming returns to it.
|
||||
- (void)reportFrame {
|
||||
if (self.closed || !self.window) return;
|
||||
BOOL zoomed = self.window.isZoomed;
|
||||
if (!zoomed) self.normalFrame = self.window.frame;
|
||||
NSRect f = self.normalFrame;
|
||||
if (f.size.width <= 0) return;
|
||||
ytGoFrame(self.goHandle, (int)f.origin.x, (int)f.origin.y, (int)f.size.width, (int)f.size.height, zoomed ? 1 : 0);
|
||||
}
|
||||
- (void)windowDidMove:(NSNotification *)n { [self reportFrame]; }
|
||||
- (void)windowDidResize:(NSNotification *)n { [self reportFrame]; }
|
||||
|
||||
- (void)windowWillClose:(NSNotification *)n {
|
||||
[self reportFrame];
|
||||
if (self.closed) return;
|
||||
ytGoClosed(self.goHandle);
|
||||
yt_stop();
|
||||
@@ -102,7 +128,19 @@ static void ensureApp(void) {
|
||||
[NSApp finishLaunching];
|
||||
}
|
||||
|
||||
void *yt_create(uintptr_t goHandle, const char *title, int width, int height, int debug, const char *scheme) {
|
||||
// onScreen: at least a 120 x 60 point piece of the frame (with its title
|
||||
// bar) is on a connected screen -- a frame saved on a display that is
|
||||
// gone is not restored off screen.
|
||||
static BOOL onScreen(NSRect f) {
|
||||
for (NSScreen *s in [NSScreen screens]) {
|
||||
NSRect i = NSIntersectionRect(f, s.visibleFrame);
|
||||
if (i.size.width >= 120 && i.size.height >= 60) return YES;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
void *yt_create(uintptr_t goHandle, const char *title, int width, int height, int debug, const char *scheme,
|
||||
int hasFrame, int fx, int fy, int fw, int fh, int zoomed) {
|
||||
@autoreleasepool {
|
||||
ensureApp();
|
||||
YTHost *host = [YTHost new];
|
||||
@@ -115,7 +153,7 @@ void *yt_create(uintptr_t goHandle, const char *title, int width, int height, in
|
||||
if (debug) [cfg.preferences setValue:@YES forKey:@"developerExtrasEnabled"];
|
||||
|
||||
NSRect frame = NSMakeRect(0, 0, width, height);
|
||||
host.webview = [[WKWebView alloc] initWithFrame:frame configuration:cfg];
|
||||
host.webview = [[YTWebView alloc] initWithFrame:frame configuration:cfg];
|
||||
if (@available(macOS 13.3, *)) { if (debug) host.webview.inspectable = YES; }
|
||||
|
||||
host.window = [[NSWindow alloc] initWithContentRect:frame
|
||||
@@ -126,9 +164,17 @@ void *yt_create(uintptr_t goHandle, const char *title, int width, int height, in
|
||||
host.window.releasedWhenClosed = NO;
|
||||
host.window.title = str(title);
|
||||
host.window.contentView = host.webview;
|
||||
NSRect saved = NSMakeRect(fx, fy, fw, fh);
|
||||
BOOL restore = hasFrame && onScreen(saved);
|
||||
if (restore) {
|
||||
[host.window setFrame:saved display:NO];
|
||||
} else {
|
||||
[host.window center];
|
||||
}
|
||||
host.normalFrame = host.window.frame;
|
||||
host.window.delegate = host;
|
||||
[host.window center];
|
||||
[host.window makeKeyAndOrderFront:nil];
|
||||
if (restore && zoomed && !host.window.isZoomed) [host.window zoom:nil];
|
||||
[NSApp activateIgnoringOtherApps:YES];
|
||||
return (__bridge_retained void *)host;
|
||||
}
|
||||
@@ -171,6 +217,7 @@ void yt_stop(void) {
|
||||
|
||||
void yt_destroy(void *h) {
|
||||
YTHost *host = (__bridge_transfer YTHost *)h; // balances __bridge_retained
|
||||
[host reportFrame]; // a window ended by Terminate, not closed by the user
|
||||
host.closed = YES;
|
||||
for (NSNumber *k in host.tasks.allKeys) {
|
||||
// Remaining tasks are failed so WebKit does not wait forever.
|
||||
|
||||
Reference in new issue
Block a user