Files
magpie/internal/gui/glide_darwin.go
T
yetone 4a2f538c61 gui: lightweight mode, off by default, lets a closed panel's or window's webview go and makes it again when it is opened (somls on #580: GUI版可否增加轻量模式,关闭面板退出webview,减少资源占用) — closing the tray panel or the main window only hid it, so each kept its page and, on a Mac, its WebKit WebContent process (about 65 MB each at start, 95–100 MB after use) for as long as magpie ran. Settings → General now has Lightweight mode (轻量模式), shown in the app only (not on a browser's page) and kept as this computer's own through a sync or a restored backup (KeepOwn): with it on, a window found closed on two looks in a row 30 s apart (lightStep) is let go 30–60 s after it was closed, so a panel opened again soon after is still the same one at once. Letting go happens on the main thread under a new h.winMu: the window field is set to nil, the panel is unhooked from the tray icon (AttachWindow(nil); left click goes through magpie's own OnClick), and the main window is marked gone so its closing hook, which otherwise cancels a close and hides, lets it close; on the Mac the WKWebView is taken out of the window first (stopLoading, delegates and script handlers cleared, removed from its superview), since Wails' NSWindow close alone left the WebContent process alive for minutes. Opening it again (the tray icon, ShowMain, an import link, the Dock) makes the window anew with the options it was started with (makeMain/makePanel, moved out of Run with their hooks), its text size and title bar or Hyprland name (madeAgain), on Windows shown only once its page has loaded; every other path that reached h.main or h.panel (fit, tint, zoom, hide, glide, the Windows title-bar tint, the Hyprland click watch, the folder picker) now goes through the main thread and does nothing to a window let go; Linux keeps running with no window left (DisableQuitOnLastWindowClosed). Measured in a sandbox build under a temp HOME on other ports: started in the tray with the setting on, two WebContent processes of ~65 MB each; about a minute later both windows were let go and both processes ended (GPU and Networking, ~56 MB, are shared and stay; magpie's own process is unchanged), so the saving is ~130 MB idle and up to ~200 MB after use; the main window and the panel were then opened again with their pages, tinted and at the tray icon, closed, let go a second time, and magpie quit cleanly, with fit/tint/zoom calls every 0.2 s through a release crashing nothing. Windows and Linux build but were not run. TestLightStep and TestLightweightKeptAndOwn are new; lightweight.test.cjs (WebKit, en and zh: the row, off by default, On/Off posted as lightweight true/false, another setting's save keeping it, no scroll, no left-border accent, no row on a browser's page) is new and passes, as do click-scroll, settings-sections and searcher-pick. go vet ./internal/..., go build (darwin GUI), go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-03 00:24:49 +08:00

174 lines
5.9 KiB
Go

//go:build !nogui
package gui
/*
#cgo CFLAGS: -x objective-c
#cgo LDFLAGS: -framework Cocoa -framework QuartzCore -framework WebKit
#import <Cocoa/Cocoa.h>
#import <QuartzCore/QuartzCore.h>
#import <WebKit/WebKit.h>
// The text size is the webview's page zoom, Safari's Cmd-+: the page is laid
// out again in larger CSS pixels. Wails' own zoom on the Mac is the
// webview's magnification, a pinch that lays nothing out again and scrolls
// the page sideways.
static void setPageZoom(void *w, double z) {
NSWindow *win = (NSWindow *)w;
dispatch_async(dispatch_get_main_queue(), ^{
if (![win respondsToSelector:@selector(webView)]) return;
id web = [win performSelector:@selector(webView)];
if (![web isKindOfClass:[WKWebView class]]) return;
if (@available(macOS 11.0, *)) {
((WKWebView *)web).pageZoom = z;
}
});
}
// The panel hangs from the menu bar, so its top edge stays where it is and
// the bottom moves; the system animates the frame on its own display clock.
static void glidePanel(void *w, int height, int ms, double x1, double y1, double x2, double y2) {
NSWindow *win = (NSWindow *)w;
dispatch_async(dispatch_get_main_queue(), ^{
NSRect f = win.frame;
NSRect to = NSMakeRect(f.origin.x, NSMaxY(f) - height, f.size.width, height);
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *ctx) {
ctx.duration = ms / 1000.0;
ctx.timingFunction = [CAMediaTimingFunction functionWithControlPoints:x1 :y1 :x2 :y2];
ctx.allowsImplicitAnimation = YES;
[[win animator] setFrame:to display:YES];
} completionHandler:nil];
});
}
// The page is drawn a frame or two after the panel grows, and until then
// WebKit fills the new edge with its own dark background. So the webview
// draws no background of its own (a key WebKit has long honoured, though not
// a public one) and the page's colour is painted in a view under it, which the
// system sizes along with the panel: the new edge shows that colour until the
// page catches up. Says 0 when the webview can't be made to draw none, and
// the page keeps painting its own.
static int tintPanel(void *w, int r, int g, int b, int a, int ms) {
NSWindow *win = (NSWindow *)w;
if (![win respondsToSelector:@selector(webView)]) return 0;
__block int ok = 0;
dispatch_sync(dispatch_get_main_queue(), ^{
NSView *web = [win performSelector:@selector(webView)];
NSView *host = web.superview;
if (host == nil) return;
@try {
[web setValue:@NO forKey:@"drawsBackground"];
} @catch (NSException *e) {
return;
}
NSView *tint = nil;
for (NSView *v in host.subviews) {
if ([v.identifier isEqualToString:@"magpie-tint"]) tint = v;
}
CGColorRef colour = CGColorCreateSRGB(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
if (tint == nil) {
tint = [[NSView alloc] initWithFrame:host.bounds];
tint.identifier = @"magpie-tint";
tint.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
// a layer of its own, which AppKit leaves alone
tint.layer = [CALayer layer];
tint.wantsLayer = YES;
tint.layer.cornerRadius = 12;
tint.layer.masksToBounds = YES;
tint.layer.backgroundColor = colour;
[host addSubview:tint positioned:NSWindowBelow relativeTo:web];
[tint release];
} else {
// a change of theme fades as the page's own colours do
[CATransaction begin];
CABasicAnimation *fade = [CABasicAnimation animationWithKeyPath:@"backgroundColor"];
fade.fromValue = (id)tint.layer.backgroundColor;
fade.toValue = (__bridge id)colour;
fade.duration = ms / 1000.0;
fade.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
tint.layer.backgroundColor = colour;
if (ms > 0) [tint.layer addAnimation:fade forKey:@"tint"];
[CATransaction commit];
}
CGColorRelease(colour);
ok = 1;
});
return ok;
}
// A Dock icon is the Regular activation policy, none is Accessory. Leaving
// the Dock deactivates the app, so with front set it is brought back to the
// front after, with the window the Settings page is in; a window just closed
// leaves it where it is.
static void setDock(int on, int front) {
dispatch_async(dispatch_get_main_queue(), ^{
NSApplicationActivationPolicy p = on ? NSApplicationActivationPolicyRegular : NSApplicationActivationPolicyAccessory;
if ([NSApp activationPolicy] == p) return;
[NSApp setActivationPolicy:p];
if (front) [NSApp activateIgnoringOtherApps:YES];
});
}
*/
import "C"
import (
"unsafe"
"github.com/wailsapp/wails/v3/pkg/application"
)
// glidePanel moves the shown panel to height, its top edge held under the
// menu bar. It says false when it can't, for the caller to size it plainly.
func (h *host) glidePanel(height int, g Glide) bool {
w := h.panel.NativeWindow()
if w == nil {
return false
}
c := g.Curve
C.glidePanel(w, C.int(height), C.int(g.MS), C.double(c[0]), C.double(c[1]), C.double(c[2]), C.double(c[3]))
return true
}
// TintPanel paints the panel's tint under the page, fading to it over ms.
func (h *host) TintPanel(c [4]uint8, ms int) bool {
// tintPanel waits for the main thread, so it can't run on it; winMu
// keeps lightweight mode from letting the panel go meanwhile
h.winMu.Lock()
defer h.winMu.Unlock()
w := application.InvokeSyncWithResult(func() unsafe.Pointer {
if h.panel == nil {
return nil
}
return h.panel.NativeWindow()
})
if w == nil {
return false
}
return C.tintPanel(w, C.int(c[0]), C.int(c[1]), C.int(c[2]), C.int(c[3]), C.int(ms)) == 1
}
func dockPolicy(on bool) application.ActivationPolicy {
if on {
return application.ActivationPolicyRegular
}
return application.ActivationPolicyAccessory
}
// setDock shows magpie in the Dock or takes it out, at once, bringing it to
// the front after when front.
func setDock(on, front bool) { C.setDock(C.int(boolInt(on)), C.int(boolInt(front))) }
func boolInt(b bool) int {
if b {
return 1
}
return 0
}
// setPageZoom zooms w's page to z, the text size.
func setPageZoom(w *application.WebviewWindow, z float64) {
if p := w.NativeWindow(); p != nil {
C.setPageZoom(p, C.double(z))
}
}