Created
May 15, 2023 11:57
-
-
Save davidsteppenbeck/78c1dafd5fd5ed8936ab5b0e118c5e21 to your computer and use it in GitHub Desktop.
A method for specifying multiplatform widget families in Swift.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import WidgetKit | |
@available(iOS 16.0, macOS 13.0, *) | |
public enum MultiplatformWidgetFamily: CaseIterable { | |
/// A small widget. | |
/// | |
/// The small system widget can appear on the Home Screen or in the Today View in iOS and iPadOS, or in the Notification Center on macOS. | |
case systemSmall | |
/// A medium-sized widget. | |
/// | |
/// The medium system widget can appear on the Home Screen or in the Today View in iOS and iPadOS, or in the Notification Center on macOS. | |
case systemMedium | |
/// A large widget. | |
/// | |
/// The large system widget can appear on the Home Screen or in the Today View in iOS or iPadOS, or in the Notification Center on macOS. | |
case systemLarge | |
/// An extra large widget. | |
/// | |
/// The extra-large system widget can appear on the Home Screen in iPadOS. | |
case systemExtraLarge | |
/// A circular widget. | |
/// | |
/// The accessory circular widget can appear as a complication in watchOS, or on the Lock Screen in iOS. | |
case accessoryCircular | |
/// A rectangular widget. | |
/// | |
/// The accessory rectangular widget can appear as a complication in watchOS or on the Lock Screen in iOS. | |
case accessoryRectangular | |
/// A flat widget that contains a single row of text and an optional image. | |
/// | |
/// The accessory inline widget can appear as a complication in watchOS, or on the Lock Screen in iOS. On some watch faces, the system renders the complication along a curve. | |
case accessoryInline | |
/// Provides the counterpart `WidgetFamily` value if available for the current platform. | |
var widgetFamilyValue: WidgetFamily? { | |
switch self { | |
case .systemSmall: | |
return .systemSmall | |
case .systemMedium: | |
return .systemMedium | |
case .systemLarge: | |
return .systemLarge | |
case .systemExtraLarge: | |
#if os(iOS) | |
return .systemExtraLarge | |
#elseif os(macOS) | |
return nil | |
#endif | |
case .accessoryCircular: | |
#if os(iOS) | |
return .accessoryCircular | |
#elseif os(macOS) | |
return nil | |
#endif | |
case .accessoryRectangular: | |
#if os(iOS) | |
return .accessoryRectangular | |
#elseif os(macOS) | |
return nil | |
#endif | |
case .accessoryInline: | |
#if os(iOS) | |
return .accessoryInline | |
#elseif os(macOS) | |
return nil | |
#endif | |
} | |
} | |
} | |
@available(iOS 16.0, macOS 13.0, *) | |
public extension WidgetConfiguration { | |
/// Sets the sizes that a widget supports for the families that are available for the current platform. | |
/// | |
/// - Parameters: | |
/// - multiplatformFamilies: The set of sizes the widget supports across all platforms. | |
/// | |
/// - Returns: A widget configuration that supports the available sizes. | |
func multiplatformSupportedFamilies(_ multiplatformFamilies: [MultiplatformWidgetFamily]) -> some WidgetConfiguration { | |
let widgetFamilies = multiplatformFamilies.compactMap(\.widgetFamilyValue) | |
return supportedFamilies(widgetFamilies) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment