@@ -22,42 +22,37 @@ using namespace impl;
2222
2323InputMethod::InputMethod (QObject* parent): QObject(parent) { this ->getInput (); }
2424
25- void InputMethod::sendString (const QString& text) {
25+ void InputMethod::commitString (const QString& text) {
2626 if (!this ->isActive ()) return ;
2727
2828 this ->handle ->commitString (text);
29- this ->handle ->commit ();
3029}
3130
32- void InputMethod::sendPreeditString ( const QString& text, int32_t cursorBegin, int32_t cursorEnd ) {
31+ void InputMethod::setPreeditString ( ) {
3332 if (!this ->isActive ()) return ;
3433
35- this ->handle ->sendPreeditString (text, cursorBegin, cursorEnd);
36- this ->handle ->commit ();
34+ if (mCursorEnd == -1 ) {
35+ this ->handle ->setPreeditString (mPreeditString , mCursorBegin , mCursorBegin );
36+ } else {
37+ this ->handle ->setPreeditString (mPreeditString , mCursorBegin , mCursorEnd );
38+ }
3739}
3840
39- void InputMethod::deleteText (int before, int after) {
41+ void InputMethod::deleteSuroundingText (int before, int after) {
4042 if (!this ->isActive ()) return ;
4143
4244 this ->handle ->deleteText (before, after);
43- this ->handle ->commit ();
4445}
4546
46- bool InputMethod::isActive () const { return this ->hasInput () && this ->handle ->isActive (); }
47+ void InputMethod::commit () {
48+ if (!this ->isActive ()) return ;
4749
48- QQmlComponent* InputMethod::keyboardComponent () const { return this ->mKeyboardComponent ; }
49- void InputMethod::setKeyboardComponent (QQmlComponent* keyboardComponent) {
50- if (this ->mKeyboardComponent == keyboardComponent) return ;
51- this ->mKeyboardComponent = keyboardComponent;
52- emit this ->keyboardComponentChanged ();
50+ this ->handle ->commit ();
51+ }
5352
54- if (this ->keyboard ) {
55- this ->keyboard ->deleteLater ();
56- this ->keyboard = nullptr ;
57- }
53+ bool InputMethod::isActive () const { return this ->hasInput () && this ->handle ->isActive (); }
5854
59- this ->handleKeyboardActive ();
60- }
55+ QPointer<Keyboard> InputMethod::keyboard () const { return this ->mKeyboard ; }
6156
6257bool InputMethod::hasInput () const { return this ->handle && this ->handle ->isAvailable (); }
6358
@@ -108,10 +103,10 @@ void InputMethod::releaseInput() {
108103
109104bool InputMethod::hasKeyboard () const {
110105 // The lifetime of keyboard should be less than handle's
111- if (this ->keyboard ) {
106+ if (this ->mKeyboard ) {
112107 assert (this ->handle ->hasKeyboard ());
113108 }
114- return this ->keyboard ;
109+ return this ->mKeyboard ;
115110}
116111
117112void InputMethod::grabKeyboard () {
@@ -120,44 +115,71 @@ void InputMethod::grabKeyboard() {
120115 qmlDebug (this ) << " Only one input method can grad a keyboard at any one time" ;
121116 return ;
122117 }
123- auto * instanceObj =
124- this ->mKeyboardComponent ->create (QQmlEngine::contextForObject (this ->mKeyboardComponent ));
125- auto * instance = qobject_cast<Keyboard*>(instanceObj);
126-
127- if (instance == nullptr ) {
128- qWarning () << " Failed to create input method keyboard component" ;
129- if (instanceObj != nullptr ) instanceObj->deleteLater ();
130- return ;
131- }
118+ this ->mKeyboard = new Keyboard (this );
132119
133- instance->setParent (this );
134- instance->setKeyboard (this ->handle ->grabKeyboard ());
120+ this ->mKeyboard ->setKeyboard (this ->handle ->grabKeyboard ());
135121 // Always have a way to release the keyboard
136- QObject::connect (instance , &Keyboard::escapePress, this , &InputMethod::releaseKeyboard);
122+ QObject::connect (this -> mKeyboard , &Keyboard::escapePress, this , &InputMethod::releaseKeyboard);
137123
138- this ->keyboard = instance;
139124 emit this ->hasKeyboardChanged ();
140125}
141126
142127void InputMethod::releaseKeyboard () {
143128 if (!this ->hasKeyboard ()) return ;
144- this ->keyboard -> deleteLater () ;
145- this ->keyboard = nullptr ;
129+ delete this ->mKeyboard ;
130+ this ->mKeyboard = nullptr ;
146131 this ->handle ->releaseKeyboard ();
147- if (this ->mClearPreeditOnKeyboardRelease ) this ->sendPreeditString (" " );
132+ if (this ->mClearPreeditOnKeyboardRelease ) {
133+ this ->mPreeditString = " " ;
134+ this ->mCursorBegin = -1 ;
135+ this ->mCursorEnd = -1 ;
136+ this ->setPreeditString ();
137+ this ->preeditStringChanged ();
138+ this ->cursorBeginChanged ();
139+ this ->cursorEndChanged ();
140+ }
148141 emit this ->hasKeyboardChanged ();
149142}
150143
144+ const QString& InputMethod::preeditString () const { return this ->mPreeditString ; }
145+ QString& InputMethod::preeditString () { return this ->mPreeditString ; }
146+ void InputMethod::setPreeditString (const QString& string) {
147+ if (this ->mPreeditString == string) return ;
148+ this ->mPreeditString = string;
149+ this ->setPreeditString ();
150+ this ->commit ();
151+ this ->preeditStringChanged ();
152+ }
153+ int32_t InputMethod::cursorBegin () const { return this ->mCursorBegin ; }
154+ void InputMethod::setCursorBegin (int32_t position) {
155+ if (this ->mCursorBegin == position) return ;
156+ this ->mCursorBegin = position;
157+ this ->setPreeditString ();
158+ this ->commit ();
159+ this ->cursorBeginChanged ();
160+ }
161+ int32_t InputMethod::cursorEnd () const { return this ->mCursorEnd ; }
162+ void InputMethod::setCursorEnd (int32_t position) {
163+ if (this ->mCursorEnd == position) return ;
164+ this ->mCursorEnd = position;
165+ this ->setPreeditString ();
166+ this ->commit ();
167+ this ->cursorEndChanged ();
168+ }
169+
151170void InputMethod::handleKeyboardActive () {
152- if (!this ->mKeyboardComponent ) return ;
153- if (this ->keyboard ) {
171+ if (this ->mKeyboard ) {
154172 this ->releaseKeyboard ();
155173 }
156174}
157175
158176const QString& InputMethod::surroundingText () const { return this ->handle ->surroundingText (); }
159- uint32_t InputMethod::surroundingTextCursor () const { return this ->handle ->surroundingTextCursor (); }
160- uint32_t InputMethod::surroundingTextAnchor () const { return this ->handle ->surroundingTextAnchor (); }
177+ uint32_t InputMethod::surroundingTextCursor () const {
178+ return this ->handle ->surroundingTextCursor ();
179+ }
180+ uint32_t InputMethod::surroundingTextAnchor () const {
181+ return this ->handle ->surroundingTextAnchor ();
182+ }
161183
162184QMLContentHint::Enum InputMethod::contentHint () const { return this ->handle ->contentHint (); }
163185QMLContentPurpose::Enum InputMethod::contentPurpose () const {
0 commit comments