Skip to content

Commit

Permalink
Use material3 components that more closely align with SwiftUI.
Browse files Browse the repository at this point in the history
Fixes in applying foreground and tint color
  • Loading branch information
aabewhite committed Oct 3, 2023
1 parent 29fe0b2 commit 0d00510
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 14 deletions.
5 changes: 3 additions & 2 deletions Sources/SkipUI/SkipUI/Color/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import androidx.compose.foundation.background
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.material.ContentAlpha
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -148,11 +149,11 @@ public struct Color: View, Hashable, Sendable {
})

public static let primary = Color(colorImpl: {
MaterialTheme.colorScheme.onPrimary
MaterialTheme.colorScheme.onBackground
})

public static let secondary = Color(colorImpl: {
MaterialTheme.colorScheme.onSecondary
MaterialTheme.colorScheme.onBackground.copy(alpha: ContentAlpha.medium)
})

static let systemBackground = Color(colorImpl: {
Expand Down
9 changes: 9 additions & 0 deletions Sources/SkipUI/SkipUI/Compose/ComposeContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,13 @@ public struct ComposeContext {
return context
}
}

/// Mechanism for a parent view to change how a child view is composed.
//public protocol Composer {
// @Composable public func compose(view: inout View, context: ComposeContext)
//}
//
//extension Composer {
//
//}
#endif
2 changes: 1 addition & 1 deletion Sources/SkipUI/SkipUI/Containers/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public struct List<SelectionValue, Content> : View where SelectionValue: Hashabl
private static let verticalInset = 32.0
private static let minimumItemHeight = 44.0
private static let horizontalItemInset = 16.0
private static let verticalItemInset = 8.0
private static let verticalItemInset = 4.0

@Composable private func ComposeItem(view: inout View, context: ComposeContext, style: ListStyle) {
let contentModifier = Modifier.padding(horizontal: Self.horizontalItemInset.dp, vertical: Self.verticalItemInset.dp).fillWidth().requiredHeightIn(min: Self.minimumItemHeight.dp)
Expand Down
19 changes: 14 additions & 5 deletions Sources/SkipUI/SkipUI/Controls/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@
#if SKIP
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.material.ContentAlpha
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -58,12 +55,24 @@ public struct Button<Label> : View, ListItemAdapting where Label : View {
ComposeContainer(modifier: context.modifier) { modifier in
switch buttonStyle {
case .bordered:
let colors = ButtonDefaults.filledTonalButtonColors()
let colors: ButtonColors
if let tint = EnvironmentValues.shared._tint {
let tintColor = tint.colorImpl()
colors = ButtonDefaults.filledTonalButtonColors(containerColor: tintColor.copy(alpha: Float(0.15)), contentColor: tintColor, disabledContainerColor: tintColor.copy(alpha: Float(0.15)), disabledContentColor: tintColor.copy(alpha: ContentAlpha.medium))
} else {
colors = ButtonDefaults.filledTonalButtonColors()
}
FilledTonalButton(modifier: modifier, onClick: action, colors: colors) {
label.Compose(context: contentContext)
}
case .borderedProminent:
let colors = ButtonDefaults.buttonColors()
let colors: ButtonColors
if let tint = EnvironmentValues.shared._tint {
let tintColor = tint.colorImpl()
colors = ButtonDefaults.buttonColors(containerColor: tintColor, disabledContainerColor: tintColor.copy(alpha: ContentAlpha.disabled))
} else {
colors = ButtonDefaults.buttonColors()
}
androidx.compose.material3.Button(modifier: modifier, onClick: action, colors: colors) {
label.Compose(context: contentContext)
}
Expand Down
22 changes: 17 additions & 5 deletions Sources/SkipUI/SkipUI/Controls/TextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// as published by the Free Software Foundation https://fsf.org

#if SKIP
import androidx.compose.material.ContentAlpha
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.TextFieldColors
Expand Down Expand Up @@ -61,15 +62,26 @@ public struct TextField<Label> : View where Label : View {
@ExperimentalMaterial3Api
@Composable public override func ComposeContent(context: ComposeContext) {
let contentContext = context.content()
let colors = TextFieldDefaults.outlinedTextFieldColors()
let textColor = (EnvironmentValues.shared._color ?? Color.primary).colorImpl()
let colors: TextFieldColors
if let tint = EnvironmentValues.shared._tint {
let tintColor = tint.colorImpl()
colors = TextFieldDefaults.outlinedTextFieldColors(focusedTextColor: textColor, unfocusedTextColor: textColor, disabledTextColor: textColor.copy(alpha: ContentAlpha.disabled), cursorColor: tintColor, focusedBorderColor: tintColor)
} else {
colors = TextFieldDefaults.outlinedTextFieldColors(focusedTextColor: textColor, unfocusedTextColor: textColor, disabledTextColor: textColor.copy(alpha: ContentAlpha.disabled))
}
OutlinedTextField(value: text.wrappedValue, onValueChange: { text.wrappedValue = $0 }, modifier: context.modifier.fillWidth(), placeholder: { Placeholder(context: contentContext) }, singleLine: true, colors: colors)
}

@Composable private func Placeholder(context: ComposeContext) {
if let prompt {
prompt.Compose(context: context)
} else {
label.Compose(context: context)
EnvironmentValues.shared.setValues {
$0.set_color(Color(colorImpl: { Color.primary.colorImpl().copy(alpha: ContentAlpha.disabled) }))
} in: {
if let prompt {
prompt.Compose(context: context)
} else {
label.Compose(context: context)
}
}
}
#else
Expand Down
3 changes: 2 additions & 1 deletion Sources/SkipUI/SkipUI/Controls/Toggle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ public struct Toggle<Label> : View where Label : View {
@Composable public override func ComposeContent(context: ComposeContext) {
let colors: SwitchColors
if let tint = EnvironmentValues.shared._tint {
colors = SwitchDefaults.colors(checkedTrackColor: tint.colorImpl(), disabledCheckedTrackColor: tint.colorImpl().copy(alpha: ContentAlpha.disabled))
let tintColor = tint.colorImpl()
colors = SwitchDefaults.colors(checkedTrackColor: tintColor, disabledCheckedTrackColor: tintColor.copy(alpha: ContentAlpha.disabled))
} else {
colors = SwitchDefaults.colors()
}
Expand Down

0 comments on commit 0d00510

Please sign in to comment.