From 3d04f6d21ebb9d6285f5e8317cf84178e67c6363 Mon Sep 17 00:00:00 2001 From: Qijia Liu Date: Sat, 9 Nov 2024 01:01:08 -0500 Subject: [PATCH] implement input methods removal --- common/inputmethod.cpp | 37 ++++++++++++++++++++----------------- keyboard/fcitx.cpp | 3 +++ src/CMakeLists.txt | 1 + src/ContentView.swift | 19 ++++++++++++++++++- src/fcitx.cpp | 18 ++++++++++++++++++ src/fcitx.h | 1 + 6 files changed, 61 insertions(+), 18 deletions(-) diff --git a/common/inputmethod.cpp b/common/inputmethod.cpp index a0d81ac..f8c0102 100644 --- a/common/inputmethod.cpp +++ b/common/inputmethod.cpp @@ -1,5 +1,6 @@ #include "common-public.h" #include "common.h" +#include "util.h" #include #include #include @@ -15,21 +16,23 @@ static nlohmann::json jsonDescribeIm(const fcitx::InputMethodEntry *entry) { } std::string getInputMethods() { - static std::string ret; - nlohmann::json j; - auto &imMgr = instance->inputMethodManager(); - auto group = imMgr.currentGroup(); - bool empty = true; - for (const auto &im : group.inputMethodList()) { - auto entry = imMgr.entry(im.name()); - if (!entry) - continue; - empty = false; - j.push_back(jsonDescribeIm(entry)); - } - if (empty) { // j is not treated array - return "[]"; - } - ret = j.dump(); - return ret.c_str(); + return with_fcitx([] { + static std::string ret; + nlohmann::json j; + auto &imMgr = instance->inputMethodManager(); + auto group = imMgr.currentGroup(); + bool empty = true; + for (const auto &im : group.inputMethodList()) { + auto entry = imMgr.entry(im.name()); + if (!entry) + continue; + empty = false; + j.push_back(jsonDescribeIm(entry)); + } + if (empty) { // j is not treated array + return "[]"; + } + ret = j.dump(); + return ret.c_str(); + }); } diff --git a/keyboard/fcitx.cpp b/keyboard/fcitx.cpp index 4034695..434b92a 100644 --- a/keyboard/fcitx.cpp +++ b/keyboard/fcitx.cpp @@ -4,6 +4,8 @@ #include "../common/util.h" #include "fcitx.h" +#include + fcitx::IosFrontend *frontend; void startFcitx(const char *bundlePath, const char *appGroupPath) { @@ -44,5 +46,6 @@ void reload() { instance->reloadAddonConfig(name); } } + instance->inputMethodManager().load(); }); } diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5278308..ab20cef 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -13,6 +13,7 @@ target_include_directories(${BUNDLE_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/common" "${PROJECT_BINARY_DIR}/common/$${CMAKE_XCODE_EFFECTIVE_PLATFORMS}" + "${PROJECT_BINARY_DIR}/ipc/$${CMAKE_XCODE_EFFECTIVE_PLATFORMS}" "${PROJECT_BINARY_DIR}/iosnotifications/$${CMAKE_XCODE_EFFECTIVE_PLATFORMS}" "${PROJECT_BINARY_DIR}/deps/AlertToast/$${CMAKE_XCODE_EFFECTIVE_PLATFORMS}" ) diff --git a/src/ContentView.swift b/src/ContentView.swift index ffec2e7..16b4889 100644 --- a/src/ContentView.swift +++ b/src/ContentView.swift @@ -1,6 +1,7 @@ import AlertToast import Fcitx import FcitxCommon +import FcitxIpc import NotifySwift import SwiftUI import SwiftUtil @@ -19,6 +20,13 @@ private class ViewModel: ObservableObject { inputMethods = try! JSONDecoder().decode( [InputMethod].self, from: String(getInputMethods()).data(using: .utf8)!) } + + func removeInputMethods(at offsets: IndexSet) { + inputMethods.remove(atOffsets: offsets) + setInputMethods( + String(data: try! JSONEncoder().encode(inputMethods.map { $0.name }), encoding: .utf8)!) + requestReload() + } } struct ContentView: View { @@ -36,15 +44,24 @@ struct ContentView: View { setConfig(url.absoluteString, "{}") } + func removeInputMethods(at offsets: IndexSet) { + viewModel.removeInputMethods(at: offsets) + } + var body: some View { NavigationView { List { Section(header: Text("Input Methods")) { - ForEach(viewModel.inputMethods, id: \.name) { inputMethod in + let forEach = ForEach(viewModel.inputMethods, id: \.name) { inputMethod in NavigationLink(destination: ConfigView(inputMethod: inputMethod)) { Text(inputMethod.displayName) } } + if viewModel.inputMethods.count > 1 { + forEach.onDelete(perform: removeInputMethods) + } else { + forEach + } } } .navigationTitle("Fcitx5") diff --git a/src/fcitx.cpp b/src/fcitx.cpp index 6f8004b..c85d692 100644 --- a/src/fcitx.cpp +++ b/src/fcitx.cpp @@ -1,7 +1,10 @@ #include "fcitx.h" #include "../common/common.h" +#include "../common/util.h" #include #include +#include +#include constexpr char addonConfigPrefix[] = "fcitx://config/addon/"; @@ -42,3 +45,18 @@ void setConfig(const char *uri_, const char *value) { }); } } + +void setInputMethods(const char *json) { + with_fcitx([=] { + auto &imMgr = instance->inputMethodManager(); + auto group = imMgr.currentGroup(); + auto &imList = group.inputMethodList(); + imList.clear(); + auto j = nlohmann::json::parse(json); + for (const auto &im : j) { + imList.emplace_back(im.get()); + } + imMgr.setGroup(group); + imMgr.save(); + }); +} diff --git a/src/fcitx.h b/src/fcitx.h index 80f6a8f..c4adcb0 100644 --- a/src/fcitx.h +++ b/src/fcitx.h @@ -2,3 +2,4 @@ void startFcitx(const char *bundlePath, const char *appGroupPath); void setConfig(const char *uri, const char *value); +void setInputMethods(const char *json);