diff --git a/internal/gui/assets/app.js b/internal/gui/assets/app.js index 943e95cb7..6904e3cc2 100644 --- a/internal/gui/assets/app.js +++ b/internal/gui/assets/app.js @@ -19201,7 +19201,7 @@ function renderSettings() { s.dock ? "on" : s.dockWindow ? "window" : "off", (v) => savePrefs({ ...keep, dock: v === "on", dockWindow: v === "window" }))); // a closed window's webview let go (#580): the app's windows alone $("#lightweightRow").hidden = web; - $("#lightweightSegs").replaceChildren(segs([["off", t("Off")], ["on", t("On")]], s.lightweight ? "on" : "off", + $("#lightweightSegs").replaceChildren(segs([["off", t("Off")], ["on", t("On")]], s.lightweight !== false ? "on" : "off", (v) => savePrefs({ ...keep, lightweight: v === "on" }))); // the display kept on too (#975): an agent recording the screen found it locked $("#keepAwakeSegs").replaceChildren(segs([["off", t("Off")], ["on", t("On")], ["display", t("Screen on too")]], @@ -22345,7 +22345,7 @@ function wbCheckinLine(r) { // prefsKeep is what the settings page sends of s, all of it each time. function prefsKeep(s) { - return { theme: s.theme, colorblind: !!s.colorblind, lang: s.lang, tray: s.tray, dock: !!s.dock, dockWindow: !!s.dockWindow, lightweight: !!s.lightweight, keepAwake: !!s.keepAwake, keepAwakeDisplay: !!s.keepAwakeDisplay, noWSLAgents: !!s.noWSLAgents, proxy: s.proxy || "", + return { theme: s.theme, colorblind: !!s.colorblind, lang: s.lang, tray: s.tray, dock: !!s.dock, dockWindow: !!s.dockWindow, lightweight: s.lightweight, keepAwake: !!s.keepAwake, keepAwakeDisplay: !!s.keepAwakeDisplay, noWSLAgents: !!s.noWSLAgents, proxy: s.proxy || "", sessionTerminal: s.sessionTerminal || "", uiFont: s.uiFont || null, codeFont: s.codeFont || null, otel: s.otel || {}, diff --git a/internal/gui/lightweight.go b/internal/gui/lightweight.go index 1b1235313..3fc82ed89 100644 --- a/internal/gui/lightweight.go +++ b/internal/gui/lightweight.go @@ -200,7 +200,7 @@ func (h *host) lighten() { <-h.ready var ticks [2]int // the main window's, the panel's for range time.Tick(lightEvery) { - on := settings.Load().Lightweight + on := settings.Load().LightweightOn() var let []*application.WebviewWindow h.winMu.Lock() application.InvokeSync(func() { diff --git a/internal/gui/tests/README.md b/internal/gui/tests/README.md index 5418bbc09..1af82f750 100644 --- a/internal/gui/tests/README.md +++ b/internal/gui/tests/README.md @@ -2158,8 +2158,9 @@ WebKit, in English and Chinese: node --test internal/gui/tests/plugin-progressive.test.cjs `lightweight.test.cjs` checks Settings → General's Lightweight mode row -(#580): off by default, On and Off saved as `lightweight` true and false -without the page moving, another setting's save keeping it, no left-border +(#580): on for a user who never chose, Off and On saved as `lightweight` +false and true without the page moving, another setting's save keeping the +choice or leaving it unchosen (no `lightweight` posted), no left-border accent, and no such row on a browser's page (`web`). English and Chinese, Chromium and WebKit, with `/api/settings` faked: node --test internal/gui/tests/lightweight.test.cjs diff --git a/internal/gui/tests/lightweight.test.cjs b/internal/gui/tests/lightweight.test.cjs index 376f13d5f..41b68f0ee 100644 --- a/internal/gui/tests/lightweight.test.cjs +++ b/internal/gui/tests/lightweight.test.cjs @@ -1,8 +1,9 @@ // Run with Node's test runner and Playwright on the module path; see README.md. // somls on #580: GUI版可否增加轻量模式,关闭面板退出webview,减少资源占用. -// Settings → General has a Lightweight mode row, off by default: On and Off -// are saved as lightweight true and false, the click moving nothing; the -// page saved again elsewhere keeps it; a browser's page (web) has no window +// Settings → General has a Lightweight mode row, on for a user who never +// chose: Off and On are saved as lightweight false and true, the click +// moving nothing; the page saved again elsewhere keeps the choice, or +// leaves it unchosen; a browser's page (web) has no window // to let go and shows no such row. In English and Chinese, with the API faked. const assert = require("node:assert/strict"); const fs = require("node:fs/promises"); @@ -48,7 +49,7 @@ async function open(engine, lang, web, posted, st, t) { const browser = await (engine === "webkit" ? webkit.launch() : chromium.launch({ channel: "chromium" })); t.after(() => browser.close()); // tall enough that Lightweight mode is over the window's foot: rows were added above it - const page = await (await browser.newContext({ viewport: { width: 900, height: 1000 } })).newPage(); + const page = await (await browser.newContext({ viewport: { width: 900, height: 1200 } })).newPage(); page.setDefaultTimeout(5000); const errors = []; page.on("pageerror", (e) => errors.push(e.message)); @@ -71,7 +72,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", assert.equal(await row.locator(".name").innerText(), w.name); assert.equal(await row.locator(".sub").innerText(), w.sub); const opt = (name) => row.locator("#lightweightSegs .opt", { hasText: name }); - assert.equal(await row.locator("#lightweightSegs .opt.on").innerText(), w.off, "off by default"); + assert.equal(await row.locator("#lightweightSegs .opt.on").innerText(), w.on, "on for a user who never chose"); const where = () => page.evaluate(() => [scrollX, scrollY, document.scrollingElement.scrollTop, ...[...document.querySelectorAll("*")].filter((e) => e.scrollTop).map((e) => e.scrollTop)]); const before = await where(); const click = async (loc) => { @@ -81,19 +82,25 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", const b = await loc.boundingBox(); await page.mouse.click(b.x + b.width / 2, b.y + b.height / 2); }; - await click(opt(w.on)); - for (let i = 0; i < 50 && !posted.length; i++) await page.waitForTimeout(50); - assert.equal(posted.at(-1)?.lightweight, true, `posted ${JSON.stringify(posted)}`); - assert.equal(st.lightweight, true); - assert.equal(await row.locator("#lightweightSegs .opt.on").innerText(), w.on); - // another setting saved keeps it on - const n = posted.length; + // another setting saved while it was never chosen leaves it unchosen, + // so the default goes on applying + let n = posted.length; await click(page.locator("#dockSegs .opt").first()).catch(() => {}); for (let i = 0; i < 20 && posted.length === n; i++) await page.waitForTimeout(50); - if (posted.length > n) assert.equal(posted.at(-1).lightweight, true, "another setting's save turned it off"); + if (posted.length > n) assert.equal("lightweight" in posted.at(-1), false, `another setting's save chose it: ${JSON.stringify(posted.at(-1))}`); await click(opt(w.off)); for (let i = 0; i < 50 && posted.at(-1)?.lightweight !== false; i++) await page.waitForTimeout(50); - assert.equal(posted.at(-1).lightweight, false); + assert.equal(posted.at(-1)?.lightweight, false, `posted ${JSON.stringify(posted)}`); + assert.equal(st.lightweight, false); + assert.equal(await row.locator("#lightweightSegs .opt.on").innerText(), w.off); + // another setting saved keeps it off + n = posted.length; + await click(page.locator("#dockSegs .opt").last()).catch(() => {}); + for (let i = 0; i < 20 && posted.length === n; i++) await page.waitForTimeout(50); + if (posted.length > n) assert.equal(posted.at(-1).lightweight, false, "another setting's save turned it on"); + await click(opt(w.on)); + for (let i = 0; i < 50 && posted.at(-1)?.lightweight !== true; i++) await page.waitForTimeout(50); + assert.equal(posted.at(-1).lightweight, true); assert.deepEqual(await where(), before, "the clicks moved the page"); // the row has no coloured stripe down its left const left = await row.evaluate((e) => getComputedStyle(e).borderLeftWidth); diff --git a/internal/gui/tests/segs-glide.test.cjs b/internal/gui/tests/segs-glide.test.cjs index 7e6e3bbeb..dee899467 100644 --- a/internal/gui/tests/segs-glide.test.cjs +++ b/internal/gui/tests/segs-glide.test.cjs @@ -132,7 +132,9 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", const errors = []; // WebKit's notice of a ResizeObserver loop is no error of the page's page.on("pageerror", (e) => { if (!/ResizeObserver loop/.test(e.message)) errors.push(e.message); }); - const st = {}, ctl = { hold: 0, refuse: false }; + // turned off by the user, so On is a glide away (it is on for one + // who never chose) + const st = { lightweight: false }, ctl = { hold: 0, refuse: false }; await page.route("**/*", serve(lang, st, ctl)); // opened on another tab: General is drawn while hidden await watchSettingsReads(page); diff --git a/internal/gui/tests/segs-press-redraw.test.cjs b/internal/gui/tests/segs-press-redraw.test.cjs index 083f37196..f0d998cb7 100644 --- a/internal/gui/tests/segs-press-redraw.test.cjs +++ b/internal/gui/tests/segs-press-redraw.test.cjs @@ -84,7 +84,8 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", for (const width of [900, 440]) { const w = words[lang]; test(`${engine} ${lang} ${width}px: an option pressed while Settings is redrawn is picked once, and nothing when dragged off`, async (t) => { - const posted = [], st = {}; + // Lightweight mode turned off by the user, so On is a pick away + const posted = [], st = { lightweight: false }; const { page, errors } = await open(engine, lang, width, "general", posted, st, t); const opt = (id, name) => page.locator(`#${id} .opt`, { hasText: new RegExp(`^${name}$`) }); const on = () => page.locator("#lightweightSegs .opt.on").innerText(); @@ -153,7 +154,7 @@ for (const engine of (process.env.BROWSER ? [process.env.BROWSER] : ["chromium", // as it lifts, the new On, and that is the only pick. Playwright can't // hold a tap in WebKit, so it is Chromium's, through its DevTools. if (engine === "chromium") test(`${engine} ${lang} ${width}px: an option tapped while Settings is redrawn is picked once, not twice`, async (t) => { - const posted = [], st = {}; + const posted = [], st = { lightweight: false }; const { page, errors } = await open(engine, lang, width, "general", posted, st, t, true); const opt = page.locator("#lightweightSegs .opt", { hasText: new RegExp(`^${w.on}$`) }); const [x, y] = mid(await reveal(page, opt)); diff --git a/internal/settings/lightweight_test.go b/internal/settings/lightweight_test.go index 0b90186e6..f84469de0 100644 --- a/internal/settings/lightweight_test.go +++ b/internal/settings/lightweight_test.go @@ -2,30 +2,39 @@ package settings import "testing" -// Lightweight mode (#580) is saved and read back, off by default, and is -// this computer's own: a sync or a restored backup keeps it as it is here. +// Lightweight mode (#580) is on for a user who never chose, and kept off +// for one who turned it off; it is saved and read back, and is this +// computer's own: a sync or a restored backup keeps it as it is here. func TestLightweightKeptAndOwn(t *testing.T) { t.Setenv("XDG_CONFIG_HOME", t.TempDir()) - if Load().Lightweight { - t.Fatal("on by default") + if !Load().LightweightOn() { + t.Fatal("off for a user who never chose") } + off, on := false, true s := Load() - s.Lightweight = true + s.Lightweight = &off if err := Save(s); err != nil { t.Fatal(err) } - if !Load().Lightweight { - t.Fatal("not kept") + if Load().LightweightOn() { + t.Fatal("turned off, but read back on") } - from := Settings{Theme: "dark"} + s.Theme = "dark" // another setting saved keeps it off + if err := Save(s); err != nil { + t.Fatal(err) + } + if Load().LightweightOn() { + t.Fatal("another setting's save turned it on") + } + from := Settings{Theme: "dark", Lightweight: &on} from.KeepOwn(Load()) - if !from.Lightweight || from.Theme != "dark" { + if from.LightweightOn() || from.Theme != "dark" { t.Fatalf("KeepOwn: %+v", from) } - from = Settings{Lightweight: true} + from = Settings{Lightweight: &off} from.KeepOwn(Settings{}) - if from.Lightweight { - t.Fatal("another computer's turned on here") + if from.Lightweight != nil { + t.Fatal("another computer's choice taken here") } } diff --git a/internal/settings/settings.go b/internal/settings/settings.go index a90bf649c..28f97edd3 100644 --- a/internal/settings/settings.go +++ b/internal/settings/settings.go @@ -359,8 +359,9 @@ type Settings struct { // Lightweight lets the webview of a window closed — the tray panel or // the main window — go once it has stayed closed a while, and makes it // again when it is opened (#580): less memory, a moment's wait. This - // computer's own (KeepOwn). - Lightweight bool `json:"lightweight,omitempty"` + // computer's own (KeepOwn). Nil is on: a user who never chose has it, + // one who turned it off keeps it off (LightweightOn). + Lightweight *bool `json:"lightweight,omitempty"` // KeepAwake keeps this computer from going to sleep by itself while // agents work through the gateway and for a while after (xiao_wang24004 // on X; internal/awake). This computer's own (KeepOwn). @@ -955,6 +956,12 @@ func CarryPerModel(in, cur *Settings) { // models that can take more. const WorkingWindow = 272000 +// LightweightOn says whether lightweight mode is on: unless the user +// turned it off. A hidden webview kept for good holds what its page drew +// while hidden, which grew ~20 MB a minute on Windows under steady +// requests (#2515). +func (s Settings) LightweightOn() bool { return s.Lightweight == nil || *s.Lightweight } + // Working is the context window an agent is told for a model with one of // n tokens (see FullContext). func (s Settings) Working(n int) int {