Skip to content

Commit

Permalink
implement input methods removal
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Nov 9, 2024
1 parent b097a16 commit 3d04f6d
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 18 deletions.
37 changes: 20 additions & 17 deletions common/inputmethod.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "common-public.h"
#include "common.h"
#include "util.h"
#include <fcitx/inputmethodentry.h>
#include <fcitx/inputmethodmanager.h>
#include <nlohmann/json.hpp>
Expand All @@ -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();
});
}
3 changes: 3 additions & 0 deletions keyboard/fcitx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "../common/util.h"
#include "fcitx.h"

#include <fcitx/inputmethodmanager.h>

fcitx::IosFrontend *frontend;

void startFcitx(const char *bundlePath, const char *appGroupPath) {
Expand Down Expand Up @@ -44,5 +46,6 @@ void reload() {
instance->reloadAddonConfig(name);
}
}
instance->inputMethodManager().load();
});
}
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target_include_directories(${BUNDLE_NAME} PRIVATE
"${CMAKE_CURRENT_SOURCE_DIR}"
"${PROJECT_SOURCE_DIR}/common"
"${PROJECT_BINARY_DIR}/common/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}"
"${PROJECT_BINARY_DIR}/ipc/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}"
"${PROJECT_BINARY_DIR}/iosnotifications/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}"
"${PROJECT_BINARY_DIR}/deps/AlertToast/$<CONFIG>${CMAKE_XCODE_EFFECTIVE_PLATFORMS}"
)
Expand Down
19 changes: 18 additions & 1 deletion src/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import AlertToast
import Fcitx
import FcitxCommon
import FcitxIpc
import NotifySwift
import SwiftUI
import SwiftUtil
Expand All @@ -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 {
Expand All @@ -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")
Expand Down
18 changes: 18 additions & 0 deletions src/fcitx.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#include "fcitx.h"
#include "../common/common.h"
#include "../common/util.h"
#include <fcitx-config/rawconfig.h>
#include <fcitx/addonmanager.h>
#include <fcitx/inputmethodmanager.h>
#include <nlohmann/json.hpp>

constexpr char addonConfigPrefix[] = "fcitx://config/addon/";

Expand Down Expand Up @@ -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<std::string>());
}
imMgr.setGroup(group);
imMgr.save();
});
}
1 change: 1 addition & 0 deletions src/fcitx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

0 comments on commit 3d04f6d

Please sign in to comment.