@@ -23,12 +23,14 @@ final class MenuBarLayoutManager {
2323
2424 func hide( _ items: [ MenuBarItem ] , relativeTo controlFrame: CGRect , targetAttempt: Int = 0 , completion: @escaping ( Int ) -> Void = { _ in } ) {
2525 guard isEnabled else { completion ( 0 ) ; return }
26- restoreProtectedSystemItems { [ weak self] _ in
27- self ? . hideAfterRestoringProtectedItems ( items, relativeTo: controlFrame, targetAttempt: targetAttempt, completion: completion)
26+ let managedWindowIDs = Set ( items. compactMap ( \. windowID) )
27+ let managedSystemNames = protectedNames ( for: items)
28+ restoreProtectedSystemItems ( excluding: managedWindowIDs, excludingSystemNames: managedSystemNames) { [ weak self] _ in
29+ self ? . hideAfterRestoringProtectedItems ( items, relativeTo: controlFrame, targetAttempt: targetAttempt, managedSystemNames: managedSystemNames, completion: completion)
2830 }
2931 }
3032
31- private func hideAfterRestoringProtectedItems( _ items: [ MenuBarItem ] , relativeTo controlFrame: CGRect , targetAttempt: Int , completion: @escaping ( Int ) -> Void ) {
33+ private func hideAfterRestoringProtectedItems( _ items: [ MenuBarItem ] , relativeTo controlFrame: CGRect , targetAttempt: Int , managedSystemNames : Set < String > , completion: @escaping ( Int ) -> Void ) {
3234 guard isEnabled else { completion ( 0 ) ; return }
3335 guard let target = hiddenTargetWindow ( ) else {
3436 guard targetAttempt < 10 else {
@@ -38,19 +40,19 @@ final class MenuBarLayoutManager {
3840 }
3941 logger. info ( " Control window pending; retrying attempt \( targetAttempt + 1 , privacy: . public) " )
4042 DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.15 ) { [ weak self] in
41- self ? . hideAfterRestoringProtectedItems ( items, relativeTo: controlFrame, targetAttempt: targetAttempt + 1 , completion: completion)
43+ self ? . hideAfterRestoringProtectedItems ( items, relativeTo: controlFrame, targetAttempt: targetAttempt + 1 , managedSystemNames : managedSystemNames , completion: completion)
4244 }
4345 return
4446 }
4547 logger. info ( " Hiding \( items. count, privacy: . public) items relative to window \( target. id, privacy: . public) " )
46- let protectedWindowIDs = Set ( windowRecords ( ) . filter { MenuBarSystemItemClassifier . isProtected ( $0. title, owner: $0. owner) } . map ( \. id) )
4748 let managed = items. filter {
48- ! $0. isProtectedSystemItem && $0 . windowID != target. id && $0 . windowID . map ( protectedWindowIDs . contains ) != true
49+ $0. windowID != target. id
4950 } . filter ( needsHiding)
5051 publishCurrentFrames ( for: managed)
5152 hideSequentially ( managed, index: 0 , movedCount: 0 ) { [ weak self] movedCount in
5253 self ? . publishCurrentFrames ( for: managed)
53- self ? . restoreProtectedSystemItems { _ in completion ( movedCount) }
54+ let managedWindowIDs = Set ( managed. compactMap ( \. windowID) )
55+ self ? . restoreProtectedSystemItems ( excluding: managedWindowIDs, excludingSystemNames: managedSystemNames) { _ in completion ( movedCount) }
5456 }
5557 }
5658
@@ -122,24 +124,40 @@ final class MenuBarLayoutManager {
122124 move ( item, relativeTo: target. id, placement: . left) { _ in }
123125 }
124126
125- func restoreProtectedSystemItems( attempt: Int = 0 , completion: @escaping ( Int ) -> Void = { _ in } ) {
127+ func restoreProtectedSystemItems( attempt: Int = 0 , excluding excludedWindowIDs : Set < CGWindowID > = [ ] , excludingSystemNames : Set < String > = [ ] , completion: @escaping ( Int ) -> Void = { _ in } ) {
126128 guard let target = controlTargetWindow ( ) else {
127129 guard attempt < 10 else {
128130 logger. error ( " Control target unavailable while restoring protected items " )
129131 completion ( 0 )
130132 return
131133 }
132134 DispatchQueue . main. asyncAfter ( deadline: . now( ) + 0.5 ) { [ weak self] in
133- self ? . restoreProtectedSystemItems ( attempt: attempt + 1 , completion: completion)
135+ self ? . restoreProtectedSystemItems ( attempt: attempt + 1 , excluding : excludedWindowIDs , excludingSystemNames : excludingSystemNames , completion: completion)
134136 }
135137 return
136138 }
137139 let hidden = windowRecords ( ) . filter {
138- MenuBarSystemItemClassifier . isProtected ( $0. title, owner: $0. owner) && $0. frame. maxX <= 0
140+ MenuBarSystemItemClassifier . isProtected ( $0. title, owner: $0. owner) &&
141+ $0. frame. maxX <= 0 &&
142+ !excludedWindowIDs. contains ( $0. id) &&
143+ !excludingSystemNames. contains ( $0. title) &&
144+ !excludingSystemNames. contains ( MenuBarSystemItemClassifier . canonicalName ( $0. title, owner: $0. owner) ) &&
145+ !( MenuBarSystemItemClassifier . isGenericControlCenterItem ( $0. title, owner: $0. owner) && excludingSystemNames. contains ( " Control Center Item " ) )
139146 }
140147 restoreProtectedSequentially ( hidden, index: 0 , target: target, movedCount: 0 , completion: completion)
141148 }
142149
150+ private func protectedNames( for items: [ MenuBarItem ] ) -> Set < String > {
151+ var names = Set < String > ( )
152+ for item in items where item. isProtectedSystemItem {
153+ names. insert ( item. title)
154+ if item. title == " Control Center Item " {
155+ names. insert ( " Control Center Item " )
156+ }
157+ }
158+ return names
159+ }
160+
143161 private func hideSequentially( _ items: [ MenuBarItem ] , index: Int , movedCount: Int , completion: @escaping ( Int ) -> Void ) {
144162 guard index < items. count else { completion ( movedCount) ; return }
145163 guard let target = hiddenTargetWindow ( ) else { completion ( movedCount) ; return }
0 commit comments