Files
divkit/client/ios/DivKit/Extensions/DivImage/DivImageProtocol.swift
T
babaevmm 6797545374 update paths
commit_hash:8ab26a6d1a605474126fdbc3cc4314923edbc77f
2025-03-19 17:28:58 +03:00

55 lines
1.5 KiB
Swift

import Foundation
import LayoutKit
import VGSL
protocol DivImageProtocol: DivBase, DivImageContentMode {
var aspect: DivAspect? { get }
func resolvePreview(_ expressionResolver: ExpressionResolver) -> String?
func resolvePlaceholderColor(_ expressionResolver: ExpressionResolver) -> Color
}
extension DivImageProtocol {
func resolveHeight(_ context: DivBlockModelingContext) -> ImageBlockHeight {
if let aspect {
return .ratio(aspect.resolveRatio(context.expressionResolver) ?? 0)
}
return .trait(resolveContentHeightTrait(context))
}
func resolvePlaceholder(
_ expressionResolver: ExpressionResolver,
highPriority: Bool = false
) -> ImagePlaceholder {
if let base64 = resolvePreview(expressionResolver) {
.imageData(ImageData(base64: base64, highPriority: highPriority))
} else {
.color(resolvePlaceholderColor(expressionResolver))
}
}
func checkLayoutTraits(context: DivBlockModelingContext) throws {
if case .intrinsic = resolveContentWidthTrait(context) {
throw DivBlockModelingError(
"\(typeName) has wrap_content width",
path: context.path
)
}
if case let .trait(heightTrait) = resolveHeight(context),
case .intrinsic = heightTrait {
throw DivBlockModelingError(
"\(typeName) without aspect has wrap_content height",
path: context.path
)
}
}
private var typeName: String {
guard let typeName = String(describing: self).split(separator: ".").last else {
return "DivImage"
}
return String(typeName)
}
}