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

Implement candidate action for table. #178

Merged
merged 2 commits into from
May 31, 2024
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.6.0)
project(fcitx5-chinese-addons VERSION 5.1.5)
project(fcitx5-chinese-addons VERSION 5.1.6)

find_package(ECM REQUIRED 1.0.0)
set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
Expand Down
32 changes: 32 additions & 0 deletions im/table/candidate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@
#include <cstddef>
#include <fcitx-utils/i18n.h>
#include <fcitx-utils/utf8.h>
#include <fcitx/candidateaction.h>
#include <fcitx/candidatelist.h>
#include <fcitx/inputcontext.h>
#include <fcitx/text.h>
#include <libime/table/tablebaseddictionary.h>
#include <string>
#include <utility>
#include <vector>

namespace fcitx {

Expand Down Expand Up @@ -92,4 +94,34 @@ void TablePredictCandidateWord::select(InputContext *inputContext) const {
state_->resetAndPredict();
}

TableActionableCandidateList::TableActionableCandidateList(TableState *state)
: state_(state) {}

bool TableActionableCandidateList::hasAction(
const CandidateWord &candidate) const {
return dynamic_cast<const TableCandidateWord *>(&candidate);
}
std::vector<CandidateAction> TableActionableCandidateList::candidateActions(
const CandidateWord &candidate) const {
if (!hasAction(candidate)) {
return {};
}
std::vector<CandidateAction> actions;
CandidateAction action;
action.setId(0);
action.setText(_("Forget word"));
actions.push_back(std::move(action));
return actions;
}

void TableActionableCandidateList::triggerAction(const CandidateWord &candidate,
int id) {
if (id != 0) {
return;
}
if (const auto *tableCandidate =
dynamic_cast<const TableCandidateWord *>(&candidate)) {
state_->forgetCandidateWord(tableCandidate->idx_);
}
}
} // namespace fcitx
15 changes: 15 additions & 0 deletions im/table/candidate.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@

#include "engine.h"
#include <cstddef>
#include <fcitx/candidateaction.h>
#include <fcitx/candidatelist.h>
#include <fcitx/text.h>
#include <libime/table/tablebaseddictionary.h>
#include <string>
#include <vector>

namespace fcitx {

Expand Down Expand Up @@ -62,6 +64,19 @@ class TablePredictCandidateWord : public CandidateWord {
std::string word_;
};

class TableActionableCandidateList : public ActionableCandidateList {
public:
TableActionableCandidateList(TableState *state);

bool hasAction(const CandidateWord &candidate) const override;
std::vector<CandidateAction>
candidateActions(const CandidateWord &candidate) const override;
void triggerAction(const CandidateWord &candidate, int id) override;

private:
TableState *state_;
};

} // namespace fcitx

#endif // _TABLE_CANDIDATE_H_
2 changes: 2 additions & 0 deletions im/table/state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,8 @@ void TableState::updateUI(bool keepOldCursor, bool maybePredict) {
}
candidateList->setGlobalCursorIndex(cursor);
}
candidateList->setActionableImpl(
std::make_unique<TableActionableCandidateList>(this));
inputPanel.setCandidateList(std::move(candidateList));
}
const bool useClientPreedit =
Expand Down
1 change: 1 addition & 0 deletions org.fcitx.Fcitx5.Addon.ChineseAddons.metainfo.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<url type="vcs-browser">https://github.com/fcitx/fcitx5-chinese-addons</url>
<project_group>Fcitx</project_group>
<releases>
<release version="5.1.6" date="2024-05-30"/>
<release version="5.1.5" date="2024-04-22"/>
<release version="5.1.4" date="2024-02-28"/>
<release version="5.1.3" date="2024-01-02"/>
Expand Down
69 changes: 54 additions & 15 deletions test/testtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,43 @@
*/
#include "testdir.h"
#include "testfrontend_public.h"
#include <fcitx-config/rawconfig.h>
#include <fcitx-utils/eventdispatcher.h>
#include <fcitx-utils/key.h>
#include <fcitx-utils/keysymgen.h>
#include <fcitx-utils/log.h>
#include <fcitx-utils/macros.h>
#include <fcitx-utils/standardpath.h>
#include <fcitx-utils/testing.h>
#include <fcitx/addonmanager.h>
#include <fcitx/inputmethodengine.h>
#include <fcitx/inputmethodgroup.h>
#include <fcitx/inputmethodmanager.h>
#include <fcitx/inputpanel.h>
#include <fcitx/instance.h>
#include <iostream>
#include <fcitx/userinterface.h>
#include <utility>

