Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move FluentTheme code to new FluentUI_common module #2139

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@ let targets: [Target] = [
),
.target(
name: "FluentUI_ios",
dependencies: [
.target(name: "FluentUI_common")
],
path: "Sources/FluentUI_iOS",
resources: [
.copy("Resources/Version.plist")
]
),
.target(
name: "FluentUI_macos",
dependencies: [
.target(name: "FluentUI_common")
],
path: "Sources/FluentUI_macOS"
),
.target(
name: "FluentUI_common",
path: "Sources/FluentUI_common"
)
]

Expand Down
3 changes: 2 additions & 1 deletion Sources/FluentUI/FluentUI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
// Licensed under the MIT License.
//

#if os(iOS) || os(visionOS)
@_exported import FluentUI_common
#if os(iOS) || os(visionOS) || targetEnvironment(macCatalyst)
@_exported import FluentUI_ios
#elseif os(macOS)
@_exported import FluentUI_macos
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,19 @@ extension Color {
///
/// - Parameter dynamicColor: A dynmic color structure that describes the `Color` to be created.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Not related to this PR, but there's a typo here. dynmic -> dynamic

init(dynamicColor: DynamicColor) {
if #available(iOS 17, *) {
if #available(iOS 17, macOS 14, *) {
self.init(dynamicColor)
} else {
#if os(macOS)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be a good excuse to rename Color+Extensions.swift to something more descriptive and then introduce something like DynamicColor+UIKit and DynamicColor+AppKit (or similar names)

self.init(nsColor: NSColor(dynamicColor: dynamicColor))
#else
self.init(uiColor: UIColor(dynamicColor: dynamicColor))
#endif // os(macOS)
}
}

var dynamicColor: DynamicColor {
if #available(iOS 17, *) {
if #available(iOS 17, macOS 14, *) {
var lightEnvironment = EnvironmentValues.init()
lightEnvironment.colorScheme = .light

Expand All @@ -58,9 +62,15 @@ extension Color {
return DynamicColor(light: Color(self.resolve(in: lightEnvironment)),
dark: Color(self.resolve(in: darkEnvironment)))
} else {
#if os(macOS)
let nsColor = NSColor(self)
return DynamicColor(light: Color(nsColor.light),
dark: Color(nsColor.dark))
#else
let uiColor = UIColor(self)
return DynamicColor(light: Color(uiColor.light),
dark: Color(uiColor.dark))
#endif // os(macOS)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ extension NSColor {
return resolvedColorValue(appearance: NSAppearance(named: .darkAqua))
}

convenience init(dynamicColor: DynamicColor) {
let colorResolver = { $0 != nil ? NSColor($0!) : nil }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since Optional includes support for .map() and .flatMap(), I wonder if you could remove this closure and instead do something like:

self.init(light: NSColor(dynamicColor.light),
          dark: dynamicColor.dark.map { NSColor($0) }

self.init(light: NSColor(dynamicColor.light),
dark: colorResolver(dynamicColor.dark))
}

/// Returns the version of the current color that results from the specified traits as an `NSColor`.
///
/// - Parameter appearance: The user interface appearance to use when resolving the color information.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
// Licensed under the MIT License.
//

import UIKit
#if canImport(UIKit)
import SwiftUI
import UIKit

extension UIColor {

Expand Down Expand Up @@ -172,3 +173,4 @@ extension UIColor {
return resolvedColor
}
}
#endif // canImport(UIKit)
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,31 @@
import SwiftUI

/// A container that stores a dynamic set of `Color` values.
struct DynamicColor: Hashable {
@objc(MSFDynamicColor)
public final class DynamicColor: NSObject {

/// Creates a custom `ShapeStyle` that stores a dynamic set of `Color` values.
///
/// - Parameter light: The default `Color` for a light context. Required.
/// - Parameter dark: The override `Color` for a dark context. Optional.
/// - Parameter darkElevated: The override `Color` for a dark elevated context. Optional.
init(light: Color,
dark: Color? = nil,
darkElevated: Color? = nil) {
self.light = light
public init(light: Color,
dark: Color? = nil,
darkElevated: Color? = nil) {
self.light = light
self.dark = dark
self.darkElevated = darkElevated
}

let light: Color
let dark: Color?
let darkElevated: Color?
public let light: Color
public let dark: Color?
public let darkElevated: Color?
}

@available(iOS 17, macOS 14, *)
extension DynamicColor: ShapeStyle {
/// Evaluate to a resolved `Color` (in the form of a `ShapeStyle`) given the current `environment`.
func resolve(in environment: EnvironmentValues) -> Color.Resolved {
public func resolve(in environment: EnvironmentValues) -> Color.Resolved {
if environment.colorScheme == .dark {
if environment.isPresented, let darkElevated = darkElevated {
return darkElevated.resolve(in: environment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class LinearGradientInfo: NSObject {
// MARK: - Extensions

extension LinearGradient {
/// Internal property to generate a SwiftUI `LinearGradient` from a gradient info.
init(gradientInfo: LinearGradientInfo) {
/// Generate a SwiftUI `LinearGradient` from a gradient info.
public init(gradientInfo: LinearGradientInfo) {
if let locations = gradientInfo.locations {
// Map the colors and locations together.
let stops: [Gradient.Stop] = zip(gradientInfo.colors, locations).map({ (color, location) in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Licensed under the MIT License.
//

import UIKit
import QuartzCore

/// An animation synchronizer syncs homogeneous layer animations by calculating the appropriate timeOffset
/// of a referenceLayer so that newly added animations can stay in sync with existing animations.
Expand Down