Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new API setBufferWithRestoreCallback to quickphrase #1232

Merged
merged 1 commit into from
Jan 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions src/modules/quickphrase/quickphrase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class QuickPhraseState : public InputContextProperty {
InputBuffer buffer_;
QuickPhrase *q_;

std::string originalBuffer_;
QuickPhraseRestoreCallback restoreCallback_;

bool typed_ = false;
std::string text_;
std::string prefix_;
Expand All @@ -57,6 +60,8 @@ class QuickPhraseState : public InputContextProperty {
prefix_.clear();
str_.clear();
alt_.clear();
originalBuffer_.clear();
restoreCallback_ = nullptr;
key_ = Key(FcitxKey_None);
ic->inputPanel().reset();
ic->updatePreedit();
Expand Down Expand Up @@ -222,18 +227,29 @@ QuickPhrase::QuickPhrase(Instance *instance)
return;
}
if (keyEvent.key().check(FcitxKey_BackSpace)) {
keyEvent.accept();
if (state->buffer_.empty()) {
state->reset(inputContext);
} else {
if (state->buffer_.backspace()) {
if (state->restoreCallback_ &&
state->buffer_.cursor() == state->buffer_.size() &&
state->buffer_.userInput() ==
state->originalBuffer_) {
auto callback = std::move(state->restoreCallback_);
auto original = std::move(state->originalBuffer_);
state->reset(inputContext);
callback(inputContext, original);
return;
}

if (state->buffer_.empty()) {
state->reset(inputContext);
} else {
updateUI(inputContext);
}
}
}
keyEvent.accept();
return;
}
if (keyEvent.key().check(FcitxKey_Delete)) {
Expand All @@ -256,28 +272,32 @@ QuickPhrase::QuickPhrase(Instance *instance)
if (key.check(FcitxKey_Home) || key.check(FcitxKey_KP_Home)) {
state->buffer_.setCursor(0);
keyEvent.accept();
return updateUI(inputContext);
updateUI(inputContext);
return;
}
if (key.check(FcitxKey_End) || key.check(FcitxKey_KP_End)) {
state->buffer_.setCursor(state->buffer_.size());
keyEvent.accept();
return updateUI(inputContext);
updateUI(inputContext);
return;
}
if (key.check(FcitxKey_Left) || key.check(FcitxKey_KP_Left)) {
auto cursor = state->buffer_.cursor();
if (cursor > 0) {
state->buffer_.setCursor(cursor - 1);
}
keyEvent.accept();
return updateUI(inputContext);
updateUI(inputContext);
return;
}
if (key.check(FcitxKey_Right) || key.check(FcitxKey_KP_Right)) {
auto cursor = state->buffer_.cursor();
if (cursor < state->buffer_.size()) {
state->buffer_.setCursor(cursor + 1);
}
keyEvent.accept();
return updateUI(inputContext);
updateUI(inputContext);
return;
}
}
if (!state->typed_ && !state->str_.empty() &&
Expand All @@ -294,7 +314,8 @@ QuickPhrase::QuickPhrase(Instance *instance)

// compose is invalid, ignore it.
if (!compose) {
return event.accept();
event.accept();
return;
}

if (!compose->empty()) {
Expand Down Expand Up @@ -539,6 +560,20 @@ void QuickPhrase::setBuffer(InputContext *ic, const std::string &text) {
updateUI(ic);
}

void QuickPhrase::setBufferWithRestoreCallback(
InputContext *ic, const std::string &text, const std::string &original,
QuickPhraseRestoreCallback callback) {
auto *state = ic->propertyFor(&factory_);
if (!state->enabled_) {
return;
}
state->buffer_.clear();
state->buffer_.type(text);
state->originalBuffer_ = original;
state->restoreCallback_ = std::move(callback);
updateUI(ic);
}

class QuickPhraseModuleFactory : public AddonFactory {
AddonInstance *create(AddonManager *manager) override {
return new QuickPhrase(manager->instance());
Expand Down
4 changes: 4 additions & 0 deletions src/modules/quickphrase/quickphrase.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ class QuickPhrase final : public AddonInstance {
const std::string &prefix, const std::string &str,
const std::string &alt, const Key &key);
void setBuffer(InputContext *ic, const std::string &text);
void setBufferWithRestoreCallback(InputContext *ic, const std::string &text,
const std::string &original,
QuickPhraseRestoreCallback callback);

std::unique_ptr<HandlerTableEntry<QuickPhraseProviderCallback>>
addProvider(QuickPhraseProviderCallback);
Expand All @@ -90,6 +93,7 @@ class QuickPhrase final : public AddonInstance {
FCITX_ADDON_EXPORT_FUNCTION(QuickPhrase, addProvider);
FCITX_ADDON_EXPORT_FUNCTION(QuickPhrase, addProviderV2);
FCITX_ADDON_EXPORT_FUNCTION(QuickPhrase, setBuffer);
FCITX_ADDON_EXPORT_FUNCTION(QuickPhrase, setBufferWithRestoreCallback);

void setSelectionKeys(QuickPhraseAction action);

Expand Down
10 changes: 10 additions & 0 deletions src/modules/quickphrase/quickphrase_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ using QuickPhraseAddCandidateCallbackV2 =
using QuickPhraseProviderCallbackV2 =
std::function<bool(InputContext *ic, const std::string &,
const QuickPhraseAddCandidateCallbackV2 &)>;
using QuickPhraseRestoreCallback =
std::function<void(InputContext *ic, const std::string &buffer)>;

} // namespace fcitx

Expand All @@ -52,6 +54,14 @@ FCITX_ADDON_DECLARE_FUNCTION(QuickPhrase, trigger,
const std::string &alt, const Key &key));
FCITX_ADDON_DECLARE_FUNCTION(QuickPhrase, setBuffer,
void(InputContext *ic, const std::string &text));
// Set buffer with a restore callback.
// If after "backspace", the current buffer is restore to the original value,
// the callback will be invoked, to allow input method to restore the buffer to
// original state.
FCITX_ADDON_DECLARE_FUNCTION(QuickPhrase, setBufferWithRestoreCallback,
void(InputContext *ic, const std::string &text,
const std::string &original,
QuickPhraseRestoreCallback callback));

FCITX_ADDON_DECLARE_FUNCTION(
QuickPhrase, addProvider,
Expand Down
24 changes: 24 additions & 0 deletions test/testquickphrase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "fcitx-utils/macros.h"
#include "fcitx-utils/testing.h"
#include "fcitx/addonmanager.h"
#include "fcitx/inputcontext.h"
#include "fcitx/instance.h"
#include "fcitx/userinterface.h"
#include "quickphrase_public.h"
Expand Down Expand Up @@ -194,6 +195,28 @@ void testProviderV2(Instance *instance) {
});
}

void testRestoreCallback(Instance *instance) {
instance->eventDispatcher().schedule([instance]() {
auto *testfrontend = instance->addonManager().addon("testfrontend");
auto uuid =
testfrontend->call<ITestFrontend::createInputContext>("testapp");
auto *ic = instance->inputContextManager().findByUUID(uuid);
auto *quickphrase = instance->addonManager().addon("quickphrase");
quickphrase->call<IQuickPhrase::trigger>(ic, "", "", "", "", Key());
bool restore = false;
quickphrase->call<IQuickPhrase::setBufferWithRestoreCallback>(
ic, "ABC.", "ABC",
[&restore](InputContext * /*ic*/, const std::string &origin) {
FCITX_ASSERT(origin == "ABC");
restore = true;
});
FCITX_ASSERT(!restore);
FCITX_ASSERT(testfrontend->call<ITestFrontend::sendKeyEvent>(
uuid, Key(FcitxKey_BackSpace), false));
FCITX_ASSERT(restore);
});
}

int main() {
setupTestingEnvironment(
FCITX5_BINARY_DIR,
Expand All @@ -210,6 +233,7 @@ int main() {
testInit(&instance);
testBasic(&instance);
testProviderV2(&instance);
testRestoreCallback(&instance);
instance.eventDispatcher().schedule([&instance]() {
handle.reset();
instance.exit();
Expand Down
1 change: 1 addition & 0 deletions testing/testfrontend/testfrontend_public.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#define _TESTFRONTEND_TESTFRONTEND_PUBLIC_H_

#include <string>
#include <fcitx-utils/key.h>
#include <fcitx/addoninstance.h>
#include <fcitx/inputcontext.h>

Expand Down
Loading