Skip to content

Commit 8c35f43

Browse files
committed
Respond to review
1 parent 79ed7ee commit 8c35f43

File tree

9 files changed

+218
-233
lines changed

9 files changed

+218
-233
lines changed

src/wayland/input_method/input_method.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <qdebug.h>
55
#include <qlogging.h>
66
#include <qobject.h>
7-
#include <qpointer.h>
87
#include <qstring.h>
98
#include <qtmetamacros.h>
109
#include <qwayland-input-method-unstable-v2.h>
@@ -21,12 +20,12 @@ InputMethodHandle::InputMethodHandle(QObject* parent, ::zwp_input_method_v2* inp
2120
, zwp_input_method_v2(inputMethod) {}
2221

2322
InputMethodHandle::~InputMethodHandle() {
24-
this->sendPreeditString("");
23+
this->setPreeditString("");
2524
this->destroy();
2625
}
2726

2827
void InputMethodHandle::commitString(const QString& text) { this->commit_string(text); }
29-
void InputMethodHandle::sendPreeditString(
28+
void InputMethodHandle::setPreeditString(
3029
const QString& text,
3130
int32_t cursorBegin,
3231
int32_t cursorEnd
@@ -39,7 +38,7 @@ void InputMethodHandle::deleteText(int before, int after) {
3938
void InputMethodHandle::commit() { this->zwp_input_method_v2::commit(this->serial++); }
4039

4140
bool InputMethodHandle::hasKeyboard() const { return this->keyboard != nullptr; }
42-
QPointer<InputMethodKeyboardGrab> InputMethodHandle::grabKeyboard() {
41+
InputMethodKeyboardGrab* InputMethodHandle::grabKeyboard() {
4342
if (this->keyboard) return this->keyboard;
4443

4544
this->keyboard = new InputMethodKeyboardGrab(this, this->grab_keyboard());
@@ -48,7 +47,7 @@ QPointer<InputMethodKeyboardGrab> InputMethodHandle::grabKeyboard() {
4847
}
4948
void InputMethodHandle::releaseKeyboard() {
5049
if (!this->keyboard) return;
51-
this->keyboard->deleteLater();
50+
delete this->keyboard;
5251
this->keyboard = nullptr;
5352
}
5453

src/wayland/input_method/input_method.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <cstdint>
44
#include <qobject.h>
5-
#include <qpointer.h>
65
#include <qtclasshelpermacros.h>
76
#include <qwayland-input-method-unstable-v2.h>
87

@@ -25,12 +24,12 @@ class InputMethodHandle
2524

2625
void commitString(const QString& text);
2726
// By default hides the cursor
28-
void sendPreeditString(const QString& text, int32_t cursorBegin = -1, int32_t cursorEnd = -1);
27+
void setPreeditString(const QString& text, int32_t cursorBegin = -1, int32_t cursorEnd = -1);
2928
void deleteText(int before, int after);
3029
void commit();
3130

3231
[[nodiscard]] bool hasKeyboard() const;
33-
QPointer<InputMethodKeyboardGrab> grabKeyboard();
32+
InputMethodKeyboardGrab* grabKeyboard();
3433
void releaseKeyboard();
3534

3635
[[nodiscard]] bool isActive() const;

src/wayland/input_method/keyboard_grab.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@
1616
#include <xkbcommon/xkbcommon-keysyms.h>
1717
#include <xkbcommon/xkbcommon.h>
1818

19+
#include "../../core/logcat.hpp"
1920
#include "manager.hpp"
2021
#include "types.hpp"
2122
#include "virtual_keyboard.hpp"
2223

24+
QS_LOGGING_CATEGORY(inputMethodKeyboardKeys, "quickshell.wayland.inputMethod.keyboardKeys", QtWarningMsg);
25+
2326
namespace qs::wayland::input_method::impl {
2427

2528
InputMethodKeyboardGrab::InputMethodKeyboardGrab(
@@ -91,11 +94,9 @@ void InputMethodKeyboardGrab::zwp_input_method_keyboard_grab_v2_key(
9194

9295
key += WAYLAND_KEY_OFFSET;
9396

94-
#if INPUT_METHOD_PRINT
95-
qDebug() << KeyMapState::keyStateName(static_cast<wl_keyboard_key_state>(state))
97+
qCInfo(inputMethodKeyboardKeys) << KeyMapState::keyStateName(static_cast<wl_keyboard_key_state>(state))
9698
<< this->mKeyMapState.keyName(key) << "[" << key << "]"
9799
<< this->mKeyMapState.getChar(key);
98-
#endif
99100

100101
const xkb_keysym_t sym = this->mKeyMapState.getOneSym(key);
101102

src/wayland/input_method/manager.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ InputMethodManager::InputMethodManager(): QWaylandClientExtensionTemplate(1) { t
1515
InputMethodManager::~InputMethodManager() { this->destroy(); }
1616

1717
InputMethodManager* InputMethodManager::instance() {
18-
// The OS should free this memory when we exit
19-
static auto* instance = new InputMethodManager();
18+
static InputMethodManager* instance = nullptr;
19+
20+
if (instance == nullptr) {
21+
instance = new InputMethodManager();
22+
}
23+
2024
return instance;
2125
}
2226

@@ -37,7 +41,7 @@ QPointer<InputMethodHandle> InputMethodManager::acquireInput() {
3741
}
3842

3943
void InputMethodManager::releaseInput() {
40-
this->inputMethod->deleteLater();
44+
delete this->inputMethod;
4145
this->inputMethod = nullptr;
4246
}
4347

@@ -46,8 +50,11 @@ VirtualKeyboardManager::VirtualKeyboardManager(): QWaylandClientExtensionTemplat
4650
}
4751

4852
VirtualKeyboardManager* VirtualKeyboardManager::instance() {
49-
// The OS should free this memory when we exit
50-
static auto* instance = new VirtualKeyboardManager();
53+
static VirtualKeyboardManager* instance = nullptr;
54+
55+
if (instance == nullptr) {
56+
instance = new VirtualKeyboardManager();
57+
}
5158
return instance;
5259
}
5360

src/wayland/input_method/qml.cpp

Lines changed: 63 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,37 @@ using namespace impl;
2222

2323
InputMethod::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

6257
bool InputMethod::hasInput() const { return this->handle && this->handle->isAvailable(); }
6358

@@ -108,10 +103,10 @@ void InputMethod::releaseInput() {
108103

109104
bool 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

117112
void 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

142127
void 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+
151170
void InputMethod::handleKeyboardActive() {
152-
if (!this->mKeyboardComponent) return;
153-
if (this->keyboard) {
171+
if (this->mKeyboard) {
154172
this->releaseKeyboard();
155173
}
156174
}
157175

158176
const 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

162184
QMLContentHint::Enum InputMethod::contentHint() const { return this->handle->contentHint(); }
163185
QMLContentPurpose::Enum InputMethod::contentPurpose() const {

0 commit comments

Comments
 (0)