@@ -19,6 +19,7 @@ export class KeyboardController {
19
19
private port: webview.WebMessagePort | null = null
20
20
private textInputClient: inputMethodEngine.InputClient | undefined = undefined;
21
21
private keyboardController: inputMethodEngine.KeyboardController | undefined = undefined;
22
+ private initialized = false
22
23
private preedit = ''
23
24
private preeditIndex = 0 // Bad initial value that must be reset on focus.
24
25
private textWithPreedit = ''
@@ -372,6 +373,21 @@ export class KeyboardController {
372
373
return this.processResult(res)
373
374
}
374
375
376
+ private initHelper() {
377
+ if (this.initialized) {
378
+ return
379
+ }
380
+ try {
381
+ const before = this.getTextBefore()
382
+ this.preeditIndex = before.length
383
+ resetStacks(before + this.getTextAfter())
384
+ this.initialized = true
385
+ } catch (e) { // getTextIndexAtCursorSync can throw.
386
+ console.error(`${e}`)
387
+ setTimeout(() => { this.initHelper() }, 100)
388
+ }
389
+ }
390
+
375
391
private registerListener(): void {
376
392
this.registerInputListener();
377
393
keyboardDelegate.on('keyDown', (e) => {
@@ -398,6 +414,7 @@ export class KeyboardController {
398
414
// then double click 的 to select. The last step won't update selection to 1,2.
399
415
keyboardDelegate.on('selectionChange', (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => {
400
416
console.debug(`selectionChange ${oldBegin} ${oldEnd} ${newBegin} ${newEnd}`)
417
+ this.initHelper()
401
418
this.selectionStart = newBegin
402
419
this.selectionEnd = newEnd
403
420
// Because selectByMovementSync won't call this, doing below won't interrupt selection when start and end overlap.
@@ -412,17 +429,12 @@ export class KeyboardController {
412
429
private registerInputListener(): void {
413
430
ability.on('inputStart', (kbController, textInputClient) => {
414
431
console.debug('inputStart')
432
+ this.initialized = false
415
433
this.textInputClient = textInputClient;
416
434
this.keyboardController = kbController;
417
435
this.attribute = textInputClient.getEditorAttributeSync()
418
436
fcitx.focusIn(this.attribute.isTextPreviewSupported)
419
437
this.setEnterKeyType()
420
- // If we do it synchronously, getTextIndexAtCursorSync highly likely crashes.
421
- setTimeout((): void => {
422
- const before = this.getTextBefore()
423
- this.preeditIndex = before.length
424
- resetStacks(before + this.getTextAfter())
425
- }, 0)
426
438
})
427
439
ability.on('inputStop', () => {
428
440
// This is not paired with inputStart. Only called when switching to Celia.
0 commit comments