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
4 changes: 4 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ jobs:
- run: nim r tests/test_openurl.nim
- run: nim r tests/test_macos.nim
if: matrix.os == 'macos-latest'
- run: nim r tests/test_accessibility.nim
if: matrix.os == 'macos-latest'
- run: nim r tests/test_tempfiles.nim
if: matrix.os == 'macos-latest'
- run: nim r -d:useCpu tests/test_macos.nim
if: matrix.os == 'macos-latest'
- run: nim r -d:useCpu tests/test_accessibility.nim
if: matrix.os == 'macos-latest'
- run: nim r -d:useCpu tests/test_cpu_pixels.nim
if: matrix.os == 'windows-latest'

Expand Down
6 changes: 6 additions & 0 deletions src/windy.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ elif defined(linux):
import windy/platforms/linux/platform

export common, platform, unicode, vmath

when defined(emscripten) or not defined(macosx):
proc setTextInputState*(window: Window, state: TextInputState) =
## Publishes focused editor state for native text services where supported.
## This is a no-op on platforms without native editor-state integration.
discard
29 changes: 29 additions & 0 deletions src/windy/common.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,35 @@ type
Callback* = proc()
ButtonCallback* = proc(button: Button)
RuneCallback* = proc(rune: Rune)
TextInputCallback* = proc(text: string)
## Receives committed text as one edit, instead of individual onRune calls.
## Takes priority over onRune and requires runeInputEnabled.
## Native commits may contain multiple runes and line breaks.
TextInputState* = object
## Describes the focused editor for native text services on macOS.
## Selection and visible ranges use half-open rune offsets.
id*: string
## A nonempty field identity activates the editor; an empty one clears it.
text*: string
selectionStart*, selectionEnd*: int
visibleStart*, visibleEnd*: int
bounds*: Rect
## Bounds use framebuffer pixels measured from the window's top-left.
multiline*, password*: bool

TextInputEditKind* = enum
ReplaceText, SelectText

TextInputEdit* = object
## Requests an edit over a half-open range of rune offsets.
kind*: TextInputEditKind
text*: string
start*, length*: int

TextInputEditCallback* = proc(edit: TextInputEdit)
## Receives native selection or replacement requests on macOS.
## Apply the request to the editor, then publish its state again.
## Requires runeInputEnabled and an active, nonsecure TextInputState.
HttpErrorCallback* = proc(msg: string)
HttpResponseCallback* = proc(response: HttpResponse)
HttpProgressCallback* = proc(completed, total: int)
Expand Down
17 changes: 15 additions & 2 deletions src/windy/internal.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import common, pixie, std/random
import common, pixie, std/[random, unicode]

const
multiClickRadius = 4
Expand Down Expand Up @@ -163,9 +163,22 @@ template handleRuneTemplate*() =
return
if rune.uint32 < 32 or (rune.uint32 > 126 and rune.uint32 < 160):
return
if window.onRune != nil:
if window.onTextInput != nil:
window.onTextInput($rune)
elif window.onRune != nil:
window.onRune(rune)

template handleTextInputTemplate*(value: string) =
## Delivers a committed text edit while preserving legacy rune callbacks.
if window.state.runeInputEnabled:
let text = value
if text.len > 0:
if window.onTextInput != nil:
window.onTextInput(text)
else:
for rune in text.runes:
window.handleRune(rune)

proc addDefaultHeaders*(headers: var seq[HttpHeader]) =
if headers["user-agent"].len == 0:
headers["user-agent"] = "Windy"
Expand Down
2 changes: 2 additions & 0 deletions src/windy/platforms/emscripten/platform.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type
onButtonPress*: ButtonCallback
onButtonRelease*: ButtonCallback
onRune*: RuneCallback
onTextInput*: TextInputCallback
onTextEdit*: TextInputEditCallback
onImeChange*: Callback
onFileDrop*: FileDropCallback

Expand Down
4 changes: 4 additions & 0 deletions src/windy/platforms/linux/x11.nim
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type
onButtonPress*: ButtonCallback
onButtonRelease*: ButtonCallback
onRune*: RuneCallback
onTextInput*: TextInputCallback
onTextEdit*: TextInputEditCallback
onImeChange*: Callback
onFileDrop*: FileDropCallback

Expand Down Expand Up @@ -398,6 +400,8 @@ proc destroy(window: Window) =
proc closed*(window: Window): bool = window.closed

proc close*(window: Window) =
window.onTextInput = nil
window.onTextEdit = nil
destroy window

proc makeContextCurrent*(window: Window) =
Expand Down
Loading
Loading