-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
224 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
133 changes: 133 additions & 0 deletions
133
Sources/SFSafeSymbols/Initializers/SwiftUI/SwiftUIButtonExtension.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
#if canImport(SwiftUI) | ||
|
||
import SwiftUI | ||
import AppIntents | ||
|
||
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) | ||
public extension Button where Label == SwiftUI.Label<Text, Image>{ | ||
|
||
/// Creates a button that generates its label from a string and an `SFSymbol`. | ||
/// | ||
/// - Parameters | ||
/// - title: A string that describes the purpose of the button’s action. | ||
/// - systemSymbol: The `SFSymbol` describing this image. | ||
/// - action: The action to perform when the user triggers the button. | ||
init<S>( | ||
_ title: S, | ||
systemSymbol: SFSymbol, | ||
action: @escaping () -> Void | ||
) where S : StringProtocol { | ||
self.init( | ||
title, | ||
systemImage: systemSymbol.rawValue, | ||
action: action | ||
) | ||
} | ||
|
||
/// Creates a button that generates its label from a localized string key and an `SFSymbol`. | ||
/// | ||
/// - Parameters | ||
/// - titleKey: The key for the button’s localized title, that describes the purpose of the button’s action. | ||
/// - systemSymbol: The `SFSymbol` describing this image. | ||
/// - action: The action to perform when the user triggers the button. | ||
init( | ||
_ titleKey: LocalizedStringKey, | ||
systemSymbol: SFSymbol, | ||
action: @escaping () -> Void | ||
) { | ||
self.init( | ||
titleKey, | ||
systemImage: systemSymbol.rawValue, | ||
action: action | ||
) | ||
} | ||
|
||
/// Creates a button with a specified role that generates its label from a string and an `SFSymbol`. | ||
/// | ||
/// - Parameters | ||
/// - title: A string that describes the purpose of the button’s action. | ||
/// - systemSymbol: The `SFSymbol` describing this image. | ||
/// - role: An optional semantic role describing the button. A value of nil means that the button doesn’t have an assigned role. | ||
/// - action: The action to perform when the user triggers the button. | ||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | ||
init<S>( | ||
_ title: S, | ||
systemSymbol: SFSymbol, | ||
role: ButtonRole?, | ||
action: @escaping () -> Void | ||
) where S : StringProtocol { | ||
self.init( | ||
title, | ||
systemImage: systemSymbol.rawValue, | ||
role: role, | ||
action: action | ||
) | ||
} | ||
|
||
/// Creates a button with a specified role that generates its label from a localized string key and an `SFSymbol`. | ||
/// | ||
/// - Parameters | ||
/// - titleKey: The key for the button’s localized title, that describes the purpose of the button’s action | ||
/// - systemSymbol: The `SFSymbol` describing this image. | ||
/// - role: An optional semantic role describing the button. A value of nil means that the button doesn’t have an assigned role. | ||
/// - action: The action to perform when the user triggers the button. | ||
@available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) | ||
init( | ||
_ titleKey: LocalizedStringKey, | ||
systemSymbol: SFSymbol, | ||
role: ButtonRole?, | ||
action: @escaping () -> Void | ||
) { | ||
self.init( | ||
titleKey, | ||
systemImage: systemSymbol.rawValue, | ||
role: role, | ||
action: action | ||
) | ||
} | ||
|
||
/// Creates a button with a specified role that performs an AppIntent and generates its label from a localized string key and an `SFSymbol`. | ||
/// | ||
/// - Parameters | ||
/// - titleKey: The key for the button’s localized title, that describes the purpose of the button’s intent. | ||
/// - systemSymbol: The `SFSymbol` describing this image. | ||
/// - role: An optional semantic role describing the button. A value of nil means that the button doesn’t have an assigned role. | ||
/// - intent: The AppIntent to execute. | ||
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) | ||
init( | ||
_ titleKey: LocalizedStringKey, | ||
systemSymbol: SFSymbol, | ||
role: ButtonRole? = nil, | ||
intent: some AppIntent | ||
) { | ||
self.init( | ||
titleKey, | ||
systemImage: systemSymbol.rawValue, | ||
role: role, | ||
intent: intent | ||
) | ||
} | ||
|
||
/// Creates a button with a specified role that generates its label from a string and an `SFSymbol`. | ||
/// | ||
/// - Parameters | ||
/// - title: A string that describes the purpose of the button’s intent. | ||
/// - systemSymbol: The `SFSymbol` describing this image. | ||
/// - role: An optional semantic role describing the button. A value of nil means that the button doesn’t have an assigned role. | ||
/// - intent: The AppIntent to execute. | ||
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) | ||
init( | ||
_ title: some StringProtocol, | ||
systemSymbol: SFSymbol, | ||
role: ButtonRole? = nil, | ||
intent: some AppIntent | ||
) { | ||
self.init( | ||
title, | ||
systemImage: systemSymbol.rawValue, | ||
role: role, | ||
intent: intent | ||
) | ||
} | ||
} | ||
#endif |
71 changes: 71 additions & 0 deletions
71
Tests/SFSafeSymbolsTests/SwiftUI/SwiftUIButtonExtensionTests.swift
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
@testable import SFSafeSymbols | ||
|
||
#if !os(watchOS) | ||
|
||
import XCTest | ||
|
||
#if canImport(SwiftUI) | ||
|
||
import SwiftUI | ||
import AppIntents | ||
|
||
|
||
class ButtonExtensionTests: XCTestCase { | ||
/// Tests, whether the `Label` retrieved via SFSafeSymbols can be retrieved without a crash | ||
func testInit() { | ||
if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) { | ||
for symbol in TestHelper.allSymbolsWithVariants { | ||
print("Testing validity of \"\(symbol.rawValue)\" via Label init") | ||
|
||
// If this doesn't crash, everything works fine | ||
let title: String = "" | ||
_ = Button(title, systemSymbol: symbol) {} | ||
|
||
let localizedStringKey: LocalizedStringKey = "" | ||
_ = Button(localizedStringKey, systemSymbol: symbol) {} | ||
|
||
if #available(iOS 15.0, macOS 12.0, tvOS 15.0, watchOS 8.0, visionOS 1.0, *) { | ||
let title: String = "" | ||
_ = Button(title, systemSymbol: symbol, role: .cancel) {} | ||
|
||
let localizedStringKey: LocalizedStringKey = "" | ||
_ = Button(localizedStringKey, systemSymbol: symbol, role: .cancel) {} | ||
} | ||
|
||
if #available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) { | ||
let title: String = "" | ||
_ = Button(title, systemSymbol: symbol, role: .cancel, intent: OrderSoupIntent()) | ||
|
||
let localizedStringKey: LocalizedStringKey = "" | ||
_ = Button(localizedStringKey, systemSymbol: symbol, role: .cancel, intent: OrderSoupIntent()) | ||
} | ||
} | ||
} else { | ||
print("To test the Button initializer, iOS 14, macOS 11.0 or tvOS 14 is required.") | ||
} | ||
} | ||
|
||
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *) | ||
struct OrderSoupIntent: AppIntent { | ||
static var title = LocalizedStringResource("Order Soup") | ||
static var description = IntentDescription("Orders a soup from your favorite restaurant.") | ||
|
||
init() { } | ||
|
||
func perform() async throws -> some IntentResult { | ||
return .result() | ||
} | ||
} | ||
} | ||
|
||
#else | ||
|
||
class JustFail: XCTestCase { | ||
func justFail() { | ||
XCTFail("SwiftUI should be available when testing.") | ||
} | ||
} | ||
|
||
#endif | ||
|
||
#endif |