Skip to content

Commit d172f95

Browse files
committed
content hint and purpose
1 parent acaaa42 commit d172f95

File tree

9 files changed

+288
-64
lines changed

9 files changed

+288
-64
lines changed

src/wayland/input_method/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ qt_add_library(quickshell-wayland-input-method STATIC
99
qml.cpp
1010
qml_helpers.cpp
1111
c_helpers.cpp
12+
types.cpp
1213
)
1314

1415
target_compile_definitions(quickshell-wayland-input-method PRIVATE INPUT_METHOD_PRINT=0)
@@ -27,10 +28,11 @@ install_qml_module(quickshell-wayland-input-method)
2728

2829
wl_proto(zwp-input-method input-method-unstable-v2 "${CMAKE_CURRENT_SOURCE_DIR}")
2930
wl_proto(zwp-virtual-keyboard virtual-keyboard-unstable-v1 "${CMAKE_CURRENT_SOURCE_DIR}")
31+
wl_proto(zwp-text-input text-input-unstable-v3 "${WAYLAND_PROTOCOLS}/unstable/text-input")
3032

3133
target_link_libraries(quickshell-wayland-input-method PRIVATE
3234
Qt::Quick Qt::WaylandClient Qt::WaylandClientPrivate wayland-client
33-
zwp-input-method zwp-virtual-keyboard
35+
zwp-input-method zwp-virtual-keyboard zwp-text-input
3436
)
3537

3638
qs_module_pch(quickshell-wayland-input-method SET large)

src/wayland/input_method/input_method.cpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,35 @@ void InputMethodHandle::releaseKeyboard() {
5050
this->keyboard = nullptr;
5151
}
5252

53-
bool InputMethodHandle::isActive() const { return this->mActivated; }
53+
bool InputMethodHandle::isActive() const { return this->mState.activated; }
5454
bool InputMethodHandle::isAvailable() const { return this->mAvailable; }
5555

56-
void InputMethodHandle::zwp_input_method_v2_activate() { this->mNewActive = true; }
56+
void InputMethodHandle::zwp_input_method_v2_activate() { this->mNewState.activated = true; }
5757

58-
void InputMethodHandle::zwp_input_method_v2_deactivate() { this->mNewActive = false; }
58+
void InputMethodHandle::zwp_input_method_v2_deactivate() { this->mNewState.activated = false; }
59+
60+
61+
void InputMethodHandle::zwp_input_method_v2_content_type(uint32_t hint, uint32_t purpose) {
62+
this->mNewState.contentHint = static_cast<ContentHint>(hint);
63+
this->mNewState.contentPurpose = static_cast<ContentPurpose>(purpose);
64+
};
5965

6066
void InputMethodHandle::zwp_input_method_v2_done() {
61-
if (this->mActivated == this->mNewActive) return;
62-
this->mActivated = this->mNewActive;
63-
if (this->mActivated) emit activated();
64-
else emit deactivated();
67+
if (this->mState.activated != this->mNewState.activated){
68+
if (this->mNewState.activated) emit activated();
69+
else emit deactivated();
70+
}
71+
72+
if (this->mState.contentHint != this->mNewState.contentHint){
73+
emit contentHintChanged(this->mNewState.contentHint);
74+
}
75+
76+
77+
if (this->mState.contentPurpose != this->mNewState.contentPurpose){
78+
emit contentPurposeChanged(this->mNewState.contentPurpose);
79+
}
80+
81+
this->mState = this->mNewState;
6582
}
6683

6784
void InputMethodHandle::zwp_input_method_v2_unavailable() {

src/wayland/input_method/input_method.hpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <qtclasshelpermacros.h>
66
#include <qwayland-input-method-unstable-v2.h>
77

8+
#include "types.hpp"
9+
810
namespace qs::wayland::input_method::impl {
911

1012
class InputMethodKeyboardGrab;
@@ -37,17 +39,28 @@ class InputMethodHandle
3739
void activated();
3840
void deactivated();
3941

42+
void contentHintChanged(ContentHint);
43+
void contentPurposeChanged(ContentPurpose);
44+
4045
private:
4146
void zwp_input_method_v2_activate() override;
4247
void zwp_input_method_v2_deactivate() override;
48+
void zwp_input_method_v2_content_type(uint32_t hint, uint32_t purpose) override;
4349
void zwp_input_method_v2_done() override;
4450
void zwp_input_method_v2_unavailable() override;
4551

46-
bool mActivated = false;
47-
bool mNewActive = false;
4852
bool mAvailable = true;
4953
int serial = 0;
5054

55+
struct State {
56+
bool activated = false;
57+
ContentHint contentHint = ContentHint::NONE;
58+
ContentPurpose contentPurpose = ContentPurpose::NORMAL;
59+
};
60+
61+
State mState;
62+
State mNewState;
63+
5164
InputMethodKeyboardGrab* keyboard = nullptr;
5265
};
5366

src/wayland/input_method/qml.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ void InputMethod::getInput() {
7777
this,
7878
&InputMethod::onHandleActiveChanged
7979
);
80+
QObject::connect(
81+
this->handle.get(),
82+
&InputMethodHandle::contentHintChanged,
83+
this,
84+
&InputMethod::onContentHintChanged
85+
);
86+
QObject::connect(
87+
this->handle.get(),
88+
&InputMethodHandle::contentPurposeChanged,
89+
this,
90+
&InputMethod::onContentPurposeChanged
91+
);
8092

8193
emit hasInputChanged();
8294
}
@@ -136,11 +148,28 @@ void InputMethod::handleKeyboardActive() {
136148
}
137149
}
138150

