From 91d37aa4bfc0c592dd5979d37d619310bd5dd511 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Fri, 20 Nov 2026 22:30:34 +0800 Subject: [PATCH] fix(templates): fit File and Code Templates in narrow settings (#4805) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Settings › Editor › File and Code Templates uses the shared compact tabs and a resizable list / editor split that keeps the editor usable in narrow settings windows. --- e2e/fileTemplateLayout.spec.ts | 67 +++ e2e/settingsPaneLayout.spec.ts | 13 - .../settings/FileTemplatesPage.module.css | 98 +++++ .../templates/settings/FileTemplatesPage.tsx | 408 ++++++++++-------- src/platform/locales/text/en.json | 1 + src/platform/locales/text/zh.json | 1 + 6 files changed, 385 insertions(+), 203 deletions(-) create mode 100644 e2e/fileTemplateLayout.spec.ts create mode 100644 src/features/templates/settings/FileTemplatesPage.module.css diff --git a/e2e/fileTemplateLayout.spec.ts b/e2e/fileTemplateLayout.spec.ts new file mode 100644 index 000000000..5fba6ac76 --- /dev/null +++ b/e2e/fileTemplateLayout.spec.ts @@ -0,0 +1,67 @@ +import { expect, test } from "./fixtures"; + +test.beforeEach(async ({ workbench: page }) => { + await page.keyboard.press("Meta+,"); + await expect(page.getByRole("dialog", { name: "Settings", exact: true })).toBeVisible(); + await page.evaluate(async () => { + const path = "/src/features/settings/model/staging.ts"; + (await import(/* @vite-ignore */ path)).selectPage("editor.fileTemplates"); + }); +}); + +test("file template fields and editors fit in narrow settings", async ({ workbench: page }) => { + await page.setViewportSize({ width: 520, height: 760 }); + const tabs = page.getByRole("tablist", { name: "Template kinds" }); + const name = page.getByRole("textbox", { name: "Template name" }); + const extension = page.getByRole("textbox", { name: "Extension" }); + const style = page.getByRole("button", { name: "File name style" }); + const body = page.getByRole("textbox", { name: "Template body", exact: true }); + + await expect(body).toBeInViewport({ ratio: 0.98 }); + expect((await body.boundingBox())!.width).toBeGreaterThan(150); + await expect(extension).toBeInViewport({ ratio: 0.98 }); + await expect(style).toBeInViewport({ ratio: 0.98 }); + expect((await extension.boundingBox())!.y).toBeGreaterThan((await name.boundingBox())!.y); + expect((await style.boundingBox())!.y).toBeGreaterThan((await extension.boundingBox())!.y); + await style.click(); + await expect(page.getByRole("option", { name: "PascalCase" })).toBeVisible(); + await page.keyboard.press("Escape"); + + for (const [tab, editorName] of [ + ["Includes", "Include body"], + ["Code", "Code template body"], + ]) { + await tabs.getByRole("tab", { name: tab, exact: true }).click(); + await expect(page.getByRole("textbox", { name: editorName, exact: true })).toBeInViewport({ ratio: 0.98 }); + } +}); + +test("file template tabs share a remembered, keyboard-adjustable divider", async ({ workbench: page }) => { + await page.setViewportSize({ width: 1100, height: 760 }); + const tabs = page.getByRole("tablist", { name: "Template kinds" }); + const name = page.getByRole("textbox", { name: "Template name" }); + const extension = page.getByRole("textbox", { name: "Extension" }); + const divider = page.getByRole("separator", { name: "Resize file template panes" }); + const width = async (listName: string) => + (await page.getByRole("listbox", { name: listName, exact: true }).boundingBox())!.width; + + await expect(divider).toBeVisible(); + expect((await extension.boundingBox())!.y).toBe((await name.boundingBox())!.y); + const initial = await width("File templates"); + await divider.press("ArrowRight"); + await expect.poll(() => width("File templates")).toBeGreaterThan(initial + 10); + const resized = await width("File templates"); + + await tabs.getByRole("tab", { name: "Files", exact: true }).press("ArrowRight"); + await expect(tabs.getByRole("tab", { name: "Includes", exact: true })).toBeFocused(); + await expect.poll(() => width("Includes")).toBeCloseTo(resized, 0); + await page.getByRole("button", { name: "Add Include" }).click(); + const include = page.getByRole("textbox", { name: "Include body" }); + await include.fill("// Shared header"); + + await tabs.getByRole("tab", { name: "Code", exact: true }).click(); + await expect.poll(() => width("Code templates")).toBeCloseTo(resized, 0); + await tabs.getByRole("tab", { name: "Includes", exact: true }).click(); + await page.getByRole("option", { name: "New Include", exact: true }).click(); + await expect(include).toHaveValue("// Shared header"); +}); diff --git a/e2e/settingsPaneLayout.spec.ts b/e2e/settingsPaneLayout.spec.ts index 5a075adb3..ca693e8b4 100644 --- a/e2e/settingsPaneLayout.spec.ts +++ b/e2e/settingsPaneLayout.spec.ts @@ -69,19 +69,6 @@ test("every color scheme page keeps its attribute tree visible", async ({ workbe } }); -test("File and Code Templates keeps its editor reachable beside the template list", async ({ workbench: page }) => { - await page.keyboard.press("Meta+,"); - await expect(page.getByRole("dialog", { name: "Settings", exact: true })).toBeVisible(); - await page.evaluate(async () => { - const path = "/src/features/settings/model/staging.ts"; - (await import(/* @vite-ignore */ path)).selectPage("editor.fileTemplates"); - }); - await page.setViewportSize({ width: 520, height: 760 }); - const editor = page.locator("[class*='split'] > [class*='editor']").first(); - await expect(editor).toBeAttached(); - expect((await editor.boundingBox())!.width).toBeGreaterThan(200); -}); - test("Live Templates gives the editor the full settings pane width and keeps variable fields reachable", async ({ workbench: page, }) => { diff --git a/src/features/templates/settings/FileTemplatesPage.module.css b/src/features/templates/settings/FileTemplatesPage.module.css new file mode 100644 index 000000000..2f44c0c72 --- /dev/null +++ b/src/features/templates/settings/FileTemplatesPage.module.css @@ -0,0 +1,98 @@ +.scheme { + flex-wrap: wrap; +} + +.tabPanel { + display: flex; + flex: 1; + min-height: 0; + min-width: 0; +} + +.split { + flex: 1; + min-height: 280px; + border: 1px solid var(--border-strong); + border-radius: var(--radius-small); +} + +.list { + display: flex; + flex-direction: column; +} + +.item { + padding-inline: var(--space-2); +} + +.itemIcon { + flex: none; +} + +.itemName { + overflow: hidden; + text-overflow: ellipsis; +} + +.editor { + container: file-template-editor / inline-size; + overflow-wrap: anywhere; +} + +.field { + display: flex; + align-items: center; + gap: var(--space-2); + min-width: 0; +} + +.field > span:first-child { + width: min(92px, 34%); +} + +.identity { + display: grid; + grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); + gap: var(--space-2) var(--space-4); +} + +.extension > span:first-child { + width: auto; + max-width: 58%; +} + +.kind { + max-width: 100%; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; +} + +.text { + display: flex; + flex: 1 0 auto; + flex-direction: column; + gap: var(--space-1); + min-width: 0; + color: var(--fg-muted); +} + +.text textarea { + flex: 1; + field-sizing: fixed; +} + +.notes { + padding-top: var(--space-2); + border-top: 1px solid var(--border); +} + +@container file-template-editor (width < 400px) { + .identity { + grid-template-columns: minmax(0, 1fr); + } + + .extension > span:first-child { + width: min(92px, 34%); + } +} diff --git a/src/features/templates/settings/FileTemplatesPage.tsx b/src/features/templates/settings/FileTemplatesPage.tsx index a91859efa..c190e3295 100644 --- a/src/features/templates/settings/FileTemplatesPage.tsx +++ b/src/features/templates/settings/FileTemplatesPage.tsx @@ -1,8 +1,8 @@ /** Settings › Editor › File and Code Templates: Files, Includes and Code tabs per scheme. */ -import { useEffect, useState } from "react"; +import { useEffect, useId, useState, type ReactNode } from "react"; import clsx from "clsx"; import { tx, useTx } from "@/platform/i18n"; -import { Button, Checkbox, Icon, IconButton, Input, Select, T } from "@/ui"; +import { Button, Checkbox, Icon, IconButton, Input, Select, SplitGroup, SplitHandle, SplitPanel, T, Tabs } from "@/ui"; import { reformatsOnCreate, type FileTemplate, type NameStyle, type TemplateFragment } from "../files/model"; import { isBuiltinFileTemplate, @@ -24,6 +24,7 @@ import { type TemplateScheme, } from "../files/store"; import styles from "./Settings.module.css"; +import layout from "./FileTemplatesPage.module.css"; const NAME_STYLES: Array<{ value: NameStyle; label: string }> = [ { value: "asis", label: "As typed" }, @@ -37,8 +38,7 @@ const SCHEMES: Array<{ value: TemplateScheme; label: string }> = [ { value: "project", label: "Project" }, ]; -type TabId = "files" | "includes" | "code"; -const TABS: Array<{ id: TabId; label: string }> = [ +const TABS = [ { id: "files", label: "Files" }, { id: "includes", label: "Includes" }, { id: "code", label: "Code" }, @@ -47,12 +47,13 @@ const TABS: Array<{ id: TabId; label: string }> = [ export function FileTemplatesPage() { const scheme = useFileTemplates((s) => s.scheme); const root = useFileTemplates((s) => s.root); - const [tab, setTab] = useState("files"); + const [tab, setTab] = useState("files"); + const tabsId = useId(); useEffect(() => reloadFileTemplates(), []); return (
-
+
Scheme @@ -63,27 +64,42 @@ export function FileTemplatesPage() { )}
-
- {TABS.map((t) => ( - - ))} + +
+ {tab === "files" && } + {tab === "includes" && } + {tab === "code" && }
- {tab === "files" && } - {tab === "includes" && } - {tab === "code" && }
); } +function TemplateSplit({ list, children }: { list: ReactNode; children: ReactNode }) { + return ( + + + {list} + + + + {children} + + + ); +} + function FilesTab() { const templates = useFileTemplates((s) => s.templates); useFileTemplates((s) => s.custom); @@ -104,56 +120,60 @@ function FilesTab() { }; return ( -
-
-
- - { - if (!selected) return; - removeFileTemplate(selected.id); - setSelectedId(null); - }} - /> -
-
- {templates.map((t) => ( -
setSelectedId(t.id)} - > - saveFileTemplate({ ...t, enabled: v })} - aria-label={tx`Enable ${t.name}`} - /> - - {t.name} -
- ))} -
-
-
- {selected ? ( - - ) : ( -
- Select a template to edit it + +
+ + { + if (!selected) return; + removeFileTemplate(selected.id); + setSelectedId(null); + }} + />
- )} -
-
+
+ {templates.map((t) => ( +
setSelectedId(t.id)} + > + saveFileTemplate({ ...t, enabled: v })} + aria-label={tx`Enable ${t.name}`} + /> + + + {t.name} + +
+ ))} +
+ + } + > + {selected ? ( + + ) : ( +
+ Select a template to edit it +
+ )} + ); } @@ -166,39 +186,45 @@ function FileTemplateEditor({ template: t }: { template: FileTemplate }) { return ( <> -
- - Name - - update({ name: e.target.value })} - aria-label="Template name" - /> +
+ +
-
- - Extension - - update({ extension: e.target.value.replace(/^\./, "").trim() })} - aria-label="Extension" - placeholder="none" - /> +
File name