Skip to content

Commit 9327a54

Browse files
authored
add chromepanel (#67)
1 parent 5d4aed1 commit 9327a54

File tree

9 files changed

+119
-11
lines changed

9 files changed

+119
-11
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ add_subdirectory(wasmnotifications)
8282
add_subdirectory(webkeyboard)
8383
add_subdirectory(fcitx5-webview)
8484
add_subdirectory(webpanel)
85+
add_subdirectory(chromepanel)
8586

8687
# Force cmake to re-link Fcitx5.js when non-cpp files are changed.
8788
set(DUMMY_CPP "${PROJECT_BINARY_DIR}/dummy.cpp")
@@ -90,7 +91,7 @@ file(GLOB JS_SOURCES CONFIGURE_DEPENDS "page/*")
9091
add_custom_command(
9192
OUTPUT ${DUMMY_CPP}
9293
COMMAND pnpm run build && ./scripts/install.sh
93-
DEPENDS ${JS_SOURCES} clipboard imselector spell quickphrase unicode webpanel webkeyboard wasmfrontend notifications
94+
DEPENDS ${JS_SOURCES} clipboard imselector spell quickphrase unicode webpanel webkeyboard chromepanel wasmfrontend notifications
9495
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
9596
COMMENT "Generating the pre.js, index.js and installing shared libraries"
9697
)

chromepanel/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
add_library(chromepanel STATIC chromepanel.cpp)
2+
target_link_libraries(chromepanel Fcitx5::Core nlohmann_json::nlohmann_json)
3+
4+
configure_file(chromepanel.conf.in.in chromepanel.conf.in @ONLY)
5+
fcitx5_translate_desktop_file(${CMAKE_CURRENT_BINARY_DIR}/chromepanel.conf.in chromepanel.conf)
6+
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/chromepanel.conf"
7+
DESTINATION "${CMAKE_INSTALL_PREFIX}/share/fcitx5/addon"
8+
)

chromepanel/chromepanel.conf.in.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[Addon]
2+
Name=ChromePanel
3+
Type=StaticLibrary
4+
Library=libchromepanel
5+
Category=UI
6+
Version=@PROJECT_VERSION@
7+
Configurable=False

chromepanel/chromepanel.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include "chromepanel.h"
2+
#include <emscripten.h>
3+
#include <fcitx/inputpanel.h>
4+
#include <nlohmann/json.hpp>
5+
6+
using json = nlohmann::json;
7+
8+
namespace fcitx {
9+
10+
struct Candidate {
11+
std::string text;
12+
std::string label;
13+
std::string comment;
14+
15+
friend void to_json(json &j, const Candidate &c) {
16+
j = json{{"label", c.label}, {"text", c.text}, {"comment", c.comment}};
17+
}
18+
};
19+
20+
ChromePanel::ChromePanel(Instance *instance) : instance_(instance) {}
21+
22+
void ChromePanel::update(UserInterfaceComponent component,
23+
InputContext *inputContext) {
24+
switch (component) {
25+
case UserInterfaceComponent::InputPanel: {
26+
const InputPanel &inputPanel = inputContext->inputPanel();
27+
std::vector<Candidate> candidates;
28+
int highlighted = -1;
29+
if (const auto &list = inputPanel.candidateList()) {
30+
auto size = list->size();
31+
candidates.reserve(size);
32+
for (int i = 0; i < size; ++i) {
33+
const auto &candidate = list->candidate(i);
34+
auto label = list->label(i).toString();
35+
// HACK: fcitx5's Linux UI concatenates label and text and
36+
// expects engine to append a ' ' to label.
37+
auto length = label.length();
38+
if (length && label[length - 1] == ' ') {
39+
label = label.substr(0, length - 1);
40+
}
41+
candidates.push_back(
42+
{instance_->outputFilter(inputContext, candidate.text())
43+
.toString(),
44+
label,
45+
instance_->outputFilter(inputContext, candidate.comment())
46+
.toString()});
47+
highlighted = list->cursorIndex();
48+
}
49+
}
50+
auto str =
51+
json{{"candidates", candidates}, {"highlighted", highlighted}}
52+
.dump();
53+
EM_ASM(fcitx.chrome.setCandidates(UTF8ToString($0)), str.c_str());
54+
break;
55+
}
56+
}
57+
}
58+
} // namespace fcitx
59+
60+
FCITX_ADDON_FACTORY_V2(chromepanel, fcitx::ChromePanelFactory);

