Last active
April 2, 2025 17:37
-
-
Save joethephish/1e1a295ad9bc4c6d5e278549d41debda to your computer and use it in GitHub Desktop.
Substage's flippy out command bar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import AppKit | |
import SwiftUI | |
class PromptWindowController: NSWindowController, NSWindowDelegate { | |
// Essential properties | |
private var promptWindowContext: PromptWindowContext | |
private let finderContext: FinderContext | |
init() { | |
self.finderContext = FinderContext() | |
self.promptWindowContext = PromptWindowContext() | |
super.init(window: nil) | |
// Initialize the prompt window context | |
self.promptWindowContext = PromptWindowContext( | |
window: nil, | |
onDidAnimateClosed: { self.close() }, | |
onUseWithoutSubscription: {} | |
) | |
// Create and configure the window | |
let window = NSWindow( | |
contentRect: NSRect(x: 100, y: 400, width: 800, height: promptBarHeight), | |
styleMask: [.titled, .closable, .miniaturizable, .resizable], | |
backing: .buffered, | |
defer: false | |
) | |
window.delegate = self | |
// Set up the SwiftUI view | |
let promptWindowControllerView = PromptWindowControllerView() | |
.environment(\.finderContext, finderContext) | |
.environment(\.promptWindowContext, promptWindowContext) | |
// Configure the hosting controller | |
let hostingController = NSHostingController(rootView: promptWindowControllerView) | |
hostingController.sizingOptions = NSHostingSizingOptions.maxSize | |
hostingController.view.translatesAutoresizingMaskIntoConstraints = false | |
window.contentViewController = hostingController | |
// Style the window | |
window.hasShadow = true | |
window.backgroundColor = .clear | |
window.isOpaque = false | |
// Hide standard window buttons | |
window.standardWindowButton(.closeButton)?.isHidden = true | |
window.standardWindowButton(.miniaturizeButton)?.isHidden = true | |
window.standardWindowButton(.zoomButton)?.isHidden = true | |
window.styleMask.remove(.resizable) | |
window.titleVisibility = .hidden | |
window.titlebarAppearsTransparent = true | |
window.setContentSize(NSSize(width: 800, height: promptBarHeight)) | |
self.window = window | |
// Show the window and position it | |
window.makeKeyAndOrderFront(nil) | |
updatePosition() | |
// Start monitoring Finder windows | |
finderContext.startRefreshLoop() | |
} | |
required init?(coder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
// MARK: - Window Positioning | |
func updatePosition() { | |
if let bounds = FinderWindowTracker.topFinderWindowBounds() { | |
attachWindow(belowFinderWindowWithBounds: bounds) | |
} | |
} | |
func attachWindow(belowFinderWindowWithBounds bounds: CGRect) { | |
guard let window = self.window else { return } | |
if let targetFrame = calculateTargetWindowFrame(window: window, belowFinderWindowWithBounds: bounds) { | |
window.setFrame(targetFrame, display: false) | |
promptWindowContext.windowWidth = targetFrame.width | |
} | |
// Set window level based on whether Finder is active | |
if let activeApp = NSWorkspace.shared.frontmostApplication { | |
window.level = activeApp.bundleIdentifier == "com.apple.finder" ? .floating : .normal | |
} else { | |
window.level = .normal | |
} | |
} | |
func calculateTargetWindowFrame(window: NSWindow, belowFinderWindowWithBounds bounds: CGRect) -> NSRect? { | |
// Find the primary screen | |
let primaryScreen = NSScreen.screens.first { $0.frame.origin == .zero } | |
guard let primaryScreen else { return nil } | |
// Convert Finder window bounds to NSWindow coordinate system (origin at bottom left) | |
let finderWindowBounds = CGRect( | |
x: bounds.origin.x, | |
y: primaryScreen.frame.height - bounds.origin.y - bounds.height, | |
width: bounds.width, | |
height: bounds.height | |
) | |
// Find which screen the Finder window is on | |
let finderWindowCenterX = finderWindowBounds.origin.x + finderWindowBounds.width / 2 | |
let pointBelowTopEdge = finderWindowBounds.origin.y + finderWindowBounds.height - 100 | |
let anchorPoint = CGPoint(x: finderWindowCenterX, y: pointBelowTopEdge) | |
let screenContainingCenter = NSScreen.screens.first { screen in | |
return screen.frame.contains(anchorPoint) | |
} | |
let targetScreen = screenContainingCenter ?? primaryScreen | |
// Position just below the finder window | |
let padding: CGFloat = 2 | |
let hiddenTitleBarHeight: CGFloat = 14 | |
let yPosition = finderWindowBounds.origin.y - promptBarHeight - padding + hiddenTitleBarHeight | |
let targetFrame = NSRect( | |
x: finderWindowBounds.origin.x, | |
y: yPosition, | |
width: bounds.width, | |
height: promptBarHeight | |
) | |
// Ensure window is not obscured by the dock | |
let targetVisibleFrame = targetScreen.visibleFrame | |
let clampedFrame = NSRect( | |
x: targetFrame.minX - transparentMarginWidth, | |
y: max(targetVisibleFrame.minY, targetFrame.minY), | |
width: targetFrame.width + 2*transparentMarginWidth, | |
height: targetFrame.height | |
) | |
return clampedFrame | |
} | |
// MARK: - Window Delegate Methods | |
func windowWillClose(_ notification: Notification) { | |
finderContext.stopRefreshLoop() | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
struct PromptWindowControllerView: View { | |
@State var flippedOpen: Bool = false | |
@Environment(\.promptWindowContext) var promptWindowContext | |
var body: some View { | |
VStack(spacing:0) { | |
HStack(spacing:0) { | |
PromptWindowContentView() | |
.frame(width:promptWindowContext.windowWidth - 2*transparentMarginWidth) | |
.clipped() | |
.background( | |
UnevenRoundedRectangle(topLeadingRadius: 11, | |
bottomLeadingRadius: 18, | |
bottomTrailingRadius: 18, | |
topTrailingRadius: 11) | |
.fill(Material.thickMaterial) | |
) | |
.rotation3DEffect( | |
.degrees(flippedOpen ? 0 : -90), | |
axis: (x: 1.0, y: 0.0, z: 0.0), | |
anchor:.top | |
) | |
.opacity(flippedOpen ? 1.0 : 0.0) | |
.onAppear { | |
withAnimation(.bouncy(duration: 0.4, extraBounce: 0.4)) { | |
flippedOpen = true | |
} | |
} | |
.onChange(of: promptWindowContext.shouldAnimateClosed) { | |
if promptWindowContext.shouldAnimateClosed { | |
withAnimation(.easeIn(duration: 0.2)) { | |
flippedOpen = false | |
} | |
} | |
} | |
.padding(.horizontal, transparentMarginWidth) | |
} | |
Spacer().frame(height: 20) | |
} | |
.frame(height: promptBarHeight) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment