diff --git a/e2e/previewEditorColors.spec.ts b/e2e/previewEditorColors.spec.ts new file mode 100644 index 0000000000..db6f33d4c2 --- /dev/null +++ b/e2e/previewEditorColors.spec.ts @@ -0,0 +1,194 @@ +import type { Page } from "@playwright/test"; +import { expect, openFromTree, stubDesktop, test } from "./fixtures"; + +// UI theme, then an editor colour scheme of the other brightness. +const COMBOS = [ + ["light", "dark"], + ["dark", "light"], +] as const; + +async function appearance(page: Page, theme: string, colorScheme: string) { + await page.evaluate( + async ({ theme, colorScheme }) => { + const projectModule = "/src/platform/project.ts"; + const { useProject } = await import(/* @vite-ignore */ projectModule); + const { settings, project } = useProject.getState(); + const current = settings ?? project?.settings; + if (!current) throw new Error("Expected project settings"); + useProject.setState({ + settings: { + ...current, + appearance: { ...current.appearance, theme }, + editor: { ...current.editor, colorScheme }, + }, + }); + }, + { theme, colorScheme }, + ); + await expect(page.locator("html")).toHaveAttribute("data-theme", theme); +} + +const colors = (el: Element) => { + const style = getComputedStyle(el); + return { bg: style.backgroundColor, fg: style.color }; +}; + +/** Lowest WCAG contrast of the text under `scope`, against the background actually painted behind it. */ +const minContrast = (scope: Element) => { + const parse = (c: string): number[] => { + const srgb = /color\(srgb ([\d.e-]+) ([\d.e-]+) ([\d.e-]+)(?: \/ ([\d.e-]+))?\)/.exec(c); + if (srgb) return [+srgb[1]! * 255, +srgb[2]! * 255, +srgb[3]! * 255, srgb[4] === undefined ? 1 : +srgb[4]]; + const rgb = /rgba?\(([\d.]+), ([\d.]+), ([\d.]+)(?:, ([\d.]+))?\)/.exec(c); + if (rgb) return [+rgb[1]!, +rgb[2]!, +rgb[3]!, rgb[4] === undefined ? 1 : +rgb[4]]; + throw new Error(`Unexpected colour ${c}`); + }; + const over = (top: number[], under: number[]) => + [0, 1, 2].map((i) => top[i]! * top[3]! + under[i]! * (1 - top[3]!)).concat(1); + const luminance = (c: number[]) => { + const f = (x: number) => ((x /= 255) <= 0.03928 ? x / 12.92 : ((x + 0.055) / 1.055) ** 2.4); + return 0.2126 * f(c[0]!) + 0.7152 * f(c[1]!) + 0.0722 * f(c[2]!); + }; + const ratios: number[] = []; + const walker = document.createTreeWalker(scope, NodeFilter.SHOW_TEXT); + for (let node = walker.nextNode(); node; node = walker.nextNode()) { + if (!node.textContent?.trim()) continue; + const layers: number[][] = []; + for (let e: Element | null = node.parentElement; e; e = e.parentElement) + layers.push(parse(getComputedStyle(e).backgroundColor)); + const bg = layers.reverse().reduce((under, top) => over(top, under), [255, 255, 255, 1]); + const fg = over(parse(getComputedStyle(node.parentElement!).color), bg); + const [a, b] = [luminance(fg) + 0.05, luminance(bg) + 0.05]; + ratios.push(Math.max(a, b) / Math.min(a, b)); + } + return Math.min(...ratios); +}; + +for (const [theme, scheme] of COMBOS) { + test(`${theme} theme, ${scheme} scheme: the Problems preview paints with the editor's colours`, async ({ + workbench: page, + }, info) => { + await page.setViewportSize({ width: 1000, height: 760 }); + await openFromTree(page, "build.gradle.kts"); + await appearance(page, theme, scheme); + await page.evaluate(async () => { + const projectModule = "/src/platform/project.ts"; + const problemsModule = "/src/features/code-insight/problemsStore.ts"; + const { useProject } = await import(/* @vite-ignore */ projectModule); + const { applyDiagnosticsEvent } = await import(/* @vite-ignore */ problemsModule); + const path = `${useProject.getState().project.root}/build.gradle.kts`; + applyDiagnosticsEvent(path, "kotlin", [ + { + range: { start: { line: 2, col: 0 }, end: { line: 2, col: 6 } }, + severity: "error", + source: "kotlin", + message: "Invalid plugin configuration", + }, + ]); + }); + + await page.getByRole("button", { name: "Problems", exact: true }).click(); + const pane = page.locator('[data-toolwindow="problems"]'); + await pane.getByRole("button", { name: "Options", exact: true }).click(); + const showPreview = page.getByRole("menuitemcheckbox", { name: "Show Preview", exact: true }); + if ((await showPreview.getAttribute("aria-checked")) !== "true") await showPreview.click(); + else await page.keyboard.press("Escape"); + await pane + .getByRole("tree", { name: "Problems" }) + .getByRole("treeitem", { name: /Invalid plugin configuration/ }) + .click(); + const preview = pane.getByRole("region", { name: "Preview", exact: true }); + await expect(preview).toContainText("class OrderController"); + await pane.screenshot({ path: info.outputPath("problems-preview.png"), animations: "disabled" }); + + const editor = await page.locator(".cm-editor").first().evaluate(colors); + expect(await preview.evaluate(colors)).toEqual(editor); + const gutter = await preview.locator('[data-line="0"] > span').first().evaluate(colors); + const editorGutter = await page.locator(".cm-editor .cm-gutters").first().evaluate(colors); + const lineNumber = await page + .locator(".cm-editor .cm-lineNumbers .cm-gutterElement:not(.cm-activeLineGutter)") + .filter({ hasText: /^\d+$/ }) + .first() + .evaluate(colors); + expect(gutter).toEqual({ bg: editorGutter.bg, fg: lineNumber.fg }); + }); + + test(`${theme} theme, ${scheme} scheme: Find Usages hits stay readable`, async ({ workbench: page }) => { + await page.setViewportSize({ width: 1120, height: 720 }); + await stubDesktop(page); + await appearance(page, theme, scheme); + await page.evaluate(async () => { + const lines = Array.from({ length: 12 }, () => ""); + lines[3] = " record(orderTotal);"; + lines[7] = " return orderTotal;"; + window.__ipc.handlers.fs_read_text = () => lines.join("\n"); + const actionsModule = "/src/platform/actions.ts"; + const { executeAction } = await import(/* @vite-ignore */ actionsModule); + await executeAction("find.showUsageResults", { + symbol: "orderTotal", + locations: [3, 7].map((line) => ({ + path: "/dev/acme-api/src/orderTotals.ts", + range: { start: { line, col: 9 }, end: { line, col: 19 } }, + })), + }); + }); + const view = page.getByRole("region", { name: "Find Usages", exact: true }); + await view.getByRole("treeitem").filter({ hasText: "record(orderTotal)" }).click(); + const marks = view.getByRole("region", { name: "Preview", exact: true }).locator("mark"); + await expect(marks).toHaveCount(2); + // The selected row's hit and the other one. + for (const mark of await marks.all()) expect(await mark.evaluate(minContrast)).toBeGreaterThan(4.5); + }); + + test(`${theme} theme, ${scheme} scheme: the Replace in Files preview paints with the editor's colours`, async ({ + workbench: page, + }, info) => { + await page.setViewportSize({ width: 1200, height: 760 }); + await stubDesktop(page); + await openFromTree(page, "build.gradle.kts"); + await appearance(page, theme, scheme); + await page.evaluate(() => { + const before = "package acme\nval service = OrderService()\nval count = 2\n"; + window.__ipc.handlers.search_validate = () => null; + window.__ipc.handlers.replace_preview = () => ({ + before, + after: before.replace("OrderService", "OrdersApi"), + edits: [{ range: { start: { line: 1, col: 14 }, end: { line: 1, col: 26 } }, new_text: "OrdersApi" }], + }); + window.__ipc.handlers.search_start = ({ onEvent }) => { + setTimeout(() => { + window.__ipc.send(onEvent, { + type: "file", + file: { + path: "/dev/acme-api/Orders.kt", + relPath: "Orders.kt", + matches: [{ line: 1, text: "val service = OrderService()", ranges: [[14, 26]], textOffset: 0 }], + }, + }); + window.__ipc.send(onEvent, { + type: "done", + summary: { files: 1, matches: 1, limitHit: false, cancelled: false, elapsedMs: 1 }, + }); + }, 20); + return null; + }; + }); + await page.keyboard.press("Meta+Shift+r"); + const popup = page.getByRole("dialog", { name: "Replace in Files", exact: true }); + await popup.getByRole("textbox", { name: "Text to find", exact: true }).fill("OrderService"); + await popup.getByRole("textbox", { name: "Replace with", exact: true }).fill("OrdersApi"); + await popup.getByRole("listbox", { name: "Matches" }).getByRole("option").first().click(); + await popup.getByRole("checkbox", { name: "Preview", exact: true }).check(); + const preview = popup.getByRole("region", { name: "Replace preview", exact: true }); + await expect(preview.locator('[data-kind="add"]')).toHaveCount(1); + await popup.screenshot({ path: info.outputPath("replace-preview.png"), animations: "disabled" }); + + const editor = await page.locator(".cm-editor").first().evaluate(colors); + expect(await preview.evaluate(colors)).toEqual(editor); + // The changed lines' +/- signs and text. + for (const kind of ["del", "add"]) { + const row = preview.locator(`[data-kind="${kind}"]`); + expect(await row.locator("> span").nth(1).evaluate(minContrast)).toBeGreaterThan(4.5); + expect(await row.locator("> span").nth(2).evaluate(minContrast)).toBeGreaterThan(4.5); + } + }); +} diff --git a/src/features/search/find/parts.module.css b/src/features/search/find/parts.module.css index 556d15d764..e67e736aae 100644 --- a/src/features/search/find/parts.module.css +++ b/src/features/search/find/parts.module.css @@ -26,12 +26,12 @@ flex: 1; min-height: 0; overflow: auto; - background: var(--bg-app); + background: var(--bg-editor, var(--bg-app)); font-family: var(--openidea-editor-font, var(--font-mono)); font-weight: var(--fw-code); font-size: var(--openidea-editor-font-size, var(--fs-code)); line-height: var(--openidea-editor-line-height, var(--lh-code)); - color: var(--fg); + color: var(--fg-editor, var(--fg)); } .previewEditor { position: absolute; @@ -46,6 +46,8 @@ display: flex; align-items: center; justify-content: center; + /* A message, not code: the UI's colours, as before. */ + background: var(--bg-app); color: var(--fg-faint); font-family: var(--font-ui); font-size: var(--fs-ui); @@ -67,9 +69,9 @@ width: 48px; padding-right: 12px; text-align: right; - color: var(--fg-gutter); + color: var(--fg-line-number, var(--fg-gutter)); background: inherit; - background-color: var(--bg-app); + background-color: var(--bg-editor-gutter, var(--bg-editor, var(--bg-app))); } .code { padding-right: 16px; @@ -80,9 +82,11 @@ .lineAdded { background: color-mix(in srgb, var(--success) 16%, transparent); } -.lineDeleted .gutter, +.lineDeleted .gutter { + background-color: color-mix(in srgb, var(--danger) 14%, var(--bg-editor-gutter, var(--bg-editor, var(--bg-app)))); +} .lineAdded .gutter { - background-color: transparent; + background-color: color-mix(in srgb, var(--success) 16%, var(--bg-editor-gutter, var(--bg-editor, var(--bg-app)))); } .sign { flex: none; diff --git a/src/ui/SourcePreview.module.css b/src/ui/SourcePreview.module.css index f61b174a56..dad3f85999 100644 --- a/src/ui/SourcePreview.module.css +++ b/src/ui/SourcePreview.module.css @@ -5,12 +5,12 @@ min-height: 0; height: 100%; overflow: auto; - background: var(--bg-app); + background: var(--bg-editor, var(--bg-app)); font-family: var(--openidea-editor-font, var(--font-mono)); font-weight: var(--fw-code); font-size: var(--openidea-editor-font-size, var(--fs-code)); line-height: var(--openidea-editor-line-height, var(--lh-code)); - color: var(--fg); + color: var(--fg-editor, var(--fg)); /* The workbench disables selection globally; preview text stays selectable/copyable. */ -webkit-user-select: text; user-select: text; @@ -20,6 +20,8 @@ display: flex; align-items: center; justify-content: center; + /* A message, not code: the UI's colours, as before. */ + background: var(--bg-app); color: var(--fg-faint); font-family: var(--font-ui); font-size: var(--fs-ui); @@ -45,14 +47,14 @@ width: var(--source-preview-gutter-width, 48px); padding-right: var(--space-3); text-align: right; - color: var(--fg-gutter); - background-color: var(--bg-app); + color: var(--fg-line-number, var(--fg-gutter)); + background-color: var(--bg-editor-gutter, var(--bg-editor, var(--bg-app))); /* Copying the code leaves the line numbers out. */ -webkit-user-select: none; user-select: none; } .currentLine .gutter { - color: var(--fg-muted); + color: var(--fg-line-number-current, var(--fg-muted)); background-color: var(--bg-current-line); } .code { @@ -60,6 +62,7 @@ } .match { background: var(--accent-soft); - color: var(--fg-strong); + /* The scheme's text: `--fg-strong` is the UI theme's and vanishes on a scheme of the other brightness. */ + color: inherit; border-radius: 2px; }