Skip to content

Commit

Permalink
Implement clear history shortcut setting (Clipy#207)
Browse files Browse the repository at this point in the history
* Implement clear history shortcut setting

* Add tests
  • Loading branch information
Econa77 committed Sep 8, 2017
1 parent 4062ba4 commit ef9336e
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 25 deletions.
1 change: 1 addition & 0 deletions Clipy/Sources/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ struct Constants {
static let snippetKeyCombo = "kCPYHotKeySnippetKeyCombo"
static let migrateNewKeyCombo = "kCPYMigrateNewKeyCombo"
static let folderKeyCombos = "kCPYFolderKeyCombos"
static let clearHistoryKeyCombo = "kCPYClearHistoryKeyCombo"
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="12118" systemVersion="16D32" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="12121" systemVersion="16G29" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="12118"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="12121"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11762" systemVersion="16C67" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="12121" systemVersion="16G29" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11762"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="12121"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="CPYShortcutsPreferenceViewController" customModule="Clipy" customModuleProvider="target">
<connections>
<outlet property="clearHistoryShortcutRecordView" destination="pCf-EG-Cp9" id="K22-8R-Gk9"/>
<outlet property="historyShortcutRecordView" destination="hDA-gc-n3S" id="fzg-O8-Fze"/>
<outlet property="mainShortcutRecordView" destination="ABK-GW-Xwm" id="Jr7-Dz-hqZ"/>
<outlet property="snippetShortcutRecordView" destination="q8v-jS-CAr" id="Ay0-Zr-azr"/>
Expand All @@ -16,35 +17,44 @@
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<customView id="Hz6-mo-xeY">
<rect key="frame" x="0.0" y="0.0" width="480" height="170"/>
<rect key="frame" x="0.0" y="0.0" width="480" height="275"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField verticalHuggingPriority="750" fixedFrame="YES" allowsCharacterPickerTouchBarItem="NO" preferredMaxLayoutWidth="206" translatesAutoresizingMaskIntoConstraints="NO" id="5oP-dg-DwL">
<rect key="frame" x="59" y="244" width="210" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" allowsUndo="NO" sendsActionOnEndEditing="YES" title="Menu" id="X5L-5V-1eH">
<font key="font" metaFont="systemBold"/>
<color key="textColor" red="0.2666666667" green="0.2666666667" blue="0.2666666667" alpha="1" colorSpace="calibratedRGB"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<view fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="uTq-IP-dYk">
<rect key="frame" x="44" y="15" width="392" height="140"/>
<rect key="frame" x="44" y="96" width="392" height="140"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Vzz-Iq-77h">
<textField verticalHuggingPriority="750" fixedFrame="YES" allowsCharacterPickerTouchBarItem="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Vzz-Iq-77h">
<rect key="frame" x="15" y="62" width="121" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="History Menu:" id="Abl-cO-JwU">
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="History:" id="Abl-cO-JwU">
<font key="font" metaFont="system"/>
<color key="textColor" red="0.26666666666666666" green="0.26666666666666666" blue="0.26666666666666666" alpha="1" colorSpace="calibratedRGB"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="oYq-Bg-KGQ">
<textField verticalHuggingPriority="750" fixedFrame="YES" allowsCharacterPickerTouchBarItem="YES" translatesAutoresizingMaskIntoConstraints="NO" id="oYq-Bg-KGQ">
<rect key="frame" x="15" y="109" width="121" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Main Menu:" id="5sL-G8-1io">
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Main:" id="5sL-G8-1io">
<font key="font" metaFont="system"/>
<color key="textColor" red="0.26666666666666666" green="0.26666666666666666" blue="0.26666666666666666" alpha="1" colorSpace="calibratedRGB"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="ixj-RO-bKW">
<textField verticalHuggingPriority="750" fixedFrame="YES" allowsCharacterPickerTouchBarItem="YES" translatesAutoresizingMaskIntoConstraints="NO" id="ixj-RO-bKW">
<rect key="frame" x="15" y="13" width="121" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Snippets Menu:" id="fXr-ry-5ds">
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Snippets:" id="fXr-ry-5ds">
<font key="font" metaFont="system"/>
<color key="textColor" red="0.26666666666666666" green="0.26666666666666666" blue="0.26666666666666666" alpha="1" colorSpace="calibratedRGB"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
Expand Down Expand Up @@ -109,8 +119,53 @@
</customView>
</subviews>
</view>
<textField verticalHuggingPriority="750" fixedFrame="YES" allowsCharacterPickerTouchBarItem="NO" preferredMaxLayoutWidth="206" translatesAutoresizingMaskIntoConstraints="NO" id="fZN-Ox-42G">
<rect key="frame" x="59" y="66" width="210" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" allowsUndo="NO" sendsActionOnEndEditing="YES" title="History" id="Txl-Qh-RzP">
<font key="font" metaFont="systemBold"/>
<color key="textColor" red="0.2666666667" green="0.2666666667" blue="0.2666666667" alpha="1" colorSpace="calibratedRGB"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<view fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="zgf-1z-HW3">
<rect key="frame" x="44" y="14" width="392" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<subviews>
<textField verticalHuggingPriority="750" fixedFrame="YES" allowsCharacterPickerTouchBarItem="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Qah-qM-yU1">
<rect key="frame" x="15" y="14" width="121" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Clear History:" id="f3b-Ht-X5w">
<font key="font" metaFont="system"/>
<color key="textColor" red="0.2666666667" green="0.2666666667" blue="0.2666666667" alpha="1" colorSpace="calibratedRGB"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<customView fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="pCf-EG-Cp9" customClass="RecordView" customModule="KeyHolder">
<rect key="frame" x="142" y="5" width="250" height="34"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="color" keyPath="tintColor">
<color key="value" red="0.1647058824" green="0.51764705879999995" blue="0.82352941180000006" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="backgroundColor">
<color key="value" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="cornerRadius">
<real key="value" value="17"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="borderColor">
<color key="value" red="0.66666666666666663" green="0.66666666666666663" blue="0.66666666666666663" alpha="1" colorSpace="calibratedRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="borderWidth">
<real key="value" value="1"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</customView>
</subviews>
</view>
</subviews>
<point key="canvasLocation" x="702" y="568"/>
<point key="canvasLocation" x="520" y="260.5"/>
</customView>
</objects>
</document>
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ 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() {
super.loadView()
mainShortcutRecordView.delegate = self
historyShortcutRecordView.delegate = self
snippetShortcutRecordView.delegate = self
clearHistoryShortcutRecordView.delegate = self
prepareHotKeys()
}

Expand All @@ -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
}
}

Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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:";
Original file line number Diff line number Diff line change
@@ -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" = "履歴をクリア:";
20 changes: 20 additions & 0 deletions Clipy/Sources/Services/HotKeyService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?

}

Expand All @@ -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
Expand All @@ -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?) {
Expand All @@ -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 }
Expand Down
Loading

0 comments on commit ef9336e

Please sign in to comment.