-
-
Notifications
You must be signed in to change notification settings - Fork 112
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
Add basic ButtonStyle
implementation
#214
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a59f9bc
Button styles work
diskzero e9a94c1
Button style work
SemitoneGene 95db246
Add basic `ButtonStyle` implementation
MaxDesiatov 8962950
Fix pointer events
MaxDesiatov 921f2c5
Merge branch 'main' of github.com:swiftwasm/Tokamak into buttonstyles
MaxDesiatov 8acdf2a
Fix host view environment propagation
MaxDesiatov 5b437e9
Fix unused result warnings
MaxDesiatov f0cf159
Unify environment updates in MountedElement.init
MaxDesiatov 62de63a
Set appearance to `none` for non-default styles
MaxDesiatov 5f275e1
Tweak button style reset CSS
MaxDesiatov 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2020 Tokamak contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// Created by Gene Z. Ragan on 07/22/2020. | ||
|
||
public struct ButtonStyleConfiguration { | ||
public struct Label: View { | ||
let content: AnyView | ||
public var body: Never { | ||
neverBody("ButtonStyleConfiguration.Label") | ||
} | ||
} | ||
|
||
public let label: Label | ||
public let isPressed: Bool | ||
} | ||
|
||
/// This is a helper class that works around absence of "package private" access control in Swift | ||
public struct _ButtonStyleConfigurationProxy { | ||
public struct Label { | ||
public typealias Subject = ButtonStyleConfiguration.Label | ||
public let subject: Subject | ||
|
||
public init(_ subject: Subject) { self.subject = subject } | ||
|
||
public var content: AnyView { subject.content } | ||
} | ||
|
||
public typealias Subject = ButtonStyleConfiguration | ||
public let subject: Subject | ||
|
||
public init(label: AnyView, isPressed: Bool) { | ||
subject = .init(label: .init(content: label), isPressed: isPressed) | ||
} | ||
|
||
public var label: ButtonStyleConfiguration.Label { subject.label } | ||
} | ||
|
||
public protocol ButtonStyle { | ||
associatedtype Body: View | ||
|
||
func makeBody(configuration: Self.Configuration) -> Self.Body | ||
|
||
typealias Configuration = ButtonStyleConfiguration | ||
} | ||
|
||
public struct _AnyButtonStyle: ButtonStyle { | ||
public typealias Body = AnyView | ||
|
||
private let bodyClosure: (ButtonStyleConfiguration) -> AnyView | ||
|
||
public init<S: ButtonStyle>(_ style: S) { | ||
bodyClosure = { configuration in | ||
AnyView(style.makeBody(configuration: configuration)) | ||
} | ||
} | ||
|
||
public func makeBody(configuration: ButtonStyleConfiguration) -> AnyView { | ||
bodyClosure(configuration) | ||
} | ||
} | ||
|
||
public enum _ButtonStyleKey: EnvironmentKey { | ||
public static var defaultValue: _AnyButtonStyle { | ||
fatalError("\(self) must have a renderer-provided default value") | ||
} | ||
} | ||
|
||
extension EnvironmentValues { | ||
var buttonStyle: _AnyButtonStyle { | ||
get { | ||
self[_ButtonStyleKey.self] | ||
} | ||
set { | ||
self[_ButtonStyleKey.self] = newValue | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
public func buttonStyle<S>(_ style: S) -> some View where S: ButtonStyle { | ||
environment(\.buttonStyle, _AnyButtonStyle(style)) | ||
} | ||
} |
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
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,24 @@ | ||
// Copyright 2020 Tokamak contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
// Created by Gene Z. Ragan on 07/22/2020. | ||
// | ||
|
||
import TokamakCore | ||
|
||
public struct DefaultButtonStyle: ButtonStyle { | ||
public func makeBody(configuration: ButtonStyleConfiguration) -> some View { | ||
configuration.label | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright 2020 Tokamak contributors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#if canImport(SwiftUI) | ||
import SwiftUI | ||
#else | ||
import TokamakCore | ||
import TokamakDOM | ||
#endif | ||
|
||
struct PressedButtonStyle: ButtonStyle { | ||
let pressedColor: Color | ||
|
||
func makeBody(configuration: Configuration) -> some View { | ||
configuration.label | ||
.foregroundColor(configuration.isPressed ? pressedColor : .blue) | ||
.padding(15) | ||
} | ||
} | ||
|
||
public struct ButtonStyleDemo: View { | ||
public var body: some View { | ||
VStack { | ||
Button("Default Style") { | ||
print("tapped") | ||
return | ||
} | ||
Button("Pressed Button Style") { | ||
print("tapped") | ||
return | ||
} | ||
.buttonStyle( | ||
PressedButtonStyle(pressedColor: Color.red) | ||
) | ||
} | ||
} | ||
} |
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
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.
What’s the format for the header? Are we keeping these lines or removing them?
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 personally don't mind their presence (or absence) at all. They are added by Xcode, while files with no timestamps like that were created in other editors I guess (mine certainly were). I think adding these lines to files that don't currently have them is too much hassle, so if you want consistency it would be probably easier to just remove them everywhere.