Skip to content

Commit

Permalink
Merge pull request #31 from wolveix/master
Browse files Browse the repository at this point in the history
Cleanup User Preferences page + Implement #8
  • Loading branch information
EthanSK authored Sep 26, 2024
2 parents a28cd0c + 2ef2220 commit 07e119a
Show file tree
Hide file tree
Showing 11 changed files with 805 additions and 628 deletions.
16 changes: 10 additions & 6 deletions MenuBarDock/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import ServiceManagement
@NSApplicationMain

class AppDelegate: NSObject, NSApplicationDelegate {

let popover = NSPopover()
var storyboard: NSStoryboard!
var preferencesWindow = NSWindow()
Expand Down Expand Up @@ -125,13 +124,13 @@ extension AppDelegate: AppTrackerDelegate {
}

extension AppDelegate: PreferencesViewControllerDelegate {
func maxNumRunningAppsSliderEndedChanging(_ value: Int) {
userPrefs.maxNumRunningApps = value
func maxRunningAppsSliderDidChange(_ value: Int) {
userPrefs.maxRunningApps = value
userPrefsWasUpdated()
}

func statusItemWidthSliderDidChange(_ value: Double) {
userPrefs.statusItemWidth = CGFloat(value)
func itemSlotWidthSliderDidChange(_ value: Double) {
userPrefs.itemSlotWidth = CGFloat(value)
userPrefsWasUpdated()
}

Expand Down Expand Up @@ -184,6 +183,11 @@ extension AppDelegate: PreferencesViewControllerDelegate {
userPrefsWasUpdated()
}

func rightClickByDefaultDidChange(_ value: Bool) {
userPrefs.rightClickByDefault = value
userPrefsWasUpdated()
}

func appOpeningMethodDidChange(_ value: AppOpeningMethod) {
userPrefs.defaultAppOpeningMethod = value
userPrefsWasUpdated()
Expand Down Expand Up @@ -214,7 +218,7 @@ extension AppDelegate: PreferencesViewControllerDelegate {
userPrefsWasUpdated()
}

func hideDuplicateAppsWasPressed(_ value: Bool) {
func hideDuplicateAppsDidChange(_ value: Bool) {
userPrefs.hideDuplicateApps = value
userPrefsWasUpdated()
}
Expand Down
784 changes: 449 additions & 335 deletions MenuBarDock/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

27 changes: 14 additions & 13 deletions MenuBarDock/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ enum Constants {
static let finderBundleId = "com.apple.finder"
static let regularAppsSectionTitle = "Regular Apps"
static let runningAppsSectionTitle = "Running Apps"
static let releasesURL = "https://github.com/EthanSK/Menu-Bar-Dock/releases"
}

enum UserDefaultsKeys {
Expand All @@ -33,23 +34,23 @@ enum Constants {
static let info = "InfoWindowController"
static let about = "AboutWindowController"
}

}

enum UserPrefs {
static let appIconSize = "appIconSize"
static let appOpeningMethods = "appOpeningMethods"
static let defaultAppOpeningMethod = "defaultAppOpeningMethod"
static let duplicateAppsPriority = "duplicateAppsPriority"
static let hideActiveAppFromRunningApps = "hideActiveAppFromRunningApps"
static let hideDuplicateApps = "hideDuplicateApps"
static let hideFinderFromRunningApps = "hideFinderFromRunningApps"
static let launchAtLogin = "launchAtLogin"
static let maxNumRunningApps = "maxNumRunningApps"
static let statusItemWidth = "statusItemWidth"
static let runningAppsSortingMethod = "runningAppsSortingMethod"
static let appIconSize = "appIconSize"
static let launchAtLogin = "launchAtLogin"
static let defaultAppOpeningMethod = "defaultAppOpeningMethod"
static let appOpeningMethods = "appOpeningMethods"
static let hideActiveAppFromRunningApps = "hideActiveAppFromRunningApps"
static let hideFinderFromRunningApps = "hideFinderFromRunningApps"
static let preserveAppOrder = "preserveAppOrder"
static let regularAppsUrls = "regularAppsUrls"
static let sideToShowRunningApps = "sideToShowRunningApps"
static let hideDuplicateApps = "hideDuplicateApps"
static let duplicateAppsPriority = "duplicateAppsPriority"
static let regularAppsUrls = "regularAppsUrls"
static let rightClickByDefault = "rightClickByDefault"
static let runningAppsSortingMethod = "runningAppsSortingMethod"
static let sideToShowRunningApps = "sideToShowRunningApps"
static let statusItemWidth = "statusItemWidth"
}
}
21 changes: 15 additions & 6 deletions MenuBarDock/MenuBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Cocoa

protocol MenuBarItemDataSource: AnyObject {
func appOpeningMethod(for app: OpenableApp) -> AppOpeningMethod?
var rightClickByDefault: Bool { get }
}

protocol MenuBarItemDelegate: AnyObject {
Expand All @@ -26,15 +27,15 @@ class MenuBarItem {
return statusItem.button!.superview!.window!.frame.minX
}

public weak var dataSource: MenuBarItemDataSource!
public weak var userPrefsDataSource: MenuBarItemDataSource!
public weak var delegate: MenuBarItemDelegate?

init(
statusItem: NSStatusItem,
dataSource: MenuBarItemDataSource
userPrefsDataSource: MenuBarItemDataSource
) {
self.statusItem = statusItem
self.dataSource = dataSource
self.userPrefsDataSource = userPrefsDataSource
initButton()

}
Expand Down Expand Up @@ -77,6 +78,11 @@ class MenuBarItem {
}

@objc private func handleClick() {
if userPrefsDataSource.rightClickByDefault == true {
showDropdownMenu()
return
}

let event = NSApp.currentEvent
switch event?.type {
case .rightMouseUp:
Expand All @@ -102,6 +108,7 @@ class MenuBarItem {
action: #selector(toggleAppHidden),
keyEquivalent: "h"
)

_ = addMenuItem(
menu: menu,
title: "Activate \(appName)",
Expand Down Expand Up @@ -172,13 +179,15 @@ class MenuBarItem {
action: #selector(setAppOpeningMethodLaunch),
keyEquivalent: ""
)

let activateItem = addMenuItem(
menu: appOpeningMethodMenuItem.submenu!,
title: "Activate",
action: #selector(setAppOpeningMethodActivate),
keyEquivalent: ""
)
switch dataSource.appOpeningMethod(for: app) {

switch userPrefsDataSource.appOpeningMethod(for: app) {
case .launch:
launchItem.state = .on
activateItem.state = .off
Expand Down Expand Up @@ -230,12 +239,12 @@ class MenuBarItem {

@objc private func setAppOpeningMethodLaunch() {
guard let app = app else { return }
delegate?.didSetAppOpeningMethod(dataSource.appOpeningMethod(for: app) == .launch ? nil : .launch, app) // toggle the current state
delegate?.didSetAppOpeningMethod(userPrefsDataSource.appOpeningMethod(for: app) == .launch ? nil : .launch, app) // toggle the current state
}

@objc private func setAppOpeningMethodActivate() {
guard let app = app else { return }
delegate?.didSetAppOpeningMethod(dataSource.appOpeningMethod(for: app) == .activate ? nil : .activate, app)
delegate?.didSetAppOpeningMethod(userPrefsDataSource.appOpeningMethod(for: app) == .activate ? nil : .activate, app)
}

@objc private func openPreferencesWindow() {
Expand Down
15 changes: 10 additions & 5 deletions MenuBarDock/MenuBarItems.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import Cocoa

protocol MenuBarItemsUserPrefsDataSource: AnyObject {
var appOpeningMethods: [String: AppOpeningMethod] { get }
var statusItemWidth: CGFloat { get }
var itemSlotWidth: CGFloat { get }
var appIconSize: CGFloat { get }
var preserveAppOrder: Bool { get }
var rightClickByDefault: Bool { get }
}

protocol MenuBarItemsDelegate: AnyObject {
Expand Down Expand Up @@ -53,7 +54,7 @@ class MenuBarItems {
let offset = items.count - openableApps.apps.count
let item = items[index + offset]
showItem(item: item)
item.update(for: app, appIconSize: userPrefsDataSource.appIconSize, slotWidth: userPrefsDataSource.statusItemWidth)
item.update(for: app, appIconSize: userPrefsDataSource.appIconSize, slotWidth: userPrefsDataSource.itemSlotWidth)
}

// hide the leftmost items not being used (so the weird gap glitch is as left as possible)
Expand All @@ -67,10 +68,10 @@ class MenuBarItems {
private func createEnoughStatusItems(openableApps: OpenableApps) {
let origItemCount = items.count
for index in 0...openableApps.apps.count where index >= origItemCount { // we loop to count not count - 1 so the sort order is always correct as it has sorted one item in advance. https://trello.com/c/Jz312bga
let statusItem = NSStatusBar.system.statusItem(withLength: userPrefsDataSource.statusItemWidth)
let statusItem = NSStatusBar.system.statusItem(withLength: userPrefsDataSource.itemSlotWidth)
let item = MenuBarItem(
statusItem: statusItem,
dataSource: self
userPrefsDataSource: self
)
item.delegate = self
items.append(item)// it's important we never remove items, or the position in the menu bar will be reset. only add if needed, and reuse.
Expand All @@ -88,7 +89,7 @@ class MenuBarItems {
}

private func showItem(item: MenuBarItem) {
item.statusItem.length = userPrefsDataSource.statusItemWidth
item.statusItem.length = userPrefsDataSource.itemSlotWidth

if #available(OSX 10.12, *) {
item.statusItem.isVisible = true // Don't remove this, no harm, only has benefits
Expand All @@ -101,6 +102,10 @@ class MenuBarItems {
}

extension MenuBarItems: MenuBarItemDataSource {
var rightClickByDefault: Bool {
return userPrefsDataSource.rightClickByDefault
}

func appOpeningMethod(for app: OpenableApp) -> AppOpeningMethod? {
return userPrefsDataSource.appOpeningMethods[app.id]
}
Expand Down
Loading

0 comments on commit 07e119a

Please sign in to comment.