From 43d70742e901e824f19cd8457872f37bf9634c67 Mon Sep 17 00:00:00 2001 From: tanpengsccd Date: Wed, 3 Feb 2021 22:01:51 +0800 Subject: [PATCH 1/2] add commet for file delegate --- SwitchKey/AppDelegate.swift | 58 ++++++++++++++++-------------- SwitchKey/Base.lproj/SwitchKey.xib | 9 +++-- SwitchKey/LoginServiceKit.swift | 2 +- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/SwitchKey/AppDelegate.swift b/SwitchKey/AppDelegate.swift index 19628d8..eae48b1 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,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab saveConditions() } } - + //MARK:- TableView DataSource & Delegate func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { if row > 0 { let item = conditionItems[row - 1] diff --git a/SwitchKey/Base.lproj/SwitchKey.xib b/SwitchKey/Base.lproj/SwitchKey.xib index 48ab804..fee46d1 100644 --- a/SwitchKey/Base.lproj/SwitchKey.xib +++ b/SwitchKey/Base.lproj/SwitchKey.xib @@ -1,8 +1,8 @@ - + - + @@ -37,9 +37,8 @@ - + - @@ -140,7 +139,7 @@ - + diff --git a/SwitchKey/LoginServiceKit.swift b/SwitchKey/LoginServiceKit.swift index a6eedaa..2ebb051 100644 --- a/SwitchKey/LoginServiceKit.swift +++ b/SwitchKey/LoginServiceKit.swift @@ -55,7 +55,7 @@ public extension LoginServiceKit { @discardableResult static func addLoginItems(at path: String = Bundle.main.bundlePath) -> Bool { - guard !isExistLoginItems(at: path) else { return false } + guard !isExistLoginItems(at: path) else { return false } guard let sharedFileList = LSSharedFileListCreate(nil, kLSSharedFileListSessionLoginItems.takeRetainedValue(), nil) else { return false } let loginItemList = sharedFileList.takeRetainedValue() From d92e5ca0ee40890fad71bec959334152591c5e6e Mon Sep 17 00:00:00 2001 From: tanpengsccd Date: Wed, 3 Feb 2021 23:10:12 +0800 Subject: [PATCH 2/2] fix Cell height not correct at first Launch --- SwitchKey/AppDelegate.swift | 21 +++++++++++---------- SwitchKey/Base.lproj/SwitchKey.xib | 29 +++++++++++++++-------------- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/SwitchKey/AppDelegate.swift b/SwitchKey/AppDelegate.swift index eae48b1..7b61d37 100644 --- a/SwitchKey/AppDelegate.swift +++ b/SwitchKey/AppDelegate.swift @@ -270,6 +270,9 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab } } //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] @@ -296,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() @@ -310,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 fee46d1..a3e31e0 100644 --- a/SwitchKey/Base.lproj/SwitchKey.xib +++ b/SwitchKey/Base.lproj/SwitchKey.xib @@ -23,21 +23,21 @@ - - + + - + - - + + - + @@ -54,14 +54,14 @@ - + @@ -126,16 +126,16 @@ - + - +