Skip to content

Commit 0fe99b5

Browse files
authored
Merge pull request #30 from wolveix/master
Implement `preserveAppOrder` user preference
2 parents 5e7fe71 + 4863faf commit 0fe99b5

File tree

12 files changed

+113
-74
lines changed

12 files changed

+113
-74
lines changed

MenuBarDock/AppDelegate.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ extension AppDelegate: AppTrackerDelegate {
125125
}
126126

127127
extension AppDelegate: PreferencesViewControllerDelegate {
128-
129128
func maxNumRunningAppsSliderEndedChanging(_ value: Int) {
130129
userPrefs.maxNumRunningApps = value
131130
userPrefsWasUpdated()
@@ -180,6 +179,11 @@ extension AppDelegate: PreferencesViewControllerDelegate {
180179
userPrefsWasUpdated()
181180
}
182181

182+
func preserveAppOrderDidChange(_ value: Bool) {
183+
userPrefs.preserveAppOrder = value
184+
userPrefsWasUpdated()
185+
}
186+
183187
func appOpeningMethodDidChange(_ value: AppOpeningMethod) {
184188
userPrefs.defaultAppOpeningMethod = value
185189
userPrefsWasUpdated()

MenuBarDock/AppTracker.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ protocol AppTrackerDelegate: AnyObject {
1515

1616
// tracks app activations and quits
1717
class AppTracker {
18-
1918
public weak var delegate: AppTrackerDelegate?
2019

2120
init() {
2221
trackAppsBeingActivated()
2322
trackAppsBeingQuit()
2423
}
24+
2525
private func trackAppsBeingActivated() {
2626
NSWorkspace.shared.notificationCenter.addObserver(forName: NSWorkspace.didActivateApplicationNotification, object: nil, queue: .main) { (notification) in
2727
if

MenuBarDock/Base.lproj/Main.storyboard

Lines changed: 49 additions & 37 deletions
Large diffs are not rendered by default.

MenuBarDock/Constants.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ enum Constants {
4646
static let appOpeningMethods = "appOpeningMethods"
4747
static let hideActiveAppFromRunningApps = "hideActiveAppFromRunningApps"
4848
static let hideFinderFromRunningApps = "hideFinderFromRunningApps"
49+
static let preserveAppOrder = "preserveAppOrder"
4950
static let regularAppsUrls = "regularAppsUrls"
5051
static let sideToShowRunningApps = "sideToShowRunningApps"
5152
static let hideDuplicateApps = "hideDuplicateApps"

MenuBarDock/InfoViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class InfoViewController: NSViewController {
1717
"Hold command ⌘ while dragging an icon to move its position in the menu bar.",
1818
"If clicking on an app in the menu bar doesn't have the desired effect, try changing the app opening method for that app by right clicking it in the menu bar dock",
1919
"If \(Constants.App.name) is getting slow, just restart it, and it will clear out and reset all the unused status items. This should happen very rarely if at all",
20-
"If you don't want to use the \(Constants.App.runningAppsSectionTitle) feature, just set the max number of running appps to 0. If you don't want to use the \(Constants.App.regularAppsSectionTitle) feature, just remove all the apps from the list.",
20+
"If you don't want to use the \(Constants.App.runningAppsSectionTitle) feature, just set the max number of running apps to 0. If you don't want to use the \(Constants.App.regularAppsSectionTitle) feature, just remove all the apps from the list.",
2121
"You can add multiple versions of the same app to the \(Constants.App.regularAppsSectionTitle) list as long as they have a different path on your system.",
2222
"You may notice the ordering of apps will be incorrect when you first open \(Constants.App.name). This is because it tracks what apps you open and close after opening \(Constants.App.name), so it will take a little bit of time to settle. This issue can be avoided if you simple keep 'Launch at Login' on."
2323
]

MenuBarDock/MenuBarItem.swift

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ class MenuBarItem {
4040
}
4141

4242
func update(for app: OpenableApp, appIconSize: CGFloat, slotWidth: CGFloat) {
43-
4443
self.app = app
4544
let imageSize = appIconSize
4645
let menuBarHeight: CGFloat = 22 // do not use NSApplication.shared.mainMenu?.menuBarHeight, it doesn't work on MBP 16 inch with notch, because the menu bar reports as bigger than the actual height it uses. 22 is a good fixed height.
@@ -95,14 +94,21 @@ class MenuBarItem {
9594
let menu = NSMenu()
9695
guard let appName = app?.name else { return }
9796

98-
if app?.runningApplication != nil {
99-
_ = addMenuItem(
100-
menu: menu,
101-
title: "Quit \(appName)",
102-
action: #selector(quitApp),
103-
keyEquivalent: "q"
104-
)
105-
}
97+
if let runningApplication = app?.runningApplication {
98+
// only makes sense to hide and show, and activate a running app, not just any app
99+
_ = addMenuItem(
100+
menu: menu,
101+
title: "\(runningApplication.isHidden ? "Unhide" : "Hide") \(appName)",
102+
action: #selector(toggleAppHidden),
103+
keyEquivalent: "h"
104+
)
105+
_ = addMenuItem(
106+
menu: menu,
107+
title: "Activate \(appName)",
108+
action: #selector(activateApp),
109+
keyEquivalent: "a"
110+
)
111+
}
106112

107113
_ = addMenuItem(
108114
menu: menu,
@@ -111,31 +117,23 @@ class MenuBarItem {
111117
keyEquivalent: "r"
112118
)
113119

114-
if let runningApplication = app?.runningApplication {
115-
// only makes sense to hide and show, and activate a running app, not just any app
116-
_ = addMenuItem(
117-
menu: menu,
118-
title: "\(runningApplication.isHidden ? "Unhide" : "Hide") \(appName)",
119-
action: #selector(toggleAppHidden),
120-
keyEquivalent: "h"
121-
)
122-
_ = addMenuItem(
123-
menu: menu,
124-
title: "Activate \(appName)",
125-
action: #selector(activateApp),
126-
keyEquivalent: "a"
127-
)
128-
}
129-
130120
_ = addMenuItem(
131121
menu: menu,
132122
title: "Launch \(appName)",
133123
action: #selector(launchApp),
134124
keyEquivalent: "l"
135125
)
136126

137-
// removed open new instance item because it's kinda pointless and will probably cause bugs
127+
if app?.runningApplication != nil {
128+
_ = addMenuItem(
129+
menu: menu,
130+
title: "Quit \(appName)",
131+
action: #selector(quitApp),
132+
keyEquivalent: "q"
133+
)
134+
}
138135

136+
// removed open new instance item because it's kinda pointless and will probably cause bugs
139137
addAppOpeningMethodMenuItem(menu: menu)
140138

141139
menu.addItem(NSMenuItem.separator())

MenuBarDock/MenuBarItems.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ protocol MenuBarItemsUserPrefsDataSource: AnyObject {
1212
var appOpeningMethods: [String: AppOpeningMethod] { get }
1313
var statusItemWidth: CGFloat { get }
1414
var appIconSize: CGFloat { get }
15-
15+
var preserveAppOrder: Bool { get }
1616
}
1717

1818
protocol MenuBarItemsDelegate: AnyObject {
@@ -78,10 +78,12 @@ class MenuBarItems {
7878
}
7979

8080
private func hideItem(item: MenuBarItem) {
81-
8281
item.statusItem.length = 0
82+
8383
if #available(OSX 10.12, *) {
84-
// item.statusItem.isVisible = false // this prevents the item from remembering its position. Thanks Apple.
84+
if userPrefsDataSource.preserveAppOrder == false {
85+
item.statusItem.isVisible = false // this prevents the item from remembering its position. Thanks Apple.
86+
}
8587
}
8688
}
8789

MenuBarDock/OpenableApp.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ class OpenableApp {
140140

141141
private func launchApp() {
142142
print("Launching app: ", name)
143+
144+
// workaround needed to open Steam, otherwise it opens the app path in Finder
145+
if bundleId == "com.valvesoftware.steam" {
146+
NSWorkspace.shared.launchApplication(withBundleIdentifier: "com.valvesoftware.steam", options: [], additionalEventParamDescriptor: nil, launchIdentifier: nil)
147+
return
148+
}
149+
143150
if #available(OSX 10.15, *) {
144151
let config = NSWorkspace.OpenConfiguration()
145152
config.activates = true

MenuBarDock/PreferencesViewController.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ protocol PreferencesViewControllerDelegate: AnyObject {
2121
func aboutWasPressed()
2222
func hideFinderDidChange(_ value: Bool)
2323
func hideActiveAppDidChange(_ value: Bool)
24+
func preserveAppOrderDidChange(_ value: Bool)
2425
func appOpeningMethodDidChange(_ value: AppOpeningMethod)
2526
func regularAppsUrlsWereAdded(_ value: [URL])
2627
func regularAppsUrlsWereRemoved(_ removedIndexes: IndexSet)
@@ -40,14 +41,14 @@ protocol PreferencesViewControllerUserPrefsDataSource: AnyObject {
4041
var defaultAppOpeningMethod: AppOpeningMethod { get }
4142
var hideFinderFromRunningApps: Bool { get }
4243
var hideActiveAppFromRunningApps: Bool { get }
44+
var preserveAppOrder: Bool { get }
4345
var regularAppsUrls: [URL] { get }
4446
var sideToShowRunningApps: SideToShowRunningApps { get }
4547
var hideDuplicateApps: Bool { get }
4648
var duplicateAppsPriority: DuplicateAppsPriority { get }
4749
}
4850

49-
class PreferencesViewController: NSViewController { // this should do onthing
50-
51+
class PreferencesViewController: NSViewController { // this should do nothing
5152
weak var delegate: PreferencesViewControllerDelegate?
5253
weak var userPrefsDataSource: PreferencesViewControllerUserPrefsDataSource!
5354

@@ -67,6 +68,7 @@ class PreferencesViewController: NSViewController { // this should do onthing
6768

6869
@IBOutlet weak var hideActiveAppFromRunningAppsButton: NSButton!
6970
@IBOutlet weak var hideFinderFromRunningAppsButton: NSButton!
71+
@IBOutlet weak var preserveAppOrderButton: NSButton!
7072

7173
@IBOutlet weak var sideToShowRunningAppsControl: NSSegmentedControl!
7274

@@ -114,6 +116,8 @@ class PreferencesViewController: NSViewController { // this should do onthing
114116

115117
hideFinderFromRunningAppsButton.state = userPrefsDataSource.hideFinderFromRunningApps ? .on : .off
116118

119+
preserveAppOrderButton.state = userPrefsDataSource.preserveAppOrder ? .on : .off
120+
117121
switch userPrefsDataSource.runningAppsSortingMethod {
118122
case .mostRecentOnRight:
119123
mostRecentRightRadioButton.state = .on
@@ -240,9 +244,13 @@ class PreferencesViewController: NSViewController { // this should do onthing
240244
}
241245

242246
@IBAction func hideFinderFromRunningAppsPressed(_ sender: NSButton) {
243-
delegate?.hideFinderDidChange(sender.state == .on )
247+
delegate?.hideFinderDidChange(sender.state == .on)
244248
}
245249

250+
@IBAction func preserveAppOrderPressed(_ sender: NSButton) {
251+
delegate?.preserveAppOrderDidChange(sender.state == .on)
252+
}
253+
246254
@IBAction func addOrRemovePressed(_ sender: NSSegmentedControl) {
247255
switch sender.selectedSegment {
248256
case 0:

MenuBarDock/UserPrefs.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ enum UserPrefsDefaultValues {
1818
static let appOpeningMethods: [String: AppOpeningMethod] = [:] // openableApp id is the key
1919
static let hideActiveAppFromRunningApps = true
2020
static let hideFinderFromRunningApps = false
21+
static let preserveAppOrder = true
2122
static let regularAppsUrls: [URL] = []
2223
static let sideToShowRunningApps: SideToShowRunningApps = .right
2324
static let hideDuplicateApps = true
@@ -34,6 +35,7 @@ class UserPrefs {
3435
var appOpeningMethods = UserPrefsDefaultValues.appOpeningMethods
3536
var hideActiveAppFromRunningApps = UserPrefsDefaultValues.hideActiveAppFromRunningApps
3637
var hideFinderFromRunningApps = UserPrefsDefaultValues.hideFinderFromRunningApps
38+
var preserveAppOrder = UserPrefsDefaultValues.preserveAppOrder
3739
var regularAppsUrls = UserPrefsDefaultValues.regularAppsUrls
3840
var sideToShowRunningApps = UserPrefsDefaultValues.sideToShowRunningApps
3941
var hideDuplicateApps = UserPrefsDefaultValues.hideDuplicateApps
@@ -47,6 +49,7 @@ class UserPrefs {
4749
defaultAppOpeningMethod = UserPrefsDefaultValues.defaultAppOpeningMethod
4850
hideActiveAppFromRunningApps = UserPrefsDefaultValues.hideActiveAppFromRunningApps
4951
hideFinderFromRunningApps = UserPrefsDefaultValues.hideFinderFromRunningApps
52+
preserveAppOrder = UserPrefsDefaultValues.preserveAppOrder
5053
// don't reset regularAppsUrls, it's not right
5154
sideToShowRunningApps = UserPrefsDefaultValues.sideToShowRunningApps
5255
hideDuplicateApps = UserPrefsDefaultValues.hideDuplicateApps
@@ -70,6 +73,7 @@ class UserPrefs {
7073
})), forKey: Constants.UserPrefs.appOpeningMethods)
7174
UserDefaults.standard.set(hideActiveAppFromRunningApps, forKey: Constants.UserPrefs.hideActiveAppFromRunningApps)
7275
UserDefaults.standard.set(hideFinderFromRunningApps, forKey: Constants.UserPrefs.hideFinderFromRunningApps)
76+
UserDefaults.standard.set(preserveAppOrder, forKey: Constants.UserPrefs.preserveAppOrder)
7377
UserDefaults.standard.set(regularAppsUrls.map { $0.absoluteString }, forKey: Constants.UserPrefs.regularAppsUrls)
7478
UserDefaults.standard.set(sideToShowRunningApps.rawValue, forKey: Constants.UserPrefs.sideToShowRunningApps)
7579
UserDefaults.standard.set(hideDuplicateApps, forKey: Constants.UserPrefs.hideDuplicateApps)
@@ -112,6 +116,9 @@ class UserPrefs {
112116
if let hideFinderFromRunningApps = UserDefaults.standard.object(forKey: Constants.UserPrefs.hideFinderFromRunningApps) as? Bool {
113117
self.hideFinderFromRunningApps = hideFinderFromRunningApps
114118
}
119+
if let preserveAppOrder = UserDefaults.standard.object(forKey: Constants.UserPrefs.preserveAppOrder) as? Bool {
120+
self.preserveAppOrder = preserveAppOrder
121+
}
115122

116123
if let regularAppsUrlsStrs = UserDefaults.standard.object(forKey: Constants.UserPrefs.regularAppsUrls) as? [String] {
117124
var res: [URL] = []

0 commit comments

Comments
 (0)