cmux/Sources/Panels/Panel.swift
Lawrence Chen ec10dfdaec
Add browser find focus debug logs (#1162)
* Add browser find focus debug logs

* Allow browser find bar focus in portal host

* Add split and terminal find debug logs

* Avoid stealing search focus across splits

* Generalize panel focus restore intent

* Unify split focus intent activation

* Address focus restore review feedback

* Yield inactive panel focus before restore

* Gate browser find focus retries by generation

* Avoid repeated browser focus invalidation

* Keep browser find ownership while find bar is open
2026-03-10 19:31:52 -07:00

152 lines
4.3 KiB
Swift

import Foundation
import Combine
import AppKit
/// Type of panel content
public enum PanelType: String, Codable, Sendable {
case terminal
case browser
case markdown
}
public enum TerminalPanelFocusIntent: Equatable {
case surface
case findField
}
public enum BrowserPanelFocusIntent: Equatable {
case webView
case addressBar
case findField
}
public enum PanelFocusIntent: Equatable {
case panel
case terminal(TerminalPanelFocusIntent)
case browser(BrowserPanelFocusIntent)
}
enum FocusFlashCurve: Equatable {
case easeIn
case easeOut
}
struct FocusFlashSegment: Equatable {
let delay: TimeInterval
let duration: TimeInterval
let targetOpacity: Double
let curve: FocusFlashCurve
}
enum FocusFlashPattern {
static let values: [Double] = [0, 1, 0, 1, 0]
static let keyTimes: [Double] = [0, 0.25, 0.5, 0.75, 1]
static let duration: TimeInterval = 0.9
static let curves: [FocusFlashCurve] = [.easeOut, .easeIn, .easeOut, .easeIn]
static let ringInset: Double = 6
static let ringCornerRadius: Double = 10
static var segments: [FocusFlashSegment] {
let stepCount = min(curves.count, values.count - 1, keyTimes.count - 1)
return (0..<stepCount).map { index in
let startTime = keyTimes[index]
let endTime = keyTimes[index + 1]
return FocusFlashSegment(
delay: startTime * duration,
duration: (endTime - startTime) * duration,
targetOpacity: values[index + 1],
curve: curves[index]
)
}
}
}
/// Protocol for all panel types (terminal, browser, etc.)
@MainActor
public protocol Panel: AnyObject, Identifiable, ObservableObject where ID == UUID {
/// Unique identifier for this panel
var id: UUID { get }
/// The type of panel
var panelType: PanelType { get }
/// Display title shown in tab bar
var displayTitle: String { get }
/// Optional SF Symbol icon name for the tab
var displayIcon: String? { get }
/// Whether the panel has unsaved changes
var isDirty: Bool { get }
/// Close the panel and clean up resources
func close()
/// Focus the panel for input
func focus()
/// Unfocus the panel
func unfocus()
/// Trigger a focus flash animation for this panel.
func triggerFlash()
/// Capture the panel-local focus target that should be restored later.
func captureFocusIntent(in window: NSWindow?) -> PanelFocusIntent
/// Return the best focus target to restore when this panel becomes active again.
func preferredFocusIntentForActivation() -> PanelFocusIntent
/// Prime panel-local focus state before activation side effects run.
func prepareFocusIntentForActivation(_ intent: PanelFocusIntent)
/// Restore a previously captured focus target.
@discardableResult
func restoreFocusIntent(_ intent: PanelFocusIntent) -> Bool
/// Return the semantic focus target currently owned by this panel, if any.
func ownedFocusIntent(for responder: NSResponder, in window: NSWindow) -> PanelFocusIntent?
/// Explicitly yield a previously owned focus target before another panel restores focus.
@discardableResult
func yieldFocusIntent(_ intent: PanelFocusIntent, in window: NSWindow) -> Bool
}
/// Extension providing default implementations
extension Panel {
public var displayIcon: String? { nil }
public var isDirty: Bool { false }
func captureFocusIntent(in window: NSWindow?) -> PanelFocusIntent {
_ = window
return preferredFocusIntentForActivation()
}
func preferredFocusIntentForActivation() -> PanelFocusIntent {
.panel
}
func prepareFocusIntentForActivation(_ intent: PanelFocusIntent) {
_ = intent
}
@discardableResult
func restoreFocusIntent(_ intent: PanelFocusIntent) -> Bool {
guard intent == .panel else { return false }
focus()
return true
}
func ownedFocusIntent(for responder: NSResponder, in window: NSWindow) -> PanelFocusIntent? {
_ = responder
_ = window
return nil
}
@discardableResult
func yieldFocusIntent(_ intent: PanelFocusIntent, in window: NSWindow) -> Bool {
_ = intent
_ = window
return false
}
}