diff --git a/Dozer/AppDelegate.swift b/Dozer/AppDelegate.swift index 286272d..32e2dea 100755 --- a/Dozer/AppDelegate.swift +++ b/Dozer/AppDelegate.swift @@ -20,6 +20,8 @@ final class AppDelegate: NSObject, NSApplicationDelegate { if DozerIcons.shared.hideStatusBarIconsAtLaunch { DozerIcons.shared.hide() } + + _ = DozerIcons.toggleDockIcon(showIcon: false) } // Show all Dozer icons when opening Dozer from Finder etc. diff --git a/Dozer/Constants.swift b/Dozer/Constants.swift index 83d15dd..53c893a 100644 --- a/Dozer/Constants.swift +++ b/Dozer/Constants.swift @@ -13,6 +13,7 @@ extension Defaults.Keys { static let hideAfterDelay: Defaults.Key = Key("hideAfterDelay", default: 10) static let noIconMode: Defaults.Key = Key("noIconMode", default: false) static let removeDozerIconEnabled: Defaults.Key = Key("removeStatusIconEnabled", default: false) + static let showIconAndMenuEnabled: Defaults.Key = Key("showIconAndMenuEnabled", default: false) static let iconSize: Defaults.Key = Key("fontSize", default: 10) static let buttonPadding: Defaults.Key = Key("buttonPadding", default: 25) static let animationEnabled: Defaults.Key = Key("animationEnabeld", default: false) @@ -34,7 +35,7 @@ extension PreferencePaneIdentifier { } struct AppInfo { - static let bundleIdentifier: String = "com.mortennn.Dozer" + static let bundleIdentifier: String = Bundle.main.bundleIdentifier! static var releaseVersionNumber: String? { Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String } diff --git a/Dozer/DozerIcons.swift b/Dozer/DozerIcons.swift index 9a6e37e..df8233e 100755 --- a/Dozer/DozerIcons.swift +++ b/Dozer/DozerIcons.swift @@ -10,6 +10,7 @@ public final class DozerIcons { private var dozerIcons: [HelperstatusIcon] = [] private var timerToCheckUserInteraction = Timer() private var timerToHideDozerIcons = Timer() + private var previousApp = NSRunningApplication() private init() { dozerIcons.append(NormalStatusIcon()) @@ -86,6 +87,16 @@ public final class DozerIcons { } } + public var enableIconAndMenu: Bool = Defaults[.showIconAndMenuEnabled] { + didSet { + Defaults[.showIconAndMenuEnabled] = self.enableIconAndMenu + if self.enableIconAndMenu == false { + _ = DozerIcons.toggleDockIcon(showIcon: false) + appDelegate.preferencesWindowController.show(preferencePane: .general) + } + } + } + public var iconFontSize: Int = Defaults[.iconSize] { didSet { Defaults[.iconSize] = self.iconFontSize @@ -112,15 +123,18 @@ public final class DozerIcons { perform(action: .hide, statusIcon: .normalRight) } didHideStatusBarIcons() + hideIconAndMenu() } public func show() { + resetTimer() perform(action: .hide, statusIcon: .remove) perform(action: .show, statusIcon: .normalLeft) if Defaults[.noIconMode] { perform(action: .show, statusIcon: .normalRight) } didShowStatusBarIcons() + showIconAndMenu() } public func toggle() { @@ -139,7 +153,26 @@ public final class DozerIcons { } } - /// Force show all Dozer status bar icons + public func showIconAndMenu() { + if NSWorkspace.shared.frontmostApplication?.bundleIdentifier != AppInfo.bundleIdentifier { + previousApp = NSWorkspace.shared.frontmostApplication! + } + if Defaults[.showIconAndMenuEnabled] { + _ = DozerIcons.toggleDockIcon(showIcon: true) + NSApp.activate(ignoringOtherApps: true) + } + } + + public func hideIconAndMenu() { + if Defaults[.showIconAndMenuEnabled] { + _ = DozerIcons.toggleDockIcon(showIcon: false) + if NSWorkspace.shared.frontmostApplication?.bundleIdentifier == AppInfo.bundleIdentifier { + previousApp.activate() + } + } + } + + /// Force show all Dozer icons public func showAll() { perform(action: .show, statusIcon: .remove) perform(action: .show, statusIcon: .normalLeft) @@ -148,12 +181,12 @@ public final class DozerIcons { } public func handleOptionClick() { + showIconAndMenu() if get(dozerIcon: .normalLeft).isShown { DozerIcons.shared.perform( action: .toggle, statusIcon: .remove ) - resetTimer() } else { DozerIcons.shared.perform( action: .show, @@ -164,11 +197,12 @@ public final class DozerIcons { statusIcon: .remove ) } + resetTimer() } // MARK: Show/hide lifecycle private func didShowStatusBarIcons() { - startTimer() + //startTimer() startUserInteractionTimer() } @@ -182,7 +216,7 @@ public final class DozerIcons { return } - // don't hide on user interaction with status bar + // don't hide on user interaction with menu bar guard !isUserInteractingWithStatusBar() else { resetTimer() return @@ -230,7 +264,7 @@ public final class DozerIcons { } } - /// Will crash if trying to get ´DozerIcon´ which does not exist in the status bar + /// Will crash if trying to get ´DozerIcon´ which does not exist in the menu bar private func get(dozerIcon: DozerIcon) -> HelperstatusIcon { var normalStatusIconsXPosition: [CGFloat] = [] for statusIcon in dozerIcons where statusIcon.type == .normal { @@ -255,9 +289,20 @@ public final class DozerIcons { } } - /// Determines if the user is interacting with the status bar based on level, owner and y-coordinate + /// hide and show dock icon and thus its menu bar: to free up space to show more menu bar icons + public class func toggleDockIcon(showIcon state: Bool) -> Bool { + var result: Bool + if state { + result = NSApp.setActivationPolicy(NSApplication.ActivationPolicy.regular) + } else { + result = NSApp.setActivationPolicy(NSApplication.ActivationPolicy.accessory) + } + return result + } + + /// Determines if the user is interacting with the menu bar based on level, owner and y-coordinate /// - /// - Returns: Returns whether the user is interacting with the status bar or not + /// - Returns: Returns whether the user is interacting with the menu bar or not private func isUserInteractingWithStatusBar() -> Bool { let windowListType = CGWindowListOption.optionOnScreenOnly guard let windowInfoList = CGWindowListCopyWindowInfo(windowListType, kCGNullWindowID) as NSArray? as? [[String: AnyObject]] else { @@ -267,7 +312,7 @@ public final class DozerIcons { for windowInfo in windowInfoList { guard let window = Window(windowInfo), - // If the preferences window are close to the status bar it won't auto hide + // If the preferences window are close to the menu bar it won't auto hide window.owner != "Dozer" else { continue } diff --git a/Dozer/ViewControllers/GeneralVC.swift b/Dozer/ViewControllers/GeneralVC.swift index 65b0236..ba82fef 100644 --- a/Dozer/ViewControllers/GeneralVC.swift +++ b/Dozer/ViewControllers/GeneralVC.swift @@ -25,6 +25,7 @@ final class General: NSViewController, PreferencePane { @IBOutlet private var HideStatusBarIconsSecondsPopUpButton: NSPopUpButton! @IBOutlet private var HideBothDozerIconsCheckbox: NSButton! @IBOutlet private var EnableRemoveDozerIconCheckbox: NSButton! + @IBOutlet private var ShowIconAndMenuCheckbox: NSButton! @IBOutlet private var FontSizePopUpButton: NSPopUpButton! @IBOutlet private var ButtonPaddingPopUpButton: NSPopUpButton! @IBOutlet private var ToggleMenuItemsView: MASShortcutView! @@ -45,6 +46,7 @@ final class General: NSViewController, PreferencePane { HideStatusBarIconsAfterDelayCheckbox.isChecked = Defaults[.hideAfterDelayEnabled] HideBothDozerIconsCheckbox.isChecked = Defaults[.noIconMode] EnableRemoveDozerIconCheckbox.isChecked = Defaults[.removeDozerIconEnabled] + ShowIconAndMenuCheckbox.isChecked = Defaults[.showIconAndMenuEnabled] HideStatusBarIconsSecondsPopUpButton.selectItem(withTitle: "\(Int(Defaults[.hideAfterDelay])) seconds") FontSizePopUpButton.selectItem(withTitle: "\(Int(Defaults[.iconSize])) px") ButtonPaddingPopUpButton.selectItem(withTitle: "\(Int(Defaults[.buttonPadding])) px") @@ -86,6 +88,10 @@ final class General: NSViewController, PreferencePane { DozerIcons.shared.hideBothDozerIcons = HideBothDozerIconsCheckbox.isChecked } + @IBAction private func showIconAndMenuClicked(_ sender: NSButton) { + DozerIcons.shared.enableIconAndMenu = ShowIconAndMenuCheckbox.isChecked + } + @IBAction private func fontSizeChanged(_ sender: NSPopUpButton) { DozerIcons.shared.iconFontSize = FontSizePopUpButton.selectedTag() } diff --git a/Dozer/XIB/Dozer.xib b/Dozer/XIB/Dozer.xib index 083cb70..a3e577a 100644 --- a/Dozer/XIB/Dozer.xib +++ b/Dozer/XIB/Dozer.xib @@ -1,7 +1,8 @@ - + - + + @@ -24,6 +25,8 @@ + u + @@ -50,6 +53,8 @@ + q + diff --git a/Dozer/XIB/General.xib b/Dozer/XIB/General.xib index 960b944..90aef30 100644 --- a/Dozer/XIB/General.xib +++ b/Dozer/XIB/General.xib @@ -1,8 +1,8 @@ - + - + @@ -17,6 +17,7 @@ + @@ -24,10 +25,10 @@ - + - - - - - - - - - - + + + + - - - - - - - - - - - - - - - - - - - - - - - - - + - + @@ -143,39 +96,89 @@ + + + + + + + + + + + + + + + + - - - - + + + + + + + - - - - + + + + + + + + + + + - + - + @@ -192,12 +195,21 @@ + + + + + + + + + - + - + @@ -214,21 +226,38 @@ - - + + + + + + + + + + + - - - + + + + + + + + + + + - - + + - +