| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- /// A linear gradient.
- public struct LinearGradient: ElementaryView {
- /// The gradient represented as an array of color stops, each having a parametric location value.
- public let gradient: Gradient
- /// The normalized point where the gradient begins, defined in the view's coordinate space.
- ///
- /// Use values like `.top`, `.leading`, or custom `UnitPoint(x:y:)` offsets.
- public let startPoint: UnitPoint
- /// The normalized point where the gradient ends, defined in the views coordinate space.
- ///
- /// The color interpolation moves linearly from the start point to this point.
- public let endPoint: UnitPoint
- private static let idealSize = ViewSize(10, 10)
- /// Creates a linear gradient from a base gradient.
- public init(
- gradient: Gradient,
- startPoint: UnitPoint,
- endPoint: UnitPoint
- ) {
- self.gradient = gradient
- self.startPoint = startPoint
- self.endPoint = endPoint
- }
- func asWidget<Backend: BaseAppBackend>(
- backend: Backend
- ) -> Backend.Widget {
- func castBackend_inner<NewBackend: BaseAppBackend & BackendFeatures.LinearGradients>(_ backend: NewBackend) -> NewBackend.Widget {
- return backend.createLinearGradientWidget()
- }
- guard let castedBackend = backend as? any (BaseAppBackend & BackendFeatures.LinearGradients) else {
- fatalError("Backend does not implement BackendFeatures.LinearGradients")
- }
- return castBackend_inner(castedBackend) as! Backend.Widget
- }
- public func computeLayout<Backend: BaseAppBackend>(
- _ widget: Backend.Widget,
- proposedSize: ProposedViewSize,
- environment: EnvironmentValues,
- backend: Backend
- ) -> ViewLayoutResult {
- ViewLayoutResult.leafView(
- size: proposedSize.replacingUnspecifiedDimensions(by: Self.idealSize)
- )
- }
- func commit<Backend: BaseAppBackend>(
- _ widget: Backend.Widget,
- layout: ViewLayoutResult,
- environment: EnvironmentValues,
- backend: Backend
- ) {
- func castBackend_inner<NewBackend: BaseAppBackend & BackendFeatures.LinearGradients>(_ backend: NewBackend) {
- let widget = widget as! NewBackend.Widget
- backend.setSize(of: widget, to: layout.size.vector)
- backend.updateLinearGradientWidget(
- widget,
- gradient: self,
- withSize: layout.size.vector,
- in: environment
- )
- }
- guard let castedBackend = backend as? any (BaseAppBackend & BackendFeatures.LinearGradients) else {
- fatalError("Backend does not implement BackendFeatures.LinearGradients")
- }
- castBackend_inner(castedBackend)
- }
- }
- extension LinearGradient {
- /// Creates a linear gradient from a collection of colors.
- public init(
- stops: [Gradient.Stop],
- startPoint: UnitPoint,
- endPoint: UnitPoint
- ) {
- self.init(
- gradient: Gradient(stops: stops),
- startPoint: startPoint,
- endPoint: endPoint
- )
- }
- /// Creates a linear gradient from a collection of color stops.
- public init(
- colors: [Color],
- startPoint: UnitPoint,
- endPoint: UnitPoint
- ) {
- self.init(
- gradient: Gradient(colors: colors),
- startPoint: startPoint,
- endPoint: endPoint
- )
- }
- }
|