diff --git a/Example/Shared/View/LinkExample.swift b/Example/Shared/View/LinkExample.swift new file mode 100644 index 000000000..4403eb5cb --- /dev/null +++ b/Example/Shared/View/LinkExample.swift @@ -0,0 +1,112 @@ +// +// LinkExample.swift +// Shared + +import Foundation +#if OPENSWIFTUI +import OpenSwiftUI +#else +import SwiftUI +#endif + +private let linkDestination = URL(string: "https://example.com")! + +struct LinkExample: View { + private let localizedTitle: LocalizedStringKey = "Localized title" + private let title = "String title" + + var body: some View { + VStack(alignment: .leading, spacing: 16) { + Link(localizedTitle, destination: linkDestination) + Link(title, destination: linkDestination) + Link(destination: linkDestination) { + HStack(spacing: 8) { + Color.orange + .frame(width: 16, height: 16) + Text("Custom label") + } + } + } + .padding() + } +} + +struct LinkButtonStyleExample: View { + var body: some View { + VStack(alignment: .leading, spacing: 16) { + Link("Default style", destination: linkDestination) + Link("Automatic style", destination: linkDestination) + .buttonStyle(.automatic) + Link("Plain style", destination: linkDestination) + .buttonStyle(.plain) + Link("Bordered style", destination: linkDestination) + .buttonStyle(.bordered) + Link("Custom style", destination: linkDestination) + .buttonStyle(ExampleLinkButtonStyle()) + #if os(macOS) + Link("Explicit link style", destination: linkDestination) + .buttonStyle(.link) + Button("Link button style") {} + .buttonStyle(.link) + #endif + } + .padding() + } +} + +private struct ExampleLinkButtonStyle: ButtonStyle { + func makeBody(configuration: Configuration) -> some View { + configuration.label + .foregroundStyle(.white) + .padding(8) + .background(.purple) + .opacity(configuration.isPressed ? 0.5 : 1) + } +} + +struct LinkAppearanceExample: View { + var body: some View { + VStack(alignment: .leading, spacing: 16) { + Link("Default appearance", destination: linkDestination) + Link("Tinted link", destination: linkDestination) + .tint(.purple) + Link("Foreground style", destination: linkDestination) + .foregroundStyle(.orange) + Link(destination: linkDestination) { + Text("Styled label") + .font(.headline) + .foregroundStyle(.green) + } + } + .padding() + } +} + +struct LinkShapeStyleExample: View { + var body: some View { + VStack(alignment: .leading, spacing: 16) { + Text("Link foreground style") + .foregroundStyle(.link) + Rectangle() + .fill(.link) + .frame(height: 24) + } + .padding() + } +} + +#Preview("LinkExample") { + LinkExample() +} + +#Preview("LinkButtonStyleExample") { + LinkButtonStyleExample() +} + +#Preview("LinkAppearanceExample") { + LinkAppearanceExample() +} + +#Preview("LinkShapeStyleExample") { + LinkShapeStyleExample() +} diff --git a/Example/Tests/OpenSwiftUIUITests/View/LinkUITests.swift b/Example/Tests/OpenSwiftUIUITests/View/LinkUITests.swift new file mode 100644 index 000000000..ca0cbbd86 --- /dev/null +++ b/Example/Tests/OpenSwiftUIUITests/View/LinkUITests.swift @@ -0,0 +1,47 @@ +// +// LinkUITests.swift +// OpenSwiftUIUITests + +import SnapshotTesting +import Testing +@testable import TestingHost + +@MainActor +@Suite(.snapshots(record: .never, diffTool: diffTool)) +struct LinkUITests { + @Test(.tags(.localization)) + func initializers() { + openSwiftUIAssertSnapshot( + of: LinkExample(), + size: .init(width: 320, height: 160) + ) + } + + @Test + func buttonStyles() { + openSwiftUIAssertSnapshot( + of: LinkButtonStyleExample(), + size: .init(width: 320, height: 360) + ) + } + + @Test(arguments: [false, true]) + func appearance(isDisabled: Bool) { + openSwiftUIAssertSnapshot( + of: LinkAppearanceExample() + .disabled(isDisabled), + size: .init(width: 320, height: 200), + named: isDisabled ? "disabled" : "enabled" + ) + } + + @Test(arguments: [ColorScheme.light, .dark]) + func shapeStyle(colorScheme: ColorScheme) { + openSwiftUIAssertSnapshot( + of: LinkShapeStyleExample() + .environment(\.colorScheme, colorScheme), + size: .init(width: 320, height: 120), + named: colorScheme == .dark ? "dark" : "light" + ) + } +} diff --git a/Package.resolved b/Package.resolved index 69928b8ed..640044a75 100644 --- a/Package.resolved +++ b/Package.resolved @@ -7,7 +7,7 @@ "location" : "https://github.com/OpenSwiftUIProject/DarwinPrivateFrameworks.git", "state" : { "branch" : "main", - "revision" : "491c68d2970fc5d4379dac36137744faa25e2d7f" + "revision" : "a256dadaa7531c32b706894b52f75696f2bf0bc0" } }, { diff --git a/Sources/OpenSwiftUI/Accessibility/AccessibilityConfiguration.swift b/Sources/OpenSwiftUI/Accessibility/AccessibilityConfiguration.swift index 6730d2025..5711bb7bc 100644 --- a/Sources/OpenSwiftUI/Accessibility/AccessibilityConfiguration.swift +++ b/Sources/OpenSwiftUI/Accessibility/AccessibilityConfiguration.swift @@ -197,8 +197,36 @@ struct AccessibilityAdjustableModifier: AccessibilityConfigurati // TODO: AccessibilityNavigationLinkButtonModifier // TODO: AccessibilityNavigationLinkButtonModifier.Representable -// TODO: AccessibilityLinkModifier -// TODO: AccessibilityLinkModifier.Configuration + +// MARK: - AccessibilityLinkModifier + +struct AccessibilityLinkModifier: AccessibilityConfigurationModifier { + struct Configuration { + var destination: LinkDestination + var isArchive: Bool + } + + var configuration: Configuration + + init(_ destination: LinkDestination, isArchive: Bool) { + configuration = Configuration(destination: destination, isArchive: isArchive) + } + + @ViewBuilder + func body(content: Content) -> some View { + if configuration.isArchive { + content + .accessibilityIgnoreViewResponders() + .accessibilityCombinedElement(options: .includeHiddenChild, ignoredTraits: [.isImage, .isStaticText]) + .accessibilityAddTraits(.isButton) + .accessibilityRemoveTraits(.isLink) + } else { + content + .accessibility(AccessibilityProperties.LinkDestinationKey.self, configuration.destination.configuration) + .accessibilityAddTraits(.isLink) + } + } +} // MARK: - AccessibilitySliderModifier diff --git a/Sources/OpenSwiftUI/Shape/ShapeStyle/LinkShapeStyle.swift b/Sources/OpenSwiftUI/Shape/ShapeStyle/LinkShapeStyle.swift new file mode 100644 index 000000000..0cbf25b52 --- /dev/null +++ b/Sources/OpenSwiftUI/Shape/ShapeStyle/LinkShapeStyle.swift @@ -0,0 +1,46 @@ +// +// LinkShapeStyle.swift +// OpenSwiftUI +// +// Audited for 6.5.4 +// Status: Complete + +public import OpenSwiftUICore + +// MARK: - ShapeStyle + LinkShapeStyle + +@available(OpenSwiftUI_v5_0, *) +extension ShapeStyle where Self == LinkShapeStyle { + /// A style for links. + @_alwaysEmitIntoClient + public static var link: LinkShapeStyle { + .init() + } +} + +// MARK: - LinkShapeStyle + +/// A style for links. +@available(OpenSwiftUI_v5_0, *) +@frozen +public struct LinkShapeStyle: ShapeStyle { + public init() { + _openSwiftUIEmptyStub() + } + + public func _apply(to shape: inout _ShapeStyle_Shape) { + #if os(macOS) + AppearanceCatalogShapeStyle(catalogName: "linkColor")._apply(to: &shape) + #else + let isDark = shape.environment.colorScheme == .dark + Color( + .sRGB, + red: isDark ? 9 / 255 : 0, + green: isDark ? 132 / 255 : 122 / 255, + blue: 1 + )._apply(to: &shape) + #endif + } + + public typealias Resolved = Never +} diff --git a/Sources/OpenSwiftUI/View/ArchivedView/ArchivedView.swift b/Sources/OpenSwiftUI/View/ArchivedView/ArchivedView.swift index 933e7bb8e..d4b68e45c 100644 --- a/Sources/OpenSwiftUI/View/ArchivedView/ArchivedView.swift +++ b/Sources/OpenSwiftUI/View/ArchivedView/ArchivedView.swift @@ -241,7 +241,7 @@ extension _ArchivedViewStates.DeploymentVersion { extension ViewDecoders { static func registerStandardArchiveDecoders() { registerStandard { - // TODO: ArchivableLinkModifier + ArchivableLinkModifier.registerDecoder() // TODO: TimelineProgressView.ArchivableTimelineProgressView // TODO: TimelineProgressView.ArchivableTimelineProgressView HandGestureShortcutInteractiveControl.HandGestureShortcutPreferenceWriter.registerDecoder() diff --git a/Sources/OpenSwiftUI/View/Control/Button/ButtonStyle.swift b/Sources/OpenSwiftUI/View/Control/Button/ButtonStyle.swift index 753f18484..3909294e4 100644 --- a/Sources/OpenSwiftUI/View/Control/Button/ButtonStyle.swift +++ b/Sources/OpenSwiftUI/View/Control/Button/ButtonStyle.swift @@ -169,7 +169,15 @@ extension ButtonStyleModifier { } } -// TODO: ArchivableLinkModifier +// MARK: - ArchivableLinkModifier + +struct ArchivableLinkModifier: _ArchivableViewModifier { + var configuration: LinkDestination.Configuration + + func body(content: Content) -> some View { + content.modifier(LinkButtonModifierBody(destination: LinkDestination(configuration: configuration))) + } +} // MARK: - WrappedButtonStyle @@ -193,7 +201,23 @@ extension WrappedButtonStyle: AnyDefaultStyle where Style: AnyDefaultStyle { // TODO: ArchivableButtonAppIntentModifier -// TODO: LinkButtonModifierBody +// MARK: - LinkButtonModifierBody + +private struct LinkButtonModifierBody: ViewModifier { + let destination: LinkDestination + + func body(content: Content) -> some View { + ButtonBehavior(action: destination.open) { isPressed in + StaticIf(Semantics.ArchivedLinkIsButton.self) { + content.temporalState(isPressed) + } else: { + content + .modifier(OpacityButtonHighlightModifier(highlighted: isPressed)) + .contentShape(Rectangle()) + } + } + } +} // MARK: - ResolvedButtonStyleBody diff --git a/Sources/OpenSwiftUI/View/Control/Button/LinkButtonStyle.swift b/Sources/OpenSwiftUI/View/Control/Button/LinkButtonStyle.swift new file mode 100644 index 000000000..bd59802fe --- /dev/null +++ b/Sources/OpenSwiftUI/View/Control/Button/LinkButtonStyle.swift @@ -0,0 +1,59 @@ +// +// LinkButtonStyle.swift +// OpenSwiftUI +// +// Audited for 6.5.4 +// Status: Complete + +public import OpenSwiftUICore + +// MARK: - PrimitiveButtonStyle + LinkButtonStyle + +@available(OpenSwiftUI_v1_0, *) +@available(iOS, unavailable) +@available(tvOS, unavailable) +@available(watchOS, unavailable) +@available(visionOS, unavailable) +extension PrimitiveButtonStyle where Self == LinkButtonStyle { + /// A button style that displays a link. + @_alwaysEmitIntoClient + @MainActor + @preconcurrency + public static var link: LinkButtonStyle { + .init() + } +} + +// MARK: - LinkButtonStyle + +/// A button style that displays a link. +@available(OpenSwiftUI_v1_0, *) +@available(iOS, unavailable) +@available(tvOS, unavailable) +@available(watchOS, unavailable) +@available(visionOS, unavailable) +@MainActor +@preconcurrency +public struct LinkButtonStyle: PrimitiveButtonStyle { + /// Creates a link button style. + public init() { + _openSwiftUIEmptyStub() + } + + public func makeBody(configuration: Configuration) -> some View { + #if os(macOS) + Button(configuration) + .accessibilityAddTraits(.isLink) + .modifier( + PrimitiveButtonStyleContainerModifier(style: PlatformItemListButtonStyle()) + .requiring(MenuStyleContext.self) + ) + .buttonStyle(AppKitButtonStyle(appearance: .link)) + #else + _openSwiftUIPlatformUnimplementedFailure() + #endif + } +} + +@available(*, unavailable) +extension LinkButtonStyle: Sendable {} diff --git a/Sources/OpenSwiftUI/View/Control/Button/WidgetLinkButtonStyle.swift b/Sources/OpenSwiftUI/View/Control/Button/WidgetLinkButtonStyle.swift new file mode 100644 index 000000000..a1f3e6b44 --- /dev/null +++ b/Sources/OpenSwiftUI/View/Control/Button/WidgetLinkButtonStyle.swift @@ -0,0 +1,16 @@ +// +// WidgetLinkButtonStyle.swift +// OpenSwiftUI +// +// Audited for 6.5.4 +// Status: Complete + +import OpenSwiftUICore + +struct WidgetLinkButtonStyle: ButtonStyle { + func makeBody(configuration: Configuration) -> some View { + configuration.label + .modifier(OpacityButtonHighlightModifier(highlighted: configuration.isPressed)) + .contentShape(Rectangle()) + } +} diff --git a/Sources/OpenSwiftUI/View/Control/Link/Link.swift b/Sources/OpenSwiftUI/View/Control/Link/Link.swift index 36e05451c..e49c14289 100644 --- a/Sources/OpenSwiftUI/View/Control/Link/Link.swift +++ b/Sources/OpenSwiftUI/View/Control/Link/Link.swift @@ -2,10 +2,11 @@ // Link.swift // OpenSwiftUI // -// Audited for 3.5.2 -// Status: Blocked by Text and Accessibility +// Audited for 6.5.4 +// Status: Complete public import Foundation +@_spi(Private) public import OpenSwiftUICore /// A control for navigating to a URL. /// @@ -34,50 +35,142 @@ public import Foundation /// As with other views, you can style links using standard view modifiers /// depending on the view type of the link's label. For example, a ``Text`` /// label could be modified with a custom ``View/font(_:)`` or -/// ``View/foregroundColor(_:)`` to customize the appearance of the link in +/// ``View/foregroundStyle(_:)`` to customize the appearance of the link in /// your app's UI. +@available(OpenSwiftUI_v2_0, *) public struct Link