Files
magpie/internal/gui/glide_darwin.go
T
yetone 575e694ed7 settings: a Text size preference (文字大小) that makes everything in magpie's windows larger, 100/110/125/150%, with Ctrl/Cmd + = − 0 in the window and the tray panel, and Windows' text drawn in its own UI faces (Discord, Clarence Dan: the font size is hard on the eyes on Windows) — Settings → Preferences gains a Text size row under Language whose pick posts to a new POST /api/settings/text-size (saved as settings.TextSize, validated against 100/110/125/150, 0 read as 100, and kept by the Settings page's general save, which never sends it). The size is each webview's own zoom, as a browser zooms, not CSS zoom: CSS zoom leaves getBoundingClientRect and clientX in zoomed pixels while offsets, scrollTop and 100vh are not, and app.js, routing.js and library.js place menus, popovers and the panel's height by those measures; under the webview's zoom the page is laid out again in fewer, larger CSS pixels and every measure agrees. Windows and Linux use Wails' SetZoom (WebView2's zoom factor, WebKitGTK's zoom level; the Zoom window option at creation, so the pages open at the saved size), the Mac WKWebView.pageZoom through cgo (macOS 11+), since Wails' Mac zoom is magnification, which lays nothing out again. None is under 100% because Wails clamps SetZoom below 1 on Windows and Linux. The windows grow around the pages: the panel's width and the height its page asks for (still in CSS pixels) are multiplied by the zoom, capped at the screen's work area (textsize.go panelFrame), and resized at once when the size changes; the main window's minimum is 560×420 CSS pixels at the size, capped by the screen, so a window smaller than that grows. Runtime changes wait for the app to be ready (whenReady), and at startup the Mac applies the zoom once its webviews exist. The Mac header, drawn under the native traffic lights which do not grow, divides its height, left padding and the modal's top by --zoom (set from boot.js before the first paint and from the settings), so it stays 50 points tall and level with them. The shortcuts are Cmd on the Mac and Ctrl elsewhere (the other modifier does nothing), step through the sizes and 0 goes back to 100%, with a "Text size 125%" toast; in magpie web the row is hidden and the keys are left to the browser's own zoom. On Windows the page names its faces (html.win): Segoe UI Variable Text, Segoe UI, then Microsoft YaHei UI for Chinese rather than whatever WebView2 falls back to; the Mac and Linux stack is unchanged, and there is no automatic size bump. New strings in en and zh. Tested: internal/gui/tests/text-size.test.cjs in Chromium and WebKit, en and zh, light and dark — a pick posts and does not scroll the settings page, stays picked after a reload with --zoom from boot.js, the Settings page's other saves leave it, the shortcuts step and reset in the window and the panel, and at 150% (the smallest window, 840×630 points, as 560×420 CSS pixels at device scale 1.5) no view, the header or the panel runs off to the side, the Mac header is 50 points; screenshots at 100% and 150% looked at. currency.test.cjs's viewport is 480 tall instead of 420, as settings-groups' is: the new row put the Currency control under the footer, where WebKit's hover could not reach it. Go: TestTextSizeFrames (panel and window sizes) and TestTextSizeSetting (the endpoint saves, zooms the windows, rejects 133, survives the general save, boot.js carries it) in internal/gui, and the settings round trip. go build -tags nogui (and GOOS=linux, GOOS=windows, plain darwin), go test -tags nogui ./... (TestUserPath flaked once under load and passes alone) and the whole GUI suite with --test-concurrency=2 pass. Not tried in the real Wails window on any platform: the Mac pageZoom, WebView2's zoom factor with the resized panel and the Windows font are untested there.
2026-09-30 02:51:17 +08:00

161 lines
5.7 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 "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 {
w := 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))
}
}