Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 42 additions & 37 deletions SwitchKey/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -57,15 +58,15 @@ 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()

private var conditionItems: [ConditionItem] = []

private var statusBarItem: NSStatusItem!
private var launchAtStartupItem: NSMenuItem!

//MARK:- Life Cycle
func applicationDidFinishLaunching(_ aNotification: Notification) {
if !hasAccessibilityPermission() {
askForAccessibilityPermission()
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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]] {
Expand Down Expand Up @@ -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]
Expand All @@ -292,23 +299,21 @@ 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()
view.highlight = row > 0
return view
}

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

}

class TableView: NSTableView {
Expand Down
36 changes: 18 additions & 18 deletions SwitchKey/Base.lproj/SwitchKey.xib
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="15705" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="17701" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="15705"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="17701"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
Expand All @@ -23,23 +23,22 @@
<menu id="lcZ-64-V4h">
<point key="canvasLocation" x="139" y="143"/>
</menu>
<scrollView autohidesScrollers="YES" horizontalLineScroll="19" horizontalPageScroll="10" verticalLineScroll="19" verticalPageScroll="10" usesPredominantAxisScrolling="NO" id="ycM-3L-97V">
<rect key="frame" x="0.0" y="0.0" width="362" height="2"/>
<scrollView autohidesScrollers="YES" horizontalLineScroll="26" horizontalPageScroll="10" verticalLineScroll="26" verticalPageScroll="10" usesPredominantAxisScrolling="NO" id="ycM-3L-97V">
<rect key="frame" x="0.0" y="0.0" width="368" height="184"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<clipView key="contentView" id="gGO-yu-YCr">
<rect key="frame" x="1" y="1" width="360" height="0.0"/>
<rect key="frame" x="1" y="1" width="366" height="182"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" columnReordering="NO" columnResizing="NO" multipleSelection="NO" autosaveColumns="NO" typeSelect="NO" rowSizeStyle="automatic" viewBased="YES" id="wpb-1K-Xeg" customClass="TableView" customModule="SwitchKey" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="360" height="0.0"/>
<tableView verticalHuggingPriority="750" allowsExpansionToolTips="YES" tableStyle="fullWidth" columnReordering="NO" columnResizing="NO" multipleSelection="NO" autosaveColumns="NO" typeSelect="NO" rowHeight="24" usesAutomaticRowHeights="YES" viewBased="YES" id="wpb-1K-Xeg" customClass="TableView" customModule="SwitchKey" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="366" height="182"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<size key="intercellSpacing" width="3" height="2"/>
<color key="backgroundColor" name="controlBackgroundColor" catalog="System" colorSpace="catalog"/>
<color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>
<tableColumns>
<tableColumn width="357" minWidth="40" maxWidth="1000" id="Y12-MF-ZTz">
<tableColumn width="354" minWidth="40" maxWidth="1000" id="Y12-MF-ZTz">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border">
<font key="font" metaFont="menu" size="11"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="headerColor" catalog="System" colorSpace="catalog"/>
</tableHeaderCell>
Expand All @@ -55,14 +54,14 @@
</subviews>
</clipView>
<scroller key="horizontalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="YES" id="bzu-Fw-U6m">
<rect key="frame" x="1" y="119" width="223" height="15"/>
<rect key="frame" x="1" y="167" width="366" height="16"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
<scroller key="verticalScroller" hidden="YES" wantsLayer="YES" verticalHuggingPriority="750" horizontal="NO" id="5LF-KM-I9F">
<rect key="frame" x="346" y="1" width="15" height="0.0"/>
<autoresizingMask key="autoresizingMask"/>
</scroller>
<point key="canvasLocation" x="139" y="185"/>
<point key="canvasLocation" x="132" y="-33"/>
</scrollView>
<tableCellView identifier="item-cell" id="mER-eV-t2b" customClass="ConditionCell" customModule="SwitchKey" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="341" height="64"/>
Expand Down Expand Up @@ -127,20 +126,20 @@
<outlet property="conditionEnabled" destination="Sco-0x-N6p" id="9PL-8I-EEH"/>
<outlet property="inputSourceButton" destination="p83-hg-Sms" id="Ngo-JK-4MU"/>
</connections>
<point key="canvasLocation" x="139.5" y="260"/>
<point key="canvasLocation" x="140" y="230"/>
</tableCellView>
<tableCellView identifier="edit-cell" id="tGE-D6-w3H" customClass="EditCell" customModule="SwitchKey" customModuleProvider="target">
<rect key="frame" x="0.0" y="0.0" width="109" height="20"/>
<rect key="frame" x="0.0" y="0.0" width="341" height="46"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="5k7-2A-D1v">
<rect key="frame" x="8" y="-1" width="93" height="21"/>
<rect key="frame" x="8" y="7" width="325" height="31"/>
<constraints>
<constraint firstAttribute="height" constant="20" id="IDr-fh-azF"/>
<constraint firstAttribute="height" constant="30" id="0sl-cE-5EX"/>
</constraints>
<buttonCell key="cell" type="roundRect" title="Add Current" bezelStyle="roundedRect" alignment="center" lineBreakMode="truncatingTail" state="on" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="XiT-Jr-Qlg">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="label" size="12"/>
<font key="font" metaFont="cellTitle"/>
<connections>
<action selector="addItemClicked:" target="tGE-D6-w3H" id="1xs-Ii-GnG"/>
</connections>
Expand All @@ -149,10 +148,11 @@
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="5k7-2A-D1v" secondAttribute="trailing" constant="8" id="7yE-mF-esi"/>
<constraint firstItem="5k7-2A-D1v" firstAttribute="centerY" secondItem="tGE-D6-w3H" secondAttribute="centerY" id="MTe-2i-BMr"/>
<constraint firstAttribute="bottom" secondItem="5k7-2A-D1v" secondAttribute="bottom" constant="8" id="akm-cV-9pM"/>
<constraint firstItem="5k7-2A-D1v" firstAttribute="top" secondItem="tGE-D6-w3H" secondAttribute="top" constant="8" id="lvR-yQ-29P"/>
<constraint firstItem="5k7-2A-D1v" firstAttribute="leading" secondItem="tGE-D6-w3H" secondAttribute="leading" constant="8" id="zMc-7U-kh1"/>
</constraints>
<point key="canvasLocation" x="139.5" y="341.5"/>
<point key="canvasLocation" x="139.5" y="320"/>
</tableCellView>
</objects>
<resources>
Expand Down
2 changes: 1 addition & 1 deletion SwitchKey/LoginServiceKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down