Files
divkit/client/ios/DivKitExtensions/ExtensionHandlers/AspectCorrectionExtensionHandler.swift
booster e1835e44e3 AspectCorrectionExtensionHandler implemented
commit_hash:2196cdfc30db545d1c026607dce41140164ab6b4
2026-01-21 23:27:33 +03:00

75 lines
2.0 KiB
Swift

#if os(iOS)
import CoreGraphics
import DivKit
import LayoutKit
import VGSL
/// Extension handler that ensures images are cropped to match the expected aspect ratio.
///
/// When a DivImage has an `aspect` property defined, this handler wraps the image holder
/// in an `AspectCorrectionImageHolder` that automatically crops loaded images to match
/// the expected aspect ratio.
public final class AspectCorrectionExtensionHandler: DivExtensionHandler {
public let id: String
private let aspectTolerance: CGFloat
public init(
id: String = "aspect_correction",
aspectTolerance: CGFloat = 0.001
) {
self.id = id
self.aspectTolerance = aspectTolerance
}
public func applyBeforeBaseProperties(
to block: Block,
div: DivBase,
context: DivBlockModelingContext
) -> Block {
guard
let imageBlock = block as? ImageBlock,
let divImage = div as? DivImage
else {
return block
}
guard let expectedAspect = resolveExpectedAspect(
divImage: divImage,
expressionResolver: context.expressionResolver
) else {
return block
}
let wrappedImageHolder = AspectCorrectionImageHolder(
wrapped: imageBlock.imageHolder,
expectedAspect: expectedAspect,
aspectTolerance: aspectTolerance
)
return ImageBlock(
imageHolder: wrappedImageHolder,
widthTrait: imageBlock.widthTrait,
height: imageBlock.height,
contentMode: imageBlock.contentMode,
tintColor: imageBlock.tintColor,
tintMode: imageBlock.tintMode,
effects: imageBlock.effects,
filter: imageBlock.filter,
accessibilityElement: imageBlock.accessibilityElement,
appearanceAnimation: imageBlock.appearanceAnimation,
blurUsingMetal: imageBlock.blurUsingMetal,
tintUsingMetal: imageBlock.tintUsingMetal,
path: imageBlock.path
)
}
private func resolveExpectedAspect(
divImage: DivImage,
expressionResolver: ExpressionResolver
) -> Double? {
divImage.aspect?.resolveRatio(expressionResolver)
}
}
#endif