Skip to content

Commit

Permalink
Merge pull request #24 from HsiangHo/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
HsiangHo authored Jan 14, 2019
2 parents d88d7f9 + 6961e23 commit dbf0645
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 245 deletions.
67 changes: 25 additions & 42 deletions Kaka/Kaka/HAFConfigureManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ class HAFConfigureManager: NSObject {
let kAutoToggleDarkModeBaseOnDisplayBrightnessValue = "AutoToggleDarkModeBaseOnDisplayBrightnessValueKey"
let kEnableFinderExtension = "EnableFinderExtensionKey"
let kEnableKaka = "EnableKakaKey"
let kDisplayDesktopKeyCombo = "DisplayDesktopKeyCombo"
let kHideDesktopIconsKeyCombo = "HideDesktopIconsKeyCombo"
let kTurnOffDisplayKeyCombo = "TurnOffDisplayKeyCombo"
let kTurnOnDarkModeKeyCombo = "TurnOnDarkModeKeyCombo"
let kRequestRating = "RequestRating"
let kAutoHideDesktopIconTimeOut = "AutoHideDesktopIconTimeOut"
let kAutoHideCursorTimeOut = "AutoHideCursorTimeOut"
let kDeactivateCriticalBatteryCharge = "DeactivateCriticalBatteryCharge"
let kDeactivateCriticalBatteryChargeThreshold = "DeactivateCriticalBatteryChargeThreshold"
let kInifiteLoopStatus = "InifiteLoopStatus"
let kUserdDefinedHotkeyPaths = "UserdDefinedHotkeyPaths"

func setLaunchAtLogin(bFlag: Bool) -> Void {
SMLoginItemSetEnabled(helperBundleIdentifier as CFString, bFlag)
Expand Down Expand Up @@ -129,45 +127,14 @@ class HAFConfigureManager: NSObject {
return !UserDefaults.standard.bool(forKey: kEnableKaka)
}

func setDisplayDesktopKeyCombo(keyCombo: SCKeyCombo?) -> Void{
UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: keyCombo as Any), forKey: kDisplayDesktopKeyCombo)
func setKeyComboWithIdentifier(keyCombo: SCKeyCombo?, identifier: String) -> Void{
let id = identifier + "KeyCombo"
UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: keyCombo as Any), forKey: id)
}

func displayDesktopKeyCombo() -> SCKeyCombo?{
guard let data = UserDefaults.standard.object(forKey: kDisplayDesktopKeyCombo) else {
return nil;
}
return NSKeyedUnarchiver.unarchiveObject(with: data as! Data) as? SCKeyCombo
}

func setHideDesktopIconsKeyCombo(keyCombo: SCKeyCombo?) -> Void{
UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: keyCombo as Any), forKey: kHideDesktopIconsKeyCombo)
}

func hideDesktopIconsKeyCombo() -> SCKeyCombo?{
guard let data = UserDefaults.standard.object(forKey: kHideDesktopIconsKeyCombo) else {
return nil;
}
return NSKeyedUnarchiver.unarchiveObject(with: data as! Data) as? SCKeyCombo
}

func setTurnOffDisplayKeyCombo(keyCombo: SCKeyCombo?) -> Void{
UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: keyCombo as Any), forKey: kTurnOffDisplayKeyCombo)
}

func turnOffDisplayKeyCombo() -> SCKeyCombo?{
guard let data = UserDefaults.standard.object(forKey: kTurnOffDisplayKeyCombo) else {
return nil;
}
return NSKeyedUnarchiver.unarchiveObject(with: data as! Data) as? SCKeyCombo
}

func setTurnOnDarkModeKeyCombo(keyCombo: SCKeyCombo?) -> Void{
UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: keyCombo as Any), forKey: kTurnOnDarkModeKeyCombo)
}

