-
Notifications
You must be signed in to change notification settings - Fork 81
Provide Foundations for 1024 #1078
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0bba0d6
ButtonSizing
Nexushunter f753e54
ButtonStyle
Nexushunter 4daa36c
Basic Label
Nexushunter 7331d66
Text Button
Nexushunter d176928
Label by text and image
Nexushunter 9c01906
Button foundation
Nexushunter 94ca0c9
Add example for label
Nexushunter 1b8708d
Add Label test stub and preview
Nexushunter e8c2545
clean up cb usage
Nexushunter 323a061
Add annotations to test classes
Nexushunter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
41 changes: 41 additions & 0 deletions
41
Sources/OpenSwiftUI/View/Control/Button/ButtonSizing.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /// Mark: - ButtonSizing | ||
|
|
||
| /// The sizing behaviour of Buttons and other button-like controls. | ||
| struct ButtonSizing { | ||
| static var automatic: ButtonSizing { get {.init()}} | ||
| static var fitted: ButtonSizing { get {.init()} } | ||
| static var flexible: ButtonSizing { get {.init()} } | ||
| } | ||
|
|
||
| extension ButtonSizing: Hashable {} | ||
| extension ButtonSizing: Equatable {} | ||
| extension ButtonSizing: Sendable {} | ||
|
|
||
| nonisolated struct ButtonSizingModifier<Style>: StyleModifier where Style: ButtonStyle { | ||
| init(style: Style) { | ||
| self.style = style | ||
| } | ||
|
|
||
| var style: Style; | ||
|
|
||
| func styleBody(configuration: Style.Configuration) -> some View { | ||
| style.makeBody(configuration: configuration) | ||
| } | ||
| } | ||
|
|
||
| extension View { | ||
| nonisolated public func ButtonSizing<S>(_ style: S) -> some View where S: ButtonStyle { | ||
| modifier(ButtonSizingModifier(style: style)) | ||
| } | ||
| } | ||
|
|
||
| struct ButtonSizingKey: EnvironmentKey { | ||
| static let defaultValue = ButtonSizing.automatic | ||
| } | ||
|
|
||
| extension EnvironmentValues { | ||
| var buttonSizing: ButtonSizing { | ||
| get { self[ButtonSizingKey.self] } | ||
| set { self[ButtonSizingKey.self] = newValue } | ||
| } | ||
| } |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import Foundation | ||
|
|
||
| nonisolated struct Label<Title,Icon>: View where Title: View, Icon: View { | ||
| private init(title: Title, icon: Icon) { | ||
| self.title = title; | ||
| self.icon = icon | ||
| } | ||
| init(@ViewBuilder title: () -> Title, @ViewBuilder icon: () -> Icon) { | ||
| self.title = title(); | ||
| self.icon = icon(); | ||
| } | ||
|
|
||
| var title: Title; | ||
| var icon: Icon; | ||
|
|
||
| var body: some View { | ||
| HStack(alignment: .center, spacing: 8) { | ||
| title | ||
| icon | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension Label where Title == Text, Icon == Image { | ||
| nonisolated | ||
| init(_ title: LocalizedStringResource, image name: String) { | ||
| self.init(title: Text(title), icon: Image(name)) | ||
| } | ||
|
|
||
| nonisolated | ||
| init(_ title: LocalizedStringResource, image resource: ImageResource) { | ||
| self.init(title: Text(title), icon: Image(resource)) | ||
| } | ||
| nonisolated | ||
| init(_ title: LocalizedStringKey, image name: String) { | ||
| self.init(title: Text(title), icon: Image(name)) | ||
| } | ||
|
|
||
| nonisolated | ||
| init(_ title: LocalizedStringKey, image resource: ImageResource) { | ||
| self.init(title: Text(title), icon: Image(resource)) | ||
| } | ||
| nonisolated | ||
| init<S>(_ title: S, image name: String) where S: StringProtocol { | ||
| self.init(title: Text(title), icon: Image(name)) | ||
| } | ||
|
|
||
| nonisolated | ||
| init<S>(_ titleResource: S, image resource: ImageResource) where S: StringProtocol { | ||
| self.init(title: Text(titleResource), icon: Image(resource)) | ||
| } | ||
| } | ||
|
|
||
| #Preview("Creating a label", body: {Label("label text", image: "add").body}) |
34 changes: 34 additions & 0 deletions
34
Tests/OpenSwiftUITests/View/Control/Button/ButtonTests.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // | ||
| // ButtonTests.swift | ||
| // OpenSwiftUITests | ||
|
|
||
| @testable import OpenSwiftUI | ||
| import Testing | ||
|
|
||
| @MainActor | ||
| @Suite(.snapshots(record: .never, diffTool: diffTool)) | ||
| struct ButtonTests { | ||
| @Test | ||
| func enabledButtonInvokesAction() { | ||
| var clicked = false; | ||
| var cb = { | ||
| clicked = true; | ||
| } | ||
|
|
||
| var button = Button ("Run", cb) | ||
| // TODO: Trigger attempt to press button. | ||
| expect(clicked, true); | ||
| } | ||
| @Test | ||
| func disabledButtonDoesntInvokeAction() { | ||
| var clicked = false; | ||
| var cb = { | ||
| clicked = true; | ||
| }; | ||
|
|
||
| var button = Button ("Run", cb) | ||
|
|
||
| // TODO: Attempt to trigger the button. | ||
| expect(clicked, false); | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| @testable import OpenSwiftUI | ||
| import Testing | ||
|
|
||
| @MainActor | ||
| @Suite(.snapshots(record: .never, diffTool: diffTool)) | ||
| struct LabelTests { | ||
| @Test | ||
| func hasTheExpectedChildren() { | ||
| final icon = Image("add") | ||
| final text = Text("test") | ||
| final label = Label("test", "add") | ||
| // TODO Actualy validate contents | ||
| expect(label.title == text, true) | ||
| expect(label.icon == icon, true) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't see any examples for testing gestures. Some guidance would be appreciated 😄