Skip to content

Commit 57f7769

Browse files
committed
Use candidate comment in quick phrase candidate and add provider v2.
1 parent 51c37e7 commit 57f7769

File tree

6 files changed

+135
-31
lines changed

6 files changed

+135
-31
lines changed

src/modules/quickphrase/quickphrase.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,30 @@
66
*/
77
#include "quickphrase.h"
88

9+
#include <array>
10+
#include <memory>
11+
#include <string>
912
#include <utility>
1013
#include "fcitx-config/iniparser.h"
14+
#include "fcitx-utils/capabilityflags.h"
15+
#include "fcitx-utils/handlertable.h"
1116
#include "fcitx-utils/i18n.h"
1217
#include "fcitx-utils/inputbuffer.h"
18+
#include "fcitx-utils/key.h"
19+
#include "fcitx-utils/keysym.h"
20+
#include "fcitx-utils/textformatflags.h"
1321
#include "fcitx/addonfactory.h"
22+
#include "fcitx/addoninstance.h"
1423
#include "fcitx/addonmanager.h"
1524
#include "fcitx/candidatelist.h"
25+
#include "fcitx/event.h"
1626
#include "fcitx/inputcontextmanager.h"
1727
#include "fcitx/inputpanel.h"
28+
#include "fcitx/instance.h"
29+
#include "fcitx/text.h"
30+
#include "fcitx/userinterface.h"
31+
#include "quickphrase_public.h"
32+
#include "quickphraseprovider.h"
1833

