Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit 9a34a14

Browse files
committed
Add latency parameter for speculative typing
1 parent c5b949d commit 9a34a14

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

projector-client-common/src/jsMain/kotlin/org/jetbrains/projector/client/common/misc/ParamsProvider.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ actual object ParamsProvider {
6666
private const val DEFAULT_REPAINT_INTERVAL_MS = 333
6767
private const val DEFAULT_IMAGE_CACHE_SIZE_CHARS = 5_000_000
6868
private const val DEFAULT_BLOCK_CLOSING = true
69+
private const val DEFAULT_SPECULATIVE_TYPING_LATENCY = 0
6970

7071
val SYSTEM_SCALING_RATIO
7172
get() = window.devicePixelRatio // get every time because it can be changed
@@ -101,6 +102,7 @@ actual object ParamsProvider {
101102
actual val IMAGE_CACHE_SIZE_CHARS: Int
102103
val BLOCK_CLOSING: Boolean
103104
val LAYOUT_TYPE: LayoutType
105+
val SPECULATIVE_TYPING_LATENCY: Int
104106
val SCALING_RATIO: Double
105107
get() = SYSTEM_SCALING_RATIO * USER_SCALING_RATIO
106108

@@ -162,6 +164,7 @@ actual object ParamsProvider {
162164
"frAzerty" -> LayoutType.FR_AZERTY
163165
else -> LayoutType.JS_DEFAULT
164166
}
167+
SPECULATIVE_TYPING_LATENCY = searchParams.get("speculativeTypingLatency")?.toIntOrNull() ?: DEFAULT_SPECULATIVE_TYPING_LATENCY
165168
}
166169
}
167170

projector-client-web/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Name | Type | Default value | Description
6666
`cacheSize` | Int | `5M` | Set size of cache for images in Chars.
6767
`blockClosing` | Boolean | `true` | Enable blocking of accidental closing of the web page
6868
`relayServerId` | String? | Not present | Identifier of Projector server to connect to for relay connection. Warning: Static files must be accessed via https when relay is used.
69+
`speculativeTypingLatency` | Int | `0` | Sets latency before key press event is sent to server if speculative symbol for the event was drawn.
6970

7071
## Shortcuts
7172
- `Ctrl + F10` prints statistics to the browser console. Example:

projector-client-web/src/main/kotlin/org/jetbrains/projector/client/web/speculative/Typing.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ sealed class Typing {
3939

4040
abstract fun removeSpeculativeImage()
4141

42-
abstract fun addEventChar(event: ClientKeyPressEvent)
42+
abstract fun addEventChar(event: ClientKeyPressEvent): Boolean
4343

4444
abstract fun dispose()
4545

@@ -53,8 +53,8 @@ sealed class Typing {
5353
// do nothing
5454
}
5555

56-
override fun addEventChar(event: ClientKeyPressEvent) {
57-
// do nothing
56+
override fun addEventChar(event: ClientKeyPressEvent): Boolean {
57+
return false
5858
}
5959

6060
override fun dispose() {
@@ -124,14 +124,14 @@ sealed class Typing {
124124
|| event.char.category.fromOtherUnicodeGroup
125125
}
126126

127-
override fun addEventChar(event: ClientKeyPressEvent) {
128-
if (shouldSkipEvent(event)) return
127+
override fun addEventChar(event: ClientKeyPressEvent): Boolean {
128+
if (shouldSkipEvent(event)) return false
129129

130-
val currentCarets = carets as? ServerCaretInfoChangedEvent.CaretInfoChange.Carets ?: return
130+
val currentCarets = carets as? ServerCaretInfoChangedEvent.CaretInfoChange.Carets ?: return false
131131

132-
val canvas = canvasByIdGetter(currentCarets.editorWindowId) ?: return
132+
val canvas = canvasByIdGetter(currentCarets.editorWindowId) ?: return false
133133

134-
val firstCaretLocation = currentCarets.caretInfoList.firstOrNull()?.locationInWindow ?: return // todo: support multiple carets
134+
val firstCaretLocation = currentCarets.caretInfoList.firstOrNull()?.locationInWindow ?: return false // todo: support multiple carets
135135

136136
ensureSpeculativeCanvasSize(canvas)
137137

@@ -182,6 +182,8 @@ sealed class Typing {
182182

183183
speculativeCanvasImage.style.display = "block"
184184
canvas.style.display = "none"
185+
186+
return true
185187
}
186188

187189
private fun ensureSpeculativeCanvasSize(canvas: HTMLCanvasElement) {

projector-client-web/src/main/kotlin/org/jetbrains/projector/client/web/state/ClientState.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -508,12 +508,25 @@ sealed class ClientState {
508508
is ClientAction.AddEvent -> {
509509
val event = action.event
510510

511+
fun addEvent() {
512+
eventsToSend.add(event)
513+
messagingPolicy.onAddEvent()
514+
}
515+
516+
var latency = 0
517+
511518
if (event is ClientKeyPressEvent) {
512-
typing.addEventChar(event)
519+
val added = typing.addEventChar(event)
520+
if (added) {
521+
latency = ParamsProvider.SPECULATIVE_TYPING_LATENCY
522+
}
513523
}
514524

515-
eventsToSend.add(event)
516-
messagingPolicy.onAddEvent()
525+
if (latency > 0) {
526+
window.setTimeout(::addEvent, latency)
527+
} else {
528+
addEvent()
529+
}
517530

518531
this
519532
}

0 commit comments

Comments
 (0)