Files
hapi/ios/Hapi/Features/Chat/MessageDetailView.swift
T
cb97565cdf feat(ios): polish chat chrome, session state and text selection
- Decode DSH sessions by accepting workspace-write / danger-full-access
  permission modes (the missing cases made the whole session detail fail to
  decode, so pending ask_user_question requests never reached the card).
- Carry background task snapshots from Session/SessionPatch and let the
  composer chip list the running task descriptions.
- Compact the nav subtitle to project + machine, add a tap-through session
  info sheet, and hide the duplicated status wording.
- Mute archived rows: desaturated agent icon, secondary title, hint-coloured
  detail/timestamp and an archivebox indicator.
- Add a Select Text action backed by a read-only UITextView so arbitrary
  ranges (including cross-paragraph) select with native handles; enable
  inline table selection too.
- Point home widget card taps at their session instead of the list, and keep
  the list link only as the empty fallback.
- Update LastSeen tests for the optimistic read watermark semantics.

via [HAPI](https://hapi.run)

Co-Authored-By: HAPI <noreply@hapi.run>
Co-Authored-By: Claude Code <noreply@anthropic.com>
2026-11-15 21:37:19 +08:00

153 lines
5.8 KiB
Swift

import HapiProtocol
import HapiUI
import SwiftUI
import UIKit
private struct OpenMessageKey: EnvironmentKey {
/// `(message id, full plain text)` — opens the selectable text sheet.
static let defaultValue: ((String, String) -> Void)? = nil
}
extension EnvironmentValues {
var openChatMessage: ((String, String) -> Void)? {
get { self[OpenMessageKey.self] }
set { self[OpenMessageKey.self] = newValue }
}
}
private struct MessageSelection: Identifiable {
let id: String
let text: String
}
/// A screen owns the sheet, never a recycled bubble. Retain the original
/// text as a read-only snapshot even if the message window trims that row.
struct MessagePresentationHost: ViewModifier {
let model: ChatModel
let owner: String
@State private var selection: MessageSelection?
func body(content: Content) -> some View {
content
.environment(\.openChatMessage, { id, text in
model.beginContentInspection()
model.retainSurface("message:\(owner)")
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
selection = MessageSelection(id: id, text: text)
})
.sheet(item: $selection, onDismiss: {
model.releaseSurface("message:\(owner)")
}) { message in
MessageDetailView(text: message.text).id(message.id)
}
.onChange(of: model.toolInspection.invalidation) {
selection = nil
}
}
}
struct MessageDetailView: View {
@State private var pager: MessageTextPager
@Environment(\.dismiss) private var dismiss
@Environment(\.hapiPasteboard) private var pasteboard
@Environment(\.hapiTypography) private var typography
@Environment(\.hapiTheme) private var theme
init(text: String) {
_pager = State(initialValue: MessageTextPager(source: text))
}
var body: some View {
NavigationStack {
// UIKit owns selection here: SwiftUI `Text` only produced the
// generic Copy/Share menu (no selection handles) inside a sheet.
SelectableMessageText(
text: pager.page.text,
font: .systemFont(ofSize: typography.bodySize,
weight: typography.boldText ? .semibold : .regular),
textColor: UIColor(theme.textPrimary),
accentColor: UIColor(theme.accent),
lineSpacing: typography.bodyLineSpacing
)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
.hapiReadingColumn()
.id(pager.number)
.accessibilityIdentifier("message-detail-text")
.navigationTitle("Full message")
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .cancellationAction) {
Button("Close", systemImage: "xmark") { dismiss() }
}
ToolbarItem(placement: .primaryAction) {
Button("Copy full content", systemImage: "doc.on.doc") { pasteboard.copy(pager.source) }
.accessibilityIdentifier("message-copy-full")
}
}
.safeAreaInset(edge: .bottom, spacing: 0) {
HStack {
Button { pager.previous() } label: {
Label("Previous part", systemImage: "chevron.left")
.labelStyle(.iconOnly).frame(minWidth: 44, minHeight: 44)
}
.disabled(!pager.hasPrevious)
.accessibilityIdentifier("message-previous")
Spacer()
Text(String(format: String(localized: "Part %lld"), Int64(pager.number)))
.font(.footnote.monospacedDigit())
Spacer()
Button { pager.next() } label: {
Label("Next part", systemImage: "chevron.right")
.labelStyle(.iconOnly).frame(minWidth: 44, minHeight: 44)
}
.disabled(!pager.hasNext)
.accessibilityIdentifier("message-next")
}
.hapiReadingColumn()
.background(.bar)
}
}
.hapiTypography()
.presentationDetents([.large])
.presentationDragIndicator(.visible)
}
}
/// Read-only `UITextView` bridge: native text selection (word highlight,
/// draggable handles, cross-paragraph ranges, system Copy/Share menu).
/// SwiftUI `Text.textSelection` did not engage inside this sheet.
private struct SelectableMessageText: UIViewRepresentable {
let text: String
let font: UIFont
let textColor: UIColor
let accentColor: UIColor
let lineSpacing: CGFloat
func makeUIView(context: Context) -> UITextView {
let view = UITextView()
view.isEditable = false
view.isSelectable = true
view.isScrollEnabled = true
view.alwaysBounceVertical = true
view.backgroundColor = .clear
view.dataDetectorTypes = []
view.textContainerInset = UIEdgeInsets(top: 16, left: 0, bottom: 16, right: 0)
view.textContainer.lineFragmentPadding = 0
view.tintColor = accentColor
view.adjustsFontForContentSizeCategory = false
return view
}
func updateUIView(_ view: UITextView, context: Context) {
let paragraph = NSMutableParagraphStyle()
paragraph.lineSpacing = lineSpacing
let attributes: [NSAttributedString.Key: Any] = [
.font: font,
.foregroundColor: textColor,
.paragraphStyle: paragraph,
]
view.attributedText = NSAttributedString(string: text, attributes: attributes)
view.tintColor = accentColor
}
}