fix(ios): select Quick Question draft on focus
TestFlight / Build and upload (push) Successful in 2m1s
TestFlight / Build and upload (push) Successful in 2m1s
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import MarkdownUI
|
||||
import Observation
|
||||
import SwiftUI
|
||||
import UIKit
|
||||
|
||||
struct SybilQuickQuestionView: View {
|
||||
@Bindable var viewModel: SybilViewModel
|
||||
@@ -8,7 +9,6 @@ struct SybilQuickQuestionView: View {
|
||||
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
@FocusState private var promptFocused: Bool
|
||||
@State private var promptSelection: TextSelection?
|
||||
|
||||
private var hasAnswerContent: Bool {
|
||||
!viewModel.quickQuestionMessages.isEmpty || viewModel.quickQuestionError != nil
|
||||
@@ -37,8 +37,6 @@ struct SybilQuickQuestionView: View {
|
||||
return
|
||||
}
|
||||
promptFocused = true
|
||||
let prompt = viewModel.quickQuestionPrompt
|
||||
promptSelection = TextSelection(range: prompt.startIndex..<prompt.endIndex)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -95,17 +93,29 @@ struct SybilQuickQuestionView: View {
|
||||
private var composer: some View {
|
||||
VStack(alignment: .leading, spacing: 10) {
|
||||
HStack(alignment: .bottom, spacing: 10) {
|
||||
SybilPromptField(
|
||||
QuickQuestionTextField(
|
||||
placeholder: "Ask anything...",
|
||||
text: Binding(
|
||||
get: { viewModel.quickQuestionPrompt },
|
||||
set: { viewModel.updateQuickQuestionPrompt($0) }
|
||||
),
|
||||
focused: $promptFocused,
|
||||
selection: $promptSelection,
|
||||
axis: .horizontal,
|
||||
focused: Binding(
|
||||
get: { promptFocused },
|
||||
set: { promptFocused = $0 }
|
||||
),
|
||||
focusRequest: focusRequest,
|
||||
submit: submitQuestion
|
||||
)
|
||||
.focused($promptFocused)
|
||||
.font(.sybil(.body))
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 10)
|
||||
.frame(maxWidth: .infinity, minHeight: 40, alignment: .leading)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 12)
|
||||
.fill(SybilTheme.composerGradient)
|
||||
.opacity(0.98)
|
||||
)
|
||||
|
||||
SybilPromptSendButton(canSend: viewModel.canSendQuickQuestion, submit: submitQuestion)
|
||||
.accessibilityLabel("Ask quick question")
|
||||
@@ -208,6 +218,113 @@ struct SybilQuickQuestionView: View {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private final class QuickQuestionNativeTextField: UITextField {
|
||||
var wantsFirstResponder = false {
|
||||
didSet {
|
||||
updateFirstResponder()
|
||||
}
|
||||
}
|
||||
|
||||
override func didMoveToWindow() {
|
||||
super.didMoveToWindow()
|
||||
updateFirstResponder()
|
||||
}
|
||||
|
||||
private func updateFirstResponder() {
|
||||
guard window != nil else {
|
||||
return
|
||||
}
|
||||
if wantsFirstResponder && !isFirstResponder {
|
||||
becomeFirstResponder()
|
||||
} else if !wantsFirstResponder && isFirstResponder {
|
||||
resignFirstResponder()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct QuickQuestionTextField: UIViewRepresentable {
|
||||
var placeholder: String
|
||||
@Binding var text: String
|
||||
@Binding var focused: Bool
|
||||
var focusRequest: Int
|
||||
var submit: () -> Void
|
||||
|
||||
func makeUIView(context: Context) -> QuickQuestionNativeTextField {
|
||||
SybilFontRegistry.registerIfNeeded()
|
||||
let textField = QuickQuestionNativeTextField()
|
||||
textField.delegate = context.coordinator
|
||||
textField.addTarget(context.coordinator, action: #selector(Coordinator.textDidChange(_:)), for: .editingChanged)
|
||||
textField.placeholder = placeholder
|
||||
let baseFont = UIFont(name: "Inter", size: 17) ?? UIFont.systemFont(ofSize: 17)
|
||||
textField.font = UIFontMetrics(forTextStyle: .body).scaledFont(for: baseFont)
|
||||
textField.adjustsFontForContentSizeCategory = true
|
||||
textField.textColor = UIColor(SybilTheme.text)
|
||||
textField.backgroundColor = .clear
|
||||
textField.borderStyle = .none
|
||||
textField.autocapitalizationType = .sentences
|
||||
textField.autocorrectionType = .yes
|
||||
textField.returnKeyType = .send
|
||||
textField.keyboardAppearance = .dark
|
||||
textField.accessibilityIdentifier = "quick-question-prompt"
|
||||
textField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
|
||||
textField.setContentHuggingPriority(.defaultLow, for: .horizontal)
|
||||
return textField
|
||||
}
|
||||
|
||||
func updateUIView(_ textField: QuickQuestionNativeTextField, context: Context) {
|
||||
context.coordinator.parent = self
|
||||
if textField.text != text {
|
||||
textField.text = text
|
||||
}
|
||||
textField.wantsFirstResponder = focused
|
||||
}
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator(parent: self)
|
||||
}
|
||||
|
||||
@MainActor
|
||||
final class Coordinator: NSObject, UITextFieldDelegate {
|
||||
var parent: QuickQuestionTextField
|
||||
private var lastSelectedFocusRequest: Int?
|
||||
|
||||
init(parent: QuickQuestionTextField) {
|
||||
self.parent = parent
|
||||
}
|
||||
|
||||
@objc func textDidChange(_ textField: UITextField) {
|
||||
parent.text = textField.text ?? ""
|
||||
}
|
||||
|
||||
func textFieldDidBeginEditing(_ textField: UITextField) {
|
||||
parent.focused = true
|
||||
guard lastSelectedFocusRequest != parent.focusRequest else {
|
||||
return
|
||||
}
|
||||
lastSelectedFocusRequest = parent.focusRequest
|
||||
guard let text = textField.text, !text.isEmpty,
|
||||
let range = textField.textRange(
|
||||
from: textField.beginningOfDocument,
|
||||
to: textField.endOfDocument
|
||||
)
|
||||
else {
|
||||
return
|
||||
}
|
||||
textField.selectedTextRange = range
|
||||
}
|
||||
|
||||
func textFieldDidEndEditing(_ textField: UITextField) {
|
||||
parent.focused = false
|
||||
}
|
||||
|
||||
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
||||
parent.submit()
|
||||
return false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private struct QuickQuestionPickerPill: View {
|
||||
var title: String
|
||||
|
||||
|
||||
Reference in New Issue
Block a user