diff --git a/src/noldova.ui.components.angular/src/app/components/code-block/code-block-behavior.ts b/src/noldova.ui.components.angular/src/app/components/code-block/code-block-behavior.ts index ec3f1f00..6a2acc48 100644 --- a/src/noldova.ui.components.angular/src/app/components/code-block/code-block-behavior.ts +++ b/src/noldova.ui.components.angular/src/app/components/code-block/code-block-behavior.ts @@ -77,6 +77,7 @@ export class CodeBlockBehavior { public readonly partResources: typeof CodeBlockResources = CodeBlockResources; public readonly runStates: typeof ShellRunState = ShellRunState; public readonly advanceRatio: WritableSignal = signal(null); + public readonly scrollbarHeight: WritableSignal = signal(null); public readonly isCopied: Signal = computed(() => this.copyState() === CopyState.Copied); public readonly copyGlyph: Signal = computed(() => Resources.copyGlyphs[this.copyState()]); public readonly copyLabel: Signal = computed(() => Resources.copyLabels[this.copyState()]); @@ -122,11 +123,15 @@ export class CodeBlockBehavior { afterNextRender(() => { const measureAfterLayout = this.observers.create(() => { this.measureAdvance(); + this.measureScrollbar(); this.followEnd(); }); this.destroyRef.onDestroy(() => measureAfterLayout.disconnect()); for (const element of [this.elements.text(), this.elements.advanceOne(), this.elements.advanceRun()]) measureAfterLayout.observe(element.nativeElement); + // A sideways scrollbar that takes room grows the body's border box as the line starts to scroll, and shrinks it as + // the line stops, so the block measures the bar again each time. + measureAfterLayout.observe(this.elements.body().nativeElement, { box: "border-box" }); }); } @@ -168,6 +173,13 @@ export class CodeBlockBehavior { this.advanceRatio.set(String(this.elements.advanceOne().nativeElement.getBoundingClientRect().width * CodeBlockResources.codeAdvanceSample.length / run)); } + // The room the body's sideways scrollbar takes: none while the line fits, or where the scrollbar lies over the code, as + // a phone's does. The code's bottom padding gives up as much, so a block is as tall whether its line fits or scrolls. + private measureScrollbar(): void { + const body = this.elements.body().nativeElement; + this.scrollbarHeight.set(`${String(body.offsetHeight - body.clientHeight)}px`); + } + private followEnd(): void { if (this.source.code().length > 0 && (this.grewStreaming || this.source.streaming())) this.follower.follow(this.elements.body().nativeElement); diff --git a/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.css b/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.css index 51836aaa..a44908dd 100644 --- a/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.css +++ b/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.css @@ -66,6 +66,7 @@ width: max-content; min-width: 100%; padding: var(--nol-code-block-padding); + padding-bottom: max(0px, var(--nol-code-block-padding) - var(--nol-code-scrollbar-height, var(--nol-code-scrollbar-guess, 0px))); font: inherit; letter-spacing: calc((var(--nol-code-advance-ratio, 1) - 1) * 1ch); overflow: clip; @@ -98,9 +99,12 @@ } } +/* Until the block measures its sideways scrollbar, a mouse desktop's line that scrolls likely has a classic bar. */ @container scroll-state(scrollable: inline) { - .nol-code-block-body > code { - padding-bottom: max(0px, var(--nol-code-block-padding) - var(--nol-scrollbar-size)); + @media (pointer: fine) { + .nol-code-block-body > code { + --nol-code-scrollbar-guess: var(--nol-scrollbar-size); + } } } diff --git a/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.ts b/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.ts index 906cae7a..1920c128 100644 --- a/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.ts +++ b/src/noldova.ui.components.angular/src/app/components/code-block/code-block.component.ts @@ -29,7 +29,8 @@ import { CodeBlockBehavior } from "./code-block-behavior.js"; "class": "nol-code-block", "ngSkipHydration": "true", "[class.nol-code-block-wrapped]": "wrapped()", - "[style.--nol-code-advance-ratio]": "view.advanceRatio()" + "[style.--nol-code-advance-ratio]": "view.advanceRatio()", + "[style.--nol-code-scrollbar-height]": "view.scrollbarHeight()" } }) export class CodeBlockComponent { diff --git a/src/noldova.ui.components.angular/src/app/components/code-block/site-code-block.component.ts b/src/noldova.ui.components.angular/src/app/components/code-block/site-code-block.component.ts index 8f9f469e..74d12710 100644 --- a/src/noldova.ui.components.angular/src/app/components/code-block/site-code-block.component.ts +++ b/src/noldova.ui.components.angular/src/app/components/code-block/site-code-block.component.ts @@ -31,7 +31,8 @@ import { CodeBlockBehavior } from "./code-block-behavior.js"; "class": "nol-code-block", "ngSkipHydration": "true", "[class.nol-code-block-wrapped]": "wrapped()", - "[style.--nol-code-advance-ratio]": "view.advanceRatio()" + "[style.--nol-code-advance-ratio]": "view.advanceRatio()", + "[style.--nol-code-scrollbar-height]": "view.scrollbarHeight()" } }) export class SiteCodeBlockComponent { diff --git a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size-dark.component.spec.ts b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size-dark.component.spec.ts index 12360d1c..ebd39665 100644 --- a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size-dark.component.spec.ts +++ b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size-dark.component.spec.ts @@ -40,9 +40,8 @@ describe("CodeBlockComponent", () => { range.selectNodeContents(find(".nol-code-block-body > code")); const lines = [...range.getClientRects()]; const outer = body().getBoundingClientRect(); - const scrollbar = body().scrollWidth > body().clientWidth ? AppearanceFixture.measureLook("scrollbar-size") - (body().offsetHeight - body().clientHeight) : 0; const over = (lines[0] as DOMRect).top - outer.top; - const under = outer.bottom - (lines.at(-1) as DOMRect).bottom + scrollbar; + const under = outer.bottom - (lines.at(-1) as DOMRect).bottom; const space = AppearanceFixture.toPixels(0.875); expect([Math.abs(over - under) <= 0.5, Math.abs(over - space) <= 0.5, body().scrollHeight - body().clientHeight], `${over} px over, ${under} px under, ${space} px expected`) diff --git a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size.component.spec.ts b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size.component.spec.ts index 20d5d29c..2518c6e7 100644 --- a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size.component.spec.ts +++ b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-code-size.component.spec.ts @@ -40,9 +40,8 @@ describe("CodeBlockComponent", () => { range.selectNodeContents(find(".nol-code-block-body > code")); const lines = [...range.getClientRects()]; const outer = body().getBoundingClientRect(); - const scrollbar = body().scrollWidth > body().clientWidth ? AppearanceFixture.measureLook("scrollbar-size") - (body().offsetHeight - body().clientHeight) : 0; const over = (lines[0] as DOMRect).top - outer.top; - const under = outer.bottom - (lines.at(-1) as DOMRect).bottom + scrollbar; + const under = outer.bottom - (lines.at(-1) as DOMRect).bottom; const space = AppearanceFixture.toPixels(0.875); expect([Math.abs(over - under) <= 0.5, Math.abs(over - space) <= 0.5, body().scrollHeight - body().clientHeight], `${over} px over, ${under} px under, ${space} px expected`) diff --git a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scroll-height.component.spec.ts b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scroll-height.component.spec.ts new file mode 100644 index 00000000..eef41e6f --- /dev/null +++ b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scroll-height.component.spec.ts @@ -0,0 +1,97 @@ +/** + * @license + * Copyright (c) Noldova. + * + * This source code is licensed under the license found in the + * LICENSE file in the root directory of this source tree. + */ + +import { page } from "vitest/browser"; + +import { DefaultTheme } from "../../../../src/app/models/default-theme"; +import { AppearanceFixture } from "../../../../src/testing/services/appearance-fixture"; +import { CodeBlockHarness } from "../../../fixtures/code-block-harness.fixture"; + +// A one-line block's line, short enough to fit in a phone's column, and one that scrolls there. +const FITS = "sha256sum TeamRun-0.0.5-linux-x64.deb"; +const SCROLLS = "Get-FileHash .\\TeamRun-0.0.5-windows-x64.exe -Algorithm SHA256"; + +// A classic sideways scrollbar's height, as the kit draws one. The test browser runs headless, where no scrollbar takes +// room, so its block measures none; a classic bar's case sets the measured height on the body as the block would. +const CLASSIC_BAR = 0.375; + +describe("CodeBlockComponent", () => { + const harness = new CodeBlockHarness(); + const { renderAsync, changeAsync, find, body, settledAsync } = harness; + const windowWidth = window.innerWidth; + const windowHeight = window.innerHeight; + + beforeEach(harness.configure); + + afterEach(async () => { + await harness.resetAsync(); + await page.viewport(windowWidth, windowHeight); + }); + + const rem = (value: number): number => AppearanceFixture.toPixels(value); + const bar = (): number => body().offsetHeight - body().clientHeight; + const height = (): number => find("nol-code-block").getBoundingClientRect().height; + const padding = (): number => Number.parseFloat(getComputedStyle(find(".nol-code-block-body > code")).paddingBottom); + const shape = (): string => JSON.stringify([height(), body().scrollWidth, body().clientWidth, bar(), padding()]); + + async function renderLineAsync(width: number, mode: (typeof AppearanceFixture.modes)[number], code: string): Promise { + await page.viewport(width, 800); + AppearanceFixture.apply(DefaultTheme.theme, mode); + await renderAsync(); + await changeAsync(() => { + harness.host.width.set("calc(100vw - 2.5rem)"); + harness.host.language.set(null); + harness.host.code.set(code); + }); + await settledAsync("the line", shape); + } + + for (const width of [375, 1280]) + for (const mode of AppearanceFixture.modes) + it(`is as tall with one line that scrolls as with one that fits, where the scrollbar lies over the code, at ${String(width)} pixels, in ${mode.toLowerCase()} mode`, async () => { + await renderLineAsync(width, mode, FITS); + const fits = height(); + const fitsBar = bar(); + await changeAsync(() => harness.host.code.set(SCROLLS)); + await settledAsync("the line that scrolls", shape); + + expect([fitsBar, body().scrollWidth > body().clientWidth, bar()]).toEqual([0, width < 600, 0]); + AppearanceFixture.expectPixels(padding(), rem(0.875)); + AppearanceFixture.expectPixels(height(), fits); + }); + + for (const mode of AppearanceFixture.modes) + it(`gives up a classic scrollbar's measured height from its bottom padding, so with the bar's room it is as tall as a block whose line fits, at 375 pixels, in ${mode.toLowerCase()} mode`, async () => { + await renderLineAsync(375, mode, FITS); + const fits = height(); + await changeAsync(() => harness.host.code.set(SCROLLS)); + body().style.setProperty("--nol-code-scrollbar-height", `${String(CLASSIC_BAR)}rem`); + await settledAsync("the line that scrolls over a classic bar", shape); + + expect(body().scrollWidth).toBeGreaterThan(body().clientWidth); + AppearanceFixture.expectPixels(padding(), rem(0.875 - CLASSIC_BAR)); + AppearanceFixture.expectPixels(height() + rem(CLASSIC_BAR), fits); + }); + + for (const mode of AppearanceFixture.modes) + it(`gives up the kit's scrollbar size from its bottom padding before it has measured, under a fine pointer, only while its line scrolls, at 375 pixels, in ${mode.toLowerCase()} mode`, async () => { + await renderLineAsync(375, mode, SCROLLS); + const block = find("nol-code-block"); + const measured = block.style.getPropertyValue("--nol-code-scrollbar-height"); + block.style.removeProperty("--nol-code-scrollbar-height"); + await settledAsync("the line that scrolls before the block measures", shape); + const scrolls = padding(); + await changeAsync(() => harness.host.code.set(FITS)); + await settledAsync("the line that fits before the block measures", shape); + + expect([matchMedia("(pointer: fine)").matches, measured, block.style.getPropertyValue("--nol-code-scrollbar-height")]).toEqual([true, "0px", String.empty]); + expect(body().scrollWidth).toBe(body().clientWidth); + AppearanceFixture.expectPixels(scrolls, rem(0.875) - AppearanceFixture.measureLook("scrollbar-size")); + AppearanceFixture.expectPixels(padding(), rem(0.875)); + }); +}); diff --git a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scrollbar.component.spec.ts b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scrollbar.component.spec.ts index f0a2c073..d8feb0d9 100644 --- a/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scrollbar.component.spec.ts +++ b/src/noldova.ui.components.angular/tests/app/components/code-block/code-block-scrollbar.component.spec.ts @@ -20,7 +20,7 @@ describe("CodeBlockComponent", () => { afterEach(harness.resetAsync); for (const language of [null, "TypeScript"]) - it(`takes a sideways scrollbar's height from its bottom padding only while its lines scroll, and keeps its end padding once scrolled to the end, in ${language ?? "plain"} code`, async () => { + it(`takes the room its sideways scrollbar takes from its bottom padding only while its lines scroll, and keeps its end padding once scrolled to the end, in ${language ?? "plain"} code`, async () => { AppearanceFixture.apply(); controlTokens(() => [new CodeToken(0, "const", CodeTokenKind.Keyword)]); await renderAsync(); @@ -41,7 +41,7 @@ describe("CodeBlockComponent", () => { expect(body().scrollWidth).toBeGreaterThan(body().clientWidth); expect(body().scrollWidth).toBe(Math.round(code().left + code().width) - Math.round(code().left)); AppearanceFixture.expectPixels(code().right - end(), start); - AppearanceFixture.expectPixels(bottom(), AppearanceFixture.toPixels(0.875) - AppearanceFixture.measureLook("scrollbar-size")); + AppearanceFixture.expectPixels(bottom(), AppearanceFixture.toPixels(0.875) - (body().offsetHeight - body().clientHeight)); body().scrollLeft = body().scrollWidth; diff --git a/src/noldova.ui.components.angular/tests/app/components/code-block/site-code-block.component.spec.ts b/src/noldova.ui.components.angular/tests/app/components/code-block/site-code-block.component.spec.ts index 9c1f5c98..451c53b0 100644 --- a/src/noldova.ui.components.angular/tests/app/components/code-block/site-code-block.component.spec.ts +++ b/src/noldova.ui.components.angular/tests/app/components/code-block/site-code-block.component.spec.ts @@ -72,6 +72,7 @@ describe("SiteCodeBlockComponent", () => { for (const mode of AppearanceFixture.modes) { AppearanceFixture.apply(DefaultTheme.theme, mode); await renderAsync(); + await DomChangeFixture.untilAsync(() => expect([block(), plain()].map(t => t.style.getPropertyValue("--nol-code-scrollbar-height"))).toEqual(["0px", "0px"])); expect([block().className, block().textContent]).toEqual([plain().className, plain().textContent]); expect(places(block())).toEqual(places(plain()));