Repository Public
1import AppKit2import SwiftUI3import TerminalCore45/// Hosts a session's long-lived `NEXTerminalView` inside SwiftUI by reparenting it into a6/// container. The terminal view is never created or destroyed here, so layout changes,7/// tab moves and window moves leave the PTY untouched.8struct TerminalHostView: NSViewRepresentable {9 let session: TerminalSession10 var requestFocus: Bool11 var onFocused: (() -> Void)? = nil12 var pasteConfirmation: ((String, @escaping @MainActor () -> Void) -> Void)? = nil13 var bellStyle: String = "sound"1415 func makeNSView(context: Context) -> TerminalContainerView {16 let container = TerminalContainerView()17 configure(container)18 return container19 }2021 func updateNSView(_ container: TerminalContainerView, context: Context) {22 configure(container)23 }2425 private func configure(_ container: TerminalContainerView) {26 container.attach(session.view)27 container.onFocusChange = { [weak session] focused in28 session?.setFocused(focused)29 if focused { onFocused?() }30 }31 session.view.pasteConfirmation = pasteConfirmation32 session.view.confirmMultilinePaste = pasteConfirmation != nil33 session.view.setBell(style: bellStyle)34 if requestFocus {35 if container.window != nil { container.focusTerminal() } else { container.wantsInitialFocus = true }36 }37 }3839 static func dismantleNSView(_ container: TerminalContainerView, coordinator: ()) {40 container.detach()41 }42}4344final class TerminalContainerView: NSView {45 private weak var terminal: NEXTerminalView?46 private var responderObservation: NSKeyValueObservation?47 private var lastReportedFocus: Bool?48 /// Called on the main thread whenever keyboard focus enters or leaves the hosted terminal.49 var onFocusChange: ((Bool) -> Void)?50 /// When set, the terminal grabs keyboard focus as soon as the container lands in a window.51 var wantsInitialFocus = false5253 override var isFlipped: Bool { true }5455 override func viewDidMoveToWindow() {56 super.viewDidMoveToWindow()57 responderObservation = nil58 guard let window else { return }59 // SwiftTerm only recomputes its cell grid and Metal drawable when its frame changes *while it60 // has a window*; force one real frame change now that we are on screen.61 syncTerminalFrame(force: true)62 if wantsInitialFocus {63 wantsInitialFocus = false64 DispatchQueue.main.async { [weak self] in self?.focusTerminal() }65 }66 // NSWindow.firstResponder is KVO-compliant; SwiftTerm itself keys off `window.firstResponder === self`.67 responderObservation = window.observe(\.firstResponder, options: [.initial, .new]) { [weak self] window, _ in68 MainActor.assumeIsolated {69 guard let self, let terminal = self.terminal else { return }70 let focused = window.firstResponder === terminal71 if self.lastReportedFocus != focused {72 self.lastReportedFocus = focused73 self.onFocusChange?(focused)74 }75 }76 }77 }7879 func attach(_ view: NEXTerminalView) {80 if view.superview === self { return }81 view.removeFromSuperview()82 terminal = view83 // Explicit sizing only: autoresizing masks applied while the container is still 0×0 (which84 // happens when a tab is created or revealed) leave the terminal at a stale size — observed as85 // blank panes reporting e.g. 49×22 inside a much larger window.86 view.autoresizingMask = [.width, .height]87 addSubview(view)88 lastReportedFocus = nil89 syncTerminalFrame(force: true)90 needsLayout = true91 }9293 override func setFrameSize(_ newSize: NSSize) {94 super.setFrameSize(newSize)95 syncTerminalFrame()96 }9798 override func resizeSubviews(withOldSize oldSize: NSSize) {99 super.resizeSubviews(withOldSize: oldSize)100 syncTerminalFrame()101 }102103 override func viewDidMoveToSuperview() {104 super.viewDidMoveToSuperview()105 syncTerminalFrame(force: true)106 }107108 private func syncTerminalFrame(force: Bool = false) {109 guard let terminal, window != nil, !bounds.isEmpty else { return }110 if terminal.frame == bounds {111 guard force else { return }112 // Same frame: perturb so SwiftTerm's setFrameSize path runs with a live window.113 terminal.frame = CGRect(x: 0, y: 0, width: max(1, bounds.width - 1), height: bounds.height)114 }115 terminal.frame = bounds116 }117118 /// Detaches without destroying: the session keeps the view alive.119 func detach() {120 if let terminal, terminal.superview === self {121 terminal.removeFromSuperview()122 }123 terminal = nil124 responderObservation = nil125 }126127 func focusTerminal() {128 guard let terminal, let window, window.firstResponder !== terminal else { return }129 window.makeFirstResponder(terminal)130 }131132 override func layout() {133 super.layout()134 syncTerminalFrame()135 }136}137