Skip to content

Commit 2b4a5ad

Browse files
authored
long press globe; rework status area/input methods update (#47)
1 parent 9636747 commit 2b4a5ad

File tree

9 files changed

+29
-28
lines changed

9 files changed

+29
-28
lines changed

entry/src/main/cpp/harmonyfrontend/harmonyfrontend.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
#include "harmonyfrontend.h"
2+
#include "../src/inputmethod.h"
23
#include <nlohmann/json.hpp>
34

45
using json = nlohmann::json;
56

67
namespace fcitx {
78
HarmonyFrontend::HarmonyFrontend(Instance *instance)
89
: instance_(instance), focusGroup_("harmony", instance->inputContextManager()) {
10+
handler_ = instance_->watchEvent(
11+
EventType::InputContextInputMethodActivated, EventWatcherPhase::Default, [this](Event &event) {
12+
notify_main_async(json{
13+
{"type", "INPUT_METHODS"},
14+
{"data",
15+
{{"currentInputMethod", instance_->currentInputMethod()}, {"inputMethods", getInputMethodsJson()}}}}
16+
.dump());
17+
});
918
createInputContext();
1019
}
1120

entry/src/main/cpp/harmonyfrontend/harmonyfrontend.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class HarmonyFrontend : public AddonInstance {
3131
Instance *instance_;
3232
FocusGroup focusGroup_;
3333
HarmonyInputContext *ic_;
34+
std::unique_ptr<HandlerTableEntry<EventHandler>> handler_;
3435
};
3536

3637
class HarmonyFrontendFactory : public AddonFactory {

entry/src/main/cpp/napi_init.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,20 @@ API(activateCandidateAction) {
7474
return {};
7575
}
7676

77-
API(updateStatusArea) {
78-
fcitx::updateStatusArea();
79-
return {};
80-
}
81-
8277
API(activateStatusAreaAction) {
8378
GET_ARGS(1)
8479
GET_I32(id, 0)
8580
fcitx::activateStatusAreaAction(id);
8681
return {};
8782
}
8883

84+
API(setCurrentInputMethod) {
85+
GET_ARGS(1)
86+
GET_STRING(inputMethod, 0)
87+
fcitx::setCurrentInputMethod(inputMethod);
88+
return {};
89+
}
90+
8991
static void CallJs(napi_env env, napi_value jsCb, void *context, void *data) {
9092
if (env == nullptr) {
9193
return;
@@ -122,7 +124,7 @@ static napi_value Init(napi_env env, napi_value exports) {
122124
{"selectCandidate", nullptr, selectCandidate, nullptr, nullptr, nullptr, napi_default, nullptr},
123125
{"askCandidateActions", nullptr, askCandidateActions, nullptr, nullptr, nullptr, napi_default, nullptr},
124126
{"activateCandidateAction", nullptr, activateCandidateAction, nullptr, nullptr, nullptr, napi_default, nullptr},
125-
{"updateStatusArea", nullptr, updateStatusArea, nullptr, nullptr, nullptr, napi_default, nullptr},
127+
{"setCurrentInputMethod", nullptr, setCurrentInputMethod, nullptr, nullptr, nullptr, napi_default, nullptr},
126128
{"activateStatusAreaAction", nullptr, activateStatusAreaAction, nullptr, nullptr, nullptr, napi_default,
127129
nullptr}};
128130
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ namespace fcitx {
3838
std::unique_ptr<Instance> instance;
3939
std::unique_ptr<fcitx::EventDispatcher> dispatcher;
4040
HarmonyFrontend *frontend;
41-
WebKeyboard *ui;
4241

4342
static native_streambuf log_streambuf;
4443
static std::ostream stream(&log_streambuf);
@@ -82,8 +81,7 @@ void init(const std::string &bundle, const std::string &resfile) {
8281
dispatcher->attach(&instance->eventLoop());
8382
fcitx_thread = std::thread([] { instance->eventLoop().exec(); });
8483
frontend = dynamic_cast<HarmonyFrontend *>(addonMgr.addon("harmonyfrontend"));
85-
ui = dynamic_cast<WebKeyboard *>(addonMgr.addon("webkeyboard"));
86-
setInputMethods({"keyboard-th", "pinyin"}); // XXX: for test only.
84+
setInputMethods({"keyboard-us", "keyboard-th", "pinyin"}); // XXX: for test only.
8785
}
8886

8987
void focusIn(bool clientPreedit) {
@@ -167,14 +165,6 @@ void activateCandidateAction(int index, int id) {
167165
});
168166
}
169167

170-
void updateStatusArea() {
171-
with_fcitx([] {
172-
if (auto *ic = instance->mostRecentInputContext()) {
173-
ui->updateStatusArea(ic);
174-
}
175-
});
176-
}
177-
178168
void activateStatusAreaAction(int id) {
179169
with_fcitx([id] {
180170
if (auto *ic = instance->mostRecentInputContext()) {
@@ -187,4 +177,8 @@ void activateStatusAreaAction(int id) {
187177
void toggle() {
188178
with_fcitx([] { instance->toggle(); });
189179
}
180+
181+
void setCurrentInputMethod(const std::string &inputMethod) {
182+
with_fcitx([&] { instance->setCurrentInputMethod(inputMethod); });
183+
}
190184
} // namespace fcitx

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +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();
2726
void activateStatusAreaAction(int id);
2827
void toggle();
28+
void setCurrentInputMethod(const std::string &inputMethod);
2929
} // namespace fcitx

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ export const toggle: () => void
1515
export const selectCandidate: (index: number) => void
1616
export const askCandidateActions: (index: number) => void
1717
export const activateCandidateAction: (index: number, id: number) => void
18-
export const updateStatusArea: () => void
1918
export const activateStatusAreaAction: (id: number) => void
19+
export const setCurrentInputMethod: (inputMethod: string) => void

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <fcitx/statusarea.h>
55

66
#include "../src/fcitx.h"
7-
#include "../src/inputmethod.h"
87
#include "webkeyboard.h"
98
#include <sstream>
109

@@ -91,12 +90,7 @@ void WebKeyboard::updateStatusArea(InputContext *ic) {
9190
}
9291
actions.emplace_back(actionToJson(action, ic));
9392
}
94-
notify_main_async(json{{"type", "STATUS_AREA"},
95-
{"data",
96-
{{"actions", actions},
97-
{"currentInputMethod", instance_->currentInputMethod()},
98-
{"inputMethods", getInputMethodsJson()}}}}
99-
.dump());
93+
notify_main_async(json{{"type", "STATUS_AREA"}, {"data", actions}}.dump());
10094
}
10195
} // namespace fcitx
10296

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,8 @@ export class KeyboardController {
327327
break
328328
case 'SELECT_CANDIDATE':
329329
return fcitx.selectCandidate(event.data)
330+
case 'SET_INPUT_METHOD':
331+
return fcitx.setCurrentInputMethod(event.data)
330332
case 'STATUS_AREA_ACTION':
331333
return fcitx.activateStatusAreaAction(event.data)
332334
case 'UNDO':
@@ -436,7 +438,6 @@ export class KeyboardController {
436438
this.keyboardController = kbController;
437439
this.attribute = textInputClient.getEditorAttributeSync()
438440
fcitx.focusIn(this.attribute.isTextPreviewSupported)
439-
fcitx.updateStatusArea()
440441
this.setEnterKeyType()
441442
})
442443
ability.on('inputStop', () => {

0 commit comments

Comments
 (0)