Limit image height to screen height (#1675)

* limit image height to window height minus a hardcoded value

https://github.com/Dimillian/IceCubesApp/issues/1554

* Limit image to screen height

- limit available height to 80% of screen/window height
- if image fits in available width and height, just display it at 1x (to avoid ugly resizing artifacts)
- otherwise, shrink it proportionally to fit

https://github.com/Dimillian/IceCubesApp/issues/1554
This commit is contained in:
sh95014 2023-11-17 00:42:33 -08:00 committed by GitHub
parent 59e5eba860
commit 11388757f3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 6 deletions

View file

@ -8,6 +8,10 @@ import UIKit
window?.bounds.size.width ?? UIScreen.main.bounds.size.width
}
public var windowHeight: CGFloat {
window?.bounds.size.height ?? UIScreen.main.bounds.size.height
}
public func scene(_ scene: UIScene,
willConnectTo _: UISceneSession,
options _: UIScene.ConnectionOptions)

View file

@ -68,7 +68,8 @@ public struct StatusRowMediaPreviewView: View {
imageMaxHeight: imageMaxHeight,
sensitive: sensitive,
appLayoutWidth: appLayoutWidth,
availableWidth: availableWidth
availableWidth: availableWidth,
availableHeight: sceneDelegate.windowHeight
)
.accessibilityElement(children: .ignore)
.accessibilityLabel(Self.accessibilityLabel(for: attachments[0]))
@ -200,6 +201,7 @@ private struct FeaturedImagePreView: View {
let sensitive: Bool
let appLayoutWidth: CGFloat
let availableWidth: CGFloat
let availableHeight: CGFloat
@Environment(\.isSecondaryColumn) private var isSecondaryColumn: Bool
@Environment(Theme.self) private var theme
@ -207,7 +209,7 @@ private struct FeaturedImagePreView: View {
var body: some View {
let size: CGSize = size(for: attachment) ?? .init(width: imageMaxHeight, height: imageMaxHeight)
let newSize = imageSize(from: size, newWidth: availableWidth - appLayoutWidth)
let newSize = imageSize(from: size)
Group {
switch attachment.supportedType {
case .image:
@ -255,13 +257,27 @@ private struct FeaturedImagePreView: View {
return .init(width: CGFloat(width), height: CGFloat(height))
}
private func imageSize(from: CGSize, newWidth: CGFloat) -> CGSize {
private func imageSize(from: CGSize) -> CGSize {
if isCompact || theme.statusDisplayStyle == .compact || isSecondaryColumn {
return .init(width: imageMaxHeight, height: imageMaxHeight)
}
let ratio = newWidth / from.width
let newHeight = from.height * ratio
return .init(width: newWidth, height: newHeight)
let boxWidth = availableWidth - appLayoutWidth
let boxHeight = availableHeight * 0.8 // use only 80% of window height to leave room for text
if from.width <= boxWidth && from.height <= boxHeight {
// intrinsic size of image fits just fine
return from
}
// shrink image proportionally to fit inside the box
let xRatio = boxWidth / from.width
let yRatio = boxHeight / from.height
if xRatio < yRatio {
return .init(width: boxWidth, height: from.height * xRatio)
} else {
return .init(width: from.width * yRatio, height: boxHeight)
}
}
}