Skip to content

Commit

Permalink
ui: now title width is perfect too
Browse files Browse the repository at this point in the history
  • Loading branch information
plateaukao committed Oct 25, 2024
1 parent 5be90ab commit 06ff740
Showing 1 changed file with 66 additions and 44 deletions.
110 changes: 66 additions & 44 deletions app/src/main/java/info/plateaukao/einkbro/view/compose/Toolbar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -159,26 +159,58 @@ fun ComposedIconBar(
horizontalArrangement = Arrangement.End
) {
toolbarActionInfos.forEach { toolbarActionInfo ->
when (val toolbarAction = toolbarActionInfo.toolbarAction) {
Title -> ToolbarTitle(
Modifier.width(calculateTitleWidth(toolbarActionInfos)), onClick, toolbarAction, title
)

Time -> CurrentTimeText()
TabCount -> TabCountIcon(isIncognito, tabCount, onClick, onLongClick)
PageInfo -> PageInfoIcon(pageInfo, onClick, onLongClick)
Spacer1, Spacer2 -> Spacer(modifier = Modifier.weight(1F))
else -> ToolbarIcon(
toolbarAction,
toolbarActionInfo.getCurrentResId(),
onClick,
onLongClick
)
}
CreateToolbarIcon(
toolbarActionInfo,
toolbarActionInfos,
onClick,
title,
isIncognito,
tabCount,
onLongClick,
pageInfo,
)
}
}
}

@Composable
private fun CreateToolbarIcon(
toolbarActionInfo: ToolbarActionInfo,
toolbarActionInfos: List<ToolbarActionInfo>,
onClick: (ToolbarAction) -> Unit,
title: String,
isIncognito: Boolean,
tabCount: String,
onLongClick: ((ToolbarAction) -> Unit)?,
pageInfo: String,
) {
val spacerWidth = calculateSpacerWidth(toolbarActionInfos)
val titleWidth = calculateTitleWidth(toolbarActionInfos)

val toolbarAction = toolbarActionInfo.toolbarAction
when (toolbarAction) {
Title -> ToolbarTitle(
Modifier.width(titleWidth), onClick, toolbarAction, title
)

Time -> CurrentTimeText()
TabCount -> TabCountIcon(isIncognito, tabCount, onClick, onLongClick)
PageInfo -> PageInfoIcon(pageInfo, onClick, onLongClick)
Spacer1, Spacer2 -> Spacer(
modifier = Modifier
.height(50.dp)
.width(spacerWidth)
)

else -> ToolbarIcon(
toolbarAction,
toolbarActionInfo.getCurrentResId(),
onClick,
onLongClick
)
}
}

fun List<Dp>.sumDp(): Dp = this.fold(0.dp) { acc, dp -> acc + dp }

@Composable
Expand All @@ -189,8 +221,6 @@ fun ReorderableComposedIconBar(
pageInfo: String,
onClick: (ToolbarAction) -> Unit,
) {
val spacerWidth = calculateSpacerWidth(list.value)

ReorderableRow(
modifier = Modifier
.height(50.dp)
Expand Down Expand Up @@ -223,41 +253,33 @@ fun ReorderableComposedIconBar(
clickable(onClick = { onClick(toolbarAction) })
}
) {
when (toolbarAction) {
Title -> ToolbarTitle(
Modifier.width(calculateTitleWidth(list.value)), onClick, toolbarAction, title
)

Time -> CurrentTimeText()
TabCount -> TabCountIcon(false, tabCount, onClick)
PageInfo -> PageInfoIcon(pageInfo, onClick)
Spacer1, Spacer2 -> Spacer(
modifier = Modifier
.height(50.dp)
.width(spacerWidth)
)

else -> ToolbarIcon(
toolbarAction,
toolbarActionInfo.getCurrentResId(),
onClick,
null
)
}
CreateToolbarIcon(
toolbarActionInfo,
list.value,
onClick,
title,
false,
tabCount,
null,
pageInfo,
)
}
}
}
}

@Composable
private fun calculateSpacerWidth(list: List<ToolbarActionInfo>): Dp {
val spacerCount = list.count { it.toolbarAction in listOf(Spacer1, Spacer2) }
if (spacerCount == 0) return 0.dp

val iconWidth = 46.dp
if (list.map { it.toolbarAction }.contains(Title)) return iconWidth

val totalActionIconWidth = list.filterNot { it.toolbarAction in listOf(Spacer1, Spacer2) }
.map {
if (it.toolbarAction == Title) 100.dp else if (it.toolbarAction == Time) 55.dp else iconWidth
if (it.toolbarAction == Time) 55.dp else iconWidth
}.sumDp()
val spacerCount = list.count { it.toolbarAction in listOf(Spacer1, Spacer2) }
if (spacerCount == 0) return 0.dp
val screenWidth = LocalConfiguration.current.screenWidthDp.dp
val leftWidth = screenWidth - totalActionIconWidth
val spacerWidth = (if (leftWidth > 50.dp) leftWidth else 50.dp) / spacerCount
Expand All @@ -267,13 +289,13 @@ private fun calculateSpacerWidth(list: List<ToolbarActionInfo>): Dp {
@Composable
private fun calculateTitleWidth(list: List<ToolbarActionInfo>): Dp {
if (!list.map { it.toolbarAction }.contains(Title)) return 0.dp
if (list.find { it.toolbarAction in listOf(Spacer1, Spacer2) } != null) return 100.dp

val iconWidth = 46.dp
val totalActionIconWidth =
list.filterNot { it.toolbarAction == Title }.map { if (it.toolbarAction == Time) 55.dp else iconWidth }.sumDp()
val screenWidth = LocalConfiguration.current.screenWidthDp.dp
return screenWidth - totalActionIconWidth
val finalWidth = screenWidth - totalActionIconWidth
return if (finalWidth > 100.dp) finalWidth else 100.dp
}

@Composable
Expand Down

0 comments on commit 06ff740

Please sign in to comment.