-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add controlSize/controlProminence modifiers (#431)
- Loading branch information
1 parent
6792dbb
commit 22ea230
Showing
24 changed files
with
799 additions
and
171 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright 2018-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 Carson Katri on 7/12/21. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol ControlGroupStyle { | ||
associatedtype Body: View | ||
@ViewBuilder | ||
func makeBody(configuration: Self.Configuration) -> Self.Body | ||
typealias Configuration = ControlGroupStyleConfiguration | ||
} | ||
|
||
public struct ControlGroupStyleConfiguration { | ||
public struct Content: View { | ||
public let body: AnyView | ||
} | ||
|
||
public let content: ControlGroupStyleConfiguration.Content | ||
} | ||
|
||
public struct AutomaticControlGroupStyle: ControlGroupStyle { | ||
public init() {} | ||
|
||
public func makeBody(configuration: Self.Configuration) -> some View { | ||
Picker( | ||
selection: .constant(AnyHashable?.none), | ||
label: EmptyView() | ||
) { | ||
if let parentView = configuration.content.body.view as? ParentView { | ||
ForEach(Array(parentView.children.enumerated()), id: \.offset) { | ||
$0.element | ||
} | ||
} else { | ||
configuration.content | ||
} | ||
} | ||
.pickerStyle(SegmentedPickerStyle()) | ||
} | ||
} | ||
|
||
public struct NavigationControlGroupStyle: ControlGroupStyle { | ||
public init() {} | ||
|
||
public func makeBody(configuration: Self.Configuration) -> some View { | ||
HStack { | ||
configuration.content | ||
} | ||
} | ||
} | ||
|
||
public struct _AnyControlGroupStyle: ControlGroupStyle { | ||
public typealias Body = AnyView | ||
|
||
private let bodyClosure: (ControlGroupStyleConfiguration) -> AnyView | ||
public let type: Any.Type | ||
|
||
public init<S: ControlGroupStyle>(_ style: S) { | ||
type = S.self | ||
bodyClosure = { configuration in | ||
AnyView(style.makeBody(configuration: configuration)) | ||
} | ||
} | ||
|
||
public func makeBody(configuration: ControlGroupStyleConfiguration) -> AnyView { | ||
bodyClosure(configuration) | ||
} | ||
} | ||
|
||
extension EnvironmentValues { | ||
private enum ControlGroupStyleKey: EnvironmentKey { | ||
static let defaultValue = _AnyControlGroupStyle(AutomaticControlGroupStyle()) | ||
} | ||
|
||
var controlGroupStyle: _AnyControlGroupStyle { | ||
get { | ||
self[ControlGroupStyleKey.self] | ||
} | ||
set { | ||
self[ControlGroupStyleKey.self] = newValue | ||
} | ||
} | ||
} | ||
|
||
public extension View { | ||
func controlGroupStyle<S>( | ||
_ style: S | ||
) -> some View where S: ControlGroupStyle { | ||
environment(\.controlGroupStyle, .init(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// 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 Carson Katri on 7/12/21. | ||
// | ||
|
||
public protocol PrimitiveButtonStyle { | ||
associatedtype Body: View | ||
@ViewBuilder | ||
func makeBody(configuration: Self.Configuration) -> Self.Body | ||
typealias Configuration = PrimitiveButtonStyleConfiguration | ||
} | ||
|
||
public struct PrimitiveButtonStyleConfiguration { | ||
public struct Label: View { | ||
public let body: AnyView | ||
} | ||
|
||
public let role: ButtonRole? | ||
public let label: PrimitiveButtonStyleConfiguration.Label | ||
|
||
let action: () -> () | ||
public func trigger() { action() } | ||
} | ||
|
||
public struct DefaultButtonStyle: PrimitiveButtonStyle { | ||
public init() {} | ||
|
||
public func makeBody(configuration: Configuration) -> some View { | ||
BorderedButtonStyle().makeBody(configuration: configuration) | ||
} | ||
} | ||
|
||
public struct PlainButtonStyle: ButtonStyle { | ||
public init() {} | ||
|
||
public func makeBody(configuration: Configuration) -> some View { | ||
configuration.label | ||
.foregroundColor(configuration.isPressed ? .secondary : .primary) | ||
} | ||
} | ||
|
||
public struct BorderedButtonStyle: PrimitiveButtonStyle { | ||
public init() {} | ||
|
||
public func makeBody(configuration: Configuration) -> some View { | ||
_PrimitiveButtonStyleBody(style: self, configuration: configuration) { | ||
configuration.label | ||
} | ||
} | ||
} | ||
|
||
public struct BorderlessButtonStyle: ButtonStyle { | ||
public init() {} | ||
|
||
public func makeBody(configuration: Configuration) -> some View { | ||
configuration.label | ||
.foregroundColor(configuration.isPressed ? .primary : .secondary) | ||
} | ||
} | ||
|
||
public struct LinkButtonStyle: ButtonStyle { | ||
public init() {} | ||
|
||
public func makeBody(configuration: Configuration) -> some View { | ||
configuration.label.body | ||
.foregroundColor( | ||
configuration | ||
.isPressed ? Color(red: 128 / 255, green: 192 / 255, blue: 240 / 255) : .blue | ||
) | ||
} | ||
} | ||
|
||
struct AnyPrimitiveButtonStyle: PrimitiveButtonStyle { | ||
let bodyClosure: (PrimitiveButtonStyleConfiguration) -> AnyView | ||
let type: Any.Type | ||
|
||
init<S: PrimitiveButtonStyle>(_ style: S) { | ||
type = S.self | ||
bodyClosure = { | ||
AnyView(style.makeBody(configuration: $0)) | ||
} | ||
} | ||
|
||
func makeBody(configuration: Self.Configuration) -> AnyView { | ||
bodyClosure(configuration) | ||
} | ||
} | ||
|
||
extension EnvironmentValues { | ||
enum ButtonStyleKey: EnvironmentKey { | ||
enum ButtonStyleKeyValue { | ||
case primitiveButtonStyle(AnyPrimitiveButtonStyle) | ||
case buttonStyle(AnyButtonStyle) | ||
} | ||
|
||
public static let defaultValue: ButtonStyleKeyValue = .primitiveButtonStyle( | ||
.init(DefaultButtonStyle()) | ||
) | ||
} | ||
|
||
var buttonStyle: ButtonStyleKey.ButtonStyleKeyValue { | ||
get { | ||
self[ButtonStyleKey.self] | ||
} | ||
set { | ||
self[ButtonStyleKey.self] = newValue | ||
} | ||
} | ||
} | ||
|
||
public extension View { | ||
func buttonStyle<S>( | ||
_ style: S | ||
) -> some View where S: PrimitiveButtonStyle { | ||
environment(\.buttonStyle, .primitiveButtonStyle(.init(style))) | ||
} | ||
|
||
func buttonStyle<S>(_ style: S) -> some View where S: ButtonStyle { | ||
environment(\.buttonStyle, .buttonStyle(.init(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
Oops, something went wrong.