-
-
Notifications
You must be signed in to change notification settings - Fork 348
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add preference to start at login (closes #159)
- Loading branch information
Showing
8 changed files
with
115 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import Cocoa | ||
|
||
class GeneralTab { | ||
private static let rowHeight = CGFloat(22) // height of the "Tab key" input | ||
|
||
static func make() -> NSTabViewItem { | ||
return TabViewItem.make(NSLocalizedString("General", comment: ""), NSImage.preferencesGeneralName, makeView()) | ||
} | ||
|
||
private static func makeView() -> NSGridView { | ||
// TODO: make the validators be a part of each Preference | ||
let tabKeyCodeValidator: ((String) -> Bool) = { | ||
guard let int = Int($0) else { | ||
return false | ||
} | ||
// non-special keys (mac & pc keyboards): https://eastmanreference.com/complete-list-of-applescript-key-codes | ||
var whitelistedKeycodes: [Int] = Array(0...53) | ||
whitelistedKeycodes.append(contentsOf: [65, 67, 69, 75, 76, 78, ]) | ||
whitelistedKeycodes.append(contentsOf: Array(81...89)) | ||
whitelistedKeycodes.append(contentsOf: [91, 92, 115, 116, 117, 119, 121]) | ||
whitelistedKeycodes.append(contentsOf: Array(123...126)) | ||
return whitelistedKeycodes.contains(int) | ||
} | ||
|
||
let startAtLogin = LabelAndControl.makeLabelWithCheckbox(NSLocalizedString("Start at login", comment: ""), "startAtLogin", extraAction: startAtLoginCallback) | ||
let view = GridView.make([ | ||
startAtLogin, | ||
LabelAndControl.makeLabelWithDropdown(NSLocalizedString("Alt key", comment: ""), "metaKey", MacroPreferences.metaKeyList.values.map { $0.label }), | ||
LabelAndControl.makeLabelWithInput(NSLocalizedString("Tab key", comment: ""), "tabKeyCode", 33, NSLocalizedString("KeyCodes Reference", comment: ""), "https://eastmanreference.com/complete-list-of-applescript-key-codes", tabKeyCodeValidator), | ||
]) | ||
view.column(at: 0).xPlacement = .trailing | ||
view.rowAlignment = .lastBaseline | ||
view.setRowsHeight(rowHeight) | ||
view.fit() | ||
setLoginItemIfCheckboxIsOn(startAtLogin[1] as! NSButton) | ||
return view | ||
} | ||
|
||
private static func setLoginItemIfCheckboxIsOn(_ startAtLoginCheckbox: NSButton) { | ||
if startAtLoginCheckbox.state == .on { | ||
startAtLoginCallback(startAtLoginCheckbox) | ||
} | ||
} | ||
|
||
// adding/removing login item depending on the checkbox state | ||
@available(OSX, deprecated: 10.11) | ||
@objc static func startAtLoginCallback(_ sender: NSControl) { | ||
let loginItems = LSSharedFileListCreate(nil, kLSSharedFileListSessionLoginItems.takeRetainedValue(), nil).takeRetainedValue() | ||
let loginItemsSnapshot = LSSharedFileListCopySnapshot(loginItems, nil).takeRetainedValue() as! [LSSharedFileListItem] | ||
if (sender as! NSButton).state == .on { | ||
LSSharedFileListInsertItemURL(loginItems, kLSSharedFileListItemBeforeFirst.takeRetainedValue(), nil, nil, App.url, nil, nil) | ||
} else { | ||
loginItemsSnapshot.forEach { | ||
if CFEqual(LSSharedFileListItemCopyResolvedURL($0, 0, nil).takeRetainedValue(), App.url) { | ||
LSSharedFileListItemRemove(loginItems, $0) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.