Skip to content

Commit

Permalink
add ExclusionApps setting
Browse files Browse the repository at this point in the history
  • Loading branch information
iMasanari committed Oct 31, 2016
1 parent f122ad2 commit 797f24d
Show file tree
Hide file tree
Showing 8 changed files with 404 additions and 41 deletions.
49 changes: 49 additions & 0 deletions cmd-eikana/AppData.swift
@@ -0,0 +1,49 @@
//
// AppData.swift
// ⌘英かな
//
// MIT License
// Copyright (c) 2016 iMasanari
//

import Cocoa

class AppData : NSObject {
var name: String
var id: String

init(name: String, id: String) {
self.name = name
self.id = id

super.init()
}

override init() {
self.name = ""
self.id = ""

super.init()
}

init?(dictionary : [AnyHashable: Any]) {
if let name = dictionary["name"] as? String, let id = dictionary["id"] as? String {
self.name = name
self.id = id

super.init()
}
else {
return nil
}
}

func toDictionary() -> [AnyHashable: Any] {
return [
"name": self.name,
"id": self.id
]
}
}


27 changes: 26 additions & 1 deletion cmd-eikana/AppDelegate.swift
Expand Up @@ -22,13 +22,19 @@ class AppDelegate: NSObject, NSApplicationDelegate {

// resetUserDefault() // デバッグ用

////////////////////////////
// 保存データの読み込み
////////////////////////////

let userDefaults = UserDefaults.standard

// 「ログイン後にこのアプリを起動」
if userDefaults.object(forKey: "lunchAtStartup") == nil {
setLaunchAtStartup(true)
userDefaults.set(1, forKey: "lunchAtStartup")
}

// 「起動時にアップデートを確認」
let checkUpdateState = userDefaults.object(forKey: "checkUpdateAtlaunch")

if checkUpdateState == nil {
Expand All @@ -39,6 +45,20 @@ class AppDelegate: NSObject, NSApplicationDelegate {
checkUpdate()
}

// 除外アプリ設定
if let exclusionAppsListData = userDefaults.object(forKey: "exclusionApps") as? [[AnyHashable: Any]] {
for val in exclusionAppsListData {
if let exclusionApps = AppData(dictionary: val) {
exclusionAppsList.append(exclusionApps)
}
}

for val in exclusionAppsList {
exclusionAppsDict[val.id] = val.name
}
}

// ショートカット設定
if let keyMappingListData = userDefaults.object(forKey: "mappings") as? [[AnyHashable: Any]] {
for val in keyMappingListData {
if let mapping = KeyMapping(dictionary: val) {
Expand All @@ -64,6 +84,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
userDefaults.removeObject(forKey: "oneShotModifiers")
}
else {
// 初期設定(左右のコマンドキー単体で英数/かな)
keyMappingList = [
KeyMapping(input: KeyboardShortcut(keyCode: 55), output: KeyboardShortcut(keyCode: 102)),
KeyMapping(input: KeyboardShortcut(keyCode: 54), output: KeyboardShortcut(keyCode: 104))
Expand All @@ -74,6 +95,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {
keyMappingListToShortcutList()
}

////////////////////////////
// UIの初期化
////////////////////////////

preferenceWindowController = PreferenceWindowController.getInstance()
// preferenceWindowController.showAndActivate(self)

Expand All @@ -89,7 +114,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {

let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as! String

menu.addItem(withTitle: "About ⌘英かな \(version) (webページ)", action: #selector(AppDelegate.open(_:)), keyEquivalent: "")
menu.addItem(withTitle: "About ⌘英かな \(version)", action: #selector(AppDelegate.open(_:)), keyEquivalent: "")
menu.addItem(withTitle: "Preferences...", action: #selector(AppDelegate.openPreferencesSerector(_:)), keyEquivalent: "")
menu.addItem(withTitle: "Quit", action: #selector(AppDelegate.quit(_:)), keyEquivalent: "")

Expand Down
194 changes: 175 additions & 19 deletions cmd-eikana/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

83 changes: 83 additions & 0 deletions cmd-eikana/ExclusionAppsController.swift
@@ -0,0 +1,83 @@
//
// ExclusionAppsController.swift
// ⌘英かな
//
// MIT License
// Copyright (c) 2016 iMasanari
//

import Cocoa


class ExclusionAppsController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
@IBOutlet weak var tableView: NSTableView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

NotificationCenter.default.addObserver(self,
selector: #selector(ExclusionAppsController.tableReload),
name: NSNotification.Name.NSApplicationDidBecomeActive,
object: nil)
}

func numberOfRows(in tableView: NSTableView) -> Int {
return exclusionAppsList.count + activeAppsList.count
}

func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? {
let id = tableColumn!.identifier

let isExclusion = row < exclusionAppsList.count

if id == "checkbox" {
return isExclusion
}

let value = isExclusion ? exclusionAppsList[row] : activeAppsList[row - exclusionAppsList.count]

if id == "appName" {
return value.name
}
if id == "appId" {
return value.id
}

return nil
}
func tableView(_ tableView: NSTableView, setObjectValue object: Any?, for tableColumn: NSTableColumn?, row: Int) {
let id = tableColumn!.identifier
let isExclusion = row < exclusionAppsList.count

if id != "checkbox" {
return
}

if isExclusion {
let item = exclusionAppsList.remove(at: row)
activeAppsList.insert(item, at: 0)
}
else {
let item = activeAppsList.remove(at: row - exclusionAppsList.count)
exclusionAppsList.append(item)
}

exclusionAppsDict = [:]

for val in exclusionAppsList {
exclusionAppsDict[val.id] = val.name
}

tableReload()
saveExclusionApps()
}

func tableReload() {
tableView.reloadData()
}

func saveExclusionApps() {
UserDefaults.standard.set(exclusionAppsList.map {$0.toDictionary()} , forKey: "exclusionApps")
}
}
42 changes: 40 additions & 2 deletions cmd-eikana/KeyEvent.swift
Expand Up @@ -8,12 +8,24 @@

import Cocoa

var activeAppsList: [AppData] = []
var exclusionAppsList: [AppData] = []

var exclusionAppsDict: [String: String] = [:]

class KeyEvent: NSObject {
var keyCode: CGKeyCode? = nil
var isExclusionApp = false
let bundleId = Bundle.main.infoDictionary?["CFBundleIdentifier"] as! String

override init() {
super.init()

NSWorkspace.shared().notificationCenter.addObserver(self,
selector: #selector(KeyEvent.setActiveApp(_:)),
name: NSNotification.Name.NSWorkspaceDidActivateApplication,
object:nil)

let checkOptionPrompt = kAXTrustedCheckOptionPrompt.takeRetainedValue() as NSString
let options: CFDictionary = [checkOptionPrompt: true] as NSDictionary

Expand All @@ -24,7 +36,8 @@ class KeyEvent: NSObject {
selector: #selector(KeyEvent.watchAXIsProcess(_:)),
userInfo: nil,
repeats: true)
} else {
}
else {
self.watch()
}
}
Expand All @@ -37,6 +50,23 @@ class KeyEvent: NSObject {
}
}

func setActiveApp(_ notification: NSNotification) {
let app = notification.userInfo!["NSWorkspaceApplicationKey"] as! NSRunningApplication

if let name = app.localizedName, let id = app.bundleIdentifier, id != bundleId {
isExclusionApp = exclusionAppsDict[id] != nil

if (!isExclusionApp) {
activeAppsList = activeAppsList.filter {$0.id != id}
activeAppsList.insert(AppData(name: name, id: id), at: 0)

if activeAppsList.count > 10 {
activeAppsList.removeLast()
}
}
}
}

func watch() {
let eventMaskList = [
CGEventType.keyDown,
Expand Down Expand Up @@ -85,6 +115,10 @@ class KeyEvent: NSObject {
}

func eventCallback(proxy: CGEventTapProxy, type: CGEventType, event: CGEvent) -> Unmanaged<CGEvent>? {
if isExclusionApp {
return Unmanaged.passRetained(event)
}

switch type {
case CGEventType.flagsChanged:
let keyCode = CGKeyCode(event.getIntegerValueField(.keyboardEventKeycode))
Expand All @@ -111,7 +145,7 @@ class KeyEvent: NSObject {
func keyDown(_ event: CGEvent) -> Unmanaged<CGEvent>? {
#if DEBUG
// print("keyCode: \(KeyboardShortcut(event).keyCode)")
// print(KeyboardShortcut(event).toString())
print(KeyboardShortcut(event).toString())
#endif

self.keyCode = nil
Expand Down Expand Up @@ -141,6 +175,10 @@ class KeyEvent: NSObject {
}

func modifierKeyDown(_ event: CGEvent) -> Unmanaged<CGEvent>? {
#if DEBUG
print(KeyboardShortcut(event).toString())
#endif

self.keyCode = CGKeyCode(event.getIntegerValueField(.keyboardEventKeycode))

if let keyTextField = activeKeyTextField, keyTextField.isAllowModifierOnly {
Expand Down
32 changes: 20 additions & 12 deletions cmd-eikana/ShortcutsController.swift
Expand Up @@ -34,24 +34,22 @@ func keyMappingListToShortcutList() {

class ShortcutsController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
@IBOutlet weak var tableView: NSTableView!
@IBAction func addRow(_ sender: AnyObject) {
keyMappingList.append(KeyMapping())
tableView.reloadData()
saveKeyMappings()
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

// tableView.delegate = self
// tableView.dataSource = self
NotificationCenter.default.addObserver(self,
selector: #selector(ShortcutsController.tableRreload),
name: NSNotification.Name.NSApplicationDidBecomeActive,
object: nil)
}
func applicationDidResignActive(_ notification: Notification) {
tableView.reloadData()

override func mouseDown(with event: NSEvent) {
activeKeyTextField?.blur()
}

func numberOfRows(in aTableView: NSTableView) -> Int {
func numberOfRows(in tableView: NSTableView) -> Int {
return keyMappingList.count
}

Expand Down Expand Up @@ -106,10 +104,20 @@ class ShortcutsController: NSViewController, NSTableViewDataSource, NSTableViewD
break
}

tableRreload()
}

func tableRreload() {
tableView.reloadData()
saveKeyMappings()
}
override func mouseDown(with event: NSEvent) {
activeKeyTextField?.blur()

@IBAction func quit(_ sender: AnyObject) {
NSApplication.shared().terminate(self)
}

@IBAction func addRow(_ sender: AnyObject) {
keyMappingList.append(KeyMapping())
tableRreload()
}
}
6 changes: 1 addition & 5 deletions cmd-eikana/ViewController.swift
Expand Up @@ -8,7 +8,7 @@

import Cocoa

class ViewController: NSViewController {
class ViewController: NSViewController, NSTableViewDataSource, NSTableViewDelegate {
let userDefaults = UserDefaults.standard

@IBOutlet weak var showIcon: NSButton!
Expand Down Expand Up @@ -52,10 +52,6 @@ class ViewController: NSViewController {
userDefaults.set(checkUpdateAtlaunch.state, forKey: "checkUpdateAtlaunch")
}

@IBAction func quit(_ sender: AnyObject) {
NSApplication.shared().terminate(self)
}

@IBAction func checkUpdateButton(_ sender: AnyObject) {
updateButton.isEnabled = false
checkUpdate({ (isNewVer: Bool?) -> Void in
Expand Down

0 comments on commit 797f24d

Please sign in to comment.