chromepanel/chromepanel.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include <fcitx/addonfactory.h>
4+
#include <fcitx/addonmanager.h>
5+
#include <fcitx/instance.h>
6+
7+
namespace fcitx {
8+
class ChromePanel final : public UserInterface {
9+
public:
10+
ChromePanel(Instance *);
11+
virtual ~ChromePanel() = default;
12+
void reloadConfig() override {}
13+
void setConfig(const RawConfig &config) override {}
14+
void setSubConfig(const std::string &path,
15+
const RawConfig &config) override {}
16+
bool available() override { return true; }
17+
void suspend() override {}
18+
void resume() override {}
19+
void update(UserInterfaceComponent component,
20+
InputContext *inputContext) override;
21+
22+
private:
23+
Instance *instance_;
24+
};
25+
26+
class ChromePanelFactory : public AddonFactory {
27+
public:
28+
AddonInstance *create(AddonManager *manager) override {
29+
return new ChromePanel(manager->instance());
30+
}
31+
};
32+
} // namespace fcitx

scripts/format.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
find src wasmfrontend wasmnotifications webkeyboard webpanel -name '*.h' -o -name '*.cpp' | xargs clang-format -i
1+
find src wasmfrontend wasmnotifications webkeyboard webpanel chromepanel -name '*.h' -o -name '*.cpp' | xargs clang-format -i
22
pnpm run lint:fix

scripts/install.sh

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
export DESTDIR=build/destdir
2-
cmake --install build/fcitx5
3-
cmake --install build/wasmfrontend
4-
cmake --install build/wasmnotifications
5-
cmake --install build/webkeyboard
6-
cmake --install build/webpanel
2+
for dir in chromepanel fcitx5 wasmfrontend wasmnotifications webkeyboard webpanel; do
3+
cmake --install build/$dir
4+
done
75
touch build/dummy.cpp

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ set(EXE_LINK_OPTIONS
2929
set(INSTALL_LIB_DIR "${PROJECT_BINARY_DIR}/destdir/usr/lib")
3030
set(INSTALL_SHARE_DIR "${PROJECT_BINARY_DIR}/destdir/usr/share")
3131

32-
set(STATIC_ADDONS webpanel webkeyboard wasmfrontend)
32+
set(STATIC_ADDONS webpanel webkeyboard chromepanel wasmfrontend)
3333

3434
if (RIME_ONLY)
3535
foreach(ADDON ${STATIC_ADDONS})

src/fcitx.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,24 +174,26 @@ EMSCRIPTEN_KEEPALIVE void init(const char *locale, Runtime runtime,
174174

175175
EventLoop::setEventLoopFactory(
176176
[] { return std::make_unique<JSEventLoop>(); });
177+
178+
char arg0[] = "fcitx5-js";
177179
switch (runtime) {
178180
case Runtime::web: {
179-
instance = std::make_unique<Instance>(0, nullptr);
181+
char arg1[] = "--disable=chromepanel";
182+
char *argv[] = {arg0, arg1};
183+
instance = std::make_unique<Instance>(FCITX_ARRAY_SIZE(argv), argv);
180184
if (touch) {
181185
instance->setInputMethodMode(InputMethodMode::OnScreenKeyboard);
182186
}
183187
break;
184188
}
185189
case Runtime::webworker: {
186-
char arg0[] = "fcitx5-js";
187190
char arg1[] = "--disable=all";
188191
char arg2[] = "--enable=rime,notifications";
189192
char *argv[] = {arg0, arg1, arg2};
190193
instance = std::make_unique<Instance>(FCITX_ARRAY_SIZE(argv), argv);
191194
break;
192195
}
193196
case Runtime::serviceworker: {
194-
char arg0[] = "fcitx5-js";
195197
char arg1[] = "--disable=webkeyboard,webpanel";
196198
char *argv[] = {arg0, arg1};
197199
instance = std::make_unique<Instance>(FCITX_ARRAY_SIZE(argv), argv);

0 commit comments

Comments
 (0)