fix(ios): align Quick Question composer and selection
TestFlight / Build and upload (push) Successful in 2m12s
TestFlight / Build and upload (push) Successful in 2m12s
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
import MarkdownUI
|
||||
import Observation
|
||||
import SwiftUI
|
||||
import UIKit
|
||||
|
||||
struct SybilQuickQuestionView: View {
|
||||
@Bindable var viewModel: SybilViewModel
|
||||
@@ -9,6 +8,8 @@ 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
|
||||
@@ -31,7 +32,16 @@ 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
|
||||
@@ -40,6 +50,30 @@ struct SybilQuickQuestionView: View {
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
private var header: some View {
|
||||
HStack {
|
||||
Image(systemName: "sparkles")
|
||||
@@ -93,29 +127,18 @@ struct SybilQuickQuestionView: View {
|
||||
private var composer: some View {
|
||||
VStack(alignment: .leading, spacing: 10) {
|
||||
HStack(alignment: .bottom, spacing: 10) {
|
||||
QuickQuestionTextField(
|
||||
SybilPromptField(
|
||||
placeholder: "Ask anything...",
|
||||
text: Binding(
|
||||
get: { viewModel.quickQuestionPrompt },
|
||||
set: { viewModel.updateQuickQuestionPrompt($0) }
|
||||
),
|
||||
focused: Binding(
|
||||
get: { promptFocused },
|
||||
set: { promptFocused = $0 }
|
||||
),
|
||||
focusRequest: focusRequest,
|
||||
focused: $promptFocused,
|
||||
selection: $promptSelection,
|
||||
axis: .vertical,
|
||||
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)
|
||||
)
|
||||
.accessibilityIdentifier("quick-question-prompt")
|
||||
|
||||
SybilPromptSendButton(canSend: viewModel.canSendQuickQuestion, submit: submitQuestion)
|
||||
.accessibilityLabel("Ask quick question")
|
||||
@@ -219,112 +242,6 @@ 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
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import XCTest
|
||||
import UIKit
|
||||
|
||||
/// Uses the repository's 127.0.0.1 contract fixture and the real iOS home-screen shortcut.
|
||||
@MainActor
|
||||
@@ -17,24 +18,28 @@ final class QuickQuestionSelectionUITests: XCTestCase {
|
||||
|
||||
override func tearDown() async throws {
|
||||
app.terminate()
|
||||
UIPasteboard.general.string = nil
|
||||
}
|
||||
|
||||
func testPrefilledDraftIsSelectedOnReopenAndTypingReplacesIt() async throws {
|
||||
func testQuickActionReselectsRetainedDraft() async throws {
|
||||
app.launch()
|
||||
openQuickQuestionFromHomeScreen()
|
||||
|
||||
let prompt = app.textFields["quick-question-prompt"]
|
||||
let prompt = quickQuestionPromptElement()
|
||||
XCTAssertTrue(prompt.waitForExistence(timeout: 10))
|
||||
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
|
||||
XCTAssertLessThan(prompt.frame.height, 80, "An empty Quick Question composer should stay one line tall.")
|
||||
prompt.typeText("retained draft")
|
||||
XCTAssertEqual(prompt.value as? String, "retained draft")
|
||||
|
||||
dismissQuickQuestion(prompt)
|
||||
// The real home-screen action requests a new focusRequest even while
|
||||
// this sheet remains presented. That task refocuses and reselects text.
|
||||
openQuickQuestionFromHomeScreen()
|
||||
|
||||
XCTAssertTrue(prompt.waitForExistence(timeout: 10))
|
||||
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
|
||||
XCTAssertEqual(prompt.value as? String, "retained draft")
|
||||
XCTAssertLessThan(prompt.frame.height, 80, "A one-line draft should keep the normal chat composer height.")
|
||||
|
||||
// The field is already focused with its initial draft selected. Do not tap
|
||||
// it here: typing immediately is the behavior under test.
|
||||
@@ -42,10 +47,105 @@ final class QuickQuestionSelectionUITests: XCTestCase {
|
||||
XCTAssertEqual(prompt.value as? String, "replacement")
|
||||
}
|
||||
|
||||
func testQuickQuestionComposerStartsCompactAndGrowsForMultilineText() {
|
||||
app.launch()
|
||||
openQuickQuestionFromHomeScreen()
|
||||
|
||||
let prompt = quickQuestionPromptElement()
|
||||
XCTAssertTrue(prompt.waitForExistence(timeout: 10))
|
||||
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
|
||||
let oneLineHeight = prompt.frame.height
|
||||
XCTAssertLessThan(oneLineHeight, 80, "The Quick Question field must not fill the sheet when empty.")
|
||||
captureFrame("quick-question-empty-keyboard-visible", element: prompt)
|
||||
capture("quick-question-empty-keyboard-visible")
|
||||
|
||||
let growingDraft = (1...4).map { "Question line \($0)" }.joined(separator: "\n")
|
||||
UIPasteboard.general.string = growingDraft
|
||||
prompt.press(forDuration: 1.2)
|
||||
let paste = app.descendants(matching: .any)
|
||||
.matching(NSPredicate(format: "label == %@", "Paste"))
|
||||
.firstMatch
|
||||
XCTAssertTrue(paste.waitForExistence(timeout: 5), "The multiline fixture should be available to paste into the real text field.")
|
||||
paste.tap()
|
||||
|
||||
XCTAssertEqual(prompt.value as? String, growingDraft)
|
||||
let multilineHeight = prompt.frame.height
|
||||
captureFrame("quick-question-multiline-growing-keyboard-visible", element: prompt)
|
||||
capture("quick-question-multiline-growing-keyboard-visible")
|
||||
XCTAssertGreaterThan(multilineHeight, oneLineHeight * 2, "The shared vertical field should grow as lines are added.")
|
||||
XCTAssertLessThan(multilineHeight, oneLineHeight * 6 + 16, "The growing field should remain below its six-line cap.")
|
||||
|
||||
let appendedLines = (5...9).map { "\nQuestion line \($0)" }.joined()
|
||||
prompt.typeText(appendedLines)
|
||||
let cappedDraft = growingDraft + appendedLines
|
||||
XCTAssertEqual(prompt.value as? String, cappedDraft)
|
||||
let cappedHeight = prompt.frame.height
|
||||
captureFrame("quick-question-six-line-cap-keyboard-visible", element: prompt)
|
||||
capture("quick-question-six-line-cap-keyboard-visible")
|
||||
XCTAssertGreaterThan(cappedHeight, oneLineHeight * 5, "Nine lines should reach the existing six-line height cap.")
|
||||
XCTAssertLessThan(cappedHeight, oneLineHeight * 6 + 16, "Nine lines should remain inside the existing six-line visual cap.")
|
||||
XCTAssertEqual(
|
||||
prompt.value as? String,
|
||||
cappedDraft,
|
||||
"The capped field must retain all nine lines while its visible editor stays at the six-line cap."
|
||||
)
|
||||
}
|
||||
|
||||
func testQuickQuestionOneLineHeightMatchesNormalChatComposer() {
|
||||
app.launch()
|
||||
let agentButton = app.buttons["agent-sidebar"]
|
||||
if !agentButton.waitForExistence(timeout: 2) || !agentButton.isHittable {
|
||||
let showSidebar = app.buttons["Show sidebar"]
|
||||
XCTAssertTrue(showSidebar.waitForExistence(timeout: 15))
|
||||
showSidebar.tap()
|
||||
}
|
||||
XCTAssertTrue(agentButton.waitForExistence(timeout: 5))
|
||||
agentButton.tap()
|
||||
|
||||
let textField = app.textFields["agent-composer"]
|
||||
let textView = app.textViews["agent-composer"]
|
||||
XCTAssertTrue(textField.waitForExistence(timeout: 5) || textView.waitForExistence(timeout: 5))
|
||||
let normalComposer = textField.exists ? textField : textView
|
||||
let normalHeight = normalComposer.frame.height
|
||||
XCTAssertLessThan(normalHeight, 80, "The normal-chat composer should start at one line.")
|
||||
captureFrame("normal-chat-composer", element: normalComposer)
|
||||
capture("normal-chat-one-line-keyboard-hidden")
|
||||
|
||||
XCUIDevice.shared.press(.home)
|
||||
openQuickQuestionFromHomeScreen()
|
||||
let prompt = quickQuestionPromptElement()
|
||||
XCTAssertTrue(prompt.waitForExistence(timeout: 10))
|
||||
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
|
||||
let quickQuestionHeight = prompt.frame.height
|
||||
captureFrame("quick-question-composer-keyboard-visible", element: prompt)
|
||||
capture("quick-question-one-line-keyboard-visible")
|
||||
XCTAssertLessThan(quickQuestionHeight, 80, "Quick Question should start at one line.")
|
||||
XCTAssertEqual(
|
||||
quickQuestionHeight,
|
||||
normalHeight,
|
||||
accuracy: 4,
|
||||
"Quick Question should use the same one-line field sizing as normal chat."
|
||||
)
|
||||
}
|
||||
|
||||
private func quickQuestionPromptElement() -> XCUIElement {
|
||||
let textField = app.textFields["quick-question-prompt"]
|
||||
return textField.exists ? textField : app.textViews["quick-question-prompt"]
|
||||
}
|
||||
|
||||
private func openQuickQuestionFromHomeScreen() {
|
||||
XCUIDevice.shared.press(.home)
|
||||
let sybilIcon = springboard.icons.matching(identifier: "Sybil").firstMatch
|
||||
XCTAssertTrue(sybilIcon.waitForExistence(timeout: 10))
|
||||
let homeScreen = springboard.otherElements["Home screen icons"]
|
||||
XCTAssertTrue(homeScreen.waitForExistence(timeout: 5))
|
||||
for _ in 0..<2 {
|
||||
guard !sybilIcon.isHittable else {
|
||||
break
|
||||
}
|
||||
homeScreen.swipeLeft()
|
||||
}
|
||||
XCTAssertTrue(sybilIcon.isHittable, "Navigate to the home-screen page containing Sybil before opening its quick action.")
|
||||
sybilIcon.press(forDuration: 1.2)
|
||||
|
||||
let quickQuestion = springboard.buttons["Quick question"]
|
||||
@@ -53,17 +153,19 @@ final class QuickQuestionSelectionUITests: XCTestCase {
|
||||
quickQuestion.tap()
|
||||
}
|
||||
|
||||
private func dismissQuickQuestion(_ prompt: XCUIElement) {
|
||||
if app.keyboards.firstMatch.exists, app.scrollViews.firstMatch.exists {
|
||||
app.scrollViews.firstMatch.swipeDown()
|
||||
}
|
||||
private func capture(_ name: String) {
|
||||
let attachment = XCTAttachment(screenshot: app.screenshot())
|
||||
attachment.name = name
|
||||
attachment.lifetime = .keepAlways
|
||||
add(attachment)
|
||||
}
|
||||
|
||||
let grabber = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.08))
|
||||
let lowerSheet = app.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.92))
|
||||
grabber.press(forDuration: 0.1, thenDragTo: lowerSheet)
|
||||
|
||||
let dismissed = expectation(for: NSPredicate(format: "exists == false"), evaluatedWith: prompt)
|
||||
wait(for: [dismissed], timeout: 5)
|
||||
private func captureFrame(_ name: String, element: XCUIElement) {
|
||||
let frame = element.frame
|
||||
let attachment = XCTAttachment(string: "x=\(frame.origin.x), y=\(frame.origin.y), width=\(frame.width), height=\(frame.height)")
|
||||
attachment.name = "frame-\(name)"
|
||||
attachment.lifetime = .keepAlways
|
||||
add(attachment)
|
||||
}
|
||||
|
||||
private func control(_ body: [String: Any]) async throws {
|
||||
|
||||
Reference in New Issue
Block a user