Skip to content

Commit 27b5625

Browse files
authored
workaround an intermittent crash on focus (#45)
1 parent 8ae74e5 commit 27b5625

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

entry/src/main/ets/InputMethodExtensionAbility/model/KeyboardController.ets

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export class KeyboardController {
1919
private port: webview.WebMessagePort | null = null
2020
private textInputClient: inputMethodEngine.InputClient | undefined = undefined;
2121
private keyboardController: inputMethodEngine.KeyboardController | undefined = undefined;
22+
private initialized = false
2223
private preedit = ''
2324
private preeditIndex = 0 // Bad initial value that must be reset on focus.
2425
private textWithPreedit = ''
@@ -372,6 +373,21 @@ export class KeyboardController {
372373
return this.processResult(res)
373374
}
374375

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+
375391
private registerListener(): void {
376392
this.registerInputListener();
377393
keyboardDelegate.on('keyDown', (e) => {
@@ -398,6 +414,7 @@ export class KeyboardController {
398414
// then double click 的 to select. The last step won't update selection to 1,2.
399415
keyboardDelegate.on('selectionChange', (oldBegin: number, oldEnd: number, newBegin: number, newEnd: number) => {
400416
console.debug(`selectionChange ${oldBegin} ${oldEnd} ${newBegin} ${newEnd}`)
417+
this.initHelper()
401418
this.selectionStart = newBegin
402419
this.selectionEnd = newEnd
403420
// Because selectByMovementSync won't call this, doing below won't interrupt selection when start and end overlap.
@@ -412,17 +429,12 @@ export class KeyboardController {
412429
private registerInputListener(): void {
413430
ability.on('inputStart', (kbController, textInputClient) => {
414431
console.debug('inputStart')
432+
this.initialized = false
415433
this.textInputClient = textInputClient;
416434
this.keyboardController = kbController;
417435
this.attribute = textInputClient.getEditorAttributeSync()
418436
fcitx.focusIn(this.attribute.isTextPreviewSupported)
419437
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)
426438
})
427439
ability.on('inputStop', () => {
428440
// This is not paired with inputStart. Only called when switching to Celia.

entry/src/main/ets/InputMethodExtensionAbility/model/TextOperation.ets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type TextOperation = InsertOperation | DeleteOperation | ReplaceOperation
2424

2525
const undoes: TextOperation[] = []
2626
const redoes: TextOperation[] = []
27-
let currentText: string
27+
let currentText = ''
2828
let isUndoRedo = false
2929
const STACK_LIMIT = 1024
3030
const MERGE_TIMEOUT = 5000

0 commit comments

Comments
 (0)