Skip to content

Commit

Permalink
Merge pull request #58 from skiptools/bottombarappearance
Browse files Browse the repository at this point in the history
Extend transparent appearance to unscrolled tab and bottom bars
  • Loading branch information
aabewhite authored Sep 19, 2024
2 parents 8d9e7c5 + 91ac6f4 commit 58e0fc7
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 80 deletions.
4 changes: 2 additions & 2 deletions Sources/SkipUI/SkipUI/Color/ColorScheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public enum ColorScheme : CaseIterable, Hashable, Sendable {

#if SKIP
/// Return the color scheme for the current material color scheme.
@Composable public static func fromMaterialTheme() -> ColorScheme {
@Composable public static func fromMaterialTheme(colorScheme: androidx.compose.material3.ColorScheme = MaterialTheme.colorScheme) -> ColorScheme {
// Material3 doesn't have a built-in light vs dark property, so use the luminance of the background
return MaterialTheme.colorScheme.background.luminance() > Float(0.5) ? ColorScheme.light : ColorScheme.dark
return colorScheme.background.luminance() > Float(0.5) ? ColorScheme.light : ColorScheme.dark
}

/// Return the material color scheme for this scheme.
Expand Down
20 changes: 10 additions & 10 deletions Sources/SkipUI/SkipUI/Commands/Toolbar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// as published by the Free Software Foundation https://fsf.org

#if SKIP
import androidx.compose.foundation.gestures.ScrollableState
import androidx.compose.runtime.Composable
#endif

Expand Down Expand Up @@ -298,22 +299,20 @@ struct ToolbarPreferences: Equatable {
let titleDisplayMode: ToolbarTitleDisplayMode?
let titleMenu: View?
let backButtonHidden: Bool?
let isSystemBackground: Bool?
let navigationBar: ToolbarBarPreferences?
let bottomBar: ToolbarBarPreferences?

init(content: [View]? = nil, titleDisplayMode: ToolbarTitleDisplayMode? = nil, titleMenu: View? = nil, backButtonHidden: Bool? = nil, isSystemBackground: Bool? = nil, navigationBar: ToolbarBarPreferences? = nil, bottomBar: ToolbarBarPreferences? = nil) {
init(content: [View]? = nil, titleDisplayMode: ToolbarTitleDisplayMode? = nil, titleMenu: View? = nil, backButtonHidden: Bool? = nil, navigationBar: ToolbarBarPreferences? = nil, bottomBar: ToolbarBarPreferences? = nil) {
self.content = content
self.titleDisplayMode = titleDisplayMode
self.titleMenu = titleMenu
self.backButtonHidden = backButtonHidden
self.isSystemBackground = isSystemBackground
self.navigationBar = navigationBar
self.bottomBar = bottomBar
}

init(visibility: Visibility? = nil, background: ShapeStyle? = nil, backgroundVisibility: Visibility? = nil, colorScheme: ColorScheme? = nil, for bars: [ToolbarPlacement]) {
let barPreferences = ToolbarBarPreferences(visibility: visibility, background: background, backgroundVisibility: backgroundVisibility, colorScheme: colorScheme)
init(visibility: Visibility? = nil, background: ShapeStyle? = nil, backgroundVisibility: Visibility? = nil, colorScheme: ColorScheme? = nil, isSystemBackground: Bool? = nil, scrollableState: ScrollableState? = nil, for bars: [ToolbarPlacement]) {
let barPreferences = ToolbarBarPreferences(visibility: visibility, background: background, backgroundVisibility: backgroundVisibility, colorScheme: colorScheme, isSystemBackground: isSystemBackground, scrollableState: scrollableState)
var navigationBar: ToolbarBarPreferences? = nil
var bottomBar: ToolbarBarPreferences? = nil
for bar in bars {
Expand All @@ -332,7 +331,6 @@ struct ToolbarPreferences: Equatable {
self.titleDisplayMode = nil
self.titleMenu = nil
self.backButtonHidden = nil
self.isSystemBackground = nil
}

func reduce(_ next: ToolbarPreferences) -> ToolbarPreferences {
Expand All @@ -342,7 +340,7 @@ struct ToolbarPreferences: Equatable {
} else {
rcontent = next.content ?? content
}
return ToolbarPreferences(content: rcontent, titleDisplayMode: next.titleDisplayMode ?? titleDisplayMode, titleMenu: next.titleMenu ?? titleMenu, backButtonHidden: next.backButtonHidden ?? backButtonHidden, isSystemBackground: next.isSystemBackground ?? isSystemBackground, navigationBar: reduceBar(navigationBar, next.navigationBar), bottomBar: reduceBar(bottomBar, next.bottomBar))
return ToolbarPreferences(content: rcontent, titleDisplayMode: next.titleDisplayMode ?? titleDisplayMode, titleMenu: next.titleMenu ?? titleMenu, backButtonHidden: next.backButtonHidden ?? backButtonHidden, navigationBar: reduceBar(navigationBar, next.navigationBar), bottomBar: reduceBar(bottomBar, next.bottomBar))
}

private func reduceBar(_ bar: ToolbarBarPreferences?, _ next: ToolbarBarPreferences?) -> ToolbarBarPreferences? {
Expand All @@ -354,7 +352,7 @@ struct ToolbarPreferences: Equatable {
}

public static func ==(lhs: ToolbarPreferences, rhs: ToolbarPreferences) -> Bool {
guard lhs.titleDisplayMode == rhs.titleDisplayMode, lhs.backButtonHidden == rhs.backButtonHidden, lhs.isSystemBackground == rhs.isSystemBackground, lhs.navigationBar == rhs.navigationBar, lhs.bottomBar == rhs.bottomBar else {
guard lhs.titleDisplayMode == rhs.titleDisplayMode, lhs.backButtonHidden == rhs.backButtonHidden, lhs.navigationBar == rhs.navigationBar, lhs.bottomBar == rhs.bottomBar else {
return false
}
guard (lhs.content?.count ?? 0) == (rhs.content?.count ?? 0), (lhs.titleMenu != nil) == (rhs.titleMenu != nil) else {
Expand All @@ -371,14 +369,16 @@ struct ToolbarBarPreferences: Equatable {
let background: ShapeStyle?
let backgroundVisibility: Visibility?
let colorScheme: ColorScheme?
let isSystemBackground: Bool?
let scrollableState: ScrollableState?

func reduce(_ next: ToolbarBarPreferences) -> ToolbarBarPreferences {
return ToolbarBarPreferences(visibility: next.visibility ?? visibility, background: next.background ?? background, backgroundVisibility: next.backgroundVisibility ?? backgroundVisibility, colorScheme: next.colorScheme ?? colorScheme)
return ToolbarBarPreferences(visibility: next.visibility ?? visibility, background: next.background ?? background, backgroundVisibility: next.backgroundVisibility ?? backgroundVisibility, colorScheme: next.colorScheme ?? colorScheme, isSystemBackground: next.isSystemBackground ?? isSystemBackground, scrollableState: next.scrollableState ?? scrollableState)
}

public static func ==(lhs: ToolbarBarPreferences, rhs: ToolbarBarPreferences) -> Bool {
// Don't compare on background because it will never compare equal
return lhs.visibility == rhs.visibility && lhs.backgroundVisibility == rhs.backgroundVisibility && (lhs.background != nil) == (rhs.background != nil) && lhs.colorScheme == rhs.colorScheme
return lhs.visibility == rhs.visibility && lhs.backgroundVisibility == rhs.backgroundVisibility && (lhs.background != nil) == (rhs.background != nil) && lhs.colorScheme == rhs.colorScheme && lhs.isSystemBackground == rhs.isSystemBackground && lhs.scrollableState == rhs.scrollableState
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/SkipUI/SkipUI/Containers/LazyVGrid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public struct LazyVGrid: View {
}
}
PreferenceValues.shared.contribute(context: context, key: ScrollToIDPreferenceKey.self, value: scrollToID)
PreferenceValues.shared.contribute(context: context, key: ToolbarPreferenceKey.self, value: ToolbarPreferences(scrollableState: gridState, for: [ToolbarPlacement.bottomBar]))
PreferenceValues.shared.contribute(context: context, key: TabBarPreferenceKey.self, value: ToolbarBarPreferences(scrollableState: gridState))

LazyVerticalGrid(state: gridState, modifier: modifier, columns: gridCells, horizontalArrangement: horizontalArrangement, verticalArrangement: verticalArrangement, contentPadding: EnvironmentValues.shared._contentPadding.asPaddingValues(), userScrollEnabled: isScrollEnabled) {
factoryContext.value.initialize(
Expand Down
2 changes: 2 additions & 0 deletions Sources/SkipUI/SkipUI/Containers/LazyVStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ public struct LazyVStack : View {
}
}
PreferenceValues.shared.contribute(context: context, key: ScrollToIDPreferenceKey.self, value: scrollToID)
PreferenceValues.shared.contribute(context: context, key: ToolbarPreferenceKey.self, value: ToolbarPreferences(scrollableState: listState, for: [ToolbarPlacement.bottomBar]))
PreferenceValues.shared.contribute(context: context, key: TabBarPreferenceKey.self, value: ToolbarBarPreferences(scrollableState: listState))

Box(modifier: modifier) {
LazyColumn(state: listState, modifier: Modifier.fillMaxWidth(), verticalArrangement: columnArrangement, horizontalAlignment: columnAlignment, contentPadding: EnvironmentValues.shared._contentPadding.asPaddingValues(), userScrollEnabled: isScrollEnabled) {
Expand Down
4 changes: 3 additions & 1 deletion Sources/SkipUI/SkipUI/Containers/List.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ public final class List : View {
})
modifier = modifier.reorderable(reorderableState)

PreferenceValues.shared.contribute(context: context, key: ToolbarPreferenceKey.self, value: ToolbarPreferences(isSystemBackground: styling.style != ListStyle.plain && styling.backgroundVisibility != Visibility.hidden))
// Integrate with our scroll-to-top and ScrollViewReader
let coroutineScope = rememberCoroutineScope()
PreferenceValues.shared.contribute(context: context, key: ScrollToTopPreferenceKey.self, value: {
Expand All @@ -204,6 +203,9 @@ public final class List : View {
}
}
PreferenceValues.shared.contribute(context: context, key: ScrollToIDPreferenceKey.self, value: scrollToID)
let isSystemBackground = styling.style != ListStyle.plain && styling.backgroundVisibility != Visibility.hidden
PreferenceValues.shared.contribute(context: context, key: ToolbarPreferenceKey.self, value: ToolbarPreferences(isSystemBackground: isSystemBackground, scrollableState: listState, for: [ToolbarPlacement.navigationBar, ToolbarPlacement.bottomBar]))
PreferenceValues.shared.contribute(context: context, key: TabBarPreferenceKey.self, value: ToolbarBarPreferences(isSystemBackground: isSystemBackground, scrollableState: listState))

// List item animations in Compose work by setting the `animateItemPlacement` modifier on the items. Critically,
// this must be done when the items are composed *prior* to any animated change. So by default we compose all items
Expand Down
Loading

0 comments on commit 58e0fc7

Please sign in to comment.