Vorschau: Bild im Einpassen-Modus zentriert (Hochformat nicht mehr am Rand), Zoom um die Bildmitte

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D
This commit is contained in:
Lichtkasten DevandClaude Opus 5.5 committed 2026-11-04 15:02:35 +00:00
1 parent ec1356911e
commit 44f995c3d0
1 file changed
+21 -9
@@ -31,12 +31,14 @@ final class FitScrollView: NSScrollView {
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
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))
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)
}
@@ -85,7 +87,12 @@ struct ZoomableImageView: NSViewRepresentable {
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 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
@@ -98,10 +105,14 @@ struct ZoomableImageView: NSViewRepresentable {
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)
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 { sv.animator().magnification = next }
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) }
}
@@ -113,8 +124,9 @@ 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 }
// 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
}
}