|
| 1 | +// |
| 2 | +// CodeEditWindowController+Panels.swift |
| 3 | +// CodeEdit |
| 4 | +// |
| 5 | +// Created by Simon Kudsk on 11/05/2025. |
| 6 | +// |
| 7 | + |
| 8 | +import SwiftUI |
| 9 | + |
| 10 | +extension CodeEditWindowController { |
| 11 | + @objc |
| 12 | + func objcToggleFirstPanel() { |
| 13 | + toggleFirstPanel(shouldAnimate: true) |
| 14 | + } |
| 15 | + |
| 16 | + /// Toggles the navigator pane, optionally without animation. |
| 17 | + func toggleFirstPanel(shouldAnimate: Bool = true) { |
| 18 | + guard let firstSplitView = splitViewController?.splitViewItems.first else { return } |
| 19 | + |
| 20 | + if shouldAnimate { |
| 21 | + // Standard animated toggle |
| 22 | + firstSplitView.animator().isCollapsed.toggle() |
| 23 | + } else { |
| 24 | + // Instant toggle (no animation) |
| 25 | + firstSplitView.isCollapsed.toggle() |
| 26 | + } |
| 27 | + |
| 28 | + splitViewController?.saveNavigatorCollapsedState(isCollapsed: firstSplitView.isCollapsed) |
| 29 | + } |
| 30 | + |
| 31 | + @objc |
| 32 | + func objcToggleLastPanel() { |
| 33 | + toggleLastPanel(shouldAnimate: true) |
| 34 | + } |
| 35 | + |
| 36 | + func toggleLastPanel(shouldAnimate: Bool = true) { |
| 37 | + guard let lastSplitView = splitViewController?.splitViewItems.last else { |
| 38 | + return |
| 39 | + } |
| 40 | + |
| 41 | + if shouldAnimate { |
| 42 | + // Standard animated toggle |
| 43 | + NSAnimationContext.runAnimationGroup { _ in |
| 44 | + lastSplitView.animator().isCollapsed.toggle() |
| 45 | + } |
| 46 | + } else { |
| 47 | + // Instant toggle (no animation) |
| 48 | + lastSplitView.isCollapsed.toggle() |
| 49 | + } |
| 50 | + |
| 51 | + splitViewController?.saveInspectorCollapsedState(isCollapsed: lastSplitView.isCollapsed) |
| 52 | + } |
| 53 | + |
| 54 | + // PanelDescriptor, used for an array of panels, for use with "Hide interface". |
| 55 | + private struct PanelDescriptor { |
| 56 | + /// Returns the current `isCollapsed` value for the panel. |
| 57 | + let isCollapsed: () -> Bool |
| 58 | + /// Returns the last stored previous state (or `nil` if none). |
| 59 | + let getPrevCollapsed: () -> Bool? |
| 60 | + /// Stores a new previous state (`nil` to clear). |
| 61 | + let setPrevCollapsed: (Bool?) -> Void |
| 62 | + /// Performs the actual toggle action for the panel. |
| 63 | + let toggle: () -> Void |
| 64 | + } |
| 65 | + |
| 66 | + // The panels which "Hide interface" should interact with. |
| 67 | + private var panels: [PanelDescriptor] { |
| 68 | + [ |
| 69 | + PanelDescriptor( |
| 70 | + isCollapsed: { self.navigatorCollapsed }, |
| 71 | + getPrevCollapsed: { self.prevNavigatorCollapsed }, |
| 72 | + setPrevCollapsed: { self.prevNavigatorCollapsed = $0 }, |
| 73 | + toggle: { self.toggleFirstPanel(shouldAnimate: false) } |
| 74 | + ), |
| 75 | + PanelDescriptor( |
| 76 | + isCollapsed: { self.inspectorCollapsed }, |
| 77 | + getPrevCollapsed: { self.prevInspectorCollapsed }, |
| 78 | + setPrevCollapsed: { self.prevInspectorCollapsed = $0 }, |
| 79 | + toggle: { self.toggleLastPanel(shouldAnimate: false) } |
| 80 | + ), |
| 81 | + PanelDescriptor( |
| 82 | + isCollapsed: { self.workspace?.utilityAreaModel?.isCollapsed ?? true }, |
| 83 | + getPrevCollapsed: { self.prevUtilityAreaCollapsed }, |
| 84 | + setPrevCollapsed: { self.prevUtilityAreaCollapsed = $0 }, |
| 85 | + toggle: { CommandManager.shared.executeCommand("open.drawer.no.animation") } |
| 86 | + ), |
| 87 | + PanelDescriptor( |
| 88 | + isCollapsed: { self.toolbarCollapsed }, |
| 89 | + getPrevCollapsed: { self.prevToolbarCollapsed }, |
| 90 | + setPrevCollapsed: { self.prevToolbarCollapsed = $0 }, |
| 91 | + toggle: { self.toggleToolbar() } |
| 92 | + ) |
| 93 | + ] |
| 94 | + } |
| 95 | + |
| 96 | + /// Returns `true` if at least one panel that was visible is still collapsed, meaning the interface is still hidden |
| 97 | + func isInterfaceStillHidden() -> Bool { |
| 98 | + // Some panels do not yet have a remembered state |
| 99 | + if panels.contains(where: { $0.getPrevCollapsed() == nil }) { |
| 100 | + // Hidden only if all panels are collapsed |
| 101 | + return panels.allSatisfy { $0.isCollapsed() } |
| 102 | + } |
| 103 | + |
| 104 | + // All panels have a remembered state. Check if any that were visible are still collapsed |
| 105 | + let stillHidden = panels.contains { descriptor in |
| 106 | + guard let prev = descriptor.getPrevCollapsed() else { return false } |
| 107 | + return !prev && descriptor.isCollapsed() |
| 108 | + } |
| 109 | + |
| 110 | + // If the interface has been restored, reset the remembered states |
| 111 | + if !stillHidden { |
| 112 | + resetStoredInterfaceCollapseState() |
| 113 | + } |
| 114 | + |
| 115 | + return stillHidden |
| 116 | + } |
| 117 | + |
| 118 | + /// Function for toggling the interface elements on or off |
| 119 | + /// |
| 120 | + /// - Parameter shouldHide: Pass `true` to hide all interface panels (and remember their current states), |
| 121 | + /// or `false` to restore them to how they were before hiding. |
| 122 | + func toggleInterface(shouldHide: Bool) { |
| 123 | + // Store the current layout before hiding |
| 124 | + if shouldHide { |
| 125 | + storeInterfaceCollapseState() |
| 126 | + } |
| 127 | + |
| 128 | + // Iterate over all panels and update their state as needed |
| 129 | + for panel in panels { |
| 130 | + let targetState = determineDesiredCollapseState( |
| 131 | + shouldHide: shouldHide, |
| 132 | + currentlyCollapsed: panel.isCollapsed(), |
| 133 | + previouslyCollapsed: panel.getPrevCollapsed(), |
| 134 | + ) |
| 135 | + if panel.isCollapsed() != targetState { |
| 136 | + panel.toggle() |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /// Calculates the collapse state an interface element should have after a hide / show toggle. |
| 142 | + /// - Parameters: |
| 143 | + /// - shouldHide: `true` when we’re hiding the whole interface. |
| 144 | + /// - currentlyCollapsed: The panels current state |
| 145 | + /// - previouslyCollapsed: The state we saved the last time we hid the UI, if any. |
| 146 | + /// - Returns: `true` for visible element, `false` for collapsed element |
| 147 | + func determineDesiredCollapseState(shouldHide: Bool, currentlyCollapsed: Bool, previouslyCollapsed: Bool?) -> Bool { |
| 148 | + // If ShouldHide, everything should close |
| 149 | + if shouldHide { |
| 150 | + return true |
| 151 | + } |
| 152 | + |
| 153 | + // If not hiding, and not currently collapsed, the panel should remain as such. |
| 154 | + if !currentlyCollapsed { |
| 155 | + return false |
| 156 | + } |
| 157 | + |
| 158 | + // If the panel is currently collapsed and we are "showing" or "restoring": |
| 159 | + // Option 1: Restore to its previously remembered state if available. |
| 160 | + // Option 2: If no previously remembered state, default to making it visible (not collapsed). |
| 161 | + return previouslyCollapsed ?? false |
| 162 | + } |
| 163 | + |
| 164 | + /// Function for storing the current interface visibility states |
| 165 | + func storeInterfaceCollapseState() { |
| 166 | + for panel in panels { |
| 167 | + panel.setPrevCollapsed(panel.isCollapsed()) |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /// Function for resetting the stored interface visibility states |
| 172 | + func resetStoredInterfaceCollapseState() { |
| 173 | + for panel in panels { |
| 174 | + panel.setPrevCollapsed(nil) |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments