|
| 1 | +// |
| 2 | +// BezelNotification.swift |
| 3 | +// CodeEditSourceEditor |
| 4 | +// |
| 5 | +// Created by Austin Condiff on 3/17/25. |
| 6 | +// |
| 7 | + |
| 8 | +import AppKit |
| 9 | +import SwiftUI |
| 10 | + |
| 11 | +/// A utility class for showing temporary bezel notifications with SF Symbols |
| 12 | +final class BezelNotification { |
| 13 | + private static var shared = BezelNotification() |
| 14 | + private var window: NSWindow? |
| 15 | + private var hostingView: NSHostingView<BezelView>? |
| 16 | + private var frameObserver: NSObjectProtocol? |
| 17 | + private var targetView: NSView? |
| 18 | + private var hideTimer: DispatchWorkItem? |
| 19 | + |
| 20 | + private init() {} |
| 21 | + |
| 22 | + deinit { |
| 23 | + if let observer = frameObserver { |
| 24 | + NotificationCenter.default.removeObserver(observer) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + /// Shows a bezel notification with the given SF Symbol name |
| 29 | + /// - Parameters: |
| 30 | + /// - symbolName: The name of the SF Symbol to display |
| 31 | + /// - over: The view to center the bezel over |
| 32 | + /// - duration: How long to show the bezel for (defaults to 0.75 seconds) |
| 33 | + static func show(symbolName: String, over view: NSView, duration: TimeInterval = 0.75) { |
| 34 | + shared.showBezel(symbolName: symbolName, over: view, duration: duration) |
| 35 | + } |
| 36 | + |
| 37 | + private func showBezel(symbolName: String, over view: NSView, duration: TimeInterval) { |
| 38 | + // Cancel any existing hide timer |
| 39 | + hideTimer?.cancel() |
| 40 | + hideTimer = nil |
| 41 | + |
| 42 | + // Close existing window if any |
| 43 | + cleanup() |
| 44 | + |
| 45 | + self.targetView = view |
| 46 | + |
| 47 | + // Create the window and view |
| 48 | + let bezelContent = BezelView(symbolName: symbolName) |
| 49 | + let hostingView = NSHostingView(rootView: bezelContent) |
| 50 | + self.hostingView = hostingView |
| 51 | + |
| 52 | + let window = NSPanel( |
| 53 | + contentRect: .zero, |
| 54 | + styleMask: [.borderless, .nonactivatingPanel, .hudWindow], |
| 55 | + backing: .buffered, |
| 56 | + defer: true |
| 57 | + ) |
| 58 | + window.backgroundColor = .clear |
| 59 | + window.isOpaque = false |
| 60 | + window.hasShadow = false |
| 61 | + window.level = .floating |
| 62 | + window.contentView = hostingView |
| 63 | + window.isMovable = false |
| 64 | + window.isReleasedWhenClosed = false |
| 65 | + |
| 66 | + // Make it a child window that moves with the parent |
| 67 | + if let parentWindow = view.window { |
| 68 | + parentWindow.addChildWindow(window, ordered: .above) |
| 69 | + } |
| 70 | + |
| 71 | + self.window = window |
| 72 | + |
| 73 | + // Size and position the window |
| 74 | + let size = NSSize(width: 110, height: 110) |
| 75 | + hostingView.frame.size = size |
| 76 | + |
| 77 | + // Initial position |
| 78 | + updateBezelPosition() |
| 79 | + |
| 80 | + // Observe frame changes |
| 81 | + frameObserver = NotificationCenter.default.addObserver( |
| 82 | + forName: NSView.frameDidChangeNotification, |
| 83 | + object: view, |
| 84 | + queue: .main |
| 85 | + ) { [weak self] _ in |
| 86 | + self?.updateBezelPosition() |
| 87 | + } |
| 88 | + |
| 89 | + // Show immediately without fade |
| 90 | + window.alphaValue = 1 |
| 91 | + window.orderFront(nil) |
| 92 | + |
| 93 | + // Schedule hide |
| 94 | + let timer = DispatchWorkItem { [weak self] in |
| 95 | + self?.dismiss() |
| 96 | + } |
| 97 | + self.hideTimer = timer |
| 98 | + DispatchQueue.main.asyncAfter(deadline: .now() + duration, execute: timer) |
| 99 | + } |
| 100 | + |
| 101 | + private func updateBezelPosition() { |
| 102 | + guard let window = window, |
| 103 | + let view = targetView else { return } |
| 104 | + |
| 105 | + let size = NSSize(width: 110, height: 110) |
| 106 | + |
| 107 | + // Position relative to the view's content area |
| 108 | + let visibleRect: NSRect |
| 109 | + if let scrollView = view.enclosingScrollView { |
| 110 | + // Get the visible rect in the scroll view's coordinate space |
| 111 | + visibleRect = scrollView.contentView.visibleRect |
| 112 | + } else { |
| 113 | + visibleRect = view.bounds |
| 114 | + } |
| 115 | + |
| 116 | + // Convert visible rect to window coordinates |
| 117 | + let viewFrameInWindow = view.enclosingScrollView?.contentView.convert(visibleRect, to: nil) |
| 118 | + ?? view.convert(visibleRect, to: nil) |
| 119 | + guard let screenFrame = view.window?.convertToScreen(viewFrameInWindow) else { return } |
| 120 | + |
| 121 | + // Calculate center position relative to the visible content area |
| 122 | + let xPos = screenFrame.midX - (size.width / 2) |
| 123 | + let yPos = screenFrame.midY - (size.height / 2) |
| 124 | + |
| 125 | + // Update frame |
| 126 | + let bezelFrame = NSRect(origin: NSPoint(x: xPos, y: yPos), size: size) |
| 127 | + window.setFrame(bezelFrame, display: true) |
| 128 | + } |
| 129 | + |
| 130 | + private func cleanup() { |
| 131 | + // Cancel any existing hide timer |
| 132 | + hideTimer?.cancel() |
| 133 | + hideTimer = nil |
| 134 | + |
| 135 | + // Remove frame observer |
| 136 | + if let observer = frameObserver { |
| 137 | + NotificationCenter.default.removeObserver(observer) |
| 138 | + frameObserver = nil |
| 139 | + } |
| 140 | + |
| 141 | + // Remove child window relationship |
| 142 | + if let window = window, let parentWindow = window.parent { |
| 143 | + parentWindow.removeChildWindow(window) |
| 144 | + } |
| 145 | + |
| 146 | + // Close and clean up window |
| 147 | + window?.orderOut(nil) // Ensure window is removed from screen |
| 148 | + window?.close() |
| 149 | + window = nil |
| 150 | + |
| 151 | + // Clean up hosting view |
| 152 | + hostingView?.removeFromSuperview() |
| 153 | + hostingView = nil |
| 154 | + |
| 155 | + // Clear target view reference |
| 156 | + targetView = nil |
| 157 | + } |
| 158 | + |
| 159 | + private func dismiss() { |
| 160 | + guard let window = window else { return } |
| 161 | + |
| 162 | + NSAnimationContext.runAnimationGroup({ context in |
| 163 | + context.duration = 0.15 |
| 164 | + window.animator().alphaValue = 0 |
| 165 | + }, completionHandler: { [weak self] in |
| 166 | + self?.cleanup() |
| 167 | + }) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +/// The SwiftUI view for the bezel content |
| 172 | +private struct BezelView: View { |
| 173 | + let symbolName: String |
| 174 | + |
| 175 | + var body: some View { |
| 176 | + Image(systemName: symbolName) |
| 177 | + .imageScale(.large) |
| 178 | + .font(.system(size: 56, weight: .thin)) |
| 179 | + .foregroundStyle(.secondary) |
| 180 | + .frame(width: 110, height: 110) |
| 181 | + .background(.ultraThinMaterial) |
| 182 | + .clipShape(RoundedRectangle(cornerSize: CGSize(width: 18.0, height: 18.0))) |
| 183 | + } |
| 184 | +} |
0 commit comments