Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
114 changes: 114 additions & 0 deletions tests/test_paste_hooks.nim
Original file line number Diff line number Diff line change
@@ -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."
85 changes: 85 additions & 0 deletions tests/test_richscrolls.nim
Original file line number Diff line number Diff line change
@@ -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."
Loading