Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
133 lines
5.3 KiB
Swift
133 lines
5.3 KiB
Swift
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()
|
||
}
|
||
|
||
/// Centers the image in the view. A portrait image in a landscape window gets a negative origin –
|
||
/// that is what keeps it in the middle instead of at the left edge.
|
||
func centerDocument() {
|
||
guard let doc = documentView else { return }
|
||
let clip = contentView.bounds.size // in document coordinates (already magnified)
|
||
let x = (doc.frame.width - clip.width) / 2
|
||
let y = (doc.frame.height - clip.height) / 2
|
||
contentView.setBoundsOrigin(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()
|
||
DispatchQueue.main.async { if sv.fitMode { sv.applyFit() } } // after the first layout pass
|
||
} 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
|
||
let mid = NSPoint(x: sv.contentView.bounds.midX, y: sv.contentView.bounds.midY)
|
||
sv.setMagnification(min(maxZoom, sv.magnification * 1.5), centeredAt: mid)
|
||
case .zoomOut:
|
||
let next = sv.magnification / 1.5
|
||
if next <= sv.fitMagnification { sv.fitMode = true; sv.applyFit() } else {
|
||
let mid = NSPoint(x: sv.contentView.bounds.midX, y: sv.contentView.bounds.midY)
|
||
sv.setMagnification(next, centeredAt: mid)
|
||
}
|
||
}
|
||
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 }
|
||
// Image smaller than the view in a direction → keep it centered in that direction (fit + zoomed).
|
||
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
|
||
}
|
||
}
|