func turnOnDarkModeKeyCombo() -> SCKeyCombo?{
guard let data = UserDefaults.standard.object(forKey: kTurnOnDarkModeKeyCombo) else {

func keyComboWithIdentifier(identifier: String) -> SCKeyCombo?{
let id = identifier + "KeyCombo"
guard let data = UserDefaults.standard.object(forKey: id) else {
return nil;
}
return NSKeyedUnarchiver.unarchiveObject(with: data as! Data) as? SCKeyCombo
Expand Down Expand Up @@ -227,4 +194,20 @@ class HAFConfigureManager: NSObject {
return value as! Float
}
}

func setInifiteLoopStatus(bFlag: Bool) -> Void {
UserDefaults.standard.set(bFlag, forKey: kInifiteLoopStatus)
}

func inifiteLoopStatus() -> Bool {
return UserDefaults.standard.bool(forKey: kInifiteLoopStatus)
}

func userDefinedHotkeyPaths() -> Array<String>? {
return UserDefaults.standard.stringArray(forKey: kUserdDefinedHotkeyPaths)
}

func setUserDefinedHotkeyPaths(array: Array<String>) -> Void {
UserDefaults.standard.set(array, forKey: kUserdDefinedHotkeyPaths)
}
}
188 changes: 125 additions & 63 deletions Kaka/Kaka/HAFHotkeyManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,94 +10,156 @@ import Cocoa
import ShortcutsKit
import ShadowstarKit

struct HotkeyIdentifiers {
static let displayDesktop = "displayDesktop"
static let hideDesktopIcons = "hideDesktopIcons"
static let turnOffDisplay = "turnOffDisplay"
static let turnOnDarkMode = "turnOnDarkMode"
static let sleepMac = "sleepMac"
static let enterScreensaver = "enterScreensaver"
static let preventSleep = "enterScreensaver"
static let disableMouseCursor = "disableMouseCursor"
}

class HAFHotkeyManager: NSObject {
static let sharedManager = HAFHotkeyManager()

var preDefinedHotkeys: Array<(String, SCHotkey)> = Array.init()
var userDefinedHotkeys: Array<(String, SCHotkey)> = Array.init()
private var displayDesktopHotkey: SCHotkey?
private var hideDesktopIconsHotkey: SCHotkey?
private var turnOffDisplayHotkey: SCHotkey?
private var turnOnDarkModeHotkey: SCHotkey?
private var sleepMacHotkey: SCHotkey?
private var enterScreensaverHotkey: SCHotkey?
private var preventSleepHotkey: SCHotkey?
private var disableMouseCursorHotkey: SCHotkey?

func registerAll() -> Void {
setDisplayDesktopHotkey(keyCombo: HAFConfigureManager.sharedManager.displayDesktopKeyCombo())
setHideDesktopIconsHotkey(keyCombo: HAFConfigureManager.sharedManager.hideDesktopIconsKeyCombo())
setTurnOffDisplayHotkey(keyCombo: HAFConfigureManager.sharedManager.turnOffDisplayKeyCombo())
setTurnOnDarkModeHotkey(keyCombo: HAFConfigureManager.sharedManager.turnOnDarkModeKeyCombo())
override init() {
super.init()
__initializePredefinedHotkeys()
__initializeUserDefinedHotkeys()
}

func unregisterAll() -> Void {
if nil != displayDesktopHotkey{
displayDesktopHotkey!.unregister()
}
if nil != hideDesktopIconsHotkey{
hideDesktopIconsHotkey!.unregister()
}
if nil != turnOffDisplayHotkey{
turnOffDisplayHotkey!.unregister()
}
if nil != turnOnDarkModeHotkey{
turnOnDarkModeHotkey!.unregister()
func hotkeyForIdentifier(identifier: String!) -> SCHotkey? {
var hotkey: SCHotkey? = nil
for k in preDefinedHotkeys {
if k.1.identifier == identifier{
hotkey = k.1
break
}
}
return hotkey
}

//Display desktop hotkey
func setDisplayDesktopHotkey(keyCombo: SCKeyCombo?) -> Void {
if nil != displayDesktopHotkey{
displayDesktopHotkey!.unregister()
}
if nil != keyCombo {
displayDesktopHotkey = SCHotkey.init(keyCombo: keyCombo!, identifier: "displayDesktop", handler: { (_) in
SSDesktopManager.shared().showDesktop(false)
});
displayDesktopHotkey!.register()
func addHotkeyForPath(path: String!) -> SCHotkey? {
var array = HAFConfigureManager.sharedManager.userDefinedHotkeyPaths()
if nil == array {
array = Array<String>.init()
}
HAFConfigureManager.sharedManager.setDisplayDesktopKeyCombo(keyCombo: keyCombo)
}

//Hide desktop icons hotkey
func setHideDesktopIconsHotkey(keyCombo: SCKeyCombo?) -> Void {
if nil != hideDesktopIconsHotkey{
hideDesktopIconsHotkey!.unregister()
if !array!.contains(path){
array!.append(path)
}
if nil != keyCombo {
hideDesktopIconsHotkey = SCHotkey.init(keyCombo: keyCombo!, identifier: "hideDesktopIcons", handler: { (_) in
if SSDesktopManager.shared().isAllDesktopCovered(){
SSDesktopManager.shared().uncoverAllDesktop()
HAFConfigureManager.sharedManager.setUserDefinedHotkeyPaths(array: array!)
var hotkey = userDefinedHotkeys.first { (str, k) -> Bool in
return str == path
}?.1
if nil == hotkey{
hotkey = SCHotkey.init(keyCombo: HAFConfigureManager.sharedManager.keyComboWithIdentifier(identifier: path), identifier: path, handler: { (k) in
let p: String! = k?.identifier
if p.lowercased().hasSuffix(".app"){
NSWorkspace.shared.launchApplication(p)
}else{
SSDesktopManager.shared().setupAllDesktopWithDesktopBackgroundImage()
SSDesktopManager.shared().coverAllDesktop()
NSWorkspace.shared.openFile(p)
}
});
hideDesktopIconsHotkey!.register()
})
userDefinedHotkeys.append((path, hotkey!))
}
HAFConfigureManager.sharedManager.setHideDesktopIconsKeyCombo(keyCombo: keyCombo)
return hotkey
}

//Turn off display hotkey
func setTurnOffDisplayHotkey(keyCombo: SCKeyCombo?) -> Void {
if nil != turnOffDisplayHotkey{
turnOffDisplayHotkey!.unregister()
func removeHotkeyForPath(path: String!) -> Void {
var array = HAFConfigureManager.sharedManager.userDefinedHotkeyPaths()
if nil == array {
return
}
if !array!.contains(path){
return
}
if nil != keyCombo {
turnOffDisplayHotkey = SCHotkey.init(keyCombo: keyCombo!, identifier: "turnOffDisplay", handler: { (_) in
SSEnergyManager.shared().displaySleep()
});
turnOffDisplayHotkey!.register()
if let index = array!.index(of: path) {
array!.remove(at: index)
}
HAFConfigureManager.sharedManager.setUserDefinedHotkeyPaths(array: array!)
userDefinedHotkeys.removeAll { (str, k) -> Bool in
return str == path
}
HAFConfigureManager.sharedManager.setTurnOffDisplayKeyCombo(keyCombo: keyCombo)
}

//Turn on dark mode hotkey
func setTurnOnDarkModeHotkey(keyCombo: SCKeyCombo?) -> Void {
if nil != turnOnDarkModeHotkey{
turnOnDarkModeHotkey!.unregister()
func __initializeUserDefinedHotkeys() -> Void {
guard let userDefinedPaths = HAFConfigureManager.sharedManager.userDefinedHotkeyPaths() else {
return
}
if nil != keyCombo {
turnOnDarkModeHotkey = SCHotkey.init(keyCombo: keyCombo!, identifier: "turnOnDarkMode", handler: { (_) in
SSAppearanceManager.shared().toggle()
});
turnOnDarkModeHotkey!.register()
userDefinedHotkeys = userDefinedPaths.map { path in
let hotkey = SCHotkey.init(keyCombo: HAFConfigureManager.sharedManager.keyComboWithIdentifier(identifier: path), identifier: path, handler: { (k) in
let p: String! = k?.identifier
if p.lowercased().hasSuffix(".app"){
NSWorkspace.shared.launchApplication(p)
}else{
NSWorkspace.shared.openFile(p)
}
})
return (path, hotkey!)
}
HAFConfigureManager.sharedManager.setTurnOnDarkModeKeyCombo(keyCombo: keyCombo)
}

func __initializePredefinedHotkeys() -> Void {
displayDesktopHotkey = SCHotkey.init(keyCombo: HAFConfigureManager.sharedManager.keyComboWithIdentifier(identifier: HotkeyIdentifiers.displayDesktop), identifier: HotkeyIdentifiers.displayDesktop, handler: { (_) in
SSDesktopManager.shared().showDesktop(false)
})
preDefinedHotkeys.append((NSLocalizedString("Display Desktop", comment: ""), displayDesktopHotkey!))

hideDesktopIconsHotkey = SCHotkey.init(keyCombo: HAFConfigureManager.sharedManager.keyComboWithIdentifier(identifier: HotkeyIdentifiers.hideDesktopIcons), identifier: HotkeyIdentifiers.hideDesktopIcons, handler: { (_) in
if SSDesktopManager.shared().isAllDesktopCovered(){
SSDesktopManager.shared().uncoverAllDesktop()
}else{
SSDesktopManager.shared().setupAllDesktopWithDesktopBackgroundImage()
SSDesktopManager.shared().coverAllDesktop()
}
})
preDefinedHotkeys.append((NSLocalizedString("Hide Desktop Icons", comment: ""), hideDesktopIconsHotkey!))

turnOffDisplayHotkey = SCHotkey.init(keyCombo: HAFConfigureManager.sharedManager.keyComboWithIdentifier(identifier: HotkeyIdentifiers.turnOffDisplay), identifier: HotkeyIdentifiers.turnOffDisplay, handler: { (_) in
SSEnergyManager.shared().displaySleep()
})
preDefinedHotkeys.append((NSLocalizedString("Turn Off The Display", comment: ""), turnOffDisplayHotkey!))

turnOnDarkModeHotkey = SCHotkey.init(keyCombo: HAFConfigureManager.sharedManager.keyComboWithIdentifier(identifier: HotkeyIdentifiers.turnOnDarkMode), identifier: HotkeyIdentifiers.turnOnDarkMode, handler: { (_) in
SSAppearanceManager.shared().toggle()
})
preDefinedHotkeys.append((NSLocalizedString("Turn On Dark Mode", comment: ""), turnOnDarkModeHotkey!))

sleepMacHotkey = SCHotkey.init(keyCombo: HAFConfigureManager.sharedManager.keyComboWithIdentifier(identifier: HotkeyIdentifiers.sleepMac), identifier: HotkeyIdentifiers.sleepMac, handler: { (_) in
SSEnergyManager.shared().sleep()
})
preDefinedHotkeys.append((NSLocalizedString("Sleep Mac", comment: ""), sleepMacHotkey!))

enterScreensaverHotkey = SCHotkey.init(keyCombo: HAFConfigureManager.sharedManager.keyComboWithIdentifier(identifier: HotkeyIdentifiers.enterScreensaver), identifier: HotkeyIdentifiers.enterScreensaver, handler: { (_) in
SSEnergyManager.shared().screenSaver()
})
preDefinedHotkeys.append((NSLocalizedString("Enter Screensaver", comment: ""), enterScreensaverHotkey!))

preventSleepHotkey = SCHotkey.init(keyCombo: HAFConfigureManager.sharedManager.keyComboWithIdentifier(identifier: HotkeyIdentifiers.preventSleep), identifier: HotkeyIdentifiers.preventSleep, handler: { (_) in
SSEnergyManager.shared().preventSleep(!SSEnergyManager.shared().isPreventSleepRunning())
})
preDefinedHotkeys.append((NSLocalizedString("Prevent Sleep", comment: ""), preventSleepHotkey!))

disableMouseCursorHotkey = SCHotkey.init(keyCombo: HAFConfigureManager.sharedManager.keyComboWithIdentifier(identifier: HotkeyIdentifiers.disableMouseCursor), identifier: HotkeyIdentifiers.disableMouseCursor, handler: { (_) in
if SSCursorManager.shared().isCursorEnable(){
SSCursorManager.shared().disableCursor()
}else{
SSCursorManager.shared().enableCursor()
}
})
preDefinedHotkeys.append((NSLocalizedString("Disable Mouse Cursor", comment: ""), disableMouseCursorHotkey!))
}
}
Loading

0 comments on commit dbf0645

Please sign in to comment.