Skip to content

Commit

Permalink
import customized table (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj authored Jun 19, 2024
1 parent 495df97 commit 1a7f9e1
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 4 deletions.
Binary file modified assets/zh-Hans.lproj/Localizable.strings
Binary file not shown.
94 changes: 94 additions & 0 deletions src/config/importtable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import Fcitx
import Logging
import SwiftUI

class ImportTableVM: ObservableObject {
// Record IMs and auto add new ones.
@Published private(set) var ims = [String]()
var onError: (String) -> Void = { _ in }
var finalize: () -> Void = {}

func setHandler(onError: @escaping (String) -> Void, finalize: @escaping () -> Void) {
self.onError = onError
self.finalize = finalize
}

func load() {
ims = getFileNamesWithExtension(imLocalDir.localPath(), ".conf")
}
}

private func convertTxt() -> [String] {
let converter = libraryDir.appendingPathComponent("bin/libime_tabledict").localPath()
let tables = getFileNamesWithExtension(tableLocalDir.localPath(), ".txt")
return tables.filter({ table in
let src = tableLocalDir.appendingPathComponent("\(table).txt")
return
!(exec(
converter,
[src.localPath(), tableLocalDir.appendingPathComponent("\(table).dict").localPath()])
&& removeFile(src))
})
}

struct ImportTableView: View {
@Environment(\.presentationMode) var presentationMode

@ObservedObject private var importTableVM = ImportTableVM()

func load(onError: @escaping (String) -> Void, finalize: @escaping () -> Void) -> some View {
mkdirP(imLocalDir.localPath())
mkdirP(tableLocalDir.localPath())
importTableVM.setHandler(onError: onError, finalize: finalize)
importTableVM.load()
return self
}

var body: some View {
VStack {
Button {
NSWorkspace.shared.open(imLocalDir)
} label: {
Text("Copy \\*.conf to this directory")
}
Button {
NSWorkspace.shared.open(tableLocalDir)
} label: {
Text("Copy \\*.dict/\\*.txt to this directory")
}

Spacer().frame(height: gapSize)

HStack {
Button {
presentationMode.wrappedValue.dismiss()
} label: {
Text("Cancel")
}

Button {
let existingIMs = Set(importTableVM.ims)
let failures = convertTxt()
importTableVM.load()
let newIMs = importTableVM.ims.filter({ im in !existingIMs.contains(im) })
restartAndReconnect()
if Fcitx.imGroupCount() == 1 {
for im in newIMs {
Fcitx.imAddToCurrentGroup(im)
}
}
presentationMode.wrappedValue.dismiss()
if !failures.isEmpty {
let msg = String(
format: NSLocalizedString("Failed to convert txt table(s): %@", comment: ""),
failures.joined(separator: ", "))
importTableVM.onError(msg)
}
importTableVM.finalize()
} label: {
Text("Reload")
}.buttonStyle(.borderedProminent)
}
}.padding()
}
}
30 changes: 29 additions & 1 deletion src/config/inputmethod.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import AlertToast
import Fcitx
import Logging
import SwiftUI
Expand Down Expand Up @@ -69,6 +70,10 @@ struct InputMethodConfigView: View {
@State fileprivate var addToGroup: Group?
@State var mouseHoverIMID: UUID?

@State private var showImportTable = false
@State private var importTableErrorMsg = ""
@State private var showImportTableError = false

var body: some View {
NavigationSplitView {
List(selection: $viewModel.selectedItem) {
Expand Down Expand Up @@ -189,15 +194,38 @@ struct InputMethodConfigView: View {
} label: {
Text("Cancel")
}

Spacer()

Button {
showImportTable = true
} label: {
Text("Import customized table")
}
Button {
add()
addingInputMethod = false
} label: {
Text("Add")
}.buttonStyle(.borderedProminent)
.disabled(inputMethodsToAdd.count == 0)
.disabled(inputMethodsToAdd.isEmpty)
}.padding()
}.padding([.top])
.sheet(isPresented: $showImportTable) {
ImportTableView().load(
onError: { msg in
importTableErrorMsg = msg
showImportTableError = true
},
finalize: {
refresh()
})
}
.toast(isPresenting: $showImportTableError) {
AlertToast(
displayMode: .hud,
type: .error(Color.red), title: importTableErrorMsg)
}
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/config/util.swift
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,12 @@ func exec(_ command: String, _ args: [String]) -> Bool {
process.launchPath = command
process.arguments = args

process.launch()
process.waitUntilExit()
return process.terminationStatus == 0
do {
try process.run()
process.waitUntilExit()
return process.terminationStatus == 0
} catch {
FCITX_ERROR("Fatal error executing \(command) \(args)")
return false
}
}

0 comments on commit 1a7f9e1

Please sign in to comment.