Skip to content

Commit

Permalink
Publicly expose applyShadow function for SwiftUI (microsoft#1992)
Browse files Browse the repository at this point in the history
* Move applyFluentShadow to public extension in ShadowInfo

* Add documentation for UIKit shadows

* Cleanup
  • Loading branch information
laminesm authored Apr 2, 2024
1 parent 7775a9f commit e7dd82f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ios/FluentUI/Button/FluentButtonStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
6 changes: 3 additions & 3 deletions ios/FluentUI/Core/SwiftUI+ViewModifiers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
12 changes: 9 additions & 3 deletions ios/FluentUI/Core/Theme/Tokens/ShadowInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import CoreGraphics
import UIKit
import Foundation

/// Represents a two-part shadow as used by FluentUI.
@objc(MSFShadowInfo)
Expand Down Expand Up @@ -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")
Expand All @@ -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()

Expand All @@ -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 }

Expand Down
2 changes: 1 addition & 1 deletion ios/FluentUI/Notification/FluentNotification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e7dd82f

Please sign in to comment.