2023-04-16 17:23:13 +00:00
|
|
|
//
|
|
|
|
// AttachmentDescriptionTextView.swift
|
|
|
|
// ComposeUI
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 3/12/23.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
|
2023-04-29 22:49:02 +00:00
|
|
|
private var placeholder: some View {
|
|
|
|
Text("Describe for the visually impaired…")
|
|
|
|
}
|
|
|
|
|
|
|
|
struct InlineAttachmentDescriptionView: View {
|
|
|
|
@ObservedObject private var attachment: DraftAttachment
|
2023-04-16 17:23:13 +00:00
|
|
|
private let minHeight: CGFloat
|
|
|
|
|
|
|
|
@State private var height: CGFloat?
|
|
|
|
|
2023-04-29 22:49:02 +00:00
|
|
|
init(attachment: DraftAttachment, minHeight: CGFloat) {
|
|
|
|
self.attachment = attachment
|
2023-04-16 17:23:13 +00:00
|
|
|
self.minHeight = minHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
ZStack(alignment: .topLeading) {
|
2023-04-29 22:49:02 +00:00
|
|
|
if attachment.attachmentDescription.isEmpty {
|
2023-04-16 17:23:13 +00:00
|
|
|
placeholder
|
|
|
|
.font(.body)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.offset(x: 4, y: 8)
|
|
|
|
}
|
|
|
|
|
|
|
|
WrappedTextView(
|
2023-04-29 22:49:02 +00:00
|
|
|
text: $attachment.attachmentDescription,
|
|
|
|
backgroundColor: .clear,
|
|
|
|
textDidChange: self.textDidChange
|
2023-04-16 17:23:13 +00:00
|
|
|
)
|
|
|
|
.frame(height: height ?? minHeight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func textDidChange(_ textView: UITextView) {
|
|
|
|
height = max(minHeight, textView.contentSize.height)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-29 22:49:02 +00:00
|
|
|
struct FocusedAttachmentDescriptionView: View {
|
|
|
|
@ObservedObject var attachment: DraftAttachment
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
ZStack(alignment: .topLeading) {
|
|
|
|
WrappedTextView(
|
|
|
|
text: $attachment.attachmentDescription,
|
|
|
|
backgroundColor: .secondarySystemBackground,
|
|
|
|
textDidChange: nil
|
|
|
|
)
|
|
|
|
.edgesIgnoringSafeArea([.bottom, .leading, .trailing])
|
|
|
|
|
|
|
|
if attachment.attachmentDescription.isEmpty {
|
|
|
|
placeholder
|
|
|
|
.font(.body)
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
.offset(x: 4, y: 8)
|
|
|
|
.allowsHitTesting(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-16 17:23:13 +00:00
|
|
|
private struct WrappedTextView: UIViewRepresentable {
|
|
|
|
typealias UIViewType = UITextView
|
|
|
|
|
|
|
|
@Binding var text: String
|
2023-04-29 22:49:02 +00:00
|
|
|
let backgroundColor: UIColor
|
|
|
|
let textDidChange: (((UITextView) -> Void))?
|
2023-04-16 17:23:13 +00:00
|
|
|
|
|
|
|
@Environment(\.isEnabled) private var isEnabled
|
|
|
|
|
|
|
|
func makeUIView(context: Context) -> UITextView {
|
|
|
|
let view = UITextView()
|
|
|
|
view.delegate = context.coordinator
|
2023-04-29 22:49:02 +00:00
|
|
|
view.backgroundColor = backgroundColor
|
|
|
|
view.font = .preferredFont(forTextStyle: .body)
|
2023-04-16 17:23:13 +00:00
|
|
|
view.adjustsFontForContentSizeCategory = true
|
|
|
|
view.textContainer.lineBreakMode = .byWordWrapping
|
|
|
|
return view
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUIView(_ uiView: UITextView, context: Context) {
|
|
|
|
uiView.text = text
|
|
|
|
uiView.isEditable = isEnabled
|
|
|
|
context.coordinator.textView = uiView
|
|
|
|
context.coordinator.text = $text
|
|
|
|
context.coordinator.didChange = textDidChange
|
2023-04-29 22:49:02 +00:00
|
|
|
if let textDidChange {
|
|
|
|
// wait until the next runloop iteration so that SwiftUI view updates have finished and
|
|
|
|
// the text view knows its new content size
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
textDidChange(uiView)
|
|
|
|
}
|
2023-04-16 17:23:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeCoordinator() -> Coordinator {
|
|
|
|
Coordinator(text: $text, didChange: textDidChange)
|
|
|
|
}
|
|
|
|
|
|
|
|
class Coordinator: NSObject, UITextViewDelegate, TextViewCaretScrolling {
|
|
|
|
weak var textView: UITextView?
|
|
|
|
var text: Binding<String>
|
2023-04-29 22:49:02 +00:00
|
|
|
var didChange: ((UITextView) -> Void)?
|
2023-04-16 17:23:13 +00:00
|
|
|
var caretScrollPositionAnimator: UIViewPropertyAnimator?
|
|
|
|
|
2023-04-29 22:49:02 +00:00
|
|
|
init(text: Binding<String>, didChange: ((UITextView) -> Void)?) {
|
2023-04-16 17:23:13 +00:00
|
|
|
self.text = text
|
|
|
|
self.didChange = didChange
|
|
|
|
|
|
|
|
super.init()
|
|
|
|
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: UIResponder.keyboardDidShowNotification, object: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc private func keyboardDidShow() {
|
|
|
|
guard let textView,
|
|
|
|
textView.isFirstResponder else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ensureCursorVisible(textView: textView)
|
|
|
|
}
|
|
|
|
|
|
|
|
func textViewDidChange(_ textView: UITextView) {
|
|
|
|
text.wrappedValue = textView.text
|
2023-04-29 22:49:02 +00:00
|
|
|
didChange?(textView)
|
2023-04-16 17:23:13 +00:00
|
|
|
|
|
|
|
ensureCursorVisible(textView: textView)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|