mirror of
https://github.com/wu736139669/hapi.git
synced 2026-12-04 06:57:55 +00:00
fix(ios): keep the session settings sheet stable while chat streams
ChatView rebuilt SessionConfigView (and a fresh SessionConfigModel) on every streaming update because the popover content closure lived in the toolbar body. That allocation churn made a long session feel laggy with the sheet open, and replacing the sheet content reset its @State detent, so scrolling up snapped the sheet back down and rescaled the chat behind it. - Own the SessionConfigModel and selected detent in ChatView so they survive body updates. - Host the popover content once instead of rebuilding it per update. - Make the sheet's scroll view own the gesture (presentationContentInteraction(.scrolls)) so reaching the top does not resize/collapse the sheet. Verified: iOS simulator build; SessionConfigPresentationTests pass. via [HAPI](https://hapi.run) Co-Authored-By: HAPI <noreply@hapi.run> Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -7,6 +7,10 @@ import SwiftUI
|
||||
struct SessionConfigButton<Content: View>: 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<Content: View>: 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<Content: View>: View {
|
||||
struct SessionConfigView: View {
|
||||
@State private var model: SessionConfigModel
|
||||
@State private var detent: PresentationDetent = .medium
|
||||
private let detentBinding: Binding<PresentationDetent>?
|
||||
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<PresentationDetent>? = 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<PresentationDetent> {
|
||||
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() }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user