Skip to content

Commit 9636747

Browse files
authored
toggle input method and fix initial space label (#46)
1 parent 27b5625 commit 9636747

File tree

10 files changed

+50
-19
lines changed

10 files changed

+50
-19
lines changed

entry/src/main/cpp/napi_init.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ API(reset) {
3333
return ret;
3434
}
3535

36+
API(toggle) {
37+
fcitx::toggle();
38+
return {};
39+
}
40+
3641
API(processKey) {
3742
GET_ARGS(3)
3843
GET_U32(unicode, 0)
@@ -69,6 +74,11 @@ API(activateCandidateAction) {
6974
return {};
7075
}
7176

77+
API(updateStatusArea) {
78+
fcitx::updateStatusArea();
79+
return {};
80+
}
81+
7282
API(activateStatusAreaAction) {
7383
GET_ARGS(1)
7484
GET_I32(id, 0)
@@ -107,10 +117,12 @@ static napi_value Init(napi_env env, napi_value exports) {
107117
{"focusIn", nullptr, focusIn, nullptr, nullptr, nullptr, napi_default, nullptr},
108118
{"focusOut", nullptr, focusOut, nullptr, nullptr, nullptr, napi_default, nullptr},
109119
{"reset", nullptr, reset, nullptr, nullptr, nullptr, napi_default, nullptr},
120+
{"toggle", nullptr, toggle, nullptr, nullptr, nullptr, napi_default, nullptr},
110121
{"processKey", nullptr, processKey, nullptr, nullptr, nullptr, napi_default, nullptr},
111122
{"selectCandidate", nullptr, selectCandidate, nullptr, nullptr, nullptr, napi_default, nullptr},
112123
{"askCandidateActions", nullptr, askCandidateActions, nullptr, nullptr, nullptr, napi_default, nullptr},
113124
{"activateCandidateAction", nullptr, activateCandidateAction, nullptr, nullptr, nullptr, napi_default, nullptr},
125+
{"updateStatusArea", nullptr, updateStatusArea, nullptr, nullptr, nullptr, napi_default, nullptr},
114126
{"activateStatusAreaAction", nullptr, activateStatusAreaAction, nullptr, nullptr, nullptr, napi_default,
115127
nullptr}};
116128
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);

entry/src/main/cpp/src/fcitx.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "keycode.h"
1414
#include "util.h"
1515
#include "../harmonyfrontend/harmonyfrontend.h"
16+
#include "../webkeyboard/webkeyboard.h"
1617
#include "inputmethod.h"
1718

1819
#ifdef __x86_64__
@@ -37,6 +38,7 @@ namespace fcitx {
3738
std::unique_ptr<Instance> instance;
3839
std::unique_ptr<fcitx::EventDispatcher> dispatcher;
3940
HarmonyFrontend *frontend;
41+
WebKeyboard *ui;
4042

4143
static native_streambuf log_streambuf;
4244
static std::ostream stream(&log_streambuf);
@@ -80,7 +82,8 @@ void init(const std::string &bundle, const std::string &resfile) {
8082
dispatcher->attach(&instance->eventLoop());
8183
fcitx_thread = std::thread([] { instance->eventLoop().exec(); });
8284
frontend = dynamic_cast<HarmonyFrontend *>(addonMgr.addon("harmonyfrontend"));
83-
setInputMethods({"pinyin"}); // XXX: for test only.
85+
ui = dynamic_cast<WebKeyboard *>(addonMgr.addon("webkeyboard"));
86+
setInputMethods({"keyboard-th", "pinyin"}); // XXX: for test only.
8487
}
8588

8689
void focusIn(bool clientPreedit) {
@@ -164,6 +167,14 @@ void activateCandidateAction(int index, int id) {
164167
});
165168
}
166169

170+
void updateStatusArea() {
171+
with_fcitx([] {
172+
if (auto *ic = instance->mostRecentInputContext()) {
173+
ui->updateStatusArea(ic);
174+
}
175+
});
176+
}
177+
167178
void activateStatusAreaAction(int id) {
168179
with_fcitx([id] {
169180
if (auto *ic = instance->mostRecentInputContext()) {
@@ -172,4 +183,8 @@ void activateStatusAreaAction(int id) {
172183
}
173184
});
174185
}
186+
187+
void toggle() {
188+
with_fcitx([] { instance->toggle(); });
189+
}
175190
} // namespace fcitx

entry/src/main/cpp/src/fcitx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ InputContextState processKey(uint32_t unicode, int32_t keyCode, bool isRelease);
2323
void selectCandidate(int index);
2424
void askCandidateAction(int index);
2525
void activateCandidateAction(int index, int id);
26+
void updateStatusArea();
2627
void activateStatusAreaAction(int id);
28+
void toggle();
2729
} // namespace fcitx

entry/src/main/cpp/types/libentry/Index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export interface InputContextState {
1111
}
1212
export const processKey: (unicode: number, keyCode: number, isRelease: boolean) => InputContextState
1313
export const reset: () => InputContextState
14+
export const toggle: () => void
1415
export const selectCandidate: (index: number) => void
1516
export const askCandidateActions: (index: number) => void
1617
export const activateCandidateAction: (index: number, id: number) => void
18+
export const updateStatusArea: () => void
1719
export const activateStatusAreaAction: (id: number) => void

entry/src/main/cpp/webkeyboard/utility.h

Lines changed: 0 additions & 14 deletions
This file was deleted.

entry/src/main/cpp/webkeyboard/webkeyboard.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@
66
#include "../src/fcitx.h"
77
#include "../src/inputmethod.h"
88
#include "webkeyboard.h"
9+
#include <sstream>
910

11+
#include "pugixml.hpp"
12+
13+
std::string escape_html(const std::string &content) {
14+
std::ostringstream oss;
15+
pugi::xml_document doc;
16+
doc.append_child(pugi::node_pcdata).set_value(content);
17+
doc.print(oss, "", pugi::format_raw);
18+
return oss.str();
19+
}
1020

1121
namespace fcitx {
1222

entry/src/main/cpp/webkeyboard/webkeyboard.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
#include <fcitx/instance.h>
77
#include <nlohmann/json.hpp>
88

9-
#include "utility.h"
10-
119
using json = nlohmann::json;
1210

11+
std::string escape_html(const std::string &content);
12+
1313
namespace fcitx {
1414

1515
struct Candidate {
@@ -41,12 +41,12 @@ class WebKeyboard final : public VirtualKeyboardUserInterface {
4141
bool isVirtualKeyboardVisible() const override { return true; }
4242
void showVirtualKeyboard() override;
4343
void hideVirtualKeyboard() override {}
44+
void updateStatusArea(InputContext *ic);
4445

4546
private:
4647
Instance *instance_;
4748

4849
void setCandidatesAsync(const std::vector<Candidate> &candidates, int highlighted);
49-
void updateStatusArea(InputContext *ic);
5050
};
5151

5252
class WebKeyboardFactory : public AddonFactory {

entry/src/main/ets/InputMethodExtensionAbility/model/KeyboardController.ets

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ export class KeyboardController {
310310
this.selecting = false
311311
this.setCursor(this.textInputClient?.getTextIndexAtCursorSync() ?? 0)
312312
break
313+
case 'GLOBE':
314+
return fcitx.toggle()
313315
case 'KEY_DOWN':
314316
return this.handleKey(event.data.key, convertCode(event.data.code))
315317
case 'PASTE':
@@ -434,6 +436,7 @@ export class KeyboardController {
434436
this.keyboardController = kbController;
435437
this.attribute = textInputClient.getEditorAttributeSync()
436438
fcitx.focusIn(this.attribute.isTextPreviewSupported)
439+
fcitx.updateStatusArea()
437440
this.setEnterKeyType()
438441
})
439442
ability.on('inputStop', () => {

entry/src/main/ets/InputMethodExtensionAbility/model/keycode.ets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ const jsCodeMap: Record<string, KeyCode> = {
3838
'KeyZ': KeyCode.KEYCODE_Z,
3939
'Period': KeyCode.KEYCODE_PERIOD,
4040
'Semicolon': KeyCode.KEYCODE_SEMICOLON,
41+
'Space': KeyCode.KEYCODE_SPACE,
4142
}
4243

4344
export function convertCode(jsCode: string): KeyCode | undefined {

0 commit comments

Comments
 (0)