Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 14 additions & 15 deletions macos/Sources/Features/Terminal/TerminalController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1038,27 +1038,26 @@ class TerminalController: BaseTerminalController, TabGroupCloseCoordinator.Contr
}

// Initialize our content view to the SwiftUI root
window.contentView = TerminalViewContainer {
let container = TerminalViewContainer {
TerminalView(ghostty: ghostty, viewModel: self, delegate: self)
}

// Set the initial content size on the container so that
// intrinsicContentSize returns the correct value immediately,
// without waiting for @FocusedValue to propagate through the
// SwiftUI focus chain.
container.initialContentSize = focusedSurface?.initialSize

window.contentView = container

// If we have a default size, we want to apply it.
if let defaultSize {
switch defaultSize {
case .frame:
// Frames can be applied immediately
defaultSize.apply(to: window)
defaultSize.apply(to: window)

case .contentIntrinsicSize:
// Content intrinsic size requires a short delay so that AppKit
// can layout our SwiftUI views.
DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(40)) { [weak self, weak window] in
guard let self, let window else { return }
defaultSize.apply(to: window)
if let screen = window.screen ?? NSScreen.main {
let frame = self.adjustForWindowPosition(frame: window.frame, on: screen)
window.setFrameOrigin(frame.origin)
}
if case .contentIntrinsicSize = defaultSize {
if let screen = window.screen ?? NSScreen.main {
let frame = self.adjustForWindowPosition(frame: window.frame, on: screen)
window.setFrameOrigin(frame.origin)
}
}
}
Expand Down
20 changes: 16 additions & 4 deletions macos/Sources/Features/Terminal/TerminalViewContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,23 @@ class TerminalViewContainer: NSView {
fatalError("init(coder:) has not been implemented")
}

/// To make ``TerminalController/DefaultSize/contentIntrinsicSize``
/// work in ``TerminalController/windowDidLoad()``,
/// we override this to provide the correct size.
/// The initial content size to use as a fallback before the SwiftUI
/// view hierarchy has completed layout (i.e. before @FocusedValue
/// propagates `lastFocusedSurface`). Once the hosting view reports
/// a valid intrinsic size, this fallback is no longer used.
var initialContentSize: NSSize?

override var intrinsicContentSize: NSSize {
terminalView.intrinsicContentSize
let hostingSize = terminalView.intrinsicContentSize
// The hosting view returns a valid size once SwiftUI has laid out
// with the correct idealWidth/idealHeight. Before that (when
// @FocusedValue hasn't propagated), it returns a tiny default.
// Fall back to initialContentSize in that case.
if let initialContentSize,
hostingSize.width < initialContentSize.width || hostingSize.height < initialContentSize.height {
return initialContentSize
}
return hostingSize
}

private func setup() {
Expand Down
Loading