diff --git a/ios/Hapi/Features/Chat/Attachments/AttachmentPreviewView.swift b/ios/Hapi/Features/Chat/Attachments/AttachmentPreviewView.swift index 76e49a64..ef598702 100644 --- a/ios/Hapi/Features/Chat/Attachments/AttachmentPreviewView.swift +++ b/ios/Hapi/Features/Chat/Attachments/AttachmentPreviewView.swift @@ -2,6 +2,7 @@ import CoreGraphics import HapiClient import HapiProtocol import SwiftUI +import UIKit /// One user-bubble attachment: image mimes with a decodable `previewUrl` /// data URL render a thumbnail (web `MessageAttachments` split); everything @@ -18,36 +19,50 @@ struct AttachmentPreviewView: View { /// Decode still running — render a sized neutral placeholder. case loading - case ready(Image) + case ready(UIImage) /// Not a data URL / undecodable — fall back to the filename chip. case unavailable } @State private var phase: Phase = .loading + @State private var imageOpen = false var body: some View { - if !attachment.mimeType.hasPrefix("image/") || attachment.previewUrl == nil { - AttachmentChipView(attachment: attachment) - } else { - switch phase { - case .ready(let image): - image - .resizable() - .scaledToFit() - .frame(maxHeight: 180) - .clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous)) - case .loading: - // Sized placeholder while decoding keeps the bubble from - // jumping. - RoundedRectangle(cornerRadius: 10, style: .continuous) - .fill(.background.opacity(0.4)) - .frame(width: 120, height: 90) - .task(id: attachment.previewUrl) { - await decode() - } - case .unavailable: + Group { + if !attachment.mimeType.hasPrefix("image/") || attachment.previewUrl == nil { AttachmentChipView(attachment: attachment) + } else { + switch phase { + case .ready(let image): + Button { + imageOpen = true + } label: { + Image(uiImage: image) + .resizable() + .scaledToFit() + .frame(maxHeight: 180) + .clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous)) + } + .buttonStyle(.plain) + .accessibilityLabel(Text(verbatim: attachment.filename)) + case .loading: + // Sized placeholder while decoding keeps the bubble from + // jumping. + RoundedRectangle(cornerRadius: 10, style: .continuous) + .fill(.background.opacity(0.4)) + .frame(width: 120, height: 90) + .task(id: attachment.previewUrl) { + await decode() + } + case .unavailable: + AttachmentChipView(attachment: attachment) + } + } + } + .fullScreenCover(isPresented: $imageOpen) { + if case .ready(let image) = phase { + ZoomableImageViewer(image: image, title: attachment.filename) } } } @@ -60,7 +75,7 @@ struct AttachmentPreviewView: View { // Nonisolated async helper — runs off the main actor. let decoded = await Self.decodeImage(previewUrl: previewUrl) if let decoded { - phase = .ready(Image(decoded, scale: 1, label: Text(attachment.filename))) + phase = .ready(UIImage(cgImage: decoded, scale: 1, orientation: .up)) } else { phase = .unavailable } diff --git a/ios/Hapi/Features/Chat/Blocks/GeneratedImageBlockView.swift b/ios/Hapi/Features/Chat/Blocks/GeneratedImageBlockView.swift index 7d5bf0df..1c73b0aa 100644 --- a/ios/Hapi/Features/Chat/Blocks/GeneratedImageBlockView.swift +++ b/ios/Hapi/Features/Chat/Blocks/GeneratedImageBlockView.swift @@ -78,7 +78,9 @@ private struct GeneratedMediaCard: View { if phase != .active { content.pause() } } .fullScreenCover(isPresented: $imageOpen) { - ImageViewer(image: content.image, title: content.block.fileName) + if let image = content.image { + ZoomableImageViewer(image: image, title: content.block.fileName) + } } .sheet(isPresented: $previewOpen) { if let file = content.file { @@ -190,31 +192,3 @@ private struct GeneratedMediaPreview: UIViewControllerRepresentable { } /// Minimal full-screen image viewer; keep the existing tap-to-dismiss behavior. -private struct ImageViewer: View { - let image: UIImage? - let title: String - @Environment(\.dismiss) private var dismiss - - var body: some View { - ZStack(alignment: .topTrailing) { - Color.black.ignoresSafeArea() - if let image { - Image(uiImage: image) - .resizable() - .scaledToFit() - .frame(maxWidth: .infinity, maxHeight: .infinity) - .padding(8) - } else { - Text(verbatim: title).foregroundStyle(.white) - } - Button { dismiss() } label: { - Image(systemName: "xmark.circle.fill") - .font(.title2) - .foregroundStyle(.white.opacity(0.8)) - .padding(16) - } - .accessibilityLabel("Close") - } - .onTapGesture { dismiss() } - } -} diff --git a/ios/Hapi/Features/Chat/ImageZoomView.swift b/ios/Hapi/Features/Chat/ImageZoomView.swift new file mode 100644 index 00000000..b7cb158a --- /dev/null +++ b/ios/Hapi/Features/Chat/ImageZoomView.swift @@ -0,0 +1,160 @@ +import SwiftUI +import UIKit + +/// Zoom bounds for the full-screen image viewer. Pure so the drag clamps can +/// be tested without gesture plumbing. +enum ImageZoomMath { + static let minScale: CGFloat = 1 + static let maxScale: CGFloat = 8 + + static func clampedScale(_ value: CGFloat) -> CGFloat { + min(maxScale, max(minScale, value)) + } + + /// Aspect-fit size of the image inside `container`. + static func fittedSize(for imageSize: CGSize, in container: CGSize) -> CGSize { + guard imageSize.width > 0, imageSize.height > 0, + container.width > 0, container.height > 0 else { return .zero } + let imageAspect = imageSize.width / imageSize.height + let containerAspect = container.width / container.height + if imageAspect > containerAspect { + return CGSize(width: container.width, height: container.width / imageAspect) + } + return CGSize(width: container.height * imageAspect, height: container.height) + } + + /// Keep the scaled image edges from drifting inside the viewport. + static func clampedOffset( + _ offset: CGSize, + scale: CGFloat, + imageSize: CGSize, + container: CGSize + ) -> CGSize { + let fitted = fittedSize(for: imageSize, in: container) + let maxX = max(0, (fitted.width * scale - container.width) / 2) + let maxY = max(0, (fitted.height * scale - container.height) / 2) + return CGSize( + width: min(maxX, max(-maxX, offset.width)), + height: min(maxY, max(-maxY, offset.height)) + ) + } +} + +/// Full-screen image with pinch zoom, drag panning at >1x, and double-tap +/// zoom. Used by generated media and user-message attachments. +struct ZoomableImageViewer: View { + let image: UIImage + let title: String + + @Environment(\.dismiss) private var dismiss + @State private var scale: CGFloat = 1 + @State private var lastScale: CGFloat = 1 + @State private var offset: CGSize = .zero + @State private var lastOffset: CGSize = .zero + + var body: some View { + GeometryReader { proxy in + let container = proxy.size + ZStack { + Color.black.ignoresSafeArea() + Image(uiImage: image) + .resizable() + .scaledToFit() + .scaleEffect(scale) + .offset(offset) + .gesture(magnification(container: container)) + .simultaneousGesture(drag(container: container)) + .onTapGesture(count: 2) { + toggleZoom(container: container) + } + .accessibilityLabel(Text(verbatim: title)) + } + .overlay(alignment: .topTrailing) { + Button { + dismiss() + } label: { + Image(systemName: "xmark.circle.fill") + .font(.title2) + .foregroundStyle(.white.opacity(0.85)) + .frame(width: 44, height: 44) + .contentShape(Rectangle()) + } + .padding(.top, 4) + .padding(.trailing, 8) + .accessibilityLabel("Close") + } + } + .statusBarHidden() + } + + private func magnification(container: CGSize) -> some Gesture { + MagnifyGesture() + .onChanged { value in + scale = ImageZoomMath.clampedScale(lastScale * value.magnification) + offset = ImageZoomMath.clampedOffset( + offset, + scale: scale, + imageSize: image.size, + container: container + ) + } + .onEnded { _ in + lastScale = scale + if scale <= ImageZoomMath.minScale { + resetZoom() + } else { + offset = ImageZoomMath.clampedOffset( + offset, + scale: scale, + imageSize: image.size, + container: container + ) + lastOffset = offset + } + } + } + + private func drag(container: CGSize) -> some Gesture { + DragGesture() + .onChanged { value in + guard scale > ImageZoomMath.minScale else { return } + offset = ImageZoomMath.clampedOffset( + CGSize( + width: lastOffset.width + value.translation.width, + height: lastOffset.height + value.translation.height + ), + scale: scale, + imageSize: image.size, + container: container + ) + } + .onEnded { _ in + lastOffset = offset + } + } + + private func toggleZoom(container: CGSize) { + withAnimation(.easeInOut(duration: 0.2)) { + if scale > ImageZoomMath.minScale { + resetZoom() + } else { + scale = 2.5 + lastScale = 2.5 + offset = ImageZoomMath.clampedOffset( + .zero, + scale: scale, + imageSize: image.size, + container: container + ) + lastOffset = offset + } + } + } + + private func resetZoom() { + scale = ImageZoomMath.minScale + lastScale = ImageZoomMath.minScale + offset = .zero + lastOffset = .zero + } +} diff --git a/ios/HapiTests/ImageZoomMathTests.swift b/ios/HapiTests/ImageZoomMathTests.swift new file mode 100644 index 00000000..4349cecb --- /dev/null +++ b/ios/HapiTests/ImageZoomMathTests.swift @@ -0,0 +1,64 @@ +import XCTest +@testable import Hapi + +final class ImageZoomMathTests: XCTestCase { + func testScaleStaysWithinBounds() { + XCTAssertEqual(ImageZoomMath.clampedScale(0.2), 1) + XCTAssertEqual(ImageZoomMath.clampedScale(3), 3) + XCTAssertEqual(ImageZoomMath.clampedScale(99), 8) + } + + func testFittedSizeUsesTheTighterAxis() { + let landscape = ImageZoomMath.fittedSize( + for: CGSize(width: 400, height: 200), + in: CGSize(width: 300, height: 300) + ) + XCTAssertEqual(landscape.width, 300) + XCTAssertEqual(landscape.height, 150) + + let portrait = ImageZoomMath.fittedSize( + for: CGSize(width: 200, height: 400), + in: CGSize(width: 300, height: 300) + ) + XCTAssertEqual(portrait.width, 150) + XCTAssertEqual(portrait.height, 300) + } + + func testOffsetIsClampedToTheScaledImageEdges() { + let image = CGSize(width: 400, height: 200) + let container = CGSize(width: 300, height: 300) + + // At 1x the image fits horizontally, so horizontal panning is locked. + XCTAssertEqual( + ImageZoomMath.clampedOffset( + CGSize(width: 200, height: 0), + scale: 1, + imageSize: image, + container: container + ), + .zero + ) + + // At 2x there is 150 pt of horizontal slack on each side. + XCTAssertEqual( + ImageZoomMath.clampedOffset( + CGSize(width: 500, height: 500), + scale: 2, + imageSize: image, + container: container + ), + CGSize(width: 150, height: 0) + ) + } + + func testDegenerateSizesDoNotProduceNaN() { + XCTAssertEqual( + ImageZoomMath.fittedSize(for: .zero, in: CGSize(width: 300, height: 300)), + .zero + ) + XCTAssertEqual( + ImageZoomMath.clampedOffset(.zero, scale: 2, imageSize: .zero, container: .zero), + .zero + ) + } +}