Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow to customize popup hotkey
  • Loading branch information
p0deje committed May 15, 2018
1 parent 8fe97f6 commit b755c06
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Maccy.xcodeproj/project.pbxproj
Expand Up @@ -22,6 +22,7 @@
DA573EAD1EDD410F00561FB2 /* HistoryMenuItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA573EAC1EDD410F00561FB2 /* HistoryMenuItem.swift */; };
DA5F46512020E9FB00425C11 /* Carbon.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA5F464F2020E4DF00425C11 /* Carbon.framework */; };
DA6373981E4AB9BB00263391 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DA6373971E4AB9BB00263391 /* Assets.xcassets */; };
DA6DB59720AA7D55003C255E /* GlobalHotKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA6DB59620AA7D55003C255E /* GlobalHotKey.swift */; };
DA8953B91E446A4E00884EAB /* Maccy.swift in Sources */ = {isa = PBXBuildFile; fileRef = DA8953B81E446A4E00884EAB /* Maccy.swift */; };
DAEE38471E3DBEB100DD2966 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAEE38461E3DBEB100DD2966 /* AppDelegate.swift */; };
DAEE38541E3DD6F100DD2966 /* History.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAEE38531E3DD6F100DD2966 /* History.swift */; };
Expand Down Expand Up @@ -91,6 +92,7 @@
DA573EAC1EDD410F00561FB2 /* HistoryMenuItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HistoryMenuItem.swift; sourceTree = "<group>"; };
DA5F464F2020E4DF00425C11 /* Carbon.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Carbon.framework; path = System/Library/Frameworks/Carbon.framework; sourceTree = SDKROOT; };
DA6373971E4AB9BB00263391 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
DA6DB59620AA7D55003C255E /* GlobalHotKey.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GlobalHotKey.swift; sourceTree = "<group>"; };
DA8953B81E446A4E00884EAB /* Maccy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Maccy.swift; sourceTree = "<group>"; };
DAEE38431E3DBEB100DD2966 /* Maccy.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Maccy.app; sourceTree = BUILT_PRODUCTS_DIR; };
DAEE38461E3DBEB100DD2966 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = AppDelegate.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
Expand Down Expand Up @@ -199,6 +201,7 @@
DAEE38461E3DBEB100DD2966 /* AppDelegate.swift */,
DA0EF1871E444B2A00E58577 /* Clipboard.swift */,
DA2C75292029FD6B0090965D /* FilterMenuItemView.swift */,
DA6DB59620AA7D55003C255E /* GlobalHotKey.swift */,
DAEE38531E3DD6F100DD2966 /* History.swift */,
DA573EAC1EDD410F00561FB2 /* HistoryMenuItem.swift */,
DAF176C5202AA56B00C5FD0D /* Keys.swift */,
Expand Down Expand Up @@ -476,6 +479,7 @@
buildActionMask = 2147483647;
files = (
DA2C752A2029FD6B0090965D /* FilterMenuItemView.swift in Sources */,
DA6DB59720AA7D55003C255E /* GlobalHotKey.swift in Sources */,
DAF176C6202AA56B00C5FD0D /* Keys.swift in Sources */,
DAEE38541E3DD6F100DD2966 /* History.swift in Sources */,
DA0EF1881E444B2A00E58577 /* Clipboard.swift in Sources */,
Expand Down
7 changes: 3 additions & 4 deletions Maccy/AppDelegate.swift
@@ -1,18 +1,17 @@
import Cocoa
import HotKey

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let clipboard = Clipboard()
let history = History()
let hotKey = HotKey(key: .c, modifiers: [.command, .shift])

let hotKey = GlobalHotKey()
var maccy: Maccy {
return Maccy(history: history, clipboard: clipboard)
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
maccy.start()
hotKey.keyDownHandler = { self.maccy.popUp() }
hotKey.handler = { self.maccy.popUp() }
}
}
48 changes: 48 additions & 0 deletions Maccy/GlobalHotKey.swift
@@ -0,0 +1,48 @@
import HotKey

class GlobalHotKey {
var handler: HotKey.Handler? {
get { return hotKey?.keyDownHandler }
set(newHandler) { hotKey?.keyDownHandler = newHandler }
}

private let hotKeyStore = "hotKey"
private let defaultKeyBinding = "command+shift+c"
private var hotKey: HotKey? = nil

init() {
UserDefaults.standard.register(defaults: [hotKeyStore: defaultKeyBinding])
registerHotKey()
}

private func registerHotKey() {
guard let keybindingString = UserDefaults.standard.string(forKey: hotKeyStore) else {
return
}
var keysList = keybindingString.split(separator: "+")

guard let keyString = keysList.popLast() else {
return
}
guard let key = Key(string: String(keyString)) else {
return
}

var modifiers: NSEvent.ModifierFlags = []
for keyString in keysList {
switch (keyString) {
case "command":
modifiers.insert(.command)
case "control":
modifiers.insert(.control)
case "option":
modifiers.insert(.option)
case "shift":
modifiers.insert(.shift)
default: ()
}
}

hotKey = HotKey(key: key, modifiers: modifiers)
}
}
4 changes: 2 additions & 2 deletions Maccy/History.swift
@@ -1,8 +1,8 @@
import AppKit

class History {
private var storageKey = "history"
private var sizeKey = "historySize"
private let storageKey = "history"
private let sizeKey = "historySize"

init() {
UserDefaults.standard.register(defaults: [sizeKey: 999])
Expand Down
16 changes: 10 additions & 6 deletions README.md
Expand Up @@ -25,17 +25,21 @@ Also, I wanted to learn Swift and get acquainted with macOS application developm

## Customization

### Popup Hotkey

```bash
$ defaults write org.p0deje.Maccy hotKey control+option+m # default is command+shift+c
```

### History Size

```bash
$ defaults write org.p0deje.Maccy historySize 100 # default is 999
```

## To Do
### Automatically Start at Login

- [ ] allow to customize keyboard shortcut
- [x] ~~add preferences window~~ use `defaults`
- [x] ~~automatically start at login~~ just add Maccy to your "Login Items"
- [x] add more unit tests
- [x] add UI tests
Just add Maccy to your "Login items".

## License

Expand Down

0 comments on commit b755c06

Please sign in to comment.