151+
QMLContentHint::Enum InputMethod::contentHint() const {
152+
return this->mContentHint;
153+
}
154+
QMLContentPurpose::Enum InputMethod::contentPurpose() const {
155+
return this->mContentPurpose;
156+
}
157+
139158
void InputMethod::onHandleActiveChanged() {
140159
this->handleKeyboardActive();
141160
emit activeChanged();
142161
}
143162

163+
void InputMethod::onContentHintChanged(QMLContentHint::Enum contentHint) {
164+
this->mContentHint = contentHint;
165+
emit contentHintChanged();
166+
}
167+
168+
void InputMethod::onContentPurposeChanged(QMLContentPurpose::Enum contentPurpose){
169+
this->mContentPurpose = contentPurpose;
170+
emit contentPurposeChanged();
171+
}
172+
144173
Keyboard::Keyboard(QObject* parent): QObject(parent) {}
145174

146175
void Keyboard::setKeyboard(QPointer<InputMethodKeyboardGrab> keyboard) {
@@ -161,12 +190,6 @@ void Keyboard::setKeyboard(QPointer<InputMethodKeyboardGrab> keyboard) {
161190
this,
162191
&Keyboard::returnPress
163192
);
164-
QObject::connect(
165-
this->mKeyboard,
166-
&InputMethodKeyboardGrab::directionPress,
167-
this,
168-
&Keyboard::onDirectionPress
169-
);
170193
QObject::connect(
171194
this->mKeyboard,
172195
&InputMethodKeyboardGrab::backspacePress,
@@ -181,21 +204,4 @@ void Keyboard::setKeyboard(QPointer<InputMethodKeyboardGrab> keyboard) {
181204
);
182205
}
183206

184-
void Keyboard::onDirectionPress(impl::DirectionKey direction) {
185-
emit directionPress(KeyboardDirectionKey::fromDirection(direction));
186-
}
187-
188-
QString KeyboardDirectionKey::toString(Enum direction) {
189-
switch (direction) {
190-
case UP: return "UP";
191-
case DOWN: return "DOWN";
192-
case LEFT: return "LEFT";
193-
case RIGHT: return "RIGHT";
194-
}
195-
return "UNKNOWN";
196-
}
197-
KeyboardDirectionKey::Enum KeyboardDirectionKey::fromDirection(impl::DirectionKey direction) {
198-
return static_cast<KeyboardDirectionKey::Enum>(direction);
199-
}
200-
201207
} // namespace qs::wayland::input_method

src/wayland/input_method/qml.hpp

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,6 @@ class InputMethodHandle;
1616
class InputMethodKeyboardGrab;
1717
} // namespace impl
1818

