diff --git a/SwitchKey/AppDelegate.swift b/SwitchKey/AppDelegate.swift index 19628d8..7b61d37 100644 --- a/SwitchKey/AppDelegate.swift +++ b/SwitchKey/AppDelegate.swift @@ -46,9 +46,10 @@ private func askForAccessibilityPermission() { NSApplication.shared.terminate(nil) } } - +//MARK:- AppDelegate @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTableViewDelegate { + //MARK:- View @IBOutlet weak var statusBarMenu: NSMenu! @IBOutlet weak var conditionTableView: TableView! { didSet { @@ -57,7 +58,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab conditionTableView.register(NSNib(nibNamed: "SwitchKey", bundle: nil), forIdentifier: editCellIdentifier) } } - + //MARK:- Property private var applicationObservers:[pid_t:AXObserver] = [:] private var currentPid:pid_t = getpid() @@ -65,7 +66,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab private var statusBarItem: NSStatusItem! private var launchAtStartupItem: NSMenuItem! - + //MARK:- Life Cycle func applicationDidFinishLaunching(_ aNotification: Notification) { if !hasAccessibilityPermission() { askForAccessibilityPermission() @@ -112,29 +113,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab CFRunLoopRemoveSource(RunLoop.current.getCFRunLoop(), AXObserverGetRunLoopSource(observer), .defaultMode) } } - - fileprivate func applicationSwitched() { - DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { - if let application = NSWorkspace.shared.frontmostApplication { - let switchedPid:pid_t = application.processIdentifier - if (switchedPid != self.currentPid && switchedPid != getpid()) { - for condition in self.conditionItems { - if !condition.enabled { - continue - } - if condition.applicationIdentifier == application.bundleIdentifier { - if let inputSource = InputSource.with(condition.inputSourceID) { - inputSource.activate() - } - break - } - } - self.currentPid = switchedPid - } - } - } - } - + + //MARK:- Target Action @objc private func menuDidEndTracking(_ notification: Notification) { conditionTableView.selectRowIndexes([], byExtendingSelection: false) } @@ -167,6 +147,8 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab } } + //MARK:- Private Method + //❤️ private func registerForAppSwitchNotification(_ pid: pid_t) { if pid != getpid() { if applicationObservers[pid] == nil { @@ -183,6 +165,28 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab } } } + + fileprivate func applicationSwitched() { + DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { + if let application = NSWorkspace.shared.frontmostApplication { + let switchedPid:pid_t = application.processIdentifier + if (switchedPid != self.currentPid && switchedPid != getpid()) { + for condition in self.conditionItems { + if !condition.enabled { + continue + } + if condition.applicationIdentifier == application.bundleIdentifier { + if let inputSource = InputSource.with(condition.inputSourceID) { + inputSource.activate() + } + break + } + } + self.currentPid = switchedPid + } + } + } + } func loadConditions() { if let conditions = UserDefaults.standard.array(forKey: "Conditions") as? [[String:Any]] { @@ -265,7 +269,10 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab saveConditions() } } - + //MARK:- TableView DataSource & Delegate + func numberOfRows(in tableView: NSTableView) -> Int { + return conditionItems.count + 1 + } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { if row > 0 { let item = conditionItems[row - 1] @@ -292,13 +299,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab } } - func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { - if row > 0 { - return 64 - } else { - return 24 - } - } +// func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { +// if row > 0 { +// return 64 +// } else { +// return 24 +// } +// } func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? { let view = TableRowView() @@ -306,9 +313,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab return view } - func numberOfRows(in tableView: NSTableView) -> Int { - return conditionItems.count + 1 - } + } class TableView: NSTableView { diff --git a/SwitchKey/Base.lproj/SwitchKey.xib b/SwitchKey/Base.lproj/SwitchKey.xib index 48ab804..a3e31e0 100644 --- a/SwitchKey/Base.lproj/SwitchKey.xib +++ b/SwitchKey/Base.lproj/SwitchKey.xib @@ -1,8 +1,8 @@ - + - + @@ -23,23 +23,22 @@ - - + + - + - - + + - + - @@ -55,14 +54,14 @@ - + @@ -127,20 +126,20 @@ - + - +