diff --git a/ios/Hapi/Features/Chat/ChatView.swift b/ios/Hapi/Features/Chat/ChatView.swift index 3e3fdc06..9bba8546 100644 --- a/ios/Hapi/Features/Chat/ChatView.swift +++ b/ios/Hapi/Features/Chat/ChatView.swift @@ -15,7 +15,11 @@ struct ChatView: View { @Environment(\.horizontalSizeClass) private var horizontalSizeClass @Environment(\.scenePhase) private var scenePhase @State private var model: ChatModel - /// Adaptive session settings (toolbar gear). + /// Adaptive session settings (toolbar gear). The model lives with the + /// screen: rebuilding it from ChatView's body during streaming churned + /// allocations and could reset the sheet's selected detent. + @State private var configModel: SessionConfigModel + @State private var configDetent: PresentationDetent = .medium @State private var configOpen = false /// Files browser push (toolbar folder, A-M4a). @State private var filesOpen = false @@ -67,6 +71,7 @@ struct ChatView: View { init(session: HubSession, model: ChatModel, onNavigateToSession: ((String) -> Void)? = nil) { _model = State(initialValue: model) + _configModel = State(initialValue: SessionConfigModel(interactor: model.interactor)) self.session = session self.sessionId = model.sessionId self.onNavigateToSession = onNavigateToSession @@ -154,7 +159,7 @@ struct ChatView: View { // switches, everything else behind one menu. ToolbarItem(placement: .topBarTrailing) { SessionConfigButton(isPresented: $configOpen) { - SessionConfigView(interactor: model.interactor, notice: model.notice) + SessionConfigView(model: configModel, notice: model.notice, detent: $configDetent) } } ToolbarItem(placement: .topBarTrailing) { diff --git a/ios/Hapi/Features/Chat/SessionConfigView.swift b/ios/Hapi/Features/Chat/SessionConfigView.swift index 979c7a1b..3d2d1ba0 100644 --- a/ios/Hapi/Features/Chat/SessionConfigView.swift +++ b/ios/Hapi/Features/Chat/SessionConfigView.swift @@ -7,6 +7,10 @@ import SwiftUI struct SessionConfigButton: View { @Binding var isPresented: Bool @ViewBuilder let content: () -> Content + /// Host the sheet content once. ChatView's body re-evaluates on every + /// streaming update; rebuilding the settings content there discarded its + /// sheet state (including the selected detent) and churned allocations. + @State private var hostedContent: Content? @Environment(\.horizontalSizeClass) private var horizontalSizeClass @Environment(\.verticalSizeClass) private var verticalSizeClass @Environment(\.dynamicTypeSize) private var dynamicTypeSize @@ -17,23 +21,33 @@ struct SessionConfigButton: View { var body: some View { Button { + if hostedContent == nil { hostedContent = content() } isPresented = true } label: { Image(systemName: "gearshape") } .accessibilityLabel("Session settings") .accessibilityIdentifier("session-config-open") + .onChange(of: isPresented, initial: true) { _, presented in + // Programmatic presentation (deep link, tests, accessibility) + // skips the button action; host the content before the popover asks. + if presented, hostedContent == nil { hostedContent = content() } + } .popover(isPresented: $isPresented, attachmentAnchor: .rect(.bounds), arrowEdge: .top) { - content() - // Preferred, not fixed: the system can constrain a small window. - // Read the host's traits; popover content has its own traits. - .frame( - idealWidth: prefersPopover ? 400 : nil, - maxWidth: prefersPopover ? 400 : nil, - idealHeight: prefersPopover ? popoverHeight : nil, - maxHeight: prefersPopover ? popoverHeight : nil - ) - .presentationCompactAdaptation(.sheet) + Group { + if let hostedContent { + hostedContent + // Preferred, not fixed: the system can constrain a small window. + // Read the host's traits; popover content has its own traits. + .frame( + idealWidth: prefersPopover ? 400 : nil, + maxWidth: prefersPopover ? 400 : nil, + idealHeight: prefersPopover ? popoverHeight : nil, + maxHeight: prefersPopover ? popoverHeight : nil + ) + } + } + .presentationCompactAdaptation(.sheet) } } @@ -45,6 +59,7 @@ struct SessionConfigButton: View { struct SessionConfigView: View { @State private var model: SessionConfigModel @State private var detent: PresentationDetent = .medium + private let detentBinding: Binding? let notice: String? @Environment(\.dismiss) private var dismiss @@ -54,9 +69,20 @@ struct SessionConfigView: View { self.init(model: SessionConfigModel(interactor: interactor), notice: notice) } - init(model: SessionConfigModel, notice: String? = nil) { + init(model: SessionConfigModel, notice: String? = nil, detent: Binding? = nil) { _model = State(initialValue: model) self.notice = notice + self.detentBinding = detent + } + + /// The sheet's scroll view owns the gesture: scrolling to the top must not + /// collapse/resize the sheet (and scale the chat behind it). + private var selectedDetent: Binding { + if let detentBinding { return detentBinding } + return Binding( + get: { dynamicTypeSize.isAccessibilitySize ? .large : detent }, + set: { detent = $0 } + ) } var body: some View { @@ -69,11 +95,9 @@ struct SessionConfigView: View { .safeAreaInset(edge: .bottom, spacing: 0) { feedback } .presentationDetents( dynamicTypeSize.isAccessibilitySize ? [.large] : [.medium, .large], - selection: Binding( - get: { dynamicTypeSize.isAccessibilitySize ? .large : detent }, - set: { detent = $0 } - ) + selection: selectedDetent ) + .presentationContentInteraction(.scrolls) .onAppear { model.loadModels() } }