Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c15357108 | ||
|
|
ea6ee8cc64 | ||
|
|
557775af45 |
@@ -1,7 +1,6 @@
|
||||
import MarkdownUI
|
||||
import Observation
|
||||
import SwiftUI
|
||||
import UIKit
|
||||
|
||||
struct SybilQuickQuestionView: View {
|
||||
@Bindable var viewModel: SybilViewModel
|
||||
@@ -9,6 +8,7 @@ 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,6 +37,8 @@ struct SybilQuickQuestionView: View {
|
||||
return
|
||||
}
|
||||
promptFocused = true
|
||||
let prompt = viewModel.quickQuestionPrompt
|
||||
promptSelection = TextSelection(range: prompt.startIndex..<prompt.endIndex)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,29 +95,17 @@ 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: .horizontal,
|
||||
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")
|
||||
@@ -218,113 +208,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,6 +1,6 @@
|
||||
import XCTest
|
||||
|
||||
/// Uses the repository's 127.0.0.1 contract fixture and the real iOS home-screen shortcut.
|
||||
/// Exercises Quick Question through its real home-screen action and shared SwiftUI field.
|
||||
@MainActor
|
||||
final class QuickQuestionSelectionUITests: XCTestCase {
|
||||
private let app = XCUIApplication()
|
||||
@@ -19,33 +19,60 @@ final class QuickQuestionSelectionUITests: XCTestCase {
|
||||
app.terminate()
|
||||
}
|
||||
|
||||
func testPrefilledDraftIsSelectedOnReopenAndTypingReplacesIt() async throws {
|
||||
func testQuickQuestionKeepsKeyboardVisibleAcrossSeparateKeypressesAndStaysCompact() {
|
||||
app.launch()
|
||||
openQuickQuestionFromHomeScreen()
|
||||
|
||||
let prompt = app.textFields["quick-question-prompt"]
|
||||
let prompt = quickQuestionPrompt()
|
||||
XCTAssertTrue(prompt.waitForExistence(timeout: 10))
|
||||
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
|
||||
XCTAssertLessThan(prompt.frame.height, 80, "Quick Question should retain its original compact single-line field.")
|
||||
|
||||
prompt.typeText("a")
|
||||
XCTAssertTrue(app.keyboards.firstMatch.exists, "Keyboard should remain visible after the first keypress.")
|
||||
prompt.typeText("b")
|
||||
XCTAssertTrue(app.keyboards.firstMatch.exists, "Keyboard should remain visible after the second keypress.")
|
||||
prompt.typeText("c")
|
||||
XCTAssertTrue(app.keyboards.firstMatch.exists, "Keyboard should remain visible after the third keypress.")
|
||||
XCTAssertEqual(prompt.value as? String, "abc")
|
||||
}
|
||||
|
||||
func testQuickQuestionReappearanceSelectsRetainedDraftForReplacement() {
|
||||
app.launch()
|
||||
openQuickQuestionFromHomeScreen()
|
||||
|
||||
let prompt = quickQuestionPrompt()
|
||||
XCTAssertTrue(prompt.waitForExistence(timeout: 10))
|
||||
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
|
||||
prompt.typeText("retained draft")
|
||||
XCTAssertEqual(prompt.value as? String, "retained draft")
|
||||
|
||||
dismissQuickQuestion(prompt)
|
||||
// A second real home-screen Quick Question action advances the existing
|
||||
// focus request. The original TextSelection binding selects the draft.
|
||||
openQuickQuestionFromHomeScreen()
|
||||
|
||||
XCTAssertTrue(prompt.waitForExistence(timeout: 10))
|
||||
XCTAssertTrue(app.keyboards.firstMatch.waitForExistence(timeout: 10))
|
||||
XCTAssertEqual(prompt.value as? String, "retained draft")
|
||||
|
||||
// The field is already focused with its initial draft selected. Do not tap
|
||||
// it here: typing immediately is the behavior under test.
|
||||
prompt.typeText("replacement")
|
||||
XCTAssertEqual(prompt.value as? String, "replacement")
|
||||
}
|
||||
|
||||
private func quickQuestionPrompt() -> XCUIElement {
|
||||
app.textFields["Ask anything..."]
|
||||
}
|
||||
|
||||
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"]
|
||||
if !sybilIcon.isHittable, homeScreen.waitForExistence(timeout: 5) {
|
||||
for _ in 0..<2 where !sybilIcon.isHittable {
|
||||
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,19 +80,6 @@ final class QuickQuestionSelectionUITests: XCTestCase {
|
||||
quickQuestion.tap()
|
||||
}
|
||||
|
||||
private func dismissQuickQuestion(_ prompt: XCUIElement) {
|
||||
if app.keyboards.firstMatch.exists, app.scrollViews.firstMatch.exists {
|
||||
app.scrollViews.firstMatch.swipeDown()
|
||||
}
|
||||
|
||||
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 control(_ body: [String: Any]) async throws {
|
||||
var request = URLRequest(url: URL(string: "http://127.0.0.1:8799/control")!)
|
||||
request.httpMethod = "POST"
|
||||
|
||||
Reference in New Issue
Block a user