From 7b3679d49a998496fc268238c68bb45fe87390e2 Mon Sep 17 00:00:00 2001 From: lwtt <1377477209@qq.com> Date: Tue, 28 Nov 2023 20:04:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9textInutChannel=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E4=BB=A3=E7=A0=81=EF=BC=8C=E4=BF=AE=E5=A4=8DonSubmit?= =?UTF-8?q?=E6=97=A0=E6=B3=95=E8=A7=A6=E5=8F=91=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lwtt <1377477209@qq.com> --- .../systemchannels/TextInputChannel.ets | 135 +++++++++++++----- .../plugin/editing/ListenableEditingState.ets | 26 +++- .../ets/plugin/editing/TextInputPlugin.ets | 11 +- 3 files changed, 132 insertions(+), 40 deletions(-) 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 97697f6d73..64de036552 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 @@ -19,6 +19,8 @@ import MethodChannel, { MethodCallHandler, MethodResult } from '../../../plugin/ import TextInputPlugin from '../../../plugin/editing/TextInputPlugin'; import Log from '../../../util/Log'; import DartExecutor from '../dart/DartExecutor'; +import inputMethod from '@ohos.inputMethod'; +import { DEFAULT } from '@ohos/hypium'; const TAG = "TextInputChannel"; @@ -46,11 +48,11 @@ export default class TextInputChannel { composingStart: number, composingEnd: number): EditingState { let state: EditingState = { - text: text, - selectionBase: selectionStart, - selectionExtent: selectionEnd, - composingBase: composingStart, - composingExtent: composingEnd + text: text, + selectionBase: selectionStart, + selectionExtent: selectionEnd, + composingBase: composingStart, + composingExtent: composingEnd }; return state; } @@ -123,11 +125,11 @@ export default class TextInputChannel { } interface EditingState { - text: string; - selectionBase: number; - selectionExtent: number; - composingBase: number; - composingExtent: number; + text: string; + selectionBase: number; + selectionExtent: number; + composingBase: number; + composingExtent: number; } export interface TextInputMethodHandler { @@ -154,6 +156,7 @@ export interface TextInputMethodHandler { export class Configuration { obscureText: boolean = false; autocorrect: boolean = false; + autofill: boolean = false; enableSuggestions: boolean = false; enableIMEPersonalizedLearning: boolean = false; enableDeltaModel: boolean = false; @@ -169,23 +172,95 @@ export class Configuration { enableSuggestions: boolean, enableIMEPersonalizedLearning: boolean, enableDeltaModel: boolean, + textCapitalization:TextCapitalization, inputType: InputType, inputAction: Number, - actionLabel: String, ) { + actionLabel: String, + autofill :boolean, + contentListString:[], + fields: Configuration[] + ) { + this.obscureText = obscureText; + this.autocorrect = autocorrect; + this.enableSuggestions = enableSuggestions; + this.enableIMEPersonalizedLearning = enableIMEPersonalizedLearning; + this.textCapitalization = textCapitalization + this.enableDeltaModel = enableDeltaModel; + this.inputType = inputType; + this.inputAction = inputAction; + this.actionLabel = actionLabel; + this.autofill = autofill; + this.contentCommitMimeTypes = contentListString; + this.fields = fields + } - getTextCapitalizationFromValue(encodedName: string): TextCapitalization { - let textKeys = [ - TextCapitalization.CHARACTERS, TextCapitalization.WORDS, - TextCapitalization.SENTENCES, TextCapitalization.NONE - ]; - for (let i = 0; i < textKeys.length; i++) { - let key = textKeys[i]; - if (TextCapitalization[key] === encodedName) { - return key; + static getTextCapitalizationFromValue(encodedName: string): TextCapitalization { + let textKeys= ["CHARACTERS", "WORDS","SENTENCES", "NONE"]; + for (let textKey of textKeys) { + if (TextCapitalization[textKey] === encodedName) { + return textKey as TextCapitalization; } } throw new Error("No such TextCapitalization: " + encodedName); } + private static inputActionFromTextInputAction(inputActionName:string):number{ + switch(inputActionName){ + case "TextInputAction.previous": + return inputMethod.EnterKeyType.PREVIOUS + case "TextInputAction.unspecified": + return inputMethod.EnterKeyType.UNSPECIFIED + case "TextInputAction.none": + return inputMethod.EnterKeyType.NONE + case "TextInputAction.go": + return inputMethod.EnterKeyType.GO + case "TextInputAction.search": + return inputMethod.EnterKeyType.SEARCH + case "TextInputAction.send": + return inputMethod.EnterKeyType.SEND + case "TextInputAction.next": + return inputMethod.EnterKeyType.NEXT + default: + // Present default key if bad input type is given. + return inputMethod.EnterKeyType.UNSPECIFIED + + } + } + static fromJson(json:ESObject) { + const inputActionName:string = json.inputAction; + if (!inputActionName) { + throw new Error("Configuration JSON missing 'inputAction' property."); + } + + let fields:Array = null; + if (json.fields !== null && json.fields !== undefined) { + fields = json.fields.map((field:ESObject):ESObject => Configuration.fromJson(field)); + } + + const inputAction:number = Configuration.inputActionFromTextInputAction(inputActionName); + + // Build list of content commit mime types from the data in the JSON list. + const contentList:Array = []; + if (json.contentCommitMimeTypes !== null && json.contentCommitMimeTypes !== undefined) { + json.contentCommitMimeTypes.forEach((type:ESObject) => { + contentList.push(type); + }); + } + return new Configuration( + json.obscureText ?? false, + json.autocorrect ?? true, + json.enableSuggestions ?? false, + json.enableIMEPersonalizedLearning ?? false, + json.enableDeltaModel ?? false, + Configuration.getTextCapitalizationFromValue(json.textCapitalization), + InputType.fromJson(json.inputType), + inputAction, + json.actionLabel ?? null, + json.autofill ?? null, + contentList as ESObject, + fields + ); + } + } enum TextCapitalization { @@ -226,16 +301,10 @@ export class InputType { } static getTextInputTypeFromValue(encodedName: string): TextInputType { - let textKeys = [ - TextInputType.TEXT, TextInputType.DATETIME, TextInputType.NAME, - TextInputType.POSTAL_ADDRESS, TextInputType.NUMBER, TextInputType.PHONE, - TextInputType.MULTILINE, TextInputType.EMAIL_ADDRESS, TextInputType.URL, - TextInputType.VISIBLE_PASSWORD, TextInputType.NONE - ]; - for (let i = 0; i < textKeys.length; i++) { - let key = textKeys[i]; - if (TextInputType[key] == encodedName) { - return key; + let textKeys = [ "TEXT", "DATETIME","NAME" , "POSTAL_ADDRESS", "NUMBER", "PHONE", "MULTILINE", "EMAIL_ADDRESS", "URL", "VISIBLE_PASSWORD", "NONE" ,]; + for (let textKey of textKeys) { + if (TextInputType[textKey] === encodedName) { + return textKey as TextInputType; } } throw new Error("No such TextInputType: " + encodedName); @@ -331,8 +400,9 @@ class TextInputCallback implements MethodCallHandler { break; case "TextInput.setClient": const textInputClientId: number = args[0] as number; - //TODO: parse configuration - const config: Configuration | null = null; + const jsonConfiguration :string = args[1] ; + const config: Configuration | null = Configuration.fromJson(jsonConfiguration); + this.textInputMethodHandler.setClient(textInputClientId, config); result.success(null); break; @@ -369,4 +439,5 @@ class TextInputCallback implements MethodCallHandler { break; } } + } diff --git a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets index 1ea2049bd2..cac4a34cb7 100644 --- a/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets +++ b/shell/platform/ohos/flutter_embedding/flutter/src/main/ets/plugin/editing/ListenableEditingState.ets @@ -18,9 +18,12 @@ import Log from '../../util/Log'; import inputMethod from '@ohos.inputMethod'; import ArrayList from '@ohos.util.ArrayList'; import { TextEditingDelta } from './TextEditingDelta'; - +import TextInputChannel from '../../embedding/engine/systemchannels/TextInputChannel'; const TAG = "ListenableEditingState"; export class ListenableEditingState { + + private TextInputChannel: TextInputChannel|null = null ; + private client: number = 0 //Cache used to storage software keyboard input action private mStringCache: string; private mSelectionStartCache: number = 0; @@ -42,7 +45,9 @@ export class ListenableEditingState { private mComposingEndWhenBeginBatchEdit: number = 0; - constructor() { + constructor(TextInputChannel:TextInputChannel,client:number) { + this.TextInputChannel = TextInputChannel; + this.client = client this.mStringCache = ""; this.mTextWhenBeginBatchEdit = ""; this.mSelectionStartCache = 0; @@ -237,14 +242,29 @@ export class ListenableEditingState { handleFunctionKey(functionKey: inputMethod.FunctionKey): void { switch (functionKey.enterKeyType) { case inputMethod.EnterKeyType.PREVIOUS: + this.TextInputChannel.previous(this.client); + break; case inputMethod.EnterKeyType.UNSPECIFIED: + this.TextInputChannel.unspecifiedAction(this.client); + break; case inputMethod.EnterKeyType.NONE: + this.TextInputChannel.done(this.client) + break; case inputMethod.EnterKeyType.GO: + this.TextInputChannel.go(this.client); + break; case inputMethod.EnterKeyType.SEARCH: + this.TextInputChannel.search(this.client); + break; case inputMethod.EnterKeyType.SEND: + this.TextInputChannel.send(this.client); + break; case inputMethod.EnterKeyType.NEXT: + this.TextInputChannel.next(this.client); + break; case inputMethod.EnterKeyType.DONE: - + this.TextInputChannel.done(this.client) + 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 16aa44aa78..5a47a5062c 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 @@ -65,18 +65,18 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { private configuration: Configuration | null = null; mEditable: ListenableEditingState; private mRestartInputPending: boolean = false; - private plugin: EditingStateWatcher; + private plugin: EditingStateWatcher | ESObject; private imcFlag: boolean = false; - constructor(plugin: TextInputPlugin) { + constructor(plugin: TextInputPlugin | ESObject) { this.textConfig = { inputAttribute: { textInputType: 0, enterKeyType: 1 }}; this.plugin = plugin; - this.mEditable = new ListenableEditingState(); + this.mEditable = new ListenableEditingState(null , 0); this.inputMethodController = inputMethod.getController(); this.inputTarget = new InputTarget(Type.NO_TARGET, 0); } @@ -147,7 +147,7 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { Log.e(TextInputMethodHandlerImpl.TAG, "Failed to attach:" + JSON.stringify(err)); } } - + setTextInputClient(client: number, configuration: Configuration | null): void { this.configuration = configuration; if(this.canShowTextInput()) { @@ -156,7 +156,8 @@ class TextInputMethodHandlerImpl implements TextInputMethodHandler { this.inputTarget = new InputTarget(Type.NO_TARGET, client); } this.mEditable.removeEditingStateListener(this.plugin); - this.mEditable = new ListenableEditingState(); + + this.mEditable = new ListenableEditingState(this.plugin.textInputChannel, this.inputTarget.id); this.mRestartInputPending = true; this.mEditable.addEditingStateListener(this.plugin); -- Gitee