Skip to content

Commit

Permalink
menu actions
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Sep 9, 2024
1 parent 8c00aee commit 22682ac
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
10 changes: 10 additions & 0 deletions page/Fcitx5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export type Config = {
ERROR: string
}

export interface MenuAction {
id: number
desc: string
checked?: boolean
separator?: boolean
children?: MenuAction[]
}

export interface FCITX {
enable: () => void
disable: () => void
Expand All @@ -29,6 +37,8 @@ export interface FCITX {
getConfig: (uri: string) => Config
setConfig: (uri: string, json: object) => void
jsKeyToFcitxString: (event: KeyboardEvent) => string
getMenuActions: () => MenuAction[]
activateMenuAction: (id: number) => void
}

export const fcitxReady: Promise<null>
9 changes: 9 additions & 0 deletions page/action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import Module from './module'

export function getMenuActions() {
return JSON.parse(Module.ccall('get_menu_actions', 'string', [], []))
}

export function activateMenuAction(id: number) {
return Module.ccall('activate_menu_action', 'void', ['number'], [id])
}
3 changes: 3 additions & 0 deletions page/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { jsKeyToFcitxString, keyEvent } from './keycode'
import { commit, hidePanel, placePanel, setPreedit } from './client'
import { currentInputMethod, getAllInputMethods, getInputMethods, setCurrentInputMethod, setInputMethods } from './input-method'
import { getConfig, setConfig } from './config'
import { activateMenuAction, getMenuActions } from './action'

let res: (value: any) => void

Expand Down Expand Up @@ -44,6 +45,8 @@ window.fcitx = {
getConfig,
setConfig,
jsKeyToFcitxString,
getMenuActions,
activateMenuAction,
enable() {
document.addEventListener('focus', focus, true)
document.addEventListener('blur', blur, true)
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ add_executable(Fcitx5
keycode.cpp
input_method.cpp
config.cpp
action.cpp
)

target_include_directories(Fcitx5 PRIVATE
Expand Down
57 changes: 57 additions & 0 deletions src/action.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <emscripten.h>
#include <fcitx/action.h>
#include <fcitx/instance.h>
#include <fcitx/menu.h>
#include <fcitx/statusarea.h>
#include <fcitx/userinterfacemanager.h>
#include <nlohmann/json.hpp>

namespace fcitx {
extern std::unique_ptr<Instance> instance;

static nlohmann::json actionToJson(Action *action, InputContext *ic) {
nlohmann::json j;
j["id"] = action->id();
j["desc"] = action->shortText(ic);
if (action->isSeparator()) {
j["separator"] = true;
}
if (action->isCheckable()) {
bool checked = action->isChecked(ic);
j["checked"] = checked;
}
if (auto *menu = action->menu()) {
for (auto *subaction : menu->actions()) {
j["children"].emplace_back(actionToJson(subaction, ic));
}
}
return j;
}

extern "C" {
EMSCRIPTEN_KEEPALIVE const char *get_menu_actions() {
static std::string ret;
if (auto *ic = instance->mostRecentInputContext()) {
nlohmann::json j = nlohmann::json::array();
auto &statusArea = ic->statusArea();
for (auto *action : statusArea.allActions()) {
if (!action->id()) {
// Not registered with UI manager.
continue;
}
j.emplace_back(actionToJson(action, ic));
}
ret = j.dump();
return ret.c_str();
}
return "[]";
}

EMSCRIPTEN_KEEPALIVE void activate_menu_action(int id) {
if (auto *ic = instance->mostRecentInputContext()) {
auto *action = instance->userInterfaceManager().lookupActionById(id);
action->activate(ic);
}
}
}
} // namespace fcitx

0 comments on commit 22682ac

Please sign in to comment.