Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
8 changes: 4 additions & 4 deletions Resources/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -212761,13 +212761,13 @@
"en": {
"stringUnit": {
"state": "translated",
"value": "A process wants cmux to keep this resume command for the current terminal:\n\n%@\n\nWorking directory: %@"
"value": "A process wants cmux to keep this resume command for the current terminal:\n\nWorking directory: %@\n\n%@"
}
},
"ja": {
"stringUnit": {
"state": "translated",
"value": "プロセスが現在のターミナルにこの復元コマンドを保持しようとしています:\n\n%@\n\n作業ディレクトリ: %@"
"value": "プロセスが現在のターミナルにこの復元コマンドを保持しようとしています:\n\n作業ディレクトリ: %@\n\n%@"
}
}
}
Expand Down Expand Up @@ -212795,13 +212795,13 @@
"en": {
"stringUnit": {
"state": "translated",
"value": "cmux is restoring a terminal with this resume command:\n\n%@\n\nWorking directory: %@"
"value": "cmux is restoring a terminal with this resume command:\n\nWorking directory: %@\n\n%@"
}
},
"ja": {
"stringUnit": {
"state": "translated",
"value": "cmux はこの復元コマンドでターミナルを復元しようとしています:\n\n%@\n\n作業ディレクトリ: %@"
"value": "cmux はこの復元コマンドでターミナルを復元しようとしています:\n\n作業ディレクトリ: %@\n\n%@"
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/AppDelegate+WorkspaceActionSave.swift
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ extension AppDelegate {
let accessory = WorkspaceActionSaveDialogAccessory(
snapshot: snapshot,
initialName: workspace.customTitle
?? URL(fileURLWithPath: workspace.currentDirectory).lastPathComponent
?? URL(fileURLWithPath: workspace.currentDirectory).lastPathComponent,
visibleFrame: window.screen?.visibleFrame ?? NSScreen.main?.visibleFrame
)
alert.accessoryView = accessory.view
alert.window.initialFirstResponder = accessory.nameField
Expand Down
94 changes: 94 additions & 0 deletions Sources/CmuxAlertContent.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import AppKit

/// A modal-alert body that keeps fixed explanatory copy separate from
/// user-sized details.
struct CmuxAlertContent: Equatable, Sendable {
private static let maximumFixedTextHeightFraction: CGFloat = 0.2

let informativeText: String
let scrollableDetails: String?
let flattenedText: String

init(informativeText: String) {
self.informativeText = informativeText
scrollableDetails = nil
flattenedText = informativeText
}

init(flattenedText: String, separatingScrollableDetails details: String) {
self.flattenedText = flattenedText

guard !details.isEmpty,
let range = flattenedText.range(of: details, options: .backwards) else {
informativeText = ""
scrollableDetails = flattenedText
return
}

var summary = flattenedText
summary.removeSubrange(range)
while summary.contains("\n\n\n") {
summary = summary.replacingOccurrences(of: "\n\n\n", with: "\n\n")
}
informativeText = summary.trimmingCharacters(in: .whitespacesAndNewlines)
scrollableDetails = details
}

static func scrollingAll(_ text: String) -> Self {
Self(flattenedText: text, separatingScrollableDetails: text)
}

@MainActor
func apply(to alert: NSAlert, presentingWindow: NSWindow?) {
let visibleFrame = (presentingWindow?.screen ?? alert.window.screen ?? NSScreen.main)?.visibleFrame
?? NSRect(x: 0, y: 0, width: 1024, height: 768)
apply(to: alert, visibleFrame: visibleFrame)
}

@MainActor
func apply(to alert: NSAlert, visibleFrame: NSRect) {
if let scrollableDetails, !scrollableDetails.isEmpty {
let flattenedMeasurement = CmuxAlertScrollableDetailsView(
text: flattenedText,
visibleFrame: visibleFrame
)
guard flattenedMeasurement.isContentHeightCapped else {
alert.informativeText = flattenedText
return
}

guard !informativeText.isEmpty else {
alert.informativeText = ""
alert.accessoryView = flattenedMeasurement
return
}

let summaryMeasurement = CmuxAlertScrollableDetailsView(
text: informativeText,
visibleFrame: visibleFrame
)
if summaryMeasurement.contentHeight > visibleFrame.height * Self.maximumFixedTextHeightFraction {
alert.informativeText = ""
alert.accessoryView = flattenedMeasurement
return
}
Comment thread
austinywang marked this conversation as resolved.
Resolved
alert.informativeText = informativeText
alert.accessoryView = CmuxAlertScrollableDetailsView(
text: scrollableDetails,
visibleFrame: visibleFrame
)
return
}

let overflowView = CmuxAlertScrollableDetailsView(
text: informativeText,
visibleFrame: visibleFrame
)
if overflowView.isContentHeightCapped {
alert.informativeText = ""
alert.accessoryView = overflowView
} else {
alert.informativeText = informativeText
}
}
}
82 changes: 82 additions & 0 deletions Sources/CmuxAlertScrollableDetailsView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import AppKit

/// A read-only alert details view whose viewport is capped to the visible
/// screen while its complete text remains scrollable and selectable.
@MainActor
final class CmuxAlertScrollableDetailsView: NSScrollView {
static let accessibilityIdentifier = "CmuxAlertScrollableDetails"
static let maximumVisibleFrameHeightFraction: CGFloat = 0.4

private(set) var contentHeight: CGFloat = 0
let maximumHeight: CGFloat

var isContentHeightCapped: Bool {
contentHeight > frame.height
}

static func maximumHeight(for visibleFrame: NSRect) -> CGFloat {
max(44, floor(visibleFrame.height * maximumVisibleFrameHeightFraction))
}

init(text: String, visibleFrame: NSRect) {
let width = min(420, max(260, floor(visibleFrame.width * 0.55)))
maximumHeight = Self.maximumHeight(for: visibleFrame)

super.init(frame: NSRect(x: 0, y: 0, width: width, height: maximumHeight))

borderType = .bezelBorder
drawsBackground = true
backgroundColor = .textBackgroundColor
hasHorizontalScroller = false
hasVerticalScroller = true
autohidesScrollers = true
setAccessibilityIdentifier(Self.accessibilityIdentifier)

let textView = NSTextView(frame: NSRect(x: 0, y: 0, width: contentSize.width, height: 1))
textView.string = text
textView.isEditable = false
textView.isSelectable = true
textView.drawsBackground = false
textView.font = .systemFont(ofSize: NSFont.systemFontSize)
textView.textColor = .labelColor
textView.textContainerInset = NSSize(width: 6, height: 6)
textView.isHorizontallyResizable = false
textView.isVerticallyResizable = true
textView.autoresizingMask = [.width]
textView.minSize = NSSize(width: 0, height: 0)
textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
textView.textContainer?.lineBreakMode = .byCharWrapping
textView.textContainer?.lineFragmentPadding = 0
textView.textContainer?.widthTracksTextView = true
textView.textContainer?.containerSize = NSSize(
width: contentSize.width,
height: CGFloat.greatestFiniteMagnitude
)

if let layoutManager = textView.layoutManager,
let textContainer = textView.textContainer {
layoutManager.ensureLayout(for: textContainer)
textView.sizeToFit()
layoutManager.ensureLayout(for: textContainer)
contentHeight = ceil(
layoutManager.usedRect(for: textContainer).height
+ textView.textContainerInset.height * 2
)
}

let viewportHeight = min(max(44, contentHeight), maximumHeight)
setFrameSize(NSSize(width: width, height: viewportHeight))
textView.setFrameSize(NSSize(width: contentSize.width, height: max(contentHeight, viewportHeight)))
hasVerticalScroller = contentHeight > viewportHeight
documentView = textView
}

@available(*, unavailable)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override var intrinsicContentSize: NSSize {
frame.size
}
}
10 changes: 10 additions & 0 deletions Sources/CmuxConfigExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ struct CmuxConfigExecutor {
displayTitle: displayTitle,
configPath: configPath
)
let content = CmuxAlertContent(
flattenedText: alert.informativeText,
separatingScrollableDetails: sanitizeForDisplay(command)
)
content.apply(to: alert, presentingWindow: presentingWindow)
alert.beginSheetModal(for: presentingWindow) { response in
completion(handleConfirmDialogResponse(response, descriptor: descriptor))
}
Expand All @@ -288,6 +293,11 @@ struct CmuxConfigExecutor {
displayTitle: displayTitle,
configPath: configPath
)
let content = CmuxAlertContent(
flattenedText: alert.informativeText,
separatingScrollableDetails: sanitizeForDisplay(command)
)
content.apply(to: alert, presentingWindow: nil)
return handleConfirmDialogResponse(alert.runModal(), descriptor: descriptor)
}

Expand Down
Loading