This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
import SwiftUI
|
||||
#if os(iOS)
|
||||
import UIKit
|
||||
#endif
|
||||
|
||||
struct SybilPromptField: View {
|
||||
var placeholder: String = "Enter Prompt"
|
||||
@@ -6,9 +9,50 @@ struct SybilPromptField: View {
|
||||
@FocusState.Binding var focused: Bool
|
||||
var selection: Binding<TextSelection?>? = nil
|
||||
var axis: Axis = .vertical
|
||||
var returnSubmits = false
|
||||
var submit: () -> Void
|
||||
|
||||
var body: some View {
|
||||
Group {
|
||||
#if os(iOS) && !targetEnvironment(macCatalyst)
|
||||
if returnSubmits {
|
||||
SybilReturnSubmittingTextView(
|
||||
placeholder: placeholder,
|
||||
text: $text,
|
||||
focused: $focused,
|
||||
selection: selection,
|
||||
submit: submit
|
||||
)
|
||||
.overlay(alignment: .topLeading) {
|
||||
if text.isEmpty {
|
||||
Text(placeholder)
|
||||
.font(.sybil(.body))
|
||||
.foregroundStyle(SybilTheme.textMuted)
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 10)
|
||||
.allowsHitTesting(false)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
swiftUITextField
|
||||
}
|
||||
#else
|
||||
swiftUITextField
|
||||
.submitOnMacReturn(submit)
|
||||
#endif
|
||||
}
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 10)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 12)
|
||||
.fill(SybilTheme.composerGradient)
|
||||
.opacity(0.98)
|
||||
)
|
||||
.foregroundStyle(SybilTheme.text)
|
||||
}
|
||||
|
||||
@ViewBuilder
|
||||
private var swiftUITextField: some View {
|
||||
Group {
|
||||
if let selection {
|
||||
TextField(placeholder, text: $text, selection: selection, axis: axis)
|
||||
@@ -23,18 +67,127 @@ struct SybilPromptField: View {
|
||||
.lineLimit(1 ... 6)
|
||||
.submitLabel(.send)
|
||||
.onSubmit(submit)
|
||||
.submitOnMacReturn(submit)
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 10)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 12)
|
||||
.fill(SybilTheme.composerGradient)
|
||||
.opacity(0.98)
|
||||
)
|
||||
.foregroundStyle(SybilTheme.text)
|
||||
}
|
||||
}
|
||||
|
||||
#if os(iOS) && !targetEnvironment(macCatalyst)
|
||||
private final class ReturnSubmittingUITextView: UITextView {
|
||||
var shouldFocus = false
|
||||
|
||||
override func didMoveToWindow() {
|
||||
super.didMoveToWindow()
|
||||
if shouldFocus, window != nil, !isFirstResponder {
|
||||
becomeFirstResponder()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct SybilReturnSubmittingTextView: UIViewRepresentable {
|
||||
var placeholder: String
|
||||
@Binding var text: String
|
||||
@FocusState.Binding var focused: Bool
|
||||
var selection: Binding<TextSelection?>?
|
||||
var submit: () -> Void
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator(parent: self)
|
||||
}
|
||||
|
||||
func makeUIView(context: Context) -> ReturnSubmittingUITextView {
|
||||
let textView = ReturnSubmittingUITextView()
|
||||
SybilFontRegistry.registerIfNeeded()
|
||||
textView.delegate = context.coordinator
|
||||
textView.backgroundColor = .clear
|
||||
textView.textColor = UIColor(SybilTheme.text)
|
||||
let baseFont = UIFont(name: "Inter", size: 17) ?? .preferredFont(forTextStyle: .body)
|
||||
textView.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: baseFont)
|
||||
textView.textContainerInset = .zero
|
||||
textView.textContainer.lineFragmentPadding = 0
|
||||
textView.isScrollEnabled = true
|
||||
textView.returnKeyType = .send
|
||||
textView.enablesReturnKeyAutomatically = false
|
||||
textView.autocapitalizationType = .sentences
|
||||
textView.autocorrectionType = .yes
|
||||
textView.accessibilityLabel = placeholder
|
||||
textView.accessibilityIdentifier = "quick-question-prompt"
|
||||
return textView
|
||||
}
|
||||
|
||||
func updateUIView(_ textView: ReturnSubmittingUITextView, context: Context) {
|
||||
context.coordinator.parent = self
|
||||
textView.shouldFocus = focused
|
||||
if textView.text != text {
|
||||
textView.text = text
|
||||
}
|
||||
if textView.isFirstResponder != focused {
|
||||
if focused {
|
||||
textView.becomeFirstResponder()
|
||||
} else {
|
||||
textView.resignFirstResponder()
|
||||
}
|
||||
}
|
||||
if let selection,
|
||||
let value = selection.wrappedValue,
|
||||
let selectedRange = nsRange(for: value, in: text) {
|
||||
if textView.selectedRange != selectedRange {
|
||||
textView.selectedRange = selectedRange
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private func nsRange(for selection: TextSelection, in text: String) -> NSRange? {
|
||||
switch selection.indices {
|
||||
case .selection(let range):
|
||||
return NSRange(range, in: text)
|
||||
case .multiSelection:
|
||||
return nil
|
||||
@unknown default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func sizeThatFits(_ proposal: ProposedViewSize, uiView: UITextView, context: Context) -> CGSize? {
|
||||
guard let width = proposal.width, width.isFinite, width > 0 else { return nil }
|
||||
let measured = uiView.sizeThatFits(CGSize(width: width, height: .greatestFiniteMagnitude)).height
|
||||
let lineHeight = uiView.font?.lineHeight ?? 21
|
||||
return CGSize(width: width, height: min(max(lineHeight, measured), lineHeight * 6))
|
||||
}
|
||||
|
||||
final class Coordinator: NSObject, UITextViewDelegate {
|
||||
var parent: SybilReturnSubmittingTextView
|
||||
|
||||
init(parent: SybilReturnSubmittingTextView) {
|
||||
self.parent = parent
|
||||
}
|
||||
|
||||
func textViewDidChange(_ textView: UITextView) {
|
||||
parent.text = textView.text
|
||||
}
|
||||
|
||||
func textViewDidBeginEditing(_ textView: UITextView) {
|
||||
parent.focused = true
|
||||
}
|
||||
|
||||
func textViewDidEndEditing(_ textView: UITextView) {
|
||||
parent.focused = false
|
||||
}
|
||||
|
||||
func textViewDidChangeSelection(_ textView: UITextView) {
|
||||
guard let binding = parent.selection,
|
||||
let range = Range(textView.selectedRange, in: textView.text) else { return }
|
||||
binding.wrappedValue = TextSelection(range: range)
|
||||
}
|
||||
|
||||
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText replacement: String) -> Bool {
|
||||
guard replacement == "\n" else { return true }
|
||||
guard textView.markedTextRange == nil else { return true }
|
||||
parent.submit()
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
struct SybilPromptSendButton: View {
|
||||
var systemImage: String = "arrow.up"
|
||||
var canSend: Bool
|
||||
|
||||
@@ -136,6 +136,7 @@ struct SybilQuickQuestionView: View {
|
||||
focused: $promptFocused,
|
||||
selection: $promptSelection,
|
||||
axis: .vertical,
|
||||
returnSubmits: true,
|
||||
submit: submitQuestion
|
||||
)
|
||||
.accessibilityIdentifier("quick-question-prompt")
|
||||
|
||||
Reference in New Issue
Block a user