import AppKit import SwiftUI enum ZoomAction: Equatable { case fit, actual, zoomIn, zoomOut } struct ZoomRequest: Equatable { let action: ZoomAction let token = UUID() } /// Scroll view that keeps the image fitted while in "fit" mode (also on window resize). final class FitScrollView: NSScrollView { var fitMode = true var onMagnification: ((CGFloat, Bool) -> Void)? var fitMagnification: CGFloat { guard let doc = documentView, doc.frame.width > 0, doc.frame.height > 0 else { return 1 } let size = contentView.bounds.size.applying(CGAffineTransform(scaleX: magnification, y: magnification)) return min(size.width / doc.frame.width, size.height / doc.frame.height) } override func layout() { super.layout() if fitMode { applyFit() } } func applyFit() { let m = fitMagnification minMagnification = min(m, 0.05) magnification = m centerDocument() } func centerDocument() { guard let doc = documentView else { return } let clip = contentView.bounds.size let x = max(0, (doc.frame.width - clip.width) / 2) let y = max(0, (doc.frame.height - clip.height) / 2) contentView.scroll(to: NSPoint(x: x, y: y)) reflectScrolledClipView(contentView) } override func magnify(with event: NSEvent) { super.magnify(with: event) fitMode = false onMagnification?(magnification, false) } } /// PM-style loupe: fit, 100 % (Z), pinch / +/- up to `maxZoom`. struct ZoomableImageView: NSViewRepresentable { let image: NSImage let maxZoom: CGFloat let request: ZoomRequest? let onZoomChange: (CGFloat, Bool) -> Void // magnification, isFit final class Coordinator { var lastToken: UUID?; var lastImage: NSImage? } func makeCoordinator() -> Coordinator { Coordinator() } func makeNSView(context: Context) -> FitScrollView { let sv = FitScrollView() sv.hasHorizontalScroller = true sv.hasVerticalScroller = true sv.autohidesScrollers = true sv.allowsMagnification = true sv.drawsBackground = true sv.backgroundColor = .black sv.contentView = CenteringClipView() let iv = NSImageView() iv.imageScaling = .scaleAxesIndependently sv.documentView = iv sv.onMagnification = { m, fit in onZoomChange(m, fit) } return sv } func updateNSView(_ sv: FitScrollView, context: Context) { sv.maxMagnification = maxZoom guard let iv = sv.documentView as? NSImageView else { return } if context.coordinator.lastImage !== image { context.coordinator.lastImage = image // Document size in points = pixel size / backing scale → magnification 1 == 100 % pixels. let scale = sv.window?.backingScaleFactor ?? 2 let px = image.representations.first.map { CGSize(width: $0.pixelsWide, height: $0.pixelsHigh) } ?? image.size let oldFit = sv.fitMode let oldMag = sv.magnification iv.image = image iv.frame = NSRect(origin: .zero, size: CGSize(width: px.width / scale, height: px.height / scale)) if oldFit { sv.applyFit() } else { sv.magnification = min(oldMag, maxZoom) } } if let req = request, req.token != context.coordinator.lastToken { context.coordinator.lastToken = req.token switch req.action { case .fit: sv.fitMode = true sv.applyFit() case .actual: sv.fitMode = false sv.setMagnification(1, centeredAt: NSPoint(x: iv.frame.midX, y: iv.frame.midY)) case .zoomIn: sv.fitMode = false sv.animator().magnification = min(maxZoom, sv.magnification * 1.5) case .zoomOut: let next = sv.magnification / 1.5 if next <= sv.fitMagnification { sv.fitMode = true; sv.applyFit() } else { sv.animator().magnification = next } } DispatchQueue.main.async { onZoomChange(sv.magnification, sv.fitMode) } } } } /// Keeps a smaller-than-viewport image centered. final class CenteringClipView: NSClipView { override func constrainBoundsRect(_ proposedBounds: NSRect) -> NSRect { var rect = super.constrainBoundsRect(proposedBounds) guard let doc = documentView else { return rect } if rect.width > doc.frame.width { rect.origin.x = (doc.frame.width - rect.width) / 2 } if rect.height > doc.frame.height { rect.origin.y = (doc.frame.height - rect.height) / 2 } return rect } }