From a78e28e23210d63ca6fc7c63fb4c8cd906dc6073 Mon Sep 17 00:00:00 2001 From: Bofan Wang Date: Sat, 9 Oct 2021 14:37:37 +0800 Subject: [PATCH 1/2] add opiton to set default input source --- SwitchKey.xcodeproj/project.pbxproj | 4 +- .../xcshareddata/xcschemes/SwitchKey.xcscheme | 2 +- SwitchKey/AppDelegate.swift | 85 ++++++++++++++++--- SwitchKey/Base.lproj/SwitchKey.xib | 44 +++++++--- 4 files changed, 112 insertions(+), 23 deletions(-) diff --git a/SwitchKey.xcodeproj/project.pbxproj b/SwitchKey.xcodeproj/project.pbxproj index e2e991d..8cfd9b1 100644 --- a/SwitchKey.xcodeproj/project.pbxproj +++ b/SwitchKey.xcodeproj/project.pbxproj @@ -100,7 +100,7 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 1020; - LastUpgradeCheck = 1130; + LastUpgradeCheck = 1300; ORGANIZATIONNAME = "Jinyu Li"; TargetAttributes = { 71FECF2922610BE600C51FD7 = { @@ -196,6 +196,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; @@ -257,6 +258,7 @@ CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; CLANG_WARN_STRICT_PROTOTYPES = YES; CLANG_WARN_SUSPICIOUS_MOVE = YES; diff --git a/SwitchKey.xcodeproj/xcshareddata/xcschemes/SwitchKey.xcscheme b/SwitchKey.xcodeproj/xcshareddata/xcschemes/SwitchKey.xcscheme index 80fd0cb..2e079ce 100644 --- a/SwitchKey.xcodeproj/xcshareddata/xcschemes/SwitchKey.xcscheme +++ b/SwitchKey.xcodeproj/xcshareddata/xcschemes/SwitchKey.xcscheme @@ -1,6 +1,6 @@ 0 { - conditionItems.remove(at: row - 1) + if row > 2 { + conditionItems.remove(at: row - 3) conditionTableView.reloadData() saveConditions() } } func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { - if row > 0 { - let item = conditionItems[row - 1] + if row == 0 { + return conditionTableView.makeView(withIdentifier: editCellIdentifier, owner: nil) + } else if row == 1 { + return conditionTableView.makeView(withIdentifier: defaultCellIdentifier, owner: nil) + } else if row == 2 { + let itemCell = conditionTableView.makeView(withIdentifier: itemCellIdentifier, owner: nil) as! ConditionCell + + let icon = defaultCondition.inputSourceIcon + itemCell.appName.stringValue = "Default" + itemCell.appIcon.image = NSImage() + + itemCell.inputSourceButton.image = icon + itemCell.inputSourceButton.image?.isTemplate = icon.canTemplate() + + itemCell.conditionEnabled.state = defaultCondition.enabled ? .on : .off + return itemCell + } else { + let item = conditionItems[row - 3] let itemCell = conditionTableView.makeView(withIdentifier: itemCellIdentifier, owner: nil) as! ConditionCell itemCell.appIcon.image = item.applicationIcon itemCell.appName.stringValue = item.applicationName @@ -279,21 +335,21 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab itemCell.conditionEnabled.state = item.enabled ? .on : .off return itemCell - } else { - return conditionTableView.makeView(withIdentifier: editCellIdentifier, owner: nil) } } func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? { - if row > 0 { - return conditionItems[row - 1] + if row == 2 { + return defaultCondition + } else if row > 2 { + return conditionItems[row - 3] } else { return self } } func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat { - if row > 0 { + if row > 1 { return 64 } else { return 24 @@ -302,12 +358,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSTableViewDataSource, NSTab func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? { let view = TableRowView() - view.highlight = row > 0 + view.highlight = row > 1 return view } func numberOfRows(in tableView: NSTableView) -> Int { - return conditionItems.count + 1 + return conditionItems.count + 3 } } @@ -373,3 +429,10 @@ class EditCell: NSTableCellView { app.addCondition() } } + +class DefaultCell: NSTableCellView { + @IBAction func addItemClicked(_ sender: Any) { + let app = objectValue as! AppDelegate + app.setDefaultCondition() + } +} diff --git a/SwitchKey/Base.lproj/SwitchKey.xib b/SwitchKey/Base.lproj/SwitchKey.xib index 48ab804..bc0a491 100644 --- a/SwitchKey/Base.lproj/SwitchKey.xib +++ b/SwitchKey/Base.lproj/SwitchKey.xib @@ -1,8 +1,8 @@ - + - + @@ -21,7 +21,7 @@ - + @@ -37,9 +37,8 @@ - + - @@ -76,9 +75,9 @@ - - - + + + @@ -140,7 +139,7 @@ - + @@ -152,7 +151,32 @@ - + + + + + + + + + + + + + + From 40e3656dbbf4bd4f91001fd3ce1844453b4607a6 Mon Sep 17 00:00:00 2001 From: Bofan Wang Date: Sat, 9 Oct 2021 22:10:34 +0800 Subject: [PATCH 2/2] fix layout --- SwitchKey/AppDelegate.swift | 7 +++ SwitchKey/Base.lproj/SwitchKey.xib | 69 +++++++++++++++--------------- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/SwitchKey/AppDelegate.swift b/SwitchKey/AppDelegate.swift index 688d4fb..41a8677 100644 --- a/SwitchKey/AppDelegate.swift +++ b/SwitchKey/AppDelegate.swift @@ -436,3 +436,10 @@ class DefaultCell: NSTableCellView { app.setDefaultCondition() } } + +extension String { +subscript(to: Int) -> String { + let index = self.index(self.startIndex, offsetBy: to) + return String(self[.. - + - - + + - - + + - - + + - + @@ -54,18 +54,17 @@ - + - + - @@ -75,9 +74,9 @@ - - - + + + @@ -117,7 +116,7 @@ - + @@ -126,16 +125,16 @@ - + - - + + - - - + + + + - +