PulseCanvas / app / Sources / PulseCanvas / CanvasPreviewLayout.swift
| 1 | import Foundation |
| 2 | |
| 3 | /// Short canvases share their title/footer area with the image instead of |
| 4 | /// subtracting fixed chrome that can leave the renderer only a few pixels high. |
| 5 | struct CanvasPreviewLayout { |
| 6 | let overlaysChrome: Bool |
| 7 | let padding: CGFloat |
| 8 | let preview: CGRect |
| 9 | |
| 10 | init(size: CGSize, aspectRatio: Double) { |
| 11 | // Reintroduce title space gradually as the window grows, so crossing a |
| 12 | // compact-layout threshold does not suddenly shrink the visualization. |
| 13 | let chrome = min(1, max(0, (size.height - 280) / 160)) |
| 14 | overlaysChrome = chrome < 1 |
| 15 | padding = 12 + 10 * chrome |
| 16 | let top: CGFloat = 58 * chrome |
| 17 | let bottom: CGFloat = 27 * chrome |
| 18 | let available = CGRect(x: padding, y: padding + top, |
| 19 | width: max(0, size.width - 2 * padding), |
| 20 | height: max(0, size.height - 2 * padding - top - bottom)) |
| 21 | let ratio = CGFloat(aspectRatio) |
| 22 | let width = min(available.width, available.height * ratio) |
| 23 | let height = width / ratio |
| 24 | preview = CGRect(x: available.midX - width / 2, y: available.midY - height / 2, |
| 25 | width: width, height: height) |
| 26 | } |
| 27 | } |