macOS: fit text content

pull/12736/head
Lukas 2026-05-19 09:38:38 +02:00
parent cc3be82cf9
commit 154fe8e66c
No known key found for this signature in database
GPG Key ID: 1944A0A77B561220
1 changed files with 30 additions and 1 deletions

View File

@ -51,7 +51,8 @@ class ClipboardConfirmationAlert: NSAlert, NSAlertDelegate {
informativeText = request.text()
let accessoryView = NSTextView.scrollableTextView()
accessoryView.frame = .init(x: 0, y: 0, width: 300, height: 200)
// Maximum frame when calculating the content size
accessoryView.frame = .init(x: 0, y: 0, width: 400, height: 270)
if let textView = accessoryView.documentView as? NSTextView {
textView.drawsBackground = false
textView.isEditable = false
@ -65,9 +66,37 @@ class ClipboardConfirmationAlert: NSAlert, NSAlertDelegate {
addCancelButton(Action.text(.cancel, request))
addConfirmButton(Action.text(.confirm, request))
layout()
updateContentHeight()
delegate = self
}
private func updateContentHeight() {
guard
let accessoryView,
let textView = (accessoryView as? NSScrollView)?.documentView as? NSTextView,
let layoutManager = textView.layoutManager,
let textContainer = textView.textContainer
else {
return
}
textContainer.containerSize = CGSize(
width: accessoryView.frame.width,
height: .greatestFiniteMagnitude,
)
textContainer.widthTracksTextView = false
layoutManager.ensureLayout(for: textContainer)
let usedRect = layoutManager.usedRect(for: textContainer)
accessoryView.frame.size.height = .minimum(
accessoryView.frame.height,
.maximum(10, usedRect.height),
)
}
func addCancelButton(_ buttonTitle: String) {
addButton(withTitle: buttonTitle)
.keyEquivalent = .init([KeyboardShortcut(.escape).key.character])