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
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,19 @@ jobs:
nim r -d:silkyTesting -d:nimTypeNames tests/test_inputs.nim
nim r -d:silkyTesting tests/test_buttons.nim
nim r -d:silkyTesting tests/test_focus.nim
nim r -d:silkyTesting tests/test_text_menus.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
nim r -d:silkyTesting tests/test_scroll_momentum.nim
nim r -d:silkyTesting tests/test_scroll_indicators.nim
nim r -d:silkyTesting tests/test_rendering.nim
nim r -d:silkyTesting tests/test_subwindow.nim
nim r tests/test_atlas_png.nim
nim r tests/test_atlas_paths.nim
nim r tests/test_retina_atlas.nim
nim r tests/test_shader_codegen.nim
nim r ${{ matrix.build_flags }} tests/test_density.nim
Expand Down
2 changes: 1 addition & 1 deletion nimby.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ nimsimd 1.3.2 https://github.com/treeform/nimsimd 3f6b2668ffb0867d0bf786a658b817
bumpy 1.1.3 https://github.com/treeform/bumpy dc9a3d6d15680ec7f603959d65ba8ee4f1ddbc72
crunchy 0.1.11 https://github.com/treeform/crunchy 98eb6526982bb8aae8eec6e8781f4539fa19e049
bitty 0.1.4 https://github.com/treeform/bitty/ c40e81277c04befa37234cba5052f58648bef895
windy 0.5.0 https://github.com/treeform/windy bf6b93d872de6826a69b34752c6d766e46204530
windy 0.5.0 https://github.com/treeform/windy 021664d11beeeab3999f375a6881d145a630fd8a
opengl 1.2.9 https://github.com/nim-lang/opengl 8e2e098f82dc5eefd874488c37b5830233cd18f4
urlly 1.1.1 https://github.com/treeform/urlly 99784779f05649df25fd9c33003d8ef6de027345
ws 0.5.0 https://github.com/treeform/ws cbb8f763b436669392d10baec2a45778395395cc
Expand Down
5 changes: 3 additions & 2 deletions src/silky/atlas.nim
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,13 @@ proc addDir*(
pixelScale: int = 1
) =
## Add all images in the given directory to the atlas.
let prefix = removePrefix.replace("\\", "/")
for file in walkDir(path):
if file.path.endsWith(".png"):
let image = readImage(file.path)
var key = file.path.replace("\\", "/")
if removePrefix.len > 0:
key.removePrefix(removePrefix)
if prefix.len > 0:
key.removePrefix(prefix)
key.removeSuffix(".png")
if not builder.addImage(key, image, pixelScale):
raise newException(
Expand Down
8 changes: 5 additions & 3 deletions src/silky/interactions.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type
warmLayer*: int = -1
hotId*: int = -1
previous, current: seq[HitRegion]
resolvedMouse: Vec2
invalidated: bool

proc resetInteractions*(interactor: var Interactor) =
Expand Down Expand Up @@ -44,9 +45,10 @@ proc mouseHover*(
layer: int,
widgetRect: Rect
): bool =
## Resolves a new pointer position before its first press is consumed.
if interactor.currentId == -1:
# Applications may adjust the pointer for safe areas after beginUi.
## Resolves pointer changes without renumbering this frame's hit regions.
if interactor.currentId == -1 or mousePos != interactor.resolvedMouse:
# Applications may mask background input before drawing a popup.
interactor.resolvedMouse = mousePos
interactor.hotId = -1
var hotLayer = -1
for id, region in interactor.previous:
Expand Down
107 changes: 105 additions & 2 deletions src/silky/internal/inputs.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import
std/unicode,
vmath,
vmath, bumpy,
silky/common

when defined(silkyTesting):
Expand Down Expand Up @@ -42,6 +42,13 @@ type
releaseCallback: proc(button: Button)
pendingText: TextInputState
placementSerial, placementLayer, placementVertex: int
when defined(ios) or defined(silkyTesting):
menuPending, menuVisible: bool
menuBounds: Rect
menuActions: set[TextEditAction]
menuId: string
menuEpoch, menuGeneration: uint64
menuSerial, menuLayer, menuVertex: int

proc identity[T](callback: T): CallbackIdentity =
## Identifies a callback without retaining its closure environment.
Expand Down Expand Up @@ -72,18 +79,28 @@ else:
else:
buttons

proc dismissInputMenu(inputs: TextInputs, window: Window) =
## Invalidates actions belonging to a menu that no longer owns focus.
when defined(ios) or defined(silkyTesting):
inc inputs.menuGeneration
inputs.menuPending = false
if inputs.menuVisible:
window.dismissTextEditMenu()
inputs.menuVisible = false

proc stopInput*(inputs: TextInputs, window: Window) =
## Ends text capture and discards input for the previous field.
inputs.dismissInputMenu(window)
if inputs.active:
inc inputs.epoch
inputs.active = false
inputs.editable = false
inputs.id.setLen(0)
inputs.events.setLen(0)
inputs.pendingText = TextInputState()
window.setTextInputState(TextInputState())
if window.runeInputEnabled:
window.runeInputEnabled = false
window.setTextInputState(TextInputState())

proc modifiers(inputs: TextInputs, window: Window): InputEvent =
## Captures modifier state at the time of a callback.
Expand Down Expand Up @@ -111,6 +128,7 @@ proc installPress(inputs: TextInputs, window: Window) =
let previousPress = window.onButtonPress
window.onButtonPress = proc(button: Button) =
let window = cast[Window](windowPointer)
inputs.dismissInputMenu(window)
if inputs.active and button >= Key0:
var event = inputs.modifiers(window)
event.kind = KeyDown
Expand Down Expand Up @@ -254,6 +272,8 @@ proc beginInputFrame*(inputs: TextInputs, window: Window) =
inputs.consumed = false
inputs.pendingText = TextInputState()
inputs.placementSerial = 0
when defined(ios) or defined(silkyTesting):
inputs.menuPending = false

proc textPlacementMark*(inputs: TextInputs): int =
## Marks the start of a deferred layout containing a focused editor.
Expand All @@ -269,11 +289,32 @@ proc publishTextInput*(
inputs.placementVertex = firstVertex
inc inputs.placementSerial

proc publishTextMenu*(
inputs: TextInputs, id: string, bounds: Rect,
actions: set[TextEditAction], layer, firstVertex: int
) =
## Defers a native menu until the owning widget's final placement is known.
when defined(ios) or defined(silkyTesting):
if inputs.active and inputs.id == id and actions != {}:
inc inputs.placementSerial
inputs.menuPending = true
inputs.menuId = id
inputs.menuEpoch = inputs.epoch
inputs.menuBounds = bounds
inputs.menuActions = actions
inputs.menuSerial = inputs.placementSerial
inputs.menuLayer = layer
inputs.menuVertex = firstVertex

proc translateTextInput*(inputs: TextInputs, first: int, offset: Vec2) =
## Moves an editor published inside a deferred layout span.
if inputs != nil and inputs.placementSerial > first:
inputs.pendingText.bounds.x += offset.x
inputs.pendingText.bounds.y += offset.y
when defined(ios) or defined(silkyTesting):
if inputs != nil and inputs.menuPending and inputs.menuSerial > first:
inputs.menuBounds.x += offset.x
inputs.menuBounds.y += offset.y

proc translateTextInputVertices*(
inputs: TextInputs, layer, first: int, offset: Vec2
Expand All @@ -283,11 +324,23 @@ proc translateTextInputVertices*(
inputs.placementLayer == layer and inputs.placementVertex >= first:
inputs.pendingText.bounds.x += offset.x
inputs.pendingText.bounds.y += offset.y
when defined(ios) or defined(silkyTesting):
if inputs != nil and inputs.menuPending and inputs.menuLayer == layer and
inputs.menuVertex >= first:
inputs.menuBounds.x += offset.x
inputs.menuBounds.y += offset.y

proc moveTextInputBehind*(
inputs: TextInputs, layer, spanStart, chromeStart, total: int
) =
## Preserves the editor's vertex position when parent chrome is reordered.
when defined(ios) or defined(silkyTesting):
if inputs != nil and inputs.menuPending and inputs.menuLayer == layer and
inputs.menuVertex >= spanStart and inputs.menuVertex < total:
if inputs.menuVertex < chromeStart:
inputs.menuVertex += total - chromeStart
else:
inputs.menuVertex -= chromeStart - spanStart
if inputs == nil or inputs.placementSerial == 0:
return
if inputs.placementLayer != layer or
Expand All @@ -310,6 +363,7 @@ proc submitInput*(
inputs.stopInput(window)
return
if not inputs.active or inputs.id != id:
inputs.dismissInputMenu(window)
inputs.events.setLen(0)
inc inputs.epoch
inputs.id = id
Expand All @@ -332,6 +386,50 @@ proc takeInput*(inputs: TextInputs): seq[InputEvent] =
inputs.consumed = true
swap(result, inputs.events)

when defined(ios) or defined(silkyTesting):
proc showInputMenu(inputs: TextInputs, window: Window) =
## Allocates a native action callback only when a menu is requested.
let
id = inputs.menuId
epoch = inputs.menuEpoch
actions = inputs.menuActions
generation = inputs.menuGeneration + 1
inputs.menuGeneration = generation
inputs.menuVisible = window.showTextEditMenu(
inputs.menuBounds, actions,
proc(action: TextEditAction) =
## Routes native actions through the current field's input hooks.
if not inputs.active or inputs.id != id or
inputs.epoch != epoch or inputs.menuGeneration != generation or
action notin actions:
return
inputs.menuVisible = false
inc inputs.menuGeneration
if action == PasteTextAction:
if not inputs.editable:
return
if inputs.onPaste != nil and inputs.onPaste(id):
if inputs.onEvent != nil:
inputs.onEvent()
return
if not inputs.active or inputs.id != id or
not inputs.editable or inputs.epoch != epoch:
return
let text = getClipboardString()
if text.len > 0:
inputs.addEvent(InputEvent(kind: TextCommit, text: text))
else:
let button =
case action
of CopyTextAction: KeyC
of CutTextAction: KeyX
of SelectAllTextAction: KeyA
of PasteTextAction: KeyV
inputs.addEvent(InputEvent(
kind: KeyDown, button: button, control: true
))
)

proc endInputFrame*(inputs: TextInputs, window: Window) =
## Ends capture when the active field was not submitted this frame.
if not inputs.submitted:
Expand All @@ -340,3 +438,8 @@ proc endInputFrame*(inputs: TextInputs, window: Window) =
window.setTextInputState(inputs.pendingText)
else:
window.setTextInputState(TextInputState())

when defined(ios) or defined(silkyTesting):
if inputs.menuPending and inputs.active and inputs.submitted and
inputs.id == inputs.menuId and inputs.epoch == inputs.menuEpoch:
inputs.showInputMenu(window)
41 changes: 41 additions & 0 deletions src/silky/internal/textpresses.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import vmath

const
TextPressDelay* = 500.0
TextPressSlop = 6.0'f

type
TextPressResult* = enum
NoTextPress, WaitingTextPress, HeldTextPress, ReleasedTextPress

TextPress* = object
active*: bool
preserve*: bool
position*: Vec2
started*: float64

proc beginTextPress*(position: Vec2, nowMs: float64, preserve: bool): TextPress =
## Records a stationary touch without discarding an existing selection.
TextPress(active: true, position: position, started: nowMs, preserve: preserve)

proc updateTextPress*(
press: var TextPress, point: Vec2, down, released, cancelled: bool,
nowMs: float64
): TextPressResult =
## Recognizes a hold while rejecting pans and interrupted gestures.
if not press.active:
return NoTextPress
let distance = point - press.position
if cancelled or max(abs(distance.x), abs(distance.y)) >= TextPressSlop:
press.active = false
return NoTextPress
if released:
press.active = false
return ReleasedTextPress
if not down:
press.active = false
return NoTextPress
if nowMs - press.started >= TextPressDelay:
press.active = false
return HeldTextPress
WaitingTextPress
Loading
Loading