From 44f995c3d09550e54581ff8e6343a5713d4cc19e Mon Sep 17 00:00:00 2001 From: Lichtkasten Dev Date: Wed, 4 Nov 2026 15:02:35 +0000 Subject: [PATCH] Vorschau: Bild im Einpassen-Modus zentriert (Hochformat nicht mehr am Rand), Zoom um die Bildmitte Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_012bGzgsxrmbBhDzeMV2L57D --- .../Lichtkasten/Views/ZoomableImageView.swift | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/Sources/Lichtkasten/Views/ZoomableImageView.swift b/Sources/Lichtkasten/Views/ZoomableImageView.swift index c8c714d..1f6c726 100644 --- a/Sources/Lichtkasten/Views/ZoomableImageView.swift +++ b/Sources/Lichtkasten/Views/ZoomableImageView.swift @@ -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 } }