Files
downterm/Downterm/CommandNotch/Components/SwiftTermView.swift

47 lines
1.6 KiB
Swift

import SwiftUI
import SwiftTerm
/// NSViewRepresentable wrapper that embeds a SwiftTerm TerminalView.
/// The container has a solid black background matching the notch panel.
/// All transparency is handled by the single `.opacity()` on ContentView.
struct SwiftTermView: NSViewRepresentable {
let session: TerminalSession
func makeNSView(context: Context) -> NSView {
let container = NSView()
container.wantsLayer = true
container.layer?.backgroundColor = NSColor.black.cgColor
embedTerminalView(in: container)
return container
}
func updateNSView(_ nsView: NSView, context: Context) {
let tv = session.terminalView
if nsView.subviews.first !== tv {
nsView.subviews.forEach { $0.removeFromSuperview() }
embedTerminalView(in: nsView)
}
DispatchQueue.main.async {
if let window = nsView.window, window.isKeyWindow {
window.makeFirstResponder(tv)
}
}
}
private func embedTerminalView(in container: NSView) {
let tv = session.terminalView
tv.removeFromSuperview()
container.addSubview(tv)
tv.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
tv.topAnchor.constraint(equalTo: container.topAnchor),
tv.bottomAnchor.constraint(equalTo: container.bottomAnchor),
tv.leadingAnchor.constraint(equalTo: container.leadingAnchor),
tv.trailingAnchor.constraint(equalTo: container.trailingAnchor),
])
}
}