From ef9336e637d1dad18a3159b97ed7fc18a80e5f38 Mon Sep 17 00:00:00 2001 From: Econa77 Date: Sat, 9 Sep 2017 02:54:29 +0900 Subject: [PATCH] Implement clear history shortcut setting (#207) * Implement clear history shortcut setting * Add tests --- Clipy/Sources/Constants.swift | 1 + .../CPYGeneralPreferenceViewController.xib | 4 +- .../CPYShortcutsPreferenceViewController.xib | 77 ++++++++++++++++--- ...CPYShortcutsPreferenceViewController.swift | 7 ++ ...YShortcutsPreferenceViewController.strings | 21 +++-- ...YShortcutsPreferenceViewController.strings | 21 +++-- Clipy/Sources/Services/HotKeyService.swift | 20 +++++ ClipyTests/HotKeyServiceSpec.swift | 33 ++++++++ 8 files changed, 159 insertions(+), 25 deletions(-) diff --git a/Clipy/Sources/Constants.swift b/Clipy/Sources/Constants.swift index f0cc418b..f864fb15 100644 --- a/Clipy/Sources/Constants.swift +++ b/Clipy/Sources/Constants.swift @@ -94,6 +94,7 @@ struct Constants { static let snippetKeyCombo = "kCPYHotKeySnippetKeyCombo" static let migrateNewKeyCombo = "kCPYMigrateNewKeyCombo" static let folderKeyCombos = "kCPYFolderKeyCombos" + static let clearHistoryKeyCombo = "kCPYClearHistoryKeyCombo" } } diff --git a/Clipy/Sources/Preferences/Panels/Base.lproj/CPYGeneralPreferenceViewController.xib b/Clipy/Sources/Preferences/Panels/Base.lproj/CPYGeneralPreferenceViewController.xib index 1baf6b43..63b11bb9 100644 --- a/Clipy/Sources/Preferences/Panels/Base.lproj/CPYGeneralPreferenceViewController.xib +++ b/Clipy/Sources/Preferences/Panels/Base.lproj/CPYGeneralPreferenceViewController.xib @@ -1,7 +1,7 @@ - + - + diff --git a/Clipy/Sources/Preferences/Panels/Base.lproj/CPYShortcutsPreferenceViewController.xib b/Clipy/Sources/Preferences/Panels/Base.lproj/CPYShortcutsPreferenceViewController.xib index 145fb6cf..1c2bf342 100644 --- a/Clipy/Sources/Preferences/Panels/Base.lproj/CPYShortcutsPreferenceViewController.xib +++ b/Clipy/Sources/Preferences/Panels/Base.lproj/CPYShortcutsPreferenceViewController.xib @@ -1,12 +1,13 @@ - + - + + @@ -16,35 +17,44 @@ - + + + + + + + + + + - + - + - + - + - + - + - + @@ -109,8 +119,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + diff --git a/Clipy/Sources/Preferences/Panels/CPYShortcutsPreferenceViewController.swift b/Clipy/Sources/Preferences/Panels/CPYShortcutsPreferenceViewController.swift index be6b14b3..7b7b70e4 100644 --- a/Clipy/Sources/Preferences/Panels/CPYShortcutsPreferenceViewController.swift +++ b/Clipy/Sources/Preferences/Panels/CPYShortcutsPreferenceViewController.swift @@ -16,6 +16,7 @@ class CPYShortcutsPreferenceViewController: NSViewController { @IBOutlet weak var mainShortcutRecordView: RecordView! @IBOutlet weak var historyShortcutRecordView: RecordView! @IBOutlet weak var snippetShortcutRecordView: RecordView! + @IBOutlet weak var clearHistoryShortcutRecordView: RecordView! // MARK: - Initialize override func loadView() { @@ -23,6 +24,7 @@ class CPYShortcutsPreferenceViewController: NSViewController { mainShortcutRecordView.delegate = self historyShortcutRecordView.delegate = self snippetShortcutRecordView.delegate = self + clearHistoryShortcutRecordView.delegate = self prepareHotKeys() } @@ -34,6 +36,7 @@ fileprivate extension CPYShortcutsPreferenceViewController { mainShortcutRecordView.keyCombo = AppEnvironment.current.hotKeyService.mainKeyCombo historyShortcutRecordView.keyCombo = AppEnvironment.current.hotKeyService.historyKeyCombo snippetShortcutRecordView.keyCombo = AppEnvironment.current.hotKeyService.snippetKeyCombo + clearHistoryShortcutRecordView.keyCombo = AppEnvironment.current.hotKeyService.clearHistoryKeyCombo } } @@ -55,6 +58,8 @@ extension CPYShortcutsPreferenceViewController: RecordViewDelegate { AppEnvironment.current.hotKeyService.change(with: .history, keyCombo: nil) case snippetShortcutRecordView: AppEnvironment.current.hotKeyService.change(with: .snippet, keyCombo: nil) + case clearHistoryShortcutRecordView: + AppEnvironment.current.hotKeyService.changeClearHistoryKeyCombo(nil) default: break } } @@ -67,6 +72,8 @@ extension CPYShortcutsPreferenceViewController: RecordViewDelegate { AppEnvironment.current.hotKeyService.change(with: .history, keyCombo: keyCombo) case snippetShortcutRecordView: AppEnvironment.current.hotKeyService.change(with: .snippet, keyCombo: keyCombo) + case clearHistoryShortcutRecordView: + AppEnvironment.current.hotKeyService.changeClearHistoryKeyCombo(keyCombo) default: break } } diff --git a/Clipy/Sources/Preferences/Panels/it.lproj/CPYShortcutsPreferenceViewController.strings b/Clipy/Sources/Preferences/Panels/it.lproj/CPYShortcutsPreferenceViewController.strings index 12fb814c..2876b439 100644 --- a/Clipy/Sources/Preferences/Panels/it.lproj/CPYShortcutsPreferenceViewController.strings +++ b/Clipy/Sources/Preferences/Panels/it.lproj/CPYShortcutsPreferenceViewController.strings @@ -1,9 +1,18 @@ -/* Class = "NSTextFieldCell"; title = "Main Menu:"; ObjectID = "5sL-G8-1io"; */ -"5sL-G8-1io.title" = "Menu principale:"; +/* Class = "NSTextFieldCell"; title = "Main:"; ObjectID = "5sL-G8-1io"; */ +"5sL-G8-1io.title" = "principale:"; -/* Class = "NSTextFieldCell"; title = "History Menu:"; ObjectID = "Abl-cO-JwU"; */ -"Abl-cO-JwU.title" = "Menu Cronologia:"; +/* Class = "NSTextFieldCell"; title = "History:"; ObjectID = "Abl-cO-JwU"; */ +"Abl-cO-JwU.title" = "Cronologia:"; -/* Class = "NSTextFieldCell"; title = "Snippets Menu:"; ObjectID = "fXr-ry-5ds"; */ -"fXr-ry-5ds.title" = "Menu Snippet:"; +/* Class = "NSTextFieldCell"; title = "Snippets:"; ObjectID = "fXr-ry-5ds"; */ +"fXr-ry-5ds.title" = "Snippet:"; + +/* Class = "NSTextFieldCell"; title = "Menu"; ObjectID = "X5L-5V-1eH"; */ +"X5L-5V-1eH.title" = "Menu"; + +/* Class = "NSTextFieldCell"; title = "History"; ObjectID = "Txl-Qh-RzP"; */ +"Txl-Qh-RzP.title" = "Cronologia"; + +/* Class = "NSTextFieldCell"; title = "Clear History:"; ObjectID = "f3b-Ht-X5w"; */ +"f3b-Ht-X5w.title" = "Svuota:"; diff --git a/Clipy/Sources/Preferences/Panels/ja.lproj/CPYShortcutsPreferenceViewController.strings b/Clipy/Sources/Preferences/Panels/ja.lproj/CPYShortcutsPreferenceViewController.strings index d37dfc54..b301dc05 100644 --- a/Clipy/Sources/Preferences/Panels/ja.lproj/CPYShortcutsPreferenceViewController.strings +++ b/Clipy/Sources/Preferences/Panels/ja.lproj/CPYShortcutsPreferenceViewController.strings @@ -1,9 +1,18 @@ -/* Class = "NSTextFieldCell"; title = "Main Menu:"; ObjectID = "5sL-G8-1io"; */ -"5sL-G8-1io.title" = "メインメニュー:"; +/* Class = "NSTextFieldCell"; title = "Main:"; ObjectID = "5sL-G8-1io"; */ +"5sL-G8-1io.title" = "メイン:"; -/* Class = "NSTextFieldCell"; title = "History Menu:"; ObjectID = "Abl-cO-JwU"; */ -"Abl-cO-JwU.title" = "履歴メニュー:"; +/* Class = "NSTextFieldCell"; title = "History:"; ObjectID = "Abl-cO-JwU"; */ +"Abl-cO-JwU.title" = "履歴:"; -/* Class = "NSTextFieldCell"; title = "Snippets Menu:"; ObjectID = "fXr-ry-5ds"; */ -"fXr-ry-5ds.title" = "スニペットメニュー:"; +/* Class = "NSTextFieldCell"; title = "Snippets:"; ObjectID = "fXr-ry-5ds"; */ +"fXr-ry-5ds.title" = "スニペット:"; + +/* Class = "NSTextFieldCell"; title = "Menu"; ObjectID = "X5L-5V-1eH"; */ +"X5L-5V-1eH.title" = "メニュー"; + +/* Class = "NSTextFieldCell"; title = "History"; ObjectID = "Txl-Qh-RzP"; */ +"Txl-Qh-RzP.title" = "履歴"; + +/* Class = "NSTextFieldCell"; title = "Clear History:"; ObjectID = "f3b-Ht-X5w"; */ +"f3b-Ht-X5w.title" = "履歴をクリア:"; diff --git a/Clipy/Sources/Services/HotKeyService.swift b/Clipy/Sources/Services/HotKeyService.swift index a21ec141..22bdd385 100644 --- a/Clipy/Sources/Services/HotKeyService.swift +++ b/Clipy/Sources/Services/HotKeyService.swift @@ -26,6 +26,7 @@ final class HotKeyService: NSObject { fileprivate(set) var mainKeyCombo: KeyCombo? fileprivate(set) var historyKeyCombo: KeyCombo? fileprivate(set) var snippetKeyCombo: KeyCombo? + fileprivate(set) var clearHistoryKeyCombo: KeyCombo? } @@ -42,6 +43,11 @@ extension HotKeyService { func popUpSnippetMenu() { AppEnvironment.current.menuManager.popUpMenu(.snippet) } + + func popUpClearHisotryAlert() { + guard let appDelegate = NSApp.delegate as? AppDelegate else { return } + appDelegate.clearAllHistory() + } } // MARK: - Setup @@ -62,6 +68,8 @@ extension HotKeyService { change(with: .history, keyCombo: savedKeyCombo(forKey: Constants.HotKey.historyKeyCombo)) // Snippet menu change(with: .snippet, keyCombo: savedKeyCombo(forKey: Constants.HotKey.snippetKeyCombo)) + // Clear History + changeClearHistoryKeyCombo(savedKeyCombo(forKey: Constants.HotKey.clearHistoryKeyCombo)) } func change(with type: MenuType, keyCombo: KeyCombo?) { @@ -76,6 +84,18 @@ extension HotKeyService { register(with: type, keyCombo: keyCombo) } + func changeClearHistoryKeyCombo(_ keyCombo: KeyCombo?) { + clearHistoryKeyCombo = keyCombo + AppEnvironment.current.defaults.set(keyCombo?.archive(), forKey: Constants.HotKey.clearHistoryKeyCombo) + AppEnvironment.current.defaults.synchronize() + // Reset hotkey + HotKeyCenter.shared.unregisterHotKey(with: "ClearHisotry") + // Register new hotkey + guard let keyCombo = keyCombo else { return } + let hotkey = HotKey(identifier: "ClearHisotry", keyCombo: keyCombo, target: self, action: #selector(HotKeyService.popUpClearHisotryAlert)) + hotkey.register() + } + private func savedKeyCombo(forKey key: String) -> KeyCombo? { guard let data = AppEnvironment.current.defaults.object(forKey: key) as? Data else { return nil } guard let keyCombo = NSKeyedUnarchiver.unarchiveObject(with: data) as? KeyCombo else { return nil } diff --git a/ClipyTests/HotKeyServiceSpec.swift b/ClipyTests/HotKeyServiceSpec.swift index 3d28ece8..5b345a01 100644 --- a/ClipyTests/HotKeyServiceSpec.swift +++ b/ClipyTests/HotKeyServiceSpec.swift @@ -224,6 +224,39 @@ class HotKeyServiceSpec: QuickSpec { } } + describe("Clear Hisotry HotKey") { + beforeEach { + let defaults = UserDefaults.standard + defaults.removeObject(forKey: Constants.HotKey.clearHistoryKeyCombo) + defaults.synchronize() + } + + it("Add and remove clear history hokey") { + let service = HotKeyService() + + expect(service.clearHistoryKeyCombo).to(beNil()) + + let keyCombo = KeyCombo(keyCode: 10, carbonModifiers: cmdKey) + service.changeClearHistoryKeyCombo(keyCombo) + + expect(service.clearHistoryKeyCombo).toNot(beNil()) + expect(service.clearHistoryKeyCombo).to(equal(keyCombo)) + + let savedData = UserDefaults.standard.object(forKey: Constants.HotKey.clearHistoryKeyCombo) as? Data + let savedKeyCombo = NSKeyedUnarchiver.unarchiveObject(with: savedData!) as? KeyCombo + expect(savedKeyCombo).to(equal(keyCombo)) + + service.changeClearHistoryKeyCombo(nil) + expect(service.clearHistoryKeyCombo).to(beNil()) + } + + afterEach { + let defaults = UserDefaults.standard + defaults.removeObject(forKey: Constants.HotKey.clearHistoryKeyCombo) + defaults.synchronize() + } + } + describe("Folder HotKey") { beforeEach { let defaults = UserDefaults.standard