|
| 1 | +#include "qml_helpers.hpp" |
| 2 | + |
| 3 | +namespace qs::wayland::input_method { |
| 4 | + |
| 5 | +KeyboardTextEdit::KeyboardTextEdit(QObject* parent): Keyboard(parent) { |
| 6 | + QObject::connect(this, &Keyboard::keyPress, this, &KeyboardTextEdit::onKeyPress); |
| 7 | + QObject::connect(this, &Keyboard::returnPress, this, &KeyboardTextEdit::onReturnPress); |
| 8 | + QObject::connect(this, &Keyboard::directionPress, this, &KeyboardTextEdit::onDirectionPress); |
| 9 | + QObject::connect(this, &Keyboard::backspacePress, this, &KeyboardTextEdit::onBackspacePress); |
| 10 | + QObject::connect(this, &Keyboard::deletePress, this, &KeyboardTextEdit::onDeletePress); |
| 11 | +} |
| 12 | + |
| 13 | +QJSValue KeyboardTextEdit::transform() const { return this->mTransform; } |
| 14 | + |
| 15 | +void KeyboardTextEdit::setTransform(const QJSValue& callback) { |
| 16 | + if (!callback.isCallable()) { |
| 17 | + qmlInfo(this) << "Transform must be a callable function"; |
| 18 | + return; |
| 19 | + } |
| 20 | + this->mTransform = callback; |
| 21 | +} |
| 22 | + |
| 23 | +int KeyboardTextEdit::cursor() const { return this->mCursor; } |
| 24 | +void KeyboardTextEdit::setCursor(int value) { |
| 25 | + if (value < 0) value = 0; |
| 26 | + if (value >= this->mEditText.size()) value = static_cast<int>(this->mEditText.size()); |
| 27 | + if (this->mCursor == value) return; |
| 28 | + this->mCursor = value; |
| 29 | + this->updatePreedit(); |
| 30 | + emit cursorChanged(); |
| 31 | +} |
| 32 | + |
| 33 | +QString KeyboardTextEdit::editText() const { return this->mEditText; } |
| 34 | +void KeyboardTextEdit::setEditText(const QString& value) { |
| 35 | + if (this->mEditText == value) return; |
| 36 | + this->mEditText = value; |
| 37 | + this->updatePreedit(); |
| 38 | + emit editTextChanged(); |
| 39 | +} |
| 40 | + |
| 41 | +void KeyboardTextEdit::updatePreedit() { |
| 42 | + auto* inputMethod = dynamic_cast<InputMethod*>(parent()); |
| 43 | + if (!inputMethod) { |
| 44 | + return; |
| 45 | + } |
| 46 | + inputMethod->sendPreeditString(this->mEditText, this->mCursor, this->mCursor); |
| 47 | +} |
| 48 | + |
| 49 | +void KeyboardTextEdit::onKeyPress(QChar character) { |
| 50 | + this->mEditText.insert(this->mCursor, character); |
| 51 | + this->updatePreedit(); |
| 52 | + emit editTextChanged(); |
| 53 | + this->setCursor(this->mCursor + 1); |
| 54 | +} |
| 55 | +void KeyboardTextEdit::onBackspacePress() { |
| 56 | + if (this->mCursor == 0) return; |
| 57 | + this->mEditText.remove(this->mCursor - 1, 1); |
| 58 | + this->updatePreedit(); |
| 59 | + emit editTextChanged(); |
| 60 | + this->setCursor(this->mCursor - 1); |
| 61 | +} |
| 62 | +void KeyboardTextEdit::onDeletePress() { |
| 63 | + if (this->mCursor == this->mEditText.size()) return; |
| 64 | + this->mEditText.remove(this->mCursor, 1); |
| 65 | + this->updatePreedit(); |
| 66 | + emit editTextChanged(); |
| 67 | +} |
| 68 | + |
| 69 | +void KeyboardTextEdit::onDirectionPress(KeyboardDirectionKey::Enum direction) { |
| 70 | + switch (direction) { |
| 71 | + case KeyboardDirectionKey::LEFT: { |
| 72 | + this->setCursor(this->mCursor - 1); |
| 73 | + return; |
| 74 | + } |
| 75 | + case KeyboardDirectionKey::RIGHT: { |
| 76 | + this->setCursor(this->mCursor + 1); |
| 77 | + return; |
| 78 | + } |
| 79 | + default: return; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void KeyboardTextEdit::onReturnPress() { |
| 84 | + auto* inputMethod = dynamic_cast<InputMethod*>(parent()); |
| 85 | + if (!inputMethod) return; |
| 86 | + QString text = ""; |
| 87 | + if (this->mTransform.isCallable()) |
| 88 | + text = this->mTransform.call(QJSValueList({this->mEditText})).toString(); |
| 89 | + inputMethod->sendString(text); |
| 90 | + this->setEditText(""); |
| 91 | + inputMethod->releaseKeyboard(); |
| 92 | +} |
| 93 | + |
| 94 | +} // namespace qs::wayland::input_method |
0 commit comments