19-
class KeyboardDirectionKey: public QObject {
20-
Q_OBJECT;
21-
QML_NAMED_ELEMENT(DirectionKey);
22-
QML_SINGLETON;
23-
24-
public:
25-
enum Enum : quint8 {
26-
UP = static_cast<quint8>(impl::DirectionKey::UP),
27-
DOWN = static_cast<quint8>(impl::DirectionKey::DOWN),
28-
LEFT = static_cast<quint8>(impl::DirectionKey::LEFT),
29-
RIGHT = static_cast<quint8>(impl::DirectionKey::RIGHT),
30-
};
31-
Q_ENUM(Enum);
32-
33-
Q_INVOKABLE static QString toString(Enum direction);
34-
static Enum fromDirection(impl::DirectionKey direction);
35-
};
36-
3719
/// Provides keyboard handling logic for an @@InputMethod$'s grab protocol.
3820
/// Use @@KeyboardTextEdit$ for a higher level and easier to use version.
3921
class Keyboard: public QObject {
@@ -53,13 +35,10 @@ class Keyboard: public QObject {
5335
void returnPress();
5436
/// Note that internally Quickshell will release the keyboard when escape is pressed.
5537
void escapePress();
56-
void directionPress(KeyboardDirectionKey::Enum);
38+
void directionPress(QMLDirectionKey::Enum);
5739
void backspacePress();
5840
void deletePress();
5941

60-
private slots:
61-
void onDirectionPress(impl::DirectionKey direction);
62-
6342
private:
6443
QPointer<impl::InputMethodKeyboardGrab> mKeyboard = nullptr;
6544
// QQmlComponent* mSurfaceComponent = nullptr;
@@ -87,12 +66,15 @@ class InputMethod: public QObject {
8766
Q_PROPERTY(bool hasInput READ hasInput NOTIFY hasInputChanged);
8867
/// If the input method has grabbed the keyboard
8968
Q_PROPERTY(bool hasKeyboard READ hasKeyboard NOTIFY hasKeyboardChanged);
90-
/// TODO(cmurtagh-lgtm)
69+
/// The content_hint of the text input see https://wayland.app/protocols/text-input-unstable-v3#zwp_text_input_v3:enum:content_hint
70+
Q_PROPERTY(QMLContentHint::Enum contentHint READ contentHint NOTIFY contentHintChanged);
71+
/// The content_purpose of the text input see https://wayland.app/protocols/text-input-unstable-v3#zwp_text_input_v3:enum:content_purpose
72+
Q_PROPERTY(QMLContentPurpose::Enum contentPurpose READ contentPurpose NOTIFY contentPurposeChanged);
73+
/// Clear the preedit text when the keyboard grab is released
9174
Q_PROPERTY(
9275
bool clearPreeditOnKeyboardRelease MEMBER mClearPreeditOnKeyboardRelease NOTIFY
9376
clearPreeditOnKeyboardReleaseChanged
9477
);
95-
// Q_PROPERTY(QString preedit)
9678
/// The @@Keyboard$ that will handle the grabbed keyboard.
9779
/// Use @@KeyboardTextEdit$ for most cases.
9880
Q_PROPERTY(
@@ -140,23 +122,34 @@ class InputMethod: public QObject {
140122
/// Releases the grabbed keyboard so it can be used normally.
141123
Q_INVOKABLE void releaseKeyboard();
142124

125+
Q_INVOKABLE [[nodiscard]] QMLContentHint::Enum contentHint() const;
126+
Q_INVOKABLE [[nodiscard]] QMLContentPurpose::Enum contentPurpose() const;
127+
143128
signals:
144129
void activeChanged();
145130
void hasInputChanged();
146131
void hasKeyboardChanged();
147132
void clearPreeditOnKeyboardReleaseChanged();
148133
void keyboardComponentChanged();
134+
void contentHintChanged();
135+
void contentPurposeChanged();
149136

150137
private slots:
151138
void onHandleActiveChanged();
152139

140+
void onContentHintChanged(QMLContentHint::Enum contentHint);
141+
void onContentPurposeChanged(QMLContentPurpose::Enum contentPurpose);
142+
153143
private:
154144
void handleKeyboardActive();
155145

156146
QPointer<impl::InputMethodHandle> handle;
157147
Keyboard* keyboard = nullptr;
158148
QQmlComponent* mKeyboardComponent = nullptr;
159149

150+
QMLContentHint::Enum mContentHint;
151+
QMLContentPurpose::Enum mContentPurpose;
152+
160153
bool mClearPreeditOnKeyboardRelease = true;
161154
};
162155

src/wayland/input_method/qml_helpers.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ void KeyboardTextEdit::onDeletePress() {
7272
emit editTextChanged();
7373
}
7474

75-
void KeyboardTextEdit::onDirectionPress(KeyboardDirectionKey::Enum direction) {
75+
void KeyboardTextEdit::onDirectionPress(QMLDirectionKey::Enum direction) {
7676
switch (direction) {
77-
case KeyboardDirectionKey::LEFT: {
77+
case QMLDirectionKey::LEFT: {
7878
this->setCursor(this->mCursor - 1);
7979
return;
8080
}
81-
case KeyboardDirectionKey::RIGHT: {
81+
case QMLDirectionKey::RIGHT: {
8282
this->setCursor(this->mCursor + 1);
8383
return;
8484
}

src/wayland/input_method/qml_helpers.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ namespace qs::wayland::input_method {
2121
/// KeyboardTextEdit {
2222
/// transform: function (text: string): string {
2323
/// return {
24-
/// "cool": "😎"
24+
/// "cool": "hi"
2525
/// // etc.
2626
/// }[text];
2727
/// }
@@ -66,7 +66,7 @@ class KeyboardTextEdit: public Keyboard {
6666
private slots:
6767
void onKeyPress(QChar character);
6868
void onReturnPress();
69-
void onDirectionPress(KeyboardDirectionKey::Enum direction);
69+
void onDirectionPress(QMLDirectionKey::Enum direction);
7070
void onBackspacePress();
7171
void onDeletePress();
7272

0 commit comments

Comments
 (0)