Skip to content

Commit 25ae9ba

Browse files
authored
Merge pull request #35 from mad-p/feature/menu
IME Menu - 学習/統計ファイルを更新 - 設定ファイルフォルダを開く - サンプル設定ファイルを作成
2 parents 630d5a5 + bf22cd3 commit 25ae9ba

2 files changed

Lines changed: 34 additions & 11 deletions

File tree

MacTcode/Tcode/TcodeInputController.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import InputMethodKit
1010

1111
@objc(TcodeInputController)
1212
class TcodeInputController: IMKInputController, Controller {
13+
let menuObj: NSMenu
14+
1315
var modeStack: [Mode] = []
1416
let candidateWindow: IMKCandidates
1517
var pendingKakutei: PendingKakuteiMode?
@@ -21,12 +23,37 @@ class TcodeInputController: IMKInputController, Controller {
2123
}
2224

2325
override init!(server: IMKServer!, delegate: Any!, client inputClient: Any!) {
26+
self.menuObj = NSMenu(title: "MacTcode")
27+
self.menuObj.addItem(withTitle: "学習/統計ファイルを更新", action: #selector(writeStatsToFile), keyEquivalent: "")
28+
self.menuObj.addItem(withTitle: "設定ファイルフォルダを開く", action: #selector(openConfigFolder), keyEquivalent: "")
29+
self.menuObj.addItem(withTitle: "サンプル設定ファイルを作成", action: #selector(createSampleConfigFile), keyEquivalent: "")
30+
2431
candidateWindow = IMKCandidates(server: server, panelType: kIMKSingleRowSteppingCandidatePanel)
2532
super.init(server: server, delegate: delegate, client: inputClient)
2633
setupCandidateWindow()
2734
Log.i("★★TcodeInputController: init self=\(ObjectIdentifier(self))")
2835
}
2936

37+
override func menu() -> NSMenu! {
38+
return menuObj
39+
}
40+
41+
@objc
42+
func openConfigFolder() {
43+
let url = UserConfigs.i.macTcodeURL
44+
NSWorkspace.shared.open(url)
45+
}
46+
47+
@objc
48+
func writeStatsToFile() {
49+
InputStats.i.writeStatsToFile()
50+
}
51+
52+
@objc
53+
func createSampleConfigFile() {
54+
UserConfigs.i.createSampleConfigFile()
55+
}
56+
3057
override func inputControllerWillClose() {
3158
Log.i("★★TcodeInputController: inputControllerWillClose self=\(ObjectIdentifier(self))")
3259
super.inputControllerWillClose()

MacTcode/UserConfigs.swift

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -465,13 +465,7 @@ class UserConfigs {
465465

466466
func saveConfig() {
467467
do {
468-
// 保存前に妥当性を検証
469-
try validateConfiguration(configData)
470-
471-
let encoder = JSONEncoder()
472-
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
473-
let data = try encoder.encode(configData)
474-
try data.write(to: configURL)
468+
try saveConfigToFile(configURL)
475469
log("Configuration saved successfully.")
476470
} catch {
477471
log("Failed to save configuration: \(error)")
@@ -485,7 +479,6 @@ class UserConfigs {
485479
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
486480
let data = try encoder.encode(configData)
487481
try data.write(to: fileURL)
488-
log("Configuration exported to: \(fileURL.path)")
489482
}
490483

491484
func resetToDefaults() {
@@ -515,9 +508,12 @@ class UserConfigs {
515508
}
516509

517510
func createSampleConfigFile() {
518-
if !configFileExists {
519-
saveConfig()
520-
log("Sample configuration file created at: \(configURL.path)")
511+
let sampleConfigURL = macTcodeURL.appendingPathComponent("sample-config.json")
512+
do {
513+
try saveConfigToFile(sampleConfigURL)
514+
log("Sample configuration file created at: \(sampleConfigURL.path)")
515+
} catch {
516+
log("Failed to create sample configuration file: \(error)")
521517
}
522518
}
523519

0 commit comments

Comments
 (0)