From 7c15357108073b93957a66572d5c5bf2e83f401f Mon Sep 17 00:00:00 2001 From: James Magahern Date: Tue, 17 Nov 2026 21:44:44 -0800 Subject: [PATCH] fix(ios): restore Quick Question composer behavior --- .../Sources/Sybil/SybilPromptField.swift | 171 +------------ .../Sybil/SybilQuickQuestionView.swift | 41 +--- ios/Tests/Fixtures/agent_server.py | 11 +- .../QuickQuestionSelectionUITests.swift | 225 +++--------------- 4 files changed, 44 insertions(+), 404 deletions(-) diff --git a/ios/Packages/Sybil/Sources/Sybil/SybilPromptField.swift b/ios/Packages/Sybil/Sources/Sybil/SybilPromptField.swift index fa8ec33..c0ed974 100644 --- a/ios/Packages/Sybil/Sources/Sybil/SybilPromptField.swift +++ b/ios/Packages/Sybil/Sources/Sybil/SybilPromptField.swift @@ -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? = 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? - 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 diff --git a/ios/Packages/Sybil/Sources/Sybil/SybilQuickQuestionView.swift b/ios/Packages/Sybil/Sources/Sybil/SybilQuickQuestionView.swift index c4fe8e4..1952c6d 100644 --- a/ios/Packages/Sybil/Sources/Sybil/SybilQuickQuestionView.swift +++ b/ios/Packages/Sybil/Sources/Sybil/SybilQuickQuestionView.swift @@ -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.. XCUIElement { - let textField = app.textFields["quick-question-prompt"] - return textField.exists ? textField : app.textViews["quick-question-prompt"] + private func quickQuestionPrompt() -> XCUIElement { + app.textFields["Ask anything..."] } private func openQuickQuestionFromHomeScreen() { @@ -213,12 +67,10 @@ final class QuickQuestionSelectionUITests: XCTestCase { 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 + if !sybilIcon.isHittable, homeScreen.waitForExistence(timeout: 5) { + for _ in 0..<2 where !sybilIcon.isHittable { + homeScreen.swipeLeft() } - homeScreen.swipeLeft() } XCTAssertTrue(sybilIcon.isHittable, "Navigate to the home-screen page containing Sybil before opening its quick action.") sybilIcon.press(forDuration: 1.2) @@ -228,29 +80,12 @@ final class QuickQuestionSelectionUITests: XCTestCase { quickQuestion.tap() } - private func capture(_ name: String) { - let attachment = XCTAttachment(screenshot: app.screenshot()) - attachment.name = name - attachment.lifetime = .keepAlways - add(attachment) - } - - 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) - } - - @discardableResult - private func control(_ body: [String: Any]) async throws -> [String: Any] { + private func control(_ body: [String: Any]) async throws { var request = URLRequest(url: URL(string: "http://127.0.0.1:8799/control")!) request.httpMethod = "POST" request.httpBody = try JSONSerialization.data(withJSONObject: body) request.setValue("application/json", forHTTPHeaderField: "Content-Type") - let (data, response) = try await URLSession.shared.data(for: request) + let (_, response) = try await URLSession.shared.data(for: request) XCTAssertEqual((response as? HTTPURLResponse)?.statusCode, 200) - return try XCTUnwrap(JSONSerialization.jsonObject(with: data) as? [String: Any]) } }