diff --git a/internal/gui/api.go b/internal/gui/api.go index 3653773b4..54c24252c 100644 --- a/internal/gui/api.go +++ b/internal/gui/api.go @@ -56,6 +56,9 @@ type Windows interface { // keeps up with the panel's size; false when it can't, for the page to // go on painting it itself. TintPanel(rgba [4]uint8, ms int) bool + // TintTitleBar paints the window's title bar the page's colour, where + // the system draws one (Windows); false where there is none to paint. + TintTitleBar(rgba [4]uint8, dark bool) bool // SetTextSize zooms the window's and the panel's pages to percent // (settings.TextSizes), the panel's size with them. SetTextSize(percent int) @@ -619,6 +622,11 @@ func Handler(w Windows, gw *gateway.Server) http.Handler { writeJSON(rw, map[string]bool{"ok": true}) return } + case "titlebar": + if c, _, ok := parseTint(r.URL.Query()); ok && w.TintTitleBar(c, r.URL.Query().Get("dark") == "1") { + writeJSON(rw, map[string]bool{"ok": true}) + return + } } rw.WriteHeader(http.StatusNoContent) }) diff --git a/internal/gui/app.go b/internal/gui/app.go index 829f426e8..7801ef012 100644 --- a/internal/gui/app.go +++ b/internal/gui/app.go @@ -3,6 +3,7 @@ package gui import ( + "cmp" "crypto/sha256" "embed" "encoding/hex" @@ -288,6 +289,9 @@ func Run(version string, showMain bool, link string) error { } // and no smaller than its page's least at the text size minW, minH := windowMin(zoom, 0, 0) + // Windows' title bar in the page's colour from the first frame; the + // page keeps it so as its theme changes (TintTitleBar) + winOpts, winBg := windowChrome(cmp.Or(os.Getenv("MAGPIE_THEME"), settings.Load().Theme)) h.main = h.app.Window.NewWithOptions(application.WebviewWindowOptions{ Name: "main", Title: "magpie", @@ -303,6 +307,8 @@ func Run(version string, showMain bool, link string) error { // it, tabs included; the header marks what drags instead TitleBar: application.MacTitleBarHiddenInset, }, + Windows: winOpts, + BackgroundColour: winBg, }) // A resize is kept once it settles; a maximised or full-screen window // is the screen's size, not one the user gave it. diff --git a/internal/gui/assets/app.css b/internal/gui/assets/app.css index 624584241..d2d50af38 100644 --- a/internal/gui/assets/app.css +++ b/internal/gui/assets/app.css @@ -318,8 +318,17 @@ body.window .ag-handle { width: 28px; height: 28px; } .ag-fix.busy svg { animation: spin .8s linear infinite; } /* the agent's CLI on its name's line (#202): its version faint, and a quiet pill that updates it when a newer one is out — the row keeps its height */ -body.window .row.agent .who { display: flex; align-items: center; gap: 7px; } -body.window .row.agent .who .name { flex: 0 1 auto; } +body.window .row.agent .who { display: flex; align-items: center; gap: 7px; container-type: inline-size; } +/* the name is the last to give way: where the row is tight the update + pill keeps only its arrow, then the version goes, and only then is the + name cut (it was "C." beside a full pill at the window's first width) */ +body.window .row.agent .who .name { flex: 0 0 auto; max-width: 100%; } +body.window .row.agent .who .ag-cli { flex: 0 1 auto; overflow: hidden; } +@container (max-width: 250px) { + body.window .row.agent .ag-up { padding: 0 6px; } + body.window .row.agent .ag-up > span { display: none; } +} +@container (max-width: 150px) { body.window .row.agent .ag-ver { display: none; } } .ag-cli { display: inline-flex; align-items: center; gap: 6px; flex: none; min-width: 0; } .ag-cli:empty { display: none; } .ag-ver { font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; cursor: default; } diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 6138da697..0aaec92b1 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -562,7 +562,10 @@ function cliTag(a) { function paintCLIButton(b, c, busy) { b.classList.toggle("busy", busy); b.setAttribute("aria-busy", String(busy)); - b.replaceChildren(svg(busy ? CLI_SPIN : CLI_UP, 11, 1.8), el("span", "", busy ? t("Updating…") : t("Update to {v}", { v: c.latest }))); + const text = busy ? t("Updating…") : t("Update to {v}", { v: c.latest }); + // named even where a tight row shows only the arrow + b.setAttribute("aria-label", text); + b.replaceChildren(svg(busy ? CLI_SPIN : CLI_UP, 11, 1.8), el("span", "", text)); } // paintCLI draws an agent's CLI again where it is, the row left as it is @@ -962,6 +965,32 @@ if (mode === "panel") { matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => tintPanel(450)); } +// tintTitleBar paints Windows' own title bar the window's page colour, dark +// or light as the page is, so the bar and the page are one surface as on the +// Mac, not a grey strip above it (light over a dark page). wait lets a change +// of theme finish fading first: the colour read is where it ends. +function tintTitleBar(wait = 0) { + if (mode !== "window" || web || !document.documentElement.classList.contains("win")) return; + clearTimeout(tintTitleBar.t); + tintTitleBar.t = setTimeout(async () => { + const c = document.createElement("canvas").getContext("2d", { willReadFrequently: true }); + c.fillStyle = "#fff"; + c.fillRect(0, 0, 1, 1); + c.fillStyle = getComputedStyle(document.body).backgroundColor; + c.fillRect(0, 0, 1, 1); + const [r, g, b] = c.getImageData(0, 0, 1, 1).data; + const rgba = [r, g, b, 255].join(","); + if (rgba === tintTitleBar.last) return; + tintTitleBar.last = rgba; + // the bar's own text and buttons light on a dark page, dark on a light one + const dark = 0.2126 * r + 0.7152 * g + 0.0722 * b < 128; + try { await api("window/titlebar?c=" + rgba + "&dark=" + (dark ? 1 : 0), {}); } catch { tintTitleBar.last = null; } + }, wait); +} +if (mode === "window") { + matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => tintTitleBar(460)); +} + // extra is room about to be taken (or given back), e.g. by agents unrolling; // glide moves the panel's edge there over time instead of at once. // The panel is as tall as its tallest tab, not the one showing, so it keeps @@ -998,6 +1027,7 @@ async function load() { // the library may have drawn itself before the saved language was known if (applyPrefs(state.settings, state.fx) && view === "library") window.loadLibrary?.(); tintPanel(); + tintTitleBar(); renderAgents(); loadCLIs(); // after the rows, never holding them up if (mode === "panel") { renderPanelQuota(); loadQuotas(); } @@ -7540,7 +7570,7 @@ function applyPrefs(s, rate) { applyPrefs.t = setTimeout(() => root.classList.remove("theming"), 450); } if (want) root.dataset.theme = want; else delete root.dataset.theme; - if (applyPrefs.ready) tintPanel(450); + if (applyPrefs.ready) { tintPanel(450); tintTitleBar(460); } } } applyPrefs.ready = true; diff --git a/internal/gui/dev_on.go b/internal/gui/dev_on.go index ffa646258..b73ecbced 100644 --- a/internal/gui/dev_on.go +++ b/internal/gui/dev_on.go @@ -230,6 +230,13 @@ func (c remoteWindows) TintPanel(rgba [4]uint8, ms int) bool { q := url.Values{"c": {fmt.Sprintf("%d,%d,%d,%d", rgba[0], rgba[1], rgba[2], rgba[3])}, "ms": {strconv.Itoa(ms)}} return c.post("tint", url.Values{"arg": {q.Encode()}}) == "ok" } +func (c remoteWindows) TintTitleBar(rgba [4]uint8, dark bool) bool { + q := url.Values{"c": {fmt.Sprintf("%d,%d,%d,%d", rgba[0], rgba[1], rgba[2], rgba[3])}} + if dark { + q.Set("dark", "1") + } + return c.post("titlebar", url.Values{"arg": {q.Encode()}}) == "ok" +} func (c remoteWindows) SetTextSize(percent int) { c.post("textsize", url.Values{"arg": {strconv.Itoa(percent)}}) } @@ -308,6 +315,12 @@ func devShell(h *host) http.Handler { rw.Write([]byte("ok")) return } + case "titlebar": + q, _ := url.ParseQuery(arg) + if c, _, ok := parseTint(q); ok && h.TintTitleBar(c, q.Get("dark") == "1") { + rw.Write([]byte("ok")) + return + } } rw.WriteHeader(http.StatusNoContent) }) diff --git a/internal/gui/tests/cli-update.test.cjs b/internal/gui/tests/cli-update.test.cjs index eed0c46c3..47be66182 100644 --- a/internal/gui/tests/cli-update.test.cjs +++ b/internal/gui/tests/cli-update.test.cjs @@ -147,6 +147,33 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.match(await page.locator(`${row("codex")} .ag-up`).getAttribute("title"), /通过 codex update 更新/); }); + await t.test("a tight row cuts the pill and the version before the name", async () => { + const page = await open("zh", []); + const look = () => page.evaluate((r) => { + const q = (s) => document.querySelector(r + " " + s); + const shown = (e) => !!e && e.getBoundingClientRect().width > 0 && getComputedStyle(e).display !== "none"; + const name = q(".name"), up = q(".ag-up"), words = q(".ag-up > span"); + return { cut: name.scrollWidth > name.clientWidth + 1, up: shown(up), words: shown(words), ver: shown(q(".ag-ver")), label: up?.getAttribute("aria-label") }; + }, row("codex")); + // wide: everything + assert.deepEqual(await look(), { cut: false, up: true, words: true, ver: true, label: "更新到 0.159.0" }); + // the window at its first width, or a little less: the arrow alone + // (the name was "C." here) + await page.setViewportSize({ width: 620, height: 520 }); + await page.waitForTimeout(100); + const tight = await look(); + assert.equal(tight.cut, false, "the name was cut before the pill"); + assert.equal(tight.up, true); + assert.equal(tight.words, false, "the pill kept its words in a tight row"); + assert.equal(tight.label, "更新到 0.159.0"); + // tighter still: the version goes too, the name last + await page.setViewportSize({ width: 540, height: 520 }); + await page.waitForTimeout(100); + const tighter = await look(); + assert.equal(tighter.cut, false, "the name was cut before the version"); + assert.equal(tighter.ver, false); + }); + assert.deepEqual(errors, []); }); } diff --git a/internal/gui/titletint_other.go b/internal/gui/titletint_other.go new file mode 100644 index 000000000..66f112ade --- /dev/null +++ b/internal/gui/titletint_other.go @@ -0,0 +1,13 @@ +//go:build !windows && !nogui + +package gui + +import "github.com/wailsapp/wails/v3/pkg/application" + +// TintTitleBar: the Mac's title bar is hidden in the page, and Linux's is +// the window manager's. +func (h *host) TintTitleBar([4]uint8, bool) bool { return false } + +func windowChrome(string) (application.WindowsWindow, application.RGBA) { + return application.WindowsWindow{}, application.RGBA{} +} diff --git a/internal/gui/titletint_windows.go b/internal/gui/titletint_windows.go new file mode 100644 index 000000000..cecd66ead --- /dev/null +++ b/internal/gui/titletint_windows.go @@ -0,0 +1,52 @@ +//go:build windows && !nogui + +package gui + +import ( + "github.com/wailsapp/wails/v3/pkg/application" + "github.com/wailsapp/wails/v3/pkg/w32" +) + +// TintTitleBar paints the window's title bar the page's own colour, dark +// or light as the page is, so the bar and the page read as one surface as +// the Mac's hidden title bar does; Windows' grey bar sat above the page as +// a strip of its own, and stayed light over a page set to dark. +func (h *host) TintTitleBar(c [4]uint8, dark bool) bool { + w := h.main + if w == nil || !w32.SupportsCustomThemes() { + return false + } + application.InvokeSync(func() { + hwnd := uintptr(w.NativeWindow()) + if hwnd == 0 { + return + } + w32.SetTheme(hwnd, dark) + w32.SetTitleBarColour(hwnd, uint32(c[0])|uint32(c[1])<<8|uint32(c[2])<<16) + // what shows past the page's edge while the window is resized + w.SetBackgroundColour(application.NewRGB(c[0], c[1], c[2])) + }) + return true +} + +// windowChrome is the window's title bar and background before its page +// has said what colour it is: the page's own --bg for the theme it will +// open in. +func windowChrome(theme string) (application.WindowsWindow, application.RGBA) { + dark := theme == "dark" || theme != "light" && w32.IsCurrentlyDarkMode() + light, night := application.NewRGBPtr(0xf4, 0xf4, 0xf6), application.NewRGBPtr(0x1a, 0x1a, 0x1e) + opts := application.WindowsWindow{ + Theme: application.Light, + CustomTheme: application.ThemeSettings{ + LightModeActive: &application.WindowTheme{TitleBarColour: light}, + LightModeInactive: &application.WindowTheme{TitleBarColour: light}, + DarkModeActive: &application.WindowTheme{TitleBarColour: night}, + DarkModeInactive: &application.WindowTheme{TitleBarColour: night}, + }, + } + bg := application.NewRGB(0xf4, 0xf4, 0xf6) + if dark { + opts.Theme, bg = application.Dark, application.NewRGB(0x1a, 0x1a, 0x1e) + } + return opts, bg +} diff --git a/internal/gui/web.go b/internal/gui/web.go index 5d443d0f1..854cf79a3 100644 --- a/internal/gui/web.go +++ b/internal/gui/web.go @@ -26,14 +26,15 @@ import ( // webHost is the Windows a browser tab stands for. type webHost struct{ quit func() } -func (webHost) HidePanel() {} -func (webHost) ShowMain(string) {} -func (h webHost) Quit() { h.quit() } -func (webHost) OpenURL(string) {} // the page opens links itself -func (webHost) Copy(string) bool { return false } -func (webHost) FitPanel(int, Glide) {} -func (webHost) TintPanel([4]uint8, int) bool { return false } -func (webHost) SetTextSize(int) {} // the browser zooms its own tab +func (webHost) HidePanel() {} +func (webHost) ShowMain(string) {} +func (h webHost) Quit() { h.quit() } +func (webHost) OpenURL(string) {} // the page opens links itself +func (webHost) Copy(string) bool { return false } +func (webHost) FitPanel(int, Glide) {} +func (webHost) TintPanel([4]uint8, int) bool { return false } +func (webHost) TintTitleBar([4]uint8, bool) bool { return false } +func (webHost) SetTextSize(int) {} // the browser zooms its own tab func (webHost) OpenFolder(path string) error { return errors.New("in the browser magpie can't open folders: it is " + tilde(path)) }