1934
namespace fcitx {
2035

@@ -326,9 +341,12 @@ class QuickPhraseCandidateWord : public CandidateWord {
326341
public:
327342
QuickPhraseCandidateWord(QuickPhrase *q, std::string commit,
328343
const std::string &display,
344+
const std::string &comment,
329345
QuickPhraseAction action)
330346
: CandidateWord(Text(display)), q_(q), commit_(std::move(commit)),
331-
action_(action) {}
347+
action_(action) {
348+
setComment(Text(comment));
349+
}
332350

333351
void select(InputContext *inputContext) const override {
334352
auto *state = inputContext->propertyFor(&q_->factory());
@@ -412,9 +430,9 @@ void QuickPhrase::updateUI(InputContext *inputContext) {
412430
if (!provider->populate(
413431
inputContext, state->buffer_.userInput(),
414432
[this, &candidateList, &selectionKeyAction, &autoCommit,
415-
&autoCommitSet](const std::string &word,
416-
const std::string &aux,
417-
QuickPhraseAction action) {
433+
&autoCommitSet](
434+
const std::string &word, const std::string &aux,
435+
const std::string &comment, QuickPhraseAction action) {
418436
if (!autoCommitSet &&
419437
action == QuickPhraseAction::AutoCommit) {
420438
autoCommit = word;
@@ -426,7 +444,7 @@ void QuickPhrase::updateUI(InputContext *inputContext) {
426444
}
427445
if (!word.empty()) {
428446
candidateList->append<QuickPhraseCandidateWord>(
429-
this, word, aux, action);
447+
this, word, aux, comment, action);
430448
} else {
431449
if (action == QuickPhraseAction::DigitSelection ||
432450
action == QuickPhraseAction::AlphaSelection ||
@@ -485,11 +503,17 @@ void QuickPhrase::reloadConfig() {
485503
builtinProvider_.reloadConfig();
486504
readAsIni(config_, "conf/quickphrase.conf");
487505
}
506+
488507
std::unique_ptr<HandlerTableEntry<QuickPhraseProviderCallback>>
489508
QuickPhrase::addProvider(QuickPhraseProviderCallback callback) {
490509
return callbackProvider_.addCallback(std::move(callback));
491510
}
492511

512+
std::unique_ptr<HandlerTableEntry<QuickPhraseProviderCallbackV2>>
513+
QuickPhrase::addProviderV2(QuickPhraseProviderCallbackV2 callback) {
514+
return callbackProvider_.addCallback(std::move(callback));
515+
}
516+
493517
void QuickPhrase::trigger(InputContext *ic, const std::string &text,
494518
const std::string &prefix, const std::string &str,
495519
const std::string &alt, const Key &key) {

src/modules/quickphrase/quickphrase.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
#ifndef _FCITX_MODULES_QUICKPHRASE_QUICKPHRASE_H_
88
#define _FCITX_MODULES_QUICKPHRASE_QUICKPHRASE_H_
99

10+
#include <memory>
11+
#include <string>
12+
#include <vector>
1013
#include "fcitx-config/configuration.h"
1114
#include "fcitx-config/enum.h"
1215
#include "fcitx-config/iniparser.h"
16+
#include "fcitx-config/option.h"
17+
#include "fcitx-config/rawconfig.h"
18+
#include "fcitx-utils/handlertable.h"
1319
#include "fcitx-utils/i18n.h"
1420
#include "fcitx-utils/key.h"
1521
#include "fcitx/addoninstance.h"
@@ -76,9 +82,13 @@ class QuickPhrase final : public AddonInstance {
7682
std::unique_ptr<HandlerTableEntry<QuickPhraseProviderCallback>>
7783
addProvider(QuickPhraseProviderCallback);
7884

85+
std::unique_ptr<HandlerTableEntry<QuickPhraseProviderCallbackV2>>
86+
addProviderV2(QuickPhraseProviderCallbackV2);
87+
7988
private:
8089
FCITX_ADDON_EXPORT_FUNCTION(QuickPhrase, trigger);
8190
FCITX_ADDON_EXPORT_FUNCTION(QuickPhrase, addProvider);
91+
FCITX_ADDON_EXPORT_FUNCTION(QuickPhrase, addProviderV2);
8292
FCITX_ADDON_EXPORT_FUNCTION(QuickPhrase, setBuffer);
8393

8494
void setSelectionKeys(QuickPhraseAction action);

src/modules/quickphrase/quickphrase_public.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
#define _FCITX_MODULES_QUICKPHRASE_QUICKPHRASE_PUBLIC_H_
99

1010
#include <functional>
11+
#include <memory>
1112
#include <string>
13+
#include <fcitx-utils/handlertable.h>
1214
#include <fcitx-utils/key.h>
1315
#include <fcitx-utils/metastring.h>
1416
#include <fcitx/addoninstance.h>
@@ -32,6 +34,13 @@ using QuickPhraseProviderCallback =
3234
std::function<bool(InputContext *ic, const std::string &,
3335
const QuickPhraseAddCandidateCallback &)>;
3436

37+
using QuickPhraseAddCandidateCallbackV2 =
38+
std::function<void(const std::string &, const std::string &,
39+
const std::string &, QuickPhraseAction action)>;
40+
using QuickPhraseProviderCallbackV2 =
41+
std::function<bool(InputContext *ic, const std::string &,
42+
const QuickPhraseAddCandidateCallbackV2 &)>;
43+
3544
} // namespace fcitx
3645

3746
/// Trigger quickphrase, with following format:
@@ -49,4 +58,9 @@ FCITX_ADDON_DECLARE_FUNCTION(
4958
std::unique_ptr<HandlerTableEntry<QuickPhraseProviderCallback>>(
5059
QuickPhraseProviderCallback));
5160

61+
FCITX_ADDON_DECLARE_FUNCTION(
62+
QuickPhrase, addProviderV2,
63+
std::unique_ptr<HandlerTableEntry<QuickPhraseProviderCallbackV2>>(
64+
QuickPhraseProviderCallbackV2));
65+
5266
#endif // _FCITX_MODULES_QUICKPHRASE_QUICKPHRASE_PUBLIC_H_

src/modules/quickphrase/quickphraseprovider.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,15 @@ namespace fcitx {
2626

2727
bool BuiltInQuickPhraseProvider::populate(
2828
InputContext *, const std::string &userInput,
29-
const QuickPhraseAddCandidateCallback &addCandidate) {
29+
const QuickPhraseAddCandidateCallbackV2 &addCandidate) {
3030
auto start = map_.lower_bound(userInput);
3131
auto end = map_.end();
3232

3333
for (; start != end; start++) {
3434
if (!stringutils::startsWith(start->first, userInput)) {
3535
break;
3636
}
37-
addCandidate(start->second,
38-
stringutils::concat(start->second, " ",
39-
start->first.substr(userInput.size())),
37+
addCandidate(start->second, start->second, start->first,
4038
QuickPhraseAction::Commit);
4139
}
4240
return true;
@@ -106,7 +104,7 @@ SpellQuickPhraseProvider::SpellQuickPhraseProvider(QuickPhrase *parent)
106104

107105
bool SpellQuickPhraseProvider::populate(
108106
InputContext *ic, const std::string &userInput,
109-
const QuickPhraseAddCandidateCallback &addCandidate) {
107+
const QuickPhraseAddCandidateCallbackV2 &addCandidate) {
110108
if (!*parent_->config().enableSpell) {
111109
return true;
112110
}
@@ -125,19 +123,29 @@ bool SpellQuickPhraseProvider::populate(
125123
const auto result = spell->call<ISpell::hint>(
126124
lang, userInput, instance_->globalConfig().defaultPageSize());
127125
for (const auto &word : result) {
128-
addCandidate(word, word, QuickPhraseAction::Commit);
126+
addCandidate(word, word, "", QuickPhraseAction::Commit);
129127
}
130128
return true;
131129
}
132130

133131
bool CallbackQuickPhraseProvider::populate(
134132
InputContext *ic, const std::string &userInput,
135-
const QuickPhraseAddCandidateCallback &addCandidate) {
136-
for (const auto &callback : callback_.view()) {
133+
const QuickPhraseAddCandidateCallbackV2 &addCandidate) {
134+
for (const auto &callback : callbackV2_.view()) {
137135
if (!callback(ic, userInput, addCandidate)) {
138136
return false;
139137
}
140138
}
139+
for (const auto &callback : callback_.view()) {
140+
if (!callback(ic, userInput,
141+
[&addCandidate](const std::string &word,
142+
const std::string &aux,
143+
QuickPhraseAction action) {
144+
return addCandidate(word, aux, "", action);
145+
})) {
146+
return false;
147+
}
148+
}
141149
return true;
142150
}
143151

src/modules/quickphrase/quickphraseprovider.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ class QuickPhraseProvider {
2828
virtual ~QuickPhraseProvider() = default;
2929
virtual bool
3030
populate(InputContext *ic, const std::string &userInput,
31-
const QuickPhraseAddCandidateCallback &addCandidate) = 0;
31+
const QuickPhraseAddCandidateCallbackV2 &addCandidate) = 0;
3232
};
3333

3434
class BuiltInQuickPhraseProvider : public QuickPhraseProvider {
3535
public:
36-
bool populate(InputContext *ic, const std::string &userInput,
37-
const QuickPhraseAddCandidateCallback &addCandidate) override;
36+
bool
37+
populate(InputContext *ic, const std::string &userInput,
38+
const QuickPhraseAddCandidateCallbackV2 &addCandidate) override;
3839
void reloadConfig();
3940

4041
private:
@@ -47,8 +48,9 @@ class SpellQuickPhraseProvider : public QuickPhraseProvider {
4748
SpellQuickPhraseProvider(QuickPhrase *parent);
4849
FCITX_ADDON_DEPENDENCY_LOADER(spell, instance_->addonManager());
4950

50-
bool populate(InputContext *ic, const std::string &userInput,
51-
const QuickPhraseAddCandidateCallback &addCandidate) override;
51+
bool
52+
populate(InputContext *ic, const std::string &userInput,
53+
const QuickPhraseAddCandidateCallbackV2 &addCandidate) override;
5254

5355
private:
5456
QuickPhrase *parent_;
@@ -58,16 +60,23 @@ class SpellQuickPhraseProvider : public QuickPhraseProvider {
5860
class CallbackQuickPhraseProvider : public QuickPhraseProvider,
5961
public ConnectableObject {
6062
public:
61-
bool populate(InputContext *ic, const std::string &userInput,
62-
const QuickPhraseAddCandidateCallback &addCandidate) override;
63+
bool
64+
populate(InputContext *ic, const std::string &userInput,
65+
const QuickPhraseAddCandidateCallbackV2 &addCandidate) override;
6366

6467
std::unique_ptr<HandlerTableEntry<QuickPhraseProviderCallback>>
6568
addCallback(QuickPhraseProviderCallback callback) {
6669
return callback_.add(std::move(callback));
6770
}
6871

72+
std::unique_ptr<HandlerTableEntry<QuickPhraseProviderCallbackV2>>
73+
addCallback(QuickPhraseProviderCallbackV2 callback) {
74+
return callbackV2_.add(std::move(callback));
75+
}
76+
6977
private:
7078
HandlerTable<QuickPhraseProviderCallback> callback_;
79+
HandlerTable<QuickPhraseProviderCallbackV2> callbackV2_;
7180
};
7281

7382
} // namespace fcitx

test/testquickphrase.cpp

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44
* SPDX-License-Identifier: LGPL-2.1-or-later
55
*
66
*/
7+
#include <memory>
8+
#include <string>
79
#include "fcitx-utils/eventdispatcher.h"
10+
#include "fcitx-utils/handlertable.h"
11+
#include "fcitx-utils/key.h"
12+
#include "fcitx-utils/keysym.h"
13+
#include "fcitx-utils/log.h"
14+
#include "fcitx-utils/macros.h"
815
#include "fcitx-utils/testing.h"
916
#include "fcitx/addonmanager.h"
1017
#include "fcitx/instance.h"
18+
#include "fcitx/userinterface.h"
1119
#include "quickphrase_public.h"
1220
#include "testdir.h"
1321
#include "testfrontend_public.h"
@@ -16,8 +24,8 @@ using namespace fcitx;
1624

1725
std::unique_ptr<HandlerTableEntry<QuickPhraseProviderCallback>> handle;
1826

19-
void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
20-
dispatcher->schedule([instance]() {
27+
void testInit(Instance *instance) {
28+
instance->eventDispatcher().schedule([instance]() {
2129
auto *quickphrase = instance->addonManager().addon("quickphrase", true);
2230
handle = quickphrase->call<IQuickPhrase::addProvider>(
2331
[](InputContext *, const std::string &text,
@@ -31,7 +39,10 @@ void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
3139
});
3240
FCITX_ASSERT(quickphrase);
3341
});
34-
dispatcher->schedule([dispatcher, instance]() {
42+
}
43+
44+
void testBasic(Instance *instance) {
45+
instance->eventDispatcher().schedule([instance]() {
3546
auto *testfrontend = instance->addonManager().addon("testfrontend");
3647
for (const auto *expectation :
3748
{"TEST", "abc", "abcd", "DEF", "abcd", "DEF1", "test1", "CALLBACK",
@@ -150,12 +161,36 @@ void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
150161
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("u"), false);
151162
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("t"), false);
152163
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("o"), false);
164+
});
165+
}
153166

154-
dispatcher->schedule([dispatcher, instance]() {
155-
handle.reset();
156-
dispatcher->detach();
157-
instance->exit();
158-
});
167+
void testProviderV2(Instance *instance) {
168+
instance->eventDispatcher().schedule([instance]() {
169+
auto *testfrontend = instance->addonManager().addon("testfrontend");
170+
testfrontend->call<ITestFrontend::pushCommitExpectation>("PROVIDERV2");
171+
auto uuid =
172+
testfrontend->call<ITestFrontend::createInputContext>("testapp");
173+
testfrontend->call<ITestFrontend::keyEvent>(
174+
uuid, Key(FcitxKey_BackSpace), false);
175+
auto *quickphrase = instance->addonManager().addon("quickphrase", true);
176+
auto handleV2 = quickphrase->call<IQuickPhrase::addProviderV2>(
177+
[](InputContext *, const std::string &text,
178+
const QuickPhraseAddCandidateCallbackV2 &callback) {
179+
FCITX_INFO() << "Quickphrase text: " << text;
180+
if (text == "PVD") {
181+
callback("PROVIDERV2", "V2", "COMMENT",
182+
QuickPhraseAction::Commit);
183+
return false;
184+
}
185+
return true;
186+
});
187+
188+
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("Super+grave"),
189+
false);
190+
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("P"), false);
191+
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("V"), false);
192+
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("D"), false);
193+
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("1"), false);
159194
});
160195
}
161196

@@ -172,9 +207,13 @@ int main() {
172207
char *argv[] = {arg0, arg1, arg2};
173208
Instance instance(FCITX_ARRAY_SIZE(argv), argv);
174209
instance.addonManager().registerDefaultLoader(nullptr);
175-
EventDispatcher dispatcher;
176-
dispatcher.attach(&instance.eventLoop());
177-
scheduleEvent(&dispatcher, &instance);
210+
testInit(&instance);
211+
testBasic(&instance);
212+
testProviderV2(&instance);
213+
instance.eventDispatcher().schedule([&instance]() {
214+
handle.reset();
215+
instance.exit();
216+
});
178217
instance.exec();
179218
return 0;
180219
}

0 commit comments

Comments
 (0)