From 4a5f06b8fdd330d4614d78a1e95ac7995dc5ffd7 Mon Sep 17 00:00:00 2001 From: Erik Gomez Date: Fri, 9 Feb 2024 14:20:05 -0600 Subject: [PATCH 1/6] finish wiring up the script logic --- Nudge/Utilities/Utils.swift | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/Nudge/Utilities/Utils.swift b/Nudge/Utilities/Utils.swift index adb62028..33ed45cb 100644 --- a/Nudge/Utilities/Utils.swift +++ b/Nudge/Utilities/Utils.swift @@ -774,8 +774,8 @@ struct UIUtilities { private func determineUpdateURL() -> URL? { if let actionButtonPath = FeatureVariables.actionButtonPath { if actionButtonPath.isEmpty { - LogManager.warning("actionButtonPath is set but contains an empty string. No action will be triggered.", logger: utilsLog) - return nil + LogManager.warning("actionButtonPath is set but contains an empty string. Defaulting to out of box behavior.", logger: utilsLog) + return URL(fileURLWithPath: "/System/Library/CoreServices/Software Update.app") } // Check if it's a shell command @@ -804,7 +804,7 @@ struct UIUtilities { return URL(fileURLWithPath: "/System/Library/CoreServices/Software Update.app") } - func executeShellCommand(command: String, userClicked: Bool, configuration: NSWorkspace.OpenConfiguration) { + func executeShellCommand(command: String, userClicked: Bool) { let cmds = command.components(separatedBy: " ") guard let launchPath = cmds.first, let argument = cmds.last else { LogManager.error("Invalid shell command format", logger: uiLog) @@ -872,22 +872,27 @@ struct UIUtilities { let configuration = NSWorkspace.OpenConfiguration() configuration.activates = true - guard let url = determineUpdateURL() else { - return - } + if let url = determineUpdateURL() { + let openAction = { + if url.isFileURL { + NSWorkspace.shared.openApplication(at: url, configuration: configuration) + } else { + NSWorkspace.shared.open(url) + } + } - let openAction = { - if url.isFileURL { - NSWorkspace.shared.openApplication(at: url, configuration: configuration) + // Execute the action immediately or with a delay based on user interaction + if userClicked { + DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: openAction) } else { - NSWorkspace.shared.open(url) + openAction() } - } - - if userClicked { - DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: openAction) } else { - openAction() + if let actionButtonPath = FeatureVariables.actionButtonPath { + executeShellCommand(command: actionButtonPath, userClicked: userClicked) + } else { + LogManager.error("actionButtonPath is nil.", logger: uiLog) + } } postUpdateDeviceActions(userClicked: userClicked) From 3070823f6daebada4ccbc2b6b5194f7740e39015 Mon Sep 17 00:00:00 2001 From: Erik Gomez Date: Mon, 12 Feb 2024 10:02:23 -0600 Subject: [PATCH 2/6] fix shortcut key detections --- Nudge/UI/Main.swift | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/Nudge/UI/Main.swift b/Nudge/UI/Main.swift index 46264dc6..c38030bd 100644 --- a/Nudge/UI/Main.swift +++ b/Nudge/UI/Main.swift @@ -291,9 +291,13 @@ class AppDelegate: NSObject, NSApplicationDelegate { private func detectBannedShortcutKeys(with event: NSEvent) -> Bool { guard NSApplication.shared.isActive else { return false } switch event.modifierFlags.intersection(.deviceIndependentFlagsMask) { - // Disable CMD + W - closes the Nudge window and breaks it - case [.command] where event.charactersIgnoringModifiers == "w": - LogManager.warning("Nudge detected an attempt to close the application via CMD + W shortcut key.", logger: utilsLog) + // Disable CMD + H - Hides Nudge + case [.command] where event.charactersIgnoringModifiers == "h": + LogManager.warning("Nudge detected an attempt to hide the application via CMD + H shortcut key.", logger: utilsLog) + return true + // Disable CMD + M - Minimizes Nudge + case [.command] where event.charactersIgnoringModifiers == "m": + LogManager.warning("Nudge detected an attempt to minimize the application via CMD + M shortcut key.", logger: utilsLog) return true // Disable CMD + N - closes the Nudge window and breaks it case [.command] where event.charactersIgnoringModifiers == "n": @@ -303,26 +307,27 @@ class AppDelegate: NSObject, NSApplicationDelegate { case [.command] where event.charactersIgnoringModifiers == "q": LogManager.warning("Nudge detected an attempt to quit the application via CMD + Q shortcut key.", logger: utilsLog) return true - // Disable CMD + M - Minimizes Nudge - case [.command] where event.charactersIgnoringModifiers == "m": - LogManager.warning("Nudge detected an attempt to minimize the application via CMD + M shortcut key.", logger: utilsLog) - return true - // Disable CMD + H - Hides Nudge - case [.command] where event.charactersIgnoringModifiers == "h": - LogManager.warning("Nudge detected an attempt to hide the application via CMD + H shortcut key.", logger: utilsLog) - return true - // Disable CMD + Option + Esc (Force Quit Applications) - case [.command, .option] where event.charactersIgnoringModifiers == "\u{1b}": // Escape key - LogManager.warning("Nudge detected an attempt to open Force Quit Applications via CMD + Option + Esc.", logger: utilsLog) + // Disable CMD + W - closes the Nudge window and breaks it + case [.command] where event.charactersIgnoringModifiers == "w": + LogManager.warning("Nudge detected an attempt to close the application via CMD + W shortcut key.", logger: utilsLog) return true // Disable CMD + Option + M - Minimizes Nudge - case [.command, .option] where event.charactersIgnoringModifiers == "ยต": + case [.command, .option] where event.charactersIgnoringModifiers == "m": LogManager.warning("Nudge detected an attempt to minimise the application via CMD + Option + M shortcut key.", logger: utilsLog) return true // Disable CMD + Option + N - Add tabs to Nudge window - case [.command, .option] where event.charactersIgnoringModifiers == "~": + case [.command, .option] where event.charactersIgnoringModifiers == "n": LogManager.warning("Nudge detected an attempt to add tabs to the application via CMD + Option + N shortcut key.", logger: utilsLog) return true + // Disable CMD + Option + W - Close Window + case [.command, .option] where event.charactersIgnoringModifiers == "w": + LogManager.warning("Nudge detected an attempt to add tabs to the application via CMD + Option + W shortcut key.", logger: utilsLog) + return true + // Disable CMD + Option + Esc (Force Quit Applications) + case [.command, .option] where event.charactersIgnoringModifiers == "\u{1b}": // Escape key + // This doesn't work since Apple allows that shortcut to bypass the application's memory. + LogManager.warning("Nudge detected an attempt to open Force Quit Applications via CMD + Option + Esc.", logger: utilsLog) + return true default: // Don't care about any other shortcut keys return false From ebba5c71c639089896fafbd3f9510fb435af3005 Mon Sep 17 00:00:00 2001 From: Erik Gomez Date: Mon, 12 Feb 2024 11:08:16 -0600 Subject: [PATCH 3/6] fix ui state when launching nudge for deferrals --- Nudge/UI/Main.swift | 2 +- Nudge/Utilities/UILogic.swift | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Nudge/UI/Main.swift b/Nudge/UI/Main.swift index c38030bd..fb79ddf9 100644 --- a/Nudge/UI/Main.swift +++ b/Nudge/UI/Main.swift @@ -87,7 +87,6 @@ struct ContentView: View { window?.isMovable = false window?.collectionBehavior = [.fullScreenAuxiliary] window?.delegate = UIConstants.windowDelegate - // _ = needToActivateNudge() } private func handleNudgeActivation() { @@ -177,6 +176,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { handleCommandLineArguments() applyGracePeriodLogic() applyRandomDelayIfNecessary() + updateNudgeState() handleSoftwareUpdateRequirements() } diff --git a/Nudge/Utilities/UILogic.swift b/Nudge/Utilities/UILogic.swift index 788713cc..9510d303 100644 --- a/Nudge/Utilities/UILogic.swift +++ b/Nudge/Utilities/UILogic.swift @@ -354,7 +354,7 @@ private func updateDualQuitButtonRequirement() { nudgePrimaryState.requireDualQuitButtons = AppStateManager().requireDualQuitButtons() || nudgePrimaryState.userDeferrals > deferralThreshold } -private func updateNudgeState() { +func updateNudgeState() { nudgePrimaryState.deferralCountPastThreshold = nudgePrimaryState.userDeferrals > UserExperienceVariables.allowedDeferrals if nudgePrimaryState.userDeferrals > UserExperienceVariables.allowedDeferralsUntilForcedSecondaryQuitButton { @@ -363,7 +363,7 @@ private func updateNudgeState() { if nudgePrimaryState.deferralCountPastThreshold { if !nudgePrimaryState.hasLoggedDeferralCountPastThreshold { - LogManager.notice("allowedDeferrals has been passed", logger: uiLog) + LogManager.notice("allowedDeferrals has been passed: \(UserExperienceVariables.allowedDeferrals)", logger: uiLog) nudgePrimaryState.hasLoggedDeferralCountPastThreshold = true } } From 439ed50027252473d12ffaad31fc182c69027df3 Mon Sep 17 00:00:00 2001 From: Erik Gomez Date: Mon, 12 Feb 2024 11:08:54 -0600 Subject: [PATCH 4/6] fix observers for screen locked and app launches --- Nudge/UI/Defaults.swift | 1 + Nudge/UI/Main.swift | 42 ++++++++++++++++------------------------- 2 files changed, 17 insertions(+), 26 deletions(-) diff --git a/Nudge/UI/Defaults.swift b/Nudge/UI/Defaults.swift index f42c7f92..11314570 100644 --- a/Nudge/UI/Defaults.swift +++ b/Nudge/UI/Defaults.swift @@ -18,6 +18,7 @@ var nudgeLogState = LogState() struct Globals { static let bundle = Bundle.main static let bundleID = bundle.bundleIdentifier ?? "com.github.macadmins.Nudge" + static let dnc = DistributedNotificationCenter.default() static let nc = NotificationCenter.default static let snc = NSWorkspace.shared.notificationCenter // Preferences diff --git a/Nudge/UI/Main.swift b/Nudge/UI/Main.swift index fb79ddf9..ffc9fa82 100644 --- a/Nudge/UI/Main.swift +++ b/Nudge/UI/Main.swift @@ -210,12 +210,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } - // Observe screen locking. Maybe useful later - @objc func screenLocked(_ notification: Notification) { - nudgePrimaryState.screenCurrentlyLocked = true - LogManager.info("Screen was locked", logger: utilsLog) - } - @objc func screenParametersChanged(_ notification: Notification) { LogManager.info("Screen parameters changed - Notification Center", logger: utilsLog) UIUtilities().centerNudge() @@ -226,11 +220,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { UIUtilities().centerNudge() } - @objc func screenUnlocked(_ notification: Notification) { - nudgePrimaryState.screenCurrentlyLocked = false - LogManager.info("Screen was unlocked", logger: utilsLog) - } - @objc func spacesStateChanged(_ notification: Notification) { UIUtilities().centerNudge() LogManager.info("Spaces state changed", logger: utilsLog) @@ -340,7 +329,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { if !nudgeLogState.afterFirstLaunch && OptionalFeatureVariables.terminateApplicationsOnLaunch { terminateApplications() } - Globals.nc.addObserver( + Globals.snc.addObserver( self, selector: #selector(terminateApplicationSender(_:)), name: NSWorkspace.didLaunchApplicationNotification, @@ -451,8 +440,8 @@ class AppDelegate: NSObject, NSApplicationDelegate { private func setupNotificationObservers() { setupNotificationCenterObservers() - setupScreenLockObservers() setupScreenChangeObservers() + setupScreenLockObservers() setupWorkspaceNotificationCenterObservers() } @@ -483,19 +472,20 @@ class AppDelegate: NSObject, NSApplicationDelegate { } private func setupScreenLockObservers() { - Globals.nc.addObserver( - self, - selector: #selector(screenLocked), - name: NSNotification.Name("com.apple.screenIsLocked"), - object: nil - ) - - Globals.nc.addObserver( - self, - selector: #selector(screenUnlocked), - name: NSNotification.Name("com.apple.screenIsUnlocked"), - object: nil - ) + Globals.dnc.addObserver( + forName: NSNotification.Name("com.apple.screenIsLocked"), + object: nil, + queue: .main) { _ in + nudgePrimaryState.screenCurrentlyLocked = true + utilsLog.info("Screen was locked") + } + Globals.dnc.addObserver( + forName: NSNotification.Name("com.apple.screenIsUnlocked"), + object: nil, + queue: .main) { _ in + nudgePrimaryState.screenCurrentlyLocked = false + utilsLog.info("Screen was unlocked") + } } private func setupWorkspaceNotificationCenterObservers() { From ee4f093243dea276e4b819ca387c0d6b2dece8ce Mon Sep 17 00:00:00 2001 From: Erik Gomez Date: Mon, 12 Feb 2024 11:38:14 -0600 Subject: [PATCH 5/6] async all software-updates if set --- Nudge/UI/Main.swift | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Nudge/UI/Main.swift b/Nudge/UI/Main.swift index ffc9fa82..d72f40e7 100644 --- a/Nudge/UI/Main.swift +++ b/Nudge/UI/Main.swift @@ -536,11 +536,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { return } - let shouldRunAsynchronously = OptionalFeatureVariables.asynchronousSoftwareUpdate && - !AppStateManager().requireMajorUpgrade() && - !DateManager().pastRequiredInstallationDate() - - if shouldRunAsynchronously { + if OptionalFeatureVariables.asynchronousSoftwareUpdate { runUpdateAsynchronously() } else { SoftwareUpdate().download() From 2eb1e8ee8ebaaa1914b40d72a162ee5fe69ae977 Mon Sep 17 00:00:00 2001 From: Erik Gomez Date: Mon, 12 Feb 2024 14:14:28 -0600 Subject: [PATCH 6/6] fix the blur logic --- Nudge/UI/Main.swift | 1 + Nudge/Utilities/UILogic.swift | 2 +- Nudge/Utilities/Utils.swift | 35 +++++++++++++++++++++++------------ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/Nudge/UI/Main.swift b/Nudge/UI/Main.swift index d72f40e7..fe157d8d 100644 --- a/Nudge/UI/Main.swift +++ b/Nudge/UI/Main.swift @@ -64,6 +64,7 @@ struct ContentView: View { .edgesIgnoringSafeArea(.all) .onAppear { initialLaunchLogic() + handleNudgeActivation() updateUI() } .onReceive(Intervals.nudgeRefreshCycleTimer) { _ in diff --git a/Nudge/Utilities/UILogic.swift b/Nudge/Utilities/UILogic.swift index 9510d303..29e48116 100644 --- a/Nudge/Utilities/UILogic.swift +++ b/Nudge/Utilities/UILogic.swift @@ -191,7 +191,7 @@ private func logUserSessionDeferrals(resetCount: Bool = false) { } func needToActivateNudge() -> Bool { - if NSApplication.shared.isActive { + if NSApplication.shared.isActive && nudgeLogState.afterFirstLaunch { LogManager.notice("Nudge is currently the frontmostApplication", logger: uiLog) return false } diff --git a/Nudge/Utilities/Utils.swift b/Nudge/Utilities/Utils.swift index 33ed45cb..08d2c8e9 100644 --- a/Nudge/Utilities/Utils.swift +++ b/Nudge/Utilities/Utils.swift @@ -49,17 +49,24 @@ struct AppStateManager { } private func applyBackgroundBlur(to window: NSWindow) { - // load the blur background and send it to the back if we are past the required install date - LogManager.info("Enabling blurred background", logger: uiLog) - nudgePrimaryState.backgroundBlur.removeAll() + // Figure out all the screens upon Nudge launching UIConstants.screens.forEach { screen in - let blurWindowController = BackgroundBlurWindowController() - nudgePrimaryState.backgroundBlur.append(blurWindowController) - blurWindowController.close() - blurWindowController.loadWindow() - blurWindowController.showWindow(nil) + loopedScreen = screen + } + // load the blur background and send it to the back if we are past the required install date + if nudgePrimaryState.backgroundBlur.isEmpty { + LogManager.info("Enabling blurred background", logger: uiLog) + UIConstants.screens.forEach { screen in + let blurWindowController = BackgroundBlurWindowController() + blurWindowController.loadWindow() + blurWindowController.showWindow(nil) + loopedScreen = screen + nudgePrimaryState.backgroundBlur.append(blurWindowController) + } + window.level = NSWindow.Level(rawValue: Int(CGWindowLevelForKey(.maximumWindow) + 1)) + } else { + LogManager.info("Background blur currently set", logger: uiLog) } - window.level = .floating } private func calculateNewRequiredInstallationDateIfNeeded(currentDate: Date, gracePeriodPathCreationDate: Date) -> Date { @@ -849,10 +856,14 @@ struct UIUtilities { if userClicked { LogManager.notice("User clicked updateDevice", logger: uiLog) // Remove forced blur and reset window level - nudgePrimaryState.backgroundBlur.forEach { blurWindowController in - blurWindowController.close() + if !nudgePrimaryState.backgroundBlur.isEmpty { + nudgePrimaryState.backgroundBlur.forEach { blurWindowController in + uiLog.notice("\("Attempting to remove forced blur", privacy: .public)") + blurWindowController.close() + nudgePrimaryState.backgroundBlur.removeAll() + } + NSApp.windows.first?.level = .normal } - NSApp.windows.first?.level = .normal } else { LogManager.notice("Synthetically clicked updateDevice due to allowedDeferral count", logger: uiLog) }