Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
112 changes: 112 additions & 0 deletions Example/Shared/View/LinkExample.swift
Original file line number Diff line number Diff line change
@@ -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()
}
47 changes: 47 additions & 0 deletions Example/Tests/OpenSwiftUIUITests/View/LinkUITests.swift
Original file line number Diff line number Diff line change
@@ -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"
)
}
}
2 changes: 1 addition & 1 deletion Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,36 @@ struct AccessibilityAdjustableModifier<Content, Value>: AccessibilityConfigurati

// TODO: AccessibilityNavigationLinkButtonModifier
// TODO: AccessibilityNavigationLinkButtonModifier.Representable
// TODO: AccessibilityLinkModifier
// TODO: AccessibilityLinkModifier.Configuration

// MARK: - AccessibilityLinkModifier

struct AccessibilityLinkModifier<Content: View>: 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

Expand Down
46 changes: 46 additions & 0 deletions Sources/OpenSwiftUI/Shape/ShapeStyle/LinkShapeStyle.swift
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 1 addition & 1 deletion Sources/OpenSwiftUI/View/ArchivedView/ArchivedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ extension _ArchivedViewStates.DeploymentVersion {
extension ViewDecoders {
static func registerStandardArchiveDecoders() {
registerStandard {
// TODO: ArchivableLinkModifier
ArchivableLinkModifier.registerDecoder()
// TODO: TimelineProgressView<ArchivableLinearProgressView.Base>.ArchivableTimelineProgressView
// TODO: TimelineProgressView<CircularPercentageGaugeRing>.ArchivableTimelineProgressView
HandGestureShortcutInteractiveControl.HandGestureShortcutPreferenceWriter.registerDecoder()
Expand Down
28 changes: 26 additions & 2 deletions Sources/OpenSwiftUI/View/Control/Button/ButtonStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

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

Expand Down
59 changes: 59 additions & 0 deletions Sources/OpenSwiftUI/View/Control/Button/LinkButtonStyle.swift
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
@@ -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())
}
}
Loading
Loading