using namespace fcitx;

void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
dispatcher->schedule([instance]() {
int findCandidateOrDie(InputContext *ic, std::string_view word) {
auto candList = ic->inputPanel().candidateList();
for (int i = 0; i < candList->toBulk()->totalSize(); i++) {
const auto &candidate = candList->toBulk()->candidateFromAll(i);
if (candidate.text().toString() == word) {
return i;
}
}
FCITX_ASSERT(false) << "Failed to find candidate: " << word;
return -1;
}

void scheduleEvent(Instance *instance) {
instance->eventDispatcher().schedule([instance]() {
auto *table = instance->addonManager().addon("table", true);
FCITX_ASSERT(table);
});
dispatcher->schedule([instance]() {
instance->eventDispatcher().schedule([instance]() {
auto defaultGroup = instance->inputMethodManager().currentGroup();
defaultGroup.inputMethodList().clear();
defaultGroup.inputMethodList().push_back(
Expand Down Expand Up @@ -83,7 +101,7 @@ void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("A"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("Return"), false);
});
dispatcher->schedule([dispatcher, instance]() {
instance->eventDispatcher().schedule([instance]() {
auto defaultGroup = instance->inputMethodManager().currentGroup();
defaultGroup.inputMethodList().clear();
defaultGroup.inputMethodList().push_back(
Expand Down Expand Up @@ -134,15 +152,37 @@ void scheduleEvent(EventDispatcher *dispatcher, Instance *instance) {
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("a"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("a"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("a"), false);
// This comma trigger only match commit.
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("a"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("5"), false);

dispatcher->schedule([dispatcher, instance]() {
dispatcher->detach();
instance->exit();
});
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("a"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("a"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("a"), false);
testfrontend->call<ITestFrontend::keyEvent>(uuid, Key("a"), false);

auto *candidateList = ic->inputPanel().candidateList().get();
FCITX_ASSERT(candidateList);
auto idx = findCandidateOrDie(ic, "工工工工");
auto *actionable = ic->inputPanel().candidateList()->toActionable();
FCITX_ASSERT(actionable);
FCITX_ASSERT(actionable->hasAction(candidateList->candidate(idx)));
FCITX_ASSERT(
actionable->candidateActions(candidateList->candidate(idx))[0]
.id() == 0);
actionable->triggerAction(candidateList->candidate(idx), 0);

// Check if 工工工工 is deleted.
candidateList = ic->inputPanel().candidateList().get();
FCITX_ASSERT(candidateList);
for (int i = 0; i < candidateList->size(); i++) {
FCITX_ASSERT(candidateList->candidate(i).text().toString() !=
"工工工工")
<< "Candidate " << i << " is not deleted.";
}
ic->updateUserInterface(fcitx::UserInterfaceComponent::InputPanel,
true);
});
instance->eventDispatcher().schedule([instance]() { instance->exit(); });
}

int main() {
Expand All @@ -156,14 +196,13 @@ int main() {
fcitx::Log::setLogRule("default=5,table=5,libime-table=5");
char arg0[] = "testtable";
char arg1[] = "--disable=all";
char arg2[] = "--enable=testim,testfrontend,table,quickphrase,punctuation,"
"pinyinhelper";
char arg2[] =
"--enable=testui,testim,testfrontend,table,quickphrase,punctuation,"
"pinyinhelper";
char *argv[] = {arg0, arg1, arg2};
Instance instance(FCITX_ARRAY_SIZE(argv), argv);
instance.addonManager().registerDefaultLoader(nullptr);
EventDispatcher dispatcher;
dispatcher.attach(&instance.eventLoop());
scheduleEvent(&dispatcher, &instance);
scheduleEvent(&instance);
instance.exec();

return 0;
Expand Down
Loading