From 9585c926293bc0660a54879624655f1e59b344c3 Mon Sep 17 00:00:00 2001 From: lwouis Date: Thu, 14 Nov 2024 14:07:11 +0100 Subject: [PATCH] fix: permission callout could show even with permission granted closes #3801 --- src/logic/SystemPermissions.swift | 8 ++------ src/ui/Menubar.swift | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/logic/SystemPermissions.swift b/src/logic/SystemPermissions.swift index d3dd8bc9..a0086623 100644 --- a/src/logic/SystemPermissions.swift +++ b/src/logic/SystemPermissions.swift @@ -59,9 +59,7 @@ class SystemPermissions { let accessibility = accessibilityIsGranted() let screenRecording = screenRecordingIsGranted() logger.d(accessibility, screenRecording, preStartupPermissionsPassed) - Menubar.permissionCalloutMenuItems?.forEach { - $0.isHidden = screenRecording != .skipped - } + Menubar.togglePermissionCallout(screenRecording == .skipped) if accessibility == .notGranted { App.app.restart() } @@ -82,9 +80,7 @@ class SystemPermissions { let accessibility = accessibilityIsGranted() let screenRecording = screenRecordingIsGranted() logger.d(accessibility, screenRecording, preStartupPermissionsPassed) - Menubar.permissionCalloutMenuItems?.forEach { - $0.isHidden = screenRecording != .skipped - } + Menubar.togglePermissionCallout(screenRecording == .skipped) if accessibility != App.app.permissionsWindow?.accessibilityView?.permissionStatus { App.app.permissionsWindow?.accessibilityView.updatePermissionStatus(accessibility) } diff --git a/src/ui/Menubar.swift b/src/ui/Menubar.swift index c67a6df7..7eec8f86 100644 --- a/src/ui/Menubar.swift +++ b/src/ui/Menubar.swift @@ -12,8 +12,6 @@ class Menubar { permissionCalloutMenuItem.view = PermissionCallout() let calloutSeparator = NSMenuItem.separator() permissionCalloutMenuItems = [permissionCalloutMenuItem, calloutSeparator] - menu.addItem(permissionCalloutMenuItem) - menu.addItem(calloutSeparator) menu.addItem( withTitle: String(format: NSLocalizedString("About %@", comment: "Menubar option. %@ is AltTab"), App.name), action: #selector(App.app.showAboutTab), @@ -55,6 +53,18 @@ class Menubar { menubarIconCallback(nil) } + // NSMenuItem.isHidden isn't reliable with custom views. We add/remove to hide/show these items + static func togglePermissionCallout(_ show: Bool) { + permissionCalloutMenuItems?.enumerated().forEach { offset, element in + if show && !menu.items.contains(element) { + menu.insertItem(element, at: offset) + } + if !show && menu.items.contains(element) { + menu.removeItem(element) + } + } + } + @objc static func statusItemOnClick() { // NSApp.currentEvent == nil if the icon is "clicked" through VoiceOver if let type = NSApp.currentEvent?.type, type != .leftMouseDown {