Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

categorize plugins #126

Merged
merged 2 commits into from
May 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
exit 1
fi
done
file assets/zh-Hans.lproj/Localizable.strings | grep UTF-16

build:
needs: lint
Expand Down
Binary file modified assets/zh-Hans.lproj/Localizable.strings
Binary file not shown.
73 changes: 57 additions & 16 deletions src/config/plugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,44 @@ private let pluginDirectory = libraryDir.appendingPathComponent("plugin")

struct Plugin: Identifiable, Hashable {
let id: String
let category: String
let github: String?
}

private let officialPlugins = [
"anthy",
"chinese-addons",
"skk",
"hallelujah",
"rime",
"unikey",
"thai",
"lua",
].map { Plugin(id: $0) }
Plugin(
id: "anthy", category: NSLocalizedString("Japanese", comment: ""), github: "fcitx/fcitx5-anthy"),
Plugin(
id: "chinese-addons", category: NSLocalizedString("Chinese", comment: ""),
github: "fcitx/fcitx5-chinese-addons"),
Plugin(
id: "skk", category: NSLocalizedString("Japanese", comment: ""), github: "fcitx/fcitx5-skk"),
Plugin(
id: "hallelujah", category: NSLocalizedString("English", comment: ""),
github: "fcitx-contrib/fcitx5-hallelujah"),
Plugin(
id: "rime", category: NSLocalizedString("Generic", comment: ""), github: "fcitx/fcitx5-rime"),
Plugin(
id: "unikey", category: NSLocalizedString("Vietnamese", comment: ""),
github: "fcitx/fcitx5-unikey"),
Plugin(
id: "thai", category: NSLocalizedString("Thai", comment: ""), github: "fcitx/fcitx5-libthai"),
Plugin(id: "lua", category: NSLocalizedString("Other", comment: ""), github: "fcitx/fcitx5-lua"),
]

private var pluginMap = officialPlugins.reduce(into: [String: Plugin]()) { result, plugin in
result[plugin.id] = plugin
}

// fcitx5 doesn't unload addons from memory, so once loaded, we have to restart process to use an updated version.
private var inMemoryPlugins: [String] = []
private var needsRestart = false

private func getInstalledPlugins() -> [Plugin] {
let names = getFileNamesWithExtension(pluginDirectory.localPath(), ".json")
return names.map { Plugin(id: $0) }
return names.map {
pluginMap[$0] ?? Plugin(id: $0, category: NSLocalizedString("Other", comment: ""), github: nil)
}
}

private func getFileName(_ plugin: String) -> String {
Expand Down Expand Up @@ -144,6 +162,33 @@ struct PluginView: View {
processing = false
}

private func categorizePlugins(_ plugins: [Plugin]) -> some View {
let categorizedPlugins = plugins.reduce(into: [String: [Plugin]]()) { result, plugin in
result[plugin.category, default: []].append(plugin)
}
return ForEach(categorizedPlugins.keys.sorted(), id: \.self) { category in
Section(header: Text(category)) {
ForEach(categorizedPlugins[category]!) { plugin in
HStack {
Text(plugin.id)
if plugin.github != nil,
let url = URL(string: "https://github.com/\(plugin.github!)")
{
Button(
action: {
NSWorkspace.shared.open(url)
},
label: {
Image(systemName: "arrow.up.forward.app.fill")
}
).buttonStyle(.plain).help("\(url)")
}
}
}
}
}
}

private func install(_ autoRestart: Bool) {
processing = true
mkdirP(cacheDir.localPath())
Expand Down Expand Up @@ -253,9 +298,7 @@ struct PluginView: View {
Text("Installed").font(.system(size: sectionHeaderSize)).frame(
maxWidth: .infinity, alignment: .leading)
List(selection: $selectedInstalled) {
ForEach(pluginVM.installedPlugins) { plugin in
Text(plugin.id)
}
categorizePlugins(pluginVM.installedPlugins)
}
Button("Uninstall", role: .destructive, action: uninstall).disabled(
selectedInstalled.isEmpty || processing)
Expand All @@ -264,9 +307,7 @@ struct PluginView: View {
Text("Available").font(.system(size: sectionHeaderSize)).frame(
maxWidth: .infinity, alignment: .leading)
List(selection: $selectedAvailable) {
ForEach(pluginVM.availablePlugins) { plugin in
Text(plugin.id)
}
categorizePlugins(pluginVM.availablePlugins)
}.contextMenu(forSelectionType: String.self) { items in
} primaryAction: { items in
// Double click
Expand Down