diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/systemchannels/TextInputChannel.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/systemchannels/TextInputChannel.ets index eb6eb64f598afa3e74700515599d701b44edd656..454a589053e62b3d50ad22a29a93614dfa352ec7 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/systemchannels/TextInputChannel.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/embedding/engine/systemchannels/TextInputChannel.ets @@ -23,6 +23,9 @@ import inputMethod from '@ohos.inputMethod'; import ArrayList from '@ohos.util.ArrayList'; import { TextEditingDelta, TextEditingDeltaJson } from '../../../plugin/editing/TextEditingDelta'; import Any from '../../../plugin/common/Any'; +import { window } from '@kit.ArkUI'; +import { BusinessError, print } from '@kit.BasicServicesKit'; +import { Rect } from '@kit.ArkGraphics3D'; const TAG = "TextInputChannel"; /// 规避换行标识无法显示问题,api修改后再删除 @@ -47,10 +50,10 @@ export default class TextInputChannel { } createEditingStateJSON(text: string, - selectionStart: number, - selectionEnd: number, - composingStart: number, - composingEnd: number): EditingState { + selectionStart: number, + selectionEnd: number, + composingStart: number, + composingEnd: number): EditingState { let state: EditingState = { text: text, selectionBase: selectionStart, @@ -77,11 +80,11 @@ export default class TextInputChannel { * Instructs Flutter to update its text input editing state to reflect the given configuration. */ updateEditingState(inputClientId: number, - text: string, - selectionStart: number, - selectionEnd: number, - composingStart: number, - composingEnd: number): void { + text: string, + selectionStart: number, + selectionEnd: number, + composingStart: number, + composingEnd: number): void { Log.d(TAG, "updateEditingState:" + "Text: " + text + " Selection start: " + selectionStart + " Selection end: " + selectionEnd + " Composing start: " + composingStart + " Composing end: " + composingEnd); @@ -178,6 +181,8 @@ export interface TextInputMethodHandler { setEditableSizeAndTransform(width: number, height: number, transform: number[]): void; + setCursorSizeAndPosition(cursorInfo: inputMethod.CursorInfo): void; + setEditingState(editingState: TextEditState): void; clearClient(): void; @@ -201,17 +206,17 @@ export class Configuration { fields: Configuration[] = []; constructor(obscureText: boolean, - autocorrect: boolean, - enableSuggestions: boolean, - enableIMEPersonalizedLearning: boolean, - enableDeltaModel: boolean, - textCapitalization: TextCapitalization, - inputType: InputType, - inputAction: Number, - actionLabel: String, - autofill: boolean, - contentListString: [], - fields: Configuration[] + autocorrect: boolean, + enableSuggestions: boolean, + enableIMEPersonalizedLearning: boolean, + enableDeltaModel: boolean, + textCapitalization: TextCapitalization, + inputType: InputType, + inputAction: Number, + actionLabel: String, + autofill: boolean, + contentListString: [], + fields: Configuration[] ) { this.obscureText = obscureText; this.autocorrect = autocorrect; @@ -358,10 +363,10 @@ export class TextEditState { composingEnd: number; constructor(text: string, - selectionStart: number, - selectionEnd: number, - composingStart: number, - composingEnd: number) { + selectionStart: number, + selectionEnd: number, + composingStart: number, + composingEnd: number) { if ((selectionStart != -1 || selectionEnd != -1) && (selectionStart < 0 || selectionEnd < 0)) { throw new Error("invalid selection: (" + selectionStart + ", " + selectionEnd + ")"); @@ -424,9 +429,40 @@ export class TextEditState { class TextInputCallback implements MethodCallHandler { textInputMethodHandler: TextInputMethodHandler; + windowPosition: window.Rect = { + left: 0, + top: 0, + width: 0, + height: 0, + }; + cursorPosition: window.Rect = { + left: 0, + top: 0, + width: 0, + height: 0, + } constructor(handler: TextInputMethodHandler) { this.textInputMethodHandler = handler; + const context = getContext(this) as Context + window.getLastWindow(context, (err: BusinessError, data: window.Window) => { + this.windowPosition = data.getWindowProperties().windowRect as window.Rect; + data.on('windowRectChange', (rect: window.RectChangeOptions) => { + this.windowPosition = rect.rect as window.Rect; + this.setCursorPosition(); + }) + }) + } + + setCursorPosition() { + const left = (this.windowPosition.left as number) + this.cursorPosition.left; + const top = (this.windowPosition.top as number) + this.cursorPosition.top; + this.textInputMethodHandler.setCursorSizeAndPosition({ + left: left, + top: top, + width: 100, + height: 50, + }) } onMethodCall(call: MethodCall, result: MethodResult) { @@ -482,6 +518,12 @@ class TextInputCallback implements MethodCallHandler { //TODO: result.notImplemented(); break; + case "TextInput.setCursorPotion": + let cursorPosition: window.Rect = args[0]; + this.cursorPosition = cursorPosition; + this.setCursorPosition(); + result.success(null); + break; default: result.notImplemented(); break; diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets index 8c75488d7abf4d293511f4cf695106858d3873b4..e120dfeedb3356078d869ac36c8a0dd70930c63e 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/TextInputPlugin.ets @@ -131,6 +131,10 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { } + setCursorSizeAndPosition(cursorInfo: inputMethod.CursorInfo) { + this.inputMethodController.updateCursor(cursorInfo) + } + setEditingState(editingState: TextEditState): void { Log.d(TextInputMethodHandlerImpl.TAG, "text:" + editingState.text + " selectionStart:" + editingState.selectionStart + " selectionEnd:"