mirror of
https://github.com/BetterAndroid/FlexiUI.git
synced 2025-09-07 19:14:12 +08:00
refactor: decoupling function classes to scope and impl in ActionBar, Tab
This commit is contained in:
@@ -81,7 +81,7 @@ fun TopActionBar(
|
||||
style: ActionBarStyle? = null,
|
||||
titleText: @Composable () -> Unit,
|
||||
subText: @Composable (() -> Unit)? = null,
|
||||
actions: @Composable (BasicActionBar.() -> Unit)? = null
|
||||
actions: @Composable (ActionBarScope.() -> Unit)? = null
|
||||
) {
|
||||
BasicActionBar(
|
||||
type = ActionBarType.LARGE,
|
||||
@@ -103,9 +103,9 @@ fun ActionBar(
|
||||
style: ActionBarStyle? = null,
|
||||
titleText: @Composable () -> Unit,
|
||||
subText: @Composable (() -> Unit)? = null,
|
||||
finishIcon: @Composable (BasicActionBar.() -> Unit)? = null,
|
||||
navigationIcon: @Composable (BasicActionBar.() -> Unit)? = null,
|
||||
actions: @Composable (BasicActionBar.() -> Unit)? = null
|
||||
finishIcon: @Composable (ActionBarScope.() -> Unit)? = null,
|
||||
navigationIcon: @Composable (ActionBarScope.() -> Unit)? = null,
|
||||
actions: @Composable (ActionBarScope.() -> Unit)? = null
|
||||
) {
|
||||
BasicActionBar(
|
||||
type = ActionBarType.MIDDLE,
|
||||
@@ -128,15 +128,15 @@ private fun BasicActionBar(
|
||||
style: ActionBarStyle?,
|
||||
titleText: @Composable () -> Unit,
|
||||
subText: @Composable (() -> Unit)?,
|
||||
finishIcon: @Composable (BasicActionBar.() -> Unit)?,
|
||||
navigationIcon: @Composable (BasicActionBar.() -> Unit)?,
|
||||
actions: @Composable (BasicActionBar.() -> Unit)?
|
||||
finishIcon: @Composable (ActionBarScope.() -> Unit)?,
|
||||
navigationIcon: @Composable (ActionBarScope.() -> Unit)?,
|
||||
actions: @Composable (ActionBarScope.() -> Unit)?
|
||||
) {
|
||||
CompositionLocalProvider(LocalActionBarType provides type) {
|
||||
val currentColors = colors ?: ActionBar.colors
|
||||
val currentStyle = style ?: ActionBar.style
|
||||
Box(modifier = modifier.padding(currentStyle.padding)) {
|
||||
BasicActionBar(
|
||||
ActionBarImpl(
|
||||
type = type,
|
||||
colors = currentColors,
|
||||
style = currentStyle,
|
||||
@@ -150,17 +150,8 @@ private fun BasicActionBar(
|
||||
}
|
||||
}
|
||||
|
||||
@Immutable
|
||||
class BasicActionBar internal constructor(
|
||||
private val type: ActionBarType,
|
||||
private val colors: ActionBarColors,
|
||||
private val style: ActionBarStyle,
|
||||
private val titleText: @Composable () -> Unit,
|
||||
private val subText: @Composable (() -> Unit)?,
|
||||
private val finishIcon: @Composable (BasicActionBar.() -> Unit)?,
|
||||
private val navigationIcon: @Composable (BasicActionBar.() -> Unit)?,
|
||||
private val actions: @Composable (BasicActionBar.() -> Unit)?
|
||||
) {
|
||||
@Stable
|
||||
interface ActionBarScope {
|
||||
|
||||
@Composable
|
||||
fun FinishIconButton(
|
||||
@@ -210,7 +201,7 @@ class BasicActionBar internal constructor(
|
||||
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
|
||||
content: @Composable () -> Unit
|
||||
) {
|
||||
val iconInflateSize = this.style.actionIconSize + this.style.actionIconPadding
|
||||
val iconInflateSize = impl.style.actionIconSize + impl.style.actionIconPadding
|
||||
IconButton(
|
||||
onClick = onClick,
|
||||
modifier = Modifier.size(iconInflateSize).then(modifier),
|
||||
@@ -221,9 +212,22 @@ class BasicActionBar internal constructor(
|
||||
content = content
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Immutable
|
||||
private class ActionBarImpl(
|
||||
val type: ActionBarType,
|
||||
val colors: ActionBarColors,
|
||||
val style: ActionBarStyle,
|
||||
val titleText: @Composable () -> Unit,
|
||||
val subText: @Composable (() -> Unit)?,
|
||||
val finishIcon: @Composable (ActionBarScope.() -> Unit)?,
|
||||
val navigationIcon: @Composable (ActionBarScope.() -> Unit)?,
|
||||
val actions: @Composable (ActionBarScope.() -> Unit)?
|
||||
) : ActionBarScope {
|
||||
|
||||
@Composable
|
||||
internal fun Content() {
|
||||
fun Content() {
|
||||
BoxWithConstraints(modifier = Modifier.fillMaxWidth()) {
|
||||
val contentMaxWidth = maxWidth
|
||||
Row(
|
||||
@@ -308,7 +312,10 @@ class BasicActionBar internal constructor(
|
||||
}
|
||||
|
||||
@Stable
|
||||
internal enum class ActionBarType { LARGE, MIDDLE }
|
||||
private val ActionBarScope.impl get() = this as? ActionBarImpl? ?: error("Could not got ActionBarScope's impl.")
|
||||
|
||||
@Stable
|
||||
private enum class ActionBarType { LARGE, MIDDLE }
|
||||
|
||||
object ActionBar {
|
||||
val colors: ActionBarColors
|
||||
|
@@ -102,7 +102,7 @@ fun TabRow(
|
||||
modifier: Modifier = Modifier,
|
||||
colors: TabColors = Tab.colors,
|
||||
style: TabStyle = Tab.style,
|
||||
indicator: @Composable TabRow.() -> Unit = { TabIndicator(modifier = Modifier.tabIndicatorOffset()) },
|
||||
indicator: @Composable TabRowScope.() -> Unit = { TabIndicator(modifier = Modifier.tabIndicatorOffset()) },
|
||||
tabs: @Composable () -> Unit
|
||||
) {
|
||||
TabStyleBox(modifier, colors, style) {
|
||||
@@ -127,7 +127,7 @@ fun TabRow(
|
||||
placeable.placeRelative(x = index * tabAverageWidth, y = 0)
|
||||
}
|
||||
subcompose(TabSlots.Indicator) {
|
||||
indicator(TabRow(selectedTabIndex, colors, style, tabPositions))
|
||||
indicator(TabRowImpl(selectedTabIndex, colors, style, tabPositions))
|
||||
}.forEach {
|
||||
it.measure(Constraints.fixed(tabRowWidth, tabRowHeight)).placeRelative(x = 0, y = 0)
|
||||
}
|
||||
@@ -143,7 +143,7 @@ fun ScrollableTabRow(
|
||||
colors: TabColors = Tab.colors,
|
||||
style: TabStyle = Tab.style,
|
||||
scrollState: ScrollState = rememberScrollState(),
|
||||
indicator: @Composable TabRow.() -> Unit = { TabIndicator(modifier = Modifier.tabIndicatorOffset()) },
|
||||
indicator: @Composable TabRowScope.() -> Unit = { TabIndicator(modifier = Modifier.tabIndicatorOffset()) },
|
||||
tabs: @Composable () -> Unit
|
||||
) {
|
||||
TabStyleBox(modifier, colors, style) {
|
||||
@@ -176,7 +176,7 @@ fun ScrollableTabRow(
|
||||
tabLeft += placeables.width
|
||||
}
|
||||
subcompose(TabSlots.Indicator) {
|
||||
indicator(TabRow(selectedTabIndex, colors, style, tabPositions))
|
||||
indicator(TabRowImpl(selectedTabIndex, colors, style, tabPositions))
|
||||
}.forEach {
|
||||
it.measure(Constraints.fixed(layoutWidth, layoutHeight)).placeRelative(x = 0, y = 0)
|
||||
}
|
||||
@@ -265,27 +265,22 @@ data class TabPosition(val left: Dp, val width: Dp, val tabWidth: Dp) {
|
||||
fun calculateCenter(currentWidth: Dp) = left + width / 2 - currentWidth / 2
|
||||
}
|
||||
|
||||
@Immutable
|
||||
class TabRow internal constructor(
|
||||
val selectedTabIndex: Int,
|
||||
val colors: TabColors,
|
||||
val style: TabStyle,
|
||||
val tabPositions: List<TabPosition>
|
||||
) {
|
||||
@Stable
|
||||
interface TabRowScope {
|
||||
|
||||
@Composable
|
||||
fun TabIndicator(
|
||||
modifier: Modifier = Modifier,
|
||||
color: Color = colors.indicatorColor,
|
||||
height: Dp = style.indicatorHeight,
|
||||
shape: Shape = style.indicatorShape
|
||||
color: Color = impl.colors.indicatorColor,
|
||||
height: Dp = impl.style.indicatorHeight,
|
||||
shape: Shape = impl.style.indicatorShape
|
||||
) {
|
||||
Box(modifier.height(height).background(color, shape))
|
||||
}
|
||||
|
||||
fun Modifier.tabIndicatorOffset(
|
||||
currentTabPosition: TabPosition = tabPositions[selectedTabIndex],
|
||||
indicatorWidth: Dp = style.indicatorWidth
|
||||
currentTabPosition: TabPosition = impl.tabPositions[impl.selectedTabIndex],
|
||||
indicatorWidth: Dp = impl.style.indicatorWidth
|
||||
) = composed(
|
||||
inspectorInfo = debugInspectorInfo {
|
||||
name = "tabIndicatorOffset"
|
||||
@@ -310,8 +305,8 @@ class TabRow internal constructor(
|
||||
|
||||
fun Modifier.pagerTabIndicatorOffset(
|
||||
pagerState: PagerState,
|
||||
tabPositions: List<TabPosition> = this@TabRow.tabPositions,
|
||||
indicatorWidth: Dp = style.indicatorWidth
|
||||
tabPositions: List<TabPosition> = impl.tabPositions,
|
||||
indicatorWidth: Dp = impl.style.indicatorWidth
|
||||
) = composed(
|
||||
inspectorInfo = debugInspectorInfo {
|
||||
name = "pagerTabIndicatorOffset"
|
||||
@@ -361,6 +356,17 @@ class TabRow internal constructor(
|
||||
}
|
||||
}
|
||||
|
||||
@Immutable
|
||||
private class TabRowImpl(
|
||||
val selectedTabIndex: Int,
|
||||
val colors: TabColors,
|
||||
val style: TabStyle,
|
||||
val tabPositions: List<TabPosition>
|
||||
) : TabRowScope
|
||||
|
||||
@Stable
|
||||
private val TabRowScope.impl get() = this as? TabRowImpl? ?: error("Could not got TabRowScope's impl.")
|
||||
|
||||
@Immutable
|
||||
private class ScrollableTabData(private val scrollState: ScrollState, private val coroutineScope: CoroutineScope) {
|
||||
|
||||
|
Reference in New Issue
Block a user