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
52 changes: 52 additions & 0 deletions src/silky/internal/inputs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ type
consumedKeys: set[Button]
releaseCallback: proc(button: Button)
pendingText: TextInputState
caretPending: bool
caretRect: Rect
placementSerial, placementLayer, placementVertex: int
when defined(ios) or defined(silkyTesting):
menuPending, menuVisible: bool
Expand Down Expand Up @@ -98,9 +100,13 @@ proc stopInput*(inputs: TextInputs, window: Window) =
inputs.id.setLen(0)
inputs.events.setLen(0)
inputs.pendingText = TextInputState()
inputs.caretPending = false
inputs.caretRect = rect(0, 0, 0, 0)
if window.runeInputEnabled:
window.runeInputEnabled = false
window.setTextInputState(TextInputState())
when compiles(window.imePos):
window.imePos = ivec2(0, 0)

proc modifiers(inputs: TextInputs, window: Window): InputEvent =
## Captures modifier state at the time of a callback.
Expand Down Expand Up @@ -271,6 +277,8 @@ proc beginInputFrame*(inputs: TextInputs, window: Window) =
inputs.submitted = false
inputs.consumed = false
inputs.pendingText = TextInputState()
inputs.caretPending = false
inputs.caretRect = rect(0, 0, 0, 0)
inputs.placementSerial = 0
when defined(ios) or defined(silkyTesting):
inputs.menuPending = false
Expand All @@ -280,6 +288,16 @@ proc textPlacementMark*(inputs: TextInputs): int =
if inputs != nil:
result = inputs.placementSerial

proc publishImeCaret*(
inputs: TextInputs, caret: Rect, layer, firstVertex: int
) =
## Records the caret in framebuffer pixels from the window top-left.
inputs.caretPending = true
inputs.caretRect = caret
inputs.placementLayer = layer
inputs.placementVertex = firstVertex
inc inputs.placementSerial

proc publishTextInput*(
inputs: TextInputs, state: TextInputState, layer, firstVertex: int
) =
Expand All @@ -289,6 +307,16 @@ proc publishTextInput*(
inputs.placementVertex = firstVertex
inc inputs.placementSerial

proc moveCaret(inputs: TextInputs, offset: Vec2) =
## Shifts a published caret with its editor bounds.
if inputs == nil or not inputs.caretPending:
return
inputs.caretRect.x += offset.x
inputs.caretRect.y += offset.y
when compiles(inputs.pendingText.caret):
inputs.pendingText.caret.x += offset.x
inputs.pendingText.caret.y += offset.y

proc publishTextMenu*(
inputs: TextInputs, id: string, bounds: Rect,
actions: set[TextEditAction], layer, firstVertex: int
Expand All @@ -311,6 +339,7 @@ proc translateTextInput*(inputs: TextInputs, first: int, offset: Vec2) =
if inputs != nil and inputs.placementSerial > first:
inputs.pendingText.bounds.x += offset.x
inputs.pendingText.bounds.y += offset.y
inputs.moveCaret(offset)
when defined(ios) or defined(silkyTesting):
if inputs != nil and inputs.menuPending and inputs.menuSerial > first:
inputs.menuBounds.x += offset.x
Expand All @@ -324,6 +353,7 @@ proc translateTextInputVertices*(
inputs.placementLayer == layer and inputs.placementVertex >= first:
inputs.pendingText.bounds.x += offset.x
inputs.pendingText.bounds.y += offset.y
inputs.moveCaret(offset)
when defined(ios) or defined(silkyTesting):
if inputs != nil and inputs.menuPending and inputs.menuLayer == layer and
inputs.menuVertex >= first:
Expand Down Expand Up @@ -430,6 +460,27 @@ when defined(ios) or defined(silkyTesting):
))
)

proc placeIme(inputs: TextInputs, window: Window) =
## Anchors the native candidate window to the caret baseline.
when compiles(window.imePos):
if inputs.caretPending and inputs.submitted and
inputs.editable and inputs.active:
let scale =
when compiles(window.contentScale):
let value = window.contentScale
if value > 0: value else: 1.0'f
else:
1.0'f
let unit =
when defined(windows): 1.0'f
else: scale
window.imePos = ivec2(
round(inputs.caretRect.x / unit).int32,
round((inputs.caretRect.y + inputs.caretRect.h) / unit).int32
)
else:
window.imePos = ivec2(0, 0)

