settings: lightweight mode is on for a user who never chose it, and off for one who turned it off

The owner: 改成默认开,只对没设过的用户. A webview kept for good holds what
its page drew while hidden, which grew ~20 MB a minute on Windows under
steady requests (#2515); lightweight mode lets it go.

- settings.Lightweight is a *bool: nil (never chosen) is on, false is
  off, true is on. LightweightOn() says which; gui/lightweight.go reads
  it. KeepOwn still keeps this computer's value on a sync or a restore.
- app.js shows On unless lightweight is false, and the prefs save sends
  s.lightweight as it is: unchosen stays out of the JSON, so saving
  another setting doesn't choose it.

Caveat: until now Off was a bool with omitempty, so it was never stored.
A user who chose Off before this can't be told from one who never chose,
and gets On. Only choices made from this release on are kept.

Tests:
- settings TestLightweightKeptAndOwn: on when unset, Off saved reads back
  off, another save keeps it off, KeepOwn keeps this computer's Off over
  the other's On, and an empty one stays unset. Broken by hand
  (`!= nil && *`) it fails "off for a user who never chose".
- lightweight.test.cjs: On shown for a user who never chose; another
  setting's save posts no lightweight; Off posts false and shows Off;
  another save keeps false; On posts true; nothing scrolls. Its viewport
  is 1200 tall: rows added above had put Off under the window's foot
  (y≈1008 of 1000), where the mouse click did nothing.
- segs-glide and segs-press-redraw start from a user who turned it off,
  so On is still a pick away; README's lightweight line says the new
  default.

Run at this commit's parent (the #2683 test fix on origin/main
f73378365), chromium 1234 and webkit 2336, playwright 1.62.0:
- lightweight, segs-glide, segs-press-redraw, settings-general
  (--test-concurrency=1): 90 of 90. These are every GUI file that reads
  lightweight (grep); no others reach it.
- go test -timeout 45m ./internal/settings ./internal/gui: ok (gui 554s).
- gofmt -l internal/: nothing. GOOS=linux and GOOS=windows go vet -tags
  nogui ./...: clean.

Not tried: the real app's webview being let go after this change on
Windows or Linux; the release path is the same as with On chosen before.
This commit is contained in:
yetone
2026-10-20 20:23:50 +08:00
parent 031715d5e4
commit 28bec1507c
8 changed files with 63 additions and 36 deletions
+2 -2
View File
@@ -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 || {},
+1 -1
View File
@@ -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() {
+3 -2
View File
@@ -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
+21 -14
View File
@@ -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);
+3 -1
View File
@@ -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);
@@ -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));
+21 -12
View File
@@ -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")
}
}
+9 -2
View File
@@ -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 {