From 010cd43d0f31d457c5edcd3fdfb7bd79d0e433bb Mon Sep 17 00:00:00 2001 From: treeform Date: Mon, 16 Nov 2026 06:18:20 -0800 Subject: [PATCH 1/2] Preserve paste hooks and rich scrollbar tests --- .github/workflows/build.yml | 2 + tests/test_paste_hooks.nim | 114 ++++++++++++++++++++++++++++++++++++ tests/test_richscrolls.nim | 85 +++++++++++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 tests/test_paste_hooks.nim create mode 100644 tests/test_richscrolls.nim diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7dec2d92..7c04477d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -96,6 +96,7 @@ jobs: nim r -d:silkyTesting tests/test_focus.nim nim r -d:silkyTesting tests/test_cursor_scroll.nim nim r -d:silkyTesting tests/test_input_hooks.nim + nim r -d:silkyTesting tests/test_paste_hooks.nim nim r -d:silkyTesting tests/test_secure_inputs.nim nim r -d:silkyTesting tests/test_first_touch.nim nim r -d:silkyTesting tests/test_touch_scroll.nim @@ -110,6 +111,7 @@ jobs: nim r ${{ matrix.build_flags }} tests/test_rules.nim nim r ${{ matrix.build_flags }} tests/test_quotes.nim nim r ${{ matrix.build_flags }} tests/test_richimages.nim + nim r ${{ matrix.build_flags }} tests/test_richscrolls.nim nim r ${{ matrix.build_flags }} tests/test_button_vertices.nim nim r -d:silkyTesting tests/test_layout.nim nim r -d:silkyTesting tests/test_layout_regressions.nim diff --git a/tests/test_paste_hooks.nim b/tests/test_paste_hooks.nim new file mode 100644 index 00000000..3fe4147b --- /dev/null +++ b/tests/test_paste_hooks.nim @@ -0,0 +1,114 @@ +import + silky/internal/inputs, silky/testwindow + +when not defined(silkyTesting): + {.error: "Compile with -d:silkyTesting".} + +proc paste(window: Window, modifier = KeyLeftSuper) = + ## Delivers the complete paste shortcut between UI frames. + window.pressButton(modifier) + window.pressButton(KeyV) + window.releaseButton(KeyV) + window.releaseButton(modifier) + +proc commits(inputs: TextInputs): seq[string] = + ## Returns only committed strings from the pending input batch. + for event in inputs.takeInput(): + if event.kind == TextCommit: + result.add(event.text) + +block: + echo "Testing custom paste consumption and normal text fallback" + let + window = newWindow() + inputs = newTextInputs(window) + var + calls: seq[string] + handled = true + inputs.onPaste = proc(id: string): bool = + ## Records the target field and optionally consumes its paste. + calls.add(id) + handled + setClipboardString("normal text") + inputs.beginInputFrame(window) + inputs.submitInput(window, "composer", true, true) + window.paste() + doAssert calls == @["composer"] + doAssert inputs.commits().len == 0 + doAssert not window.buttonDown[KeyV] + doAssert not window.buttonDown[KeyLeftSuper] + + inputs.beginInputFrame(window) + inputs.submitInput(window, "composer", true, true) + handled = false + window.paste(KeyLeftControl) + doAssert calls == @["composer", "composer"] + doAssert inputs.commits() == @["normal text"] + + inputs.beginInputFrame(window) + inputs.submitInput(window, "settings", true, true) + window.paste() + doAssert calls[^1] == "settings" + doAssert inputs.commits() == @["normal text"] + + inputs.beginInputFrame(window) + inputs.submitInput(window, "settings", true, true) + inputs.onPaste = nil + window.paste() + doAssert calls.len == 3 + doAssert inputs.commits() == @["normal text"] + inputs.stopInput(window) + +block: + echo "Testing readonly and unfocused fields do not intercept paste" + let + window = newWindow() + inputs = newTextInputs(window) + var calls = 0 + inputs.onPaste = proc(id: string): bool = + ## Counts unexpected calls from inactive or readonly fields. + inc calls + true + window.paste() + doAssert calls == 0 + inputs.beginInputFrame(window) + inputs.submitInput(window, "reply", true, false) + window.paste() + doAssert calls == 0 + doAssert inputs.commits().len == 0 + inputs.stopInput(window) + window.paste() + doAssert calls == 0 + inputs.onPaste = nil + +block: + echo "Testing a paste callback cannot forward text after changing focus" + let + window = newWindow() + inputs = newTextInputs(window) + var forwarded = 0 + window.onButtonRelease = proc(button: Button) = + ## Counts keyboard releases forwarded after the active field closes. + inc forwarded + inputs.beginInputFrame(window) + inputs.submitInput(window, "composer", true, true) + inputs.onPaste = proc(id: string): bool = + ## Changes the active editor while declining the original paste. + inputs.submitInput(window, "another", true, true) + false + setClipboardString("must not reach another field") + window.paste() + doAssert inputs.commits().len == 0 + doAssert forwarded == 0 + inputs.onPaste = proc(id: string): bool = + ## Closes the editor while declining the original paste. + inputs.stopInput(window) + false + inputs.beginInputFrame(window) + inputs.submitInput(window, "composer", true, true) + window.paste() + doAssert inputs.commits().len == 0 + doAssert forwarded == 0 + inputs.onPaste = nil + +echo "All paste hook tests passed." diff --git a/tests/test_richscrolls.nim b/tests/test_richscrolls.nim new file mode 100644 index 00000000..8f8234f1 --- /dev/null +++ b/tests/test_richscrolls.nim @@ -0,0 +1,85 @@ +import + std/[importutils, tables], + bumpy, pixie, vmath, windy, + silky, silky/internal/inputs + +privateAccess(Silky) + +proc fixture(builder: AtlasBuilder): Silky = + ## Queues native rich widget drawing without opening a platform window. + result = Silky( + atlas: builder.atlas, + image: builder.atlasImage, + uiScale: 2, + inFrame: true, + inputs: TextInputs(), + window: Window() + ) + new(result.drawer) + result.theme.padding = 0 + result.mousePos = vec2(-1) + result.pushLayout(vec2(0), vec2(600)) + result.pushClipRect(rect(0, 0, 600, 600)) + +proc patches(sk: Silky, name: string): seq[Rect] = + ## Collects native scrollbar quads using their packed atlas region. + let + sprite = sk.atlas.entries[name] + vertices = sk.drawer.layers[NormalLayer] + for i in countup(0, vertices.high, 6): + let uv = vertices[i].uv + if uv.x >= sprite.x.float32 and + uv.x < (sprite.x + sprite.width).float32 and + uv.y >= sprite.y.float32 and + uv.y < (sprite.y + sprite.height).float32: + result.add(rect( + vertices[i].pos, + vertices[i + 2].pos - vertices[i].pos + )) + +block: + echo "Testing mixed rich text reserves native horizontal scrollbar space" + const Source = "WW WW WW WW WW WW WW WW WW WW" + let builder = newAtlasBuilder(1024, 2) + builder.addFont("tests/data/IBMPlexSans-Regular.ttf", "Default", 18) + for name in [ + "textbox.disabled.9patch", + "scrollbar.track.9patch", + "scrollbar.9patch" + ]: + doAssert builder.addImage(name, newImage(16, 16)) + let + sk = fixture(builder) + state = newRichTextBoxState(Source) + state.blocks = @[ + TextBlock(first: 0, last: Source.len, noWrap: true) + ] + state.computeLayout(builder.atlas.fonts, "Default", 100) + richTextBoxStates["scroll-test"] = state + let height = state.innerHeight + state.horizontalScrollbarHeight(100) + sk.richTextBox( + sk.window, "scroll-test", Source, 100, height, blocks = state.blocks + ) + doAssert abs(state.editor.boxSize.y - state.innerHeight) < 0.001 + doAssert state.editor.scrollPos.y == 0 + doAssert state.editor.wordWrap + let bars = sk.patches("scrollbar.9patch") + doAssert bars.len > 0 + for bounds in bars: + doAssert bounds.y >= state.innerHeight + + echo "Testing horizontal offsets reset when a table fits after resizing" + state.editor.scrollPos.x = 200 + state.computeLayout(builder.atlas.fonts, "Default", 600) + sk.drawer.layers[NormalLayer].setLen(0) + sk.at = vec2(0) + sk.richTextBox( + sk.window, "scroll-test", Source, 600, state.innerHeight, + blocks = state.blocks + ) + doAssert state.horizontalScrollbarHeight(600) == 0 + doAssert state.editor.scrollPos.x == 0 + doAssert sk.patches("scrollbar.9patch").len == 0 + richTextBoxStates.del("scroll-test") + +echo "All rich scrollbar tests passed." From 677a5250c8d75b0146e29ca62c676285af349045 Mon Sep 17 00:00:00 2001 From: treeform Date: Mon, 16 Nov 2026 06:32:53 -0800 Subject: [PATCH 2/2] Ignore all generated test artifacts --- .gitignore | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index bc6dd93f..2f9a6328 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,5 @@ tmp # silky specific ignores: examples/*/dist/*.json examples/*/dist/*.png -tests/dist/*.json -tests/dist/*.png +/tests/dist/ examples/todomvc/todos-silky.json