proc endInputFrame*(inputs: TextInputs, window: Window) =
## Ends capture when the active field was not submitted this frame.
if not inputs.active:
Expand All @@ -441,6 +492,7 @@ proc endInputFrame*(inputs: TextInputs, window: Window) =
window.setTextInputState(inputs.pendingText)
else:
window.setTextInputState(TextInputState())
inputs.placeIme(window)

when defined(ios) or defined(silkyTesting):
if inputs.menuPending and inputs.active and inputs.submitted and
Expand Down
2 changes: 2 additions & 0 deletions src/silky/testwindow.nim
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Window* = ref object
closeRequested*: bool = true
cursor*: Cursor = Cursor(kind: ArrowCursor)
runeInputEnabled*: bool
imePos*: IVec2
contentScale*: float32 = 1
onRune*: proc(rune: Rune)
onTextInput*: proc(text: string)
onTextEdit*: TextInputEditCallback
Expand Down
64 changes: 38 additions & 26 deletions src/silky/textboxes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1371,32 +1371,44 @@ proc textBox*(
window.scrollDelta.y != 0:
state.scrollBy(
window.scrollDelta.y * TextBoxScrollSpeed, innerRect.h)
if state.focused and enabled and (not password or defined(ios)):
let selection = state.selection
var
visibleStart = state.runes.len
visibleEnd = 0
for i, bounds in state.layout:
if bounds.y + bounds.h > state.scrollPos.y and
bounds.y < state.scrollPos.y + innerRect.h and
bounds.x + bounds.w >= state.scrollPos.x and
bounds.x < state.scrollPos.x + innerRect.w:
visibleStart = min(visibleStart, i)
visibleEnd = i + 1
if visibleEnd == 0:
visibleStart = min(state.cursor, state.runes.len)
visibleEnd = visibleStart
inputs.publishTextInput(TextInputState(
id: id,
text: t,
selectionStart: selection.a,
selectionEnd: selection.b,
visibleStart: visibleStart,
visibleEnd: visibleEnd,
bounds: rect(innerRect.xy * sk.uiScale, innerRect.wh * sk.uiScale),
multiline: not singleLine,
password: password
), textLayer, firstVertex)
if state.focused and enabled:
let
localCaret = state.locationRect(state.cursor)
caretBounds = rect(
(innerRect.xy + textOffset - state.scrollPos + localCaret.xy) *
sk.uiScale,
localCaret.wh * sk.uiScale
)
inputs.publishImeCaret(caretBounds, textLayer, firstVertex)
if not password or defined(ios):
let selection = state.selection
var
visibleStart = state.runes.len
visibleEnd = 0
for i, bounds in state.layout:
if bounds.y + bounds.h > state.scrollPos.y and
bounds.y < state.scrollPos.y + innerRect.h and
bounds.x + bounds.w >= state.scrollPos.x and
bounds.x < state.scrollPos.x + innerRect.w:
visibleStart = min(visibleStart, i)
visibleEnd = i + 1
if visibleEnd == 0:
visibleStart = min(state.cursor, state.runes.len)
visibleEnd = visibleStart
var textInput = TextInputState(
id: id,
text: t,
selectionStart: selection.a,
selectionEnd: selection.b,
visibleStart: visibleStart,
visibleEnd: visibleEnd,
bounds: rect(innerRect.xy * sk.uiScale, innerRect.wh * sk.uiScale),
multiline: not singleLine,
password: password
)
when compiles(textInput.caret):
textInput.caret = caretBounds
inputs.publishTextInput(textInput, textLayer, firstVertex)
when defined(ios) or defined(silkyTesting):
if openMenu:
let anchor = rect(
Expand Down
90 changes: 87 additions & 3 deletions tests/test_inputs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Fixture = object
allowedChars: seq[Rune]
spans: seq[TextSpan]
blocks: seq[TextBlock]
height: float32
width, height: float32
clip: Rect

var
Expand Down Expand Up @@ -48,6 +48,7 @@ proc fixture(
result.text = text
result.singleLine = true
result.enabled = true
result.width = 240
result.height = 120
result.harness.window = newWindow()
let window = result.harness.window
Expand All @@ -72,15 +73,15 @@ proc frame(f: var Fixture, visible = true, duplicate = false) =
sk.at = vec2(20, 20)
if f.rich:
sk.richTextBox(
window, f.id, f.text, 240, f.height,
window, f.id, f.text, f.width, f.height,
wrapWords = true, spans = f.spans, blocks = f.blocks
)
else:
sk.textBox(
window,
f.id,
f.text,
240,
f.width,
f.height,
wrapWords = true,
singleLine = f.singleLine,
Expand Down Expand Up @@ -498,6 +499,37 @@ block:
(240 - padding * 2) * scale,
(f.height - padding * 2) * scale
)
let localCaret = f.state.locationRect(f.state.cursor)
let expectedCaret = rect(
window.textInput.bounds.xy + localCaret.xy * scale,
localCaret.wh * scale
)
when compiles(window.textInput.caret):
doAssert window.textInput.caret == expectedCaret
let unit =
when defined(windows): 1.0'f
else: window.contentScale
doAssert window.imePos == ivec2(
round(expectedCaret.x / unit).int32,
round((expectedCaret.y + expectedCaret.h) / unit).int32
)
window.contentScale = 2
f.frame()
let
placed = f.state.locationRect(f.state.cursor)
retina =
when defined(windows): 1.0'f
else: window.contentScale
let placedCaret = rect(
window.textInput.bounds.xy + placed.xy * scale,
placed.wh * scale
)
when compiles(window.textInput.caret):
doAssert window.textInput.caret == placedCaret
doAssert window.imePos == ivec2(
round(placedCaret.x / retina).int32,
round((placedCaret.y + placedCaret.h) / retina).int32
)

block:
echo "Testing native editor bounds follow nested deferred layout spans"
Expand Down Expand Up @@ -527,6 +559,12 @@ block:
)
doAssert window.textInput.bounds == expected,
$scale & ": " & $window.textInput.bounds & " != " & $expected
when compiles(window.textInput.caret):
let localCaret = f.state.locationRect(f.state.cursor)
doAssert window.textInput.caret == rect(
expected.xy + localCaret.xy * scale,
localCaret.wh * scale
), $scale & ": caret " & $window.textInput.caret

block:
echo "Testing native editor visibility follows the scrolled text"
Expand Down Expand Up @@ -558,16 +596,22 @@ block:
case mode
of 0:
f.frame(visible = false)
doAssert window.imePos == ivec2(0, 0)
of 1:
f.enabled = false
f.frame()
doAssert window.imePos == ivec2(0, 0)
of 2:
f.password = true
f.frame()
when compiles(window.textInput.caret):
doAssert window.textInput.caret == rect(0, 0, 0, 0)
doAssert window.imePos != ivec2(0, 0)
else:
window.changeFocus()
doAssert window.textInput == TextInputState()
f.frame()
doAssert window.imePos == ivec2(0, 0)
doAssert window.textInput == TextInputState()

block:
Expand Down Expand Up @@ -1041,6 +1085,46 @@ block:
textBoxStates.del(id)
richTextBoxStates.del(id)

block:
echo "Testing middle edits preserve the cursor across wrapping and resizing"
const
Prefix = "Hello "
Suffix = " café world with text after the insertion."
Inserted = "Some new words é ⍺ with enough text to add several lines "
var f = fixture(Prefix & Suffix)
f.singleLine = false
f.focus()
f.state.cursor = Prefix.runeLen
f.state.selector = f.state.cursor
let initialHeight = f.state.innerHeight
var added: string
for rune in Inserted.runes:
f.harness.window.typeRune(rune)
added.add($rune)
f.frame()
let expected = Prefix.runeLen + added.runeLen
doAssert f.text == Prefix & added & Suffix
doAssert f.state.cursor == expected
doAssert f.state.selector == expected
f.height = f.state.innerHeight + 24
f.frame()
doAssert f.state.cursor == expected
doAssert f.state.selector == expected
doAssert f.state.innerHeight > initialHeight
f.state.selector = Prefix.runeLen
let selected = f.state.selection
f.width = 140
f.frame()
doAssert f.state.selection == selected
f.width = 340
f.frame()
doAssert f.state.selection == selected
f.harness.window.typeText("é replacement ")
f.frame()
doAssert f.text == Prefix & "é replacement " & Suffix
doAssert f.state.cursor == (Prefix & "é replacement ").runeLen
doAssert f.state.selector == f.state.cursor

block:
echo "Testing one-batch editing matches separate frames and layout"
var fixtures = [fixture(""), fixture("")]
Expand Down
Loading