From e7dd82fb83f82122830faab1fb46c3b4da355c6e Mon Sep 17 00:00:00 2001 From: Lamine Male <106181067+laminesm@users.noreply.github.com> Date: Tue, 2 Apr 2024 16:04:34 -0700 Subject: [PATCH] Publicly expose applyShadow function for SwiftUI (#1992) * Move applyFluentShadow to public extension in ShadowInfo * Add documentation for UIKit shadows * Cleanup --- ios/FluentUI/Button/FluentButtonStyle.swift | 2 +- ios/FluentUI/Core/SwiftUI+ViewModifiers.swift | 6 +++--- ios/FluentUI/Core/Theme/Tokens/ShadowInfo.swift | 12 +++++++++--- ios/FluentUI/Notification/FluentNotification.swift | 2 +- 4 files changed, 14 insertions(+), 8 deletions(-) diff --git a/ios/FluentUI/Button/FluentButtonStyle.swift b/ios/FluentUI/Button/FluentButtonStyle.swift index b55a4d4446..ea8f3151be 100644 --- a/ios/FluentUI/Button/FluentButtonStyle.swift +++ b/ios/FluentUI/Button/FluentButtonStyle.swift @@ -72,7 +72,7 @@ public struct FluentButtonStyle: SwiftUI.ButtonStyle { .frame(minHeight: ButtonTokenSet.minContainerHeight(style: style, size: size)) .background(backgroundView) .overlay { overlayView } - .applyShadow(shadowInfo: shadowInfo) + .applyFluentShadow(shadowInfo: shadowInfo) .pointerInteraction(isEnabled) } diff --git a/ios/FluentUI/Core/SwiftUI+ViewModifiers.swift b/ios/FluentUI/Core/SwiftUI+ViewModifiers.swift index adc4ea225c..3570bcbfb0 100644 --- a/ios/FluentUI/Core/SwiftUI+ViewModifiers.swift +++ b/ios/FluentUI/Core/SwiftUI+ViewModifiers.swift @@ -51,11 +51,11 @@ extension View { modifier(LargeContentViewerModifier(text: text, image: image)) } - /// Applies multiple shadows on a View + /// Applies a key and an ambient shadow on a `View`. /// - Parameters: - /// - shadowInfo: The values of the two shadows to be applied + /// - shadowInfo: The values of the two shadows to be applied. /// - Returns: The modified view. - func applyShadow(shadowInfo: ShadowInfo) -> some View { + public func applyFluentShadow(shadowInfo: ShadowInfo) -> some View { modifier(ShadowModifier(shadowInfo: shadowInfo)) } diff --git a/ios/FluentUI/Core/Theme/Tokens/ShadowInfo.swift b/ios/FluentUI/Core/Theme/Tokens/ShadowInfo.swift index 5cca73c048..c038adcff7 100644 --- a/ios/FluentUI/Core/Theme/Tokens/ShadowInfo.swift +++ b/ios/FluentUI/Core/Theme/Tokens/ShadowInfo.swift @@ -5,7 +5,6 @@ import CoreGraphics import UIKit -import Foundation /// Represents a two-part shadow as used by FluentUI. @objc(MSFShadowInfo) @@ -68,7 +67,10 @@ public class ShadowInfo: NSObject { } public extension ShadowInfo { - + /// Applies a key and an ambient shadow on a `UIView`. + /// - Parameters: + /// - view: The view on which the shadows will be applied. + /// - parentController: The view controller responsible for the view on which the shadows will be applied. func applyShadow(to view: UIView, parentController: UIViewController? = nil) { guard var shadowable = (view as? Shadowable) ?? (view.superview as? Shadowable) ?? (parentController as? Shadowable) else { assertionFailure("Cannot apply Fluent shadows to a non-Shadowable view") @@ -88,6 +90,11 @@ public extension ShadowInfo { view.layer.insertSublayer(keyShadow, below: ambientShadow) } + /// Initializes a `CALayer` with the relevant `ShadowInfo` values. + /// - Parameters: + /// - view: The view on which the shadow layer will be applied. + /// - isAmbientShadow: Determines whether to apply ambient or key shadow values on the layer. + /// - Returns: The modified `CALayer`. func initializeShadowLayer(view: UIView, isAmbientShadow: Bool = false) -> CALayer { let layer = CALayer() @@ -109,7 +116,6 @@ public extension ShadowInfo { /// Public protocol that, when implemented, allows any UIView or one of its subviews to implement fluent shadows public protocol Shadowable { - /// The layer on which the ambient shadow is implemented var ambientShadow: CALayer? { get set } diff --git a/ios/FluentUI/Notification/FluentNotification.swift b/ios/FluentUI/Notification/FluentNotification.swift index bb4d6ba061..27a725fdd2 100644 --- a/ios/FluentUI/Notification/FluentNotification.swift +++ b/ios/FluentUI/Notification/FluentNotification.swift @@ -288,7 +288,7 @@ public struct FluentNotification: View, TokenizedControlView { backgroundFill .clipShape(RoundedRectangle(cornerRadius: tokenSet[.cornerRadius].float)) ) - .applyShadow(shadowInfo: shadowInfo) + .applyFluentShadow(shadowInfo: shadowInfo) ) .onTapGesture { if let messageAction = messageButtonAction {