fix(ios): restore Quick Question composer behavior
TestFlight / Build and upload (push) Successful in 1m53s
TestFlight / Build and upload (push) Successful in 1m53s
This commit is contained in:
@@ -1,7 +1,4 @@
|
||||
import SwiftUI
|
||||
#if os(iOS)
|
||||
import UIKit
|
||||
#endif
|
||||
|
||||
struct SybilPromptField: View {
|
||||
var placeholder: String = "Enter Prompt"
|
||||
@@ -9,50 +6,9 @@ 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)
|
||||
@@ -67,127 +23,18 @@ 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
|
||||
|
||||
@@ -9,7 +9,6 @@ struct SybilQuickQuestionView: View {
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@FocusState private var promptFocused: Bool
|
||||
@State private var promptSelection: TextSelection?
|
||||
@State private var autoSelectedFocusRequest: Int?
|
||||
|
||||
private var hasAnswerContent: Bool {
|
||||
!viewModel.quickQuestionMessages.isEmpty || viewModel.quickQuestionError != nil
|
||||
@@ -32,45 +31,14 @@ struct SybilQuickQuestionView: View {
|
||||
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
|
||||
.background(SybilTheme.backgroundGradient)
|
||||
.preferredColorScheme(.dark)
|
||||
.onChange(of: promptFocused) { _, isFocused in
|
||||
guard isFocused else {
|
||||
return
|
||||
}
|
||||
selectInitialPromptIfNeeded(for: focusRequest)
|
||||
}
|
||||
.task(id: focusRequest) {
|
||||
promptFocused = false
|
||||
promptSelection = nil
|
||||
autoSelectedFocusRequest = nil
|
||||
try? await Task.sleep(for: .milliseconds(260))
|
||||
guard !Task.isCancelled else {
|
||||
return
|
||||
}
|
||||
promptFocused = true
|
||||
}
|
||||
}
|
||||
|
||||
private func selectInitialPromptIfNeeded(for request: Int) {
|
||||
guard autoSelectedFocusRequest != request else {
|
||||
return
|
||||
}
|
||||
autoSelectedFocusRequest = request
|
||||
|
||||
let initialPrompt = viewModel.quickQuestionPrompt
|
||||
guard !initialPrompt.isEmpty else {
|
||||
return
|
||||
}
|
||||
|
||||
// Let SwiftUI apply focus before publishing the selection update.
|
||||
Task { @MainActor in
|
||||
await Task.yield()
|
||||
guard promptFocused,
|
||||
focusRequest == request,
|
||||
viewModel.quickQuestionPrompt == initialPrompt
|
||||
else {
|
||||
return
|
||||
}
|
||||
promptSelection = TextSelection(range: initialPrompt.startIndex..<initialPrompt.endIndex)
|
||||
let prompt = viewModel.quickQuestionPrompt
|
||||
promptSelection = TextSelection(range: prompt.startIndex..<prompt.endIndex)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,11 +103,9 @@ struct SybilQuickQuestionView: View {
|
||||
),
|
||||
focused: $promptFocused,
|
||||
selection: $promptSelection,
|
||||
axis: .vertical,
|
||||
returnSubmits: true,
|
||||
axis: .horizontal,
|
||||
submit: submitQuestion
|
||||
)
|
||||
.accessibilityIdentifier("quick-question-prompt")
|
||||
|
||||
SybilPromptSendButton(canSend: viewModel.canSendQuickQuestion, submit: submitQuestion)
|
||||
.accessibilityLabel("Ask quick question")
|
||||
@@ -242,7 +208,6 @@ struct SybilQuickQuestionView: View {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private struct QuickQuestionPickerPill: View {
|
||||
var title: String
|
||||
|
||||
|
||||
Reference in New Issue
Block a user