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:
1 parent
ec1356911e
commit
44f995c3d0
1 file changed
+21
-9
@@ -31,12 +31,14 @@ final class FitScrollView: NSScrollView {
|
|||||||
centerDocument()
|
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() {
|
func centerDocument() {
|
||||||
guard let doc = documentView else { return }
|
guard let doc = documentView else { return }
|
||||||
let clip = contentView.bounds.size
|
let clip = contentView.bounds.size // in document coordinates (already magnified)
|
||||||
let x = max(0, (doc.frame.width - clip.width) / 2)
|
let x = (doc.frame.width - clip.width) / 2
|
||||||
let y = max(0, (doc.frame.height - clip.height) / 2)
|
let y = (doc.frame.height - clip.height) / 2
|
||||||
contentView.scroll(to: NSPoint(x: x, y: y))
|
contentView.setBoundsOrigin(NSPoint(x: x, y: y))
|
||||||
reflectScrolledClipView(contentView)
|
reflectScrolledClipView(contentView)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,7 +87,12 @@ struct ZoomableImageView: NSViewRepresentable {
|
|||||||
let oldMag = sv.magnification
|
let oldMag = sv.magnification
|
||||||
iv.image = image
|
iv.image = image
|
||||||
iv.frame = NSRect(origin: .zero, size: CGSize(width: px.width / scale, height: px.height / scale))
|
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 {
|
if let req = request, req.token != context.coordinator.lastToken {
|
||||||
context.coordinator.lastToken = req.token
|
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))
|
sv.setMagnification(1, centeredAt: NSPoint(x: iv.frame.midX, y: iv.frame.midY))
|
||||||
case .zoomIn:
|
case .zoomIn:
|
||||||
sv.fitMode = false
|
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:
|
case .zoomOut:
|
||||||
let next = sv.magnification / 1.5
|
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) }
|
DispatchQueue.main.async { onZoomChange(sv.magnification, sv.fitMode) }
|
||||||
}
|
}
|
||||||
@@ -113,8 +124,9 @@ final class CenteringClipView: NSClipView {
|
|||||||
override func constrainBoundsRect(_ proposedBounds: NSRect) -> NSRect {
|
override func constrainBoundsRect(_ proposedBounds: NSRect) -> NSRect {
|
||||||
var rect = super.constrainBoundsRect(proposedBounds)
|
var rect = super.constrainBoundsRect(proposedBounds)
|
||||||
guard let doc = documentView else { return rect }
|
guard let doc = documentView else { return rect }
|
||||||
if rect.width > doc.frame.width { rect.origin.x = (doc.frame.width - rect.width) / 2 }
|
// Image smaller than the view in a direction → keep it centered in that direction (fit + zoomed).
|
||||||
if rect.height > doc.frame.height { rect.origin.y = (doc.frame.height - rect.height) / 2 }
|
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
|
return rect
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user