refactor(history): simplify the search history sheet height
Size the sheet to its content with a minimum height and a static full-height cap. Keep the drag handle below display cutouts and avoid offset-dependent insets, which can destabilize the sheet anchors. Also drop the trailing period in the empty dictionary list description.
This commit is contained in:
1 parent
d8b0f38a96
commit
cf30a75afc
2 files changed
+18
-30
No files matched your search
@@ -5,13 +5,15 @@ import androidx.compose.foundation.layout.Arrangement
|
|||||||
import androidx.compose.foundation.layout.PaddingValues
|
import androidx.compose.foundation.layout.PaddingValues
|
||||||
import androidx.compose.foundation.layout.Row
|
import androidx.compose.foundation.layout.Row
|
||||||
import androidx.compose.foundation.layout.WindowInsets
|
import androidx.compose.foundation.layout.WindowInsets
|
||||||
|
import androidx.compose.foundation.layout.WindowInsetsSides
|
||||||
import androidx.compose.foundation.layout.fillMaxWidth
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||||||
import androidx.compose.foundation.layout.height
|
import androidx.compose.foundation.layout.height
|
||||||
import androidx.compose.foundation.layout.heightIn
|
import androidx.compose.foundation.layout.heightIn
|
||||||
import androidx.compose.foundation.layout.ime
|
import androidx.compose.foundation.layout.ime
|
||||||
import androidx.compose.foundation.layout.navigationBars
|
|
||||||
import androidx.compose.foundation.layout.offset
|
import androidx.compose.foundation.layout.offset
|
||||||
|
import androidx.compose.foundation.layout.only
|
||||||
import androidx.compose.foundation.layout.padding
|
import androidx.compose.foundation.layout.padding
|
||||||
|
import androidx.compose.foundation.layout.safeDrawing
|
||||||
import androidx.compose.foundation.layout.size
|
import androidx.compose.foundation.layout.size
|
||||||
import androidx.compose.foundation.layout.windowInsetsPadding
|
import androidx.compose.foundation.layout.windowInsetsPadding
|
||||||
import androidx.compose.foundation.shape.RoundedCornerShape
|
import androidx.compose.foundation.shape.RoundedCornerShape
|
||||||
@@ -34,7 +36,6 @@ import androidx.compose.material3.rememberBottomSheetState
|
|||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.LaunchedEffect
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableIntStateOf
|
|
||||||
import androidx.compose.runtime.mutableStateOf
|
import androidx.compose.runtime.mutableStateOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
import androidx.compose.runtime.rememberCoroutineScope
|
import androidx.compose.runtime.rememberCoroutineScope
|
||||||
@@ -45,11 +46,9 @@ import androidx.compose.ui.Alignment
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.graphics.Color
|
||||||
import androidx.compose.ui.graphics.painter.Painter
|
import androidx.compose.ui.graphics.painter.Painter
|
||||||
import androidx.compose.ui.layout.boundsInWindow
|
|
||||||
import androidx.compose.ui.layout.onGloballyPositioned
|
|
||||||
import androidx.compose.ui.platform.LocalWindowInfo
|
|
||||||
import androidx.compose.ui.platform.LocalDensity
|
import androidx.compose.ui.platform.LocalDensity
|
||||||
import androidx.compose.ui.platform.LocalFocusManager
|
import androidx.compose.ui.platform.LocalFocusManager
|
||||||
|
import androidx.compose.ui.platform.LocalWindowInfo
|
||||||
import androidx.compose.ui.platform.testTag
|
import androidx.compose.ui.platform.testTag
|
||||||
import androidx.compose.ui.semantics.semantics
|
import androidx.compose.ui.semantics.semantics
|
||||||
import androidx.compose.ui.semantics.testTagsAsResourceId
|
import androidx.compose.ui.semantics.testTagsAsResourceId
|
||||||
@@ -67,12 +66,10 @@ import com.example.research.ui.theme.AppWindowInsets
|
|||||||
import com.example.research.ui.theme.Spacing
|
import com.example.research.ui.theme.Spacing
|
||||||
import kotlinx.coroutines.flow.distinctUntilChanged
|
import kotlinx.coroutines.flow.distinctUntilChanged
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlin.math.roundToInt
|
|
||||||
import kotlin.time.Duration.Companion.milliseconds
|
import kotlin.time.Duration.Companion.milliseconds
|
||||||
|
|
||||||
private val TopBarHeight = Spacing.topBarHeight
|
private val TopBarHeight = Spacing.topBarHeight
|
||||||
private val TopBarShape = RoundedCornerShape(28.dp)
|
private val TopBarShape = RoundedCornerShape(28.dp)
|
||||||
private val HistorySheetChrome = 64.dp
|
|
||||||
private val HistorySheetMinHeight = 300.dp
|
private val HistorySheetMinHeight = 300.dp
|
||||||
private val HistorySheetBottomPadding = 16.dp
|
private val HistorySheetBottomPadding = 16.dp
|
||||||
|
|
||||||
@@ -97,16 +94,13 @@ fun MainTopBar(
|
|||||||
val focusManager = LocalFocusManager.current
|
val focusManager = LocalFocusManager.current
|
||||||
|
|
||||||
var showHistorySheet by remember { mutableStateOf(false) }
|
var showHistorySheet by remember { mutableStateOf(false) }
|
||||||
var topBarBottomPx by remember { mutableIntStateOf(0) }
|
|
||||||
val coroutineScope = rememberCoroutineScope()
|
val coroutineScope = rememberCoroutineScope()
|
||||||
val density = LocalDensity.current
|
val density = LocalDensity.current
|
||||||
val imeInsets = WindowInsets.ime
|
val imeInsets = WindowInsets.ime
|
||||||
|
|
||||||
Surface(
|
Surface(
|
||||||
color = MaterialTheme.colorScheme.surface,
|
color = MaterialTheme.colorScheme.surface,
|
||||||
modifier = Modifier
|
modifier = Modifier.fillMaxWidth()
|
||||||
.fillMaxWidth()
|
|
||||||
.onGloballyPositioned { topBarBottomPx = it.boundsInWindow().bottom.roundToInt() }
|
|
||||||
) {
|
) {
|
||||||
Row(
|
Row(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
@@ -175,8 +169,6 @@ fun MainTopBar(
|
|||||||
if (showHistorySheet && searchHistory.isNotEmpty()) {
|
if (showHistorySheet && searchHistory.isNotEmpty()) {
|
||||||
SearchHistorySheet(
|
SearchHistorySheet(
|
||||||
history = searchHistory,
|
history = searchHistory,
|
||||||
isSearchBarAtBottom = isSearchBarAtBottom,
|
|
||||||
topBarBottomPx = topBarBottomPx,
|
|
||||||
onDismiss = { showHistorySheet = false },
|
onDismiss = { showHistorySheet = false },
|
||||||
onEntryClick = { entry ->
|
onEntryClick = { entry ->
|
||||||
focusManager.clearFocus(force = true)
|
focusManager.clearFocus(force = true)
|
||||||
@@ -285,8 +277,6 @@ private fun SearchField(
|
|||||||
@Composable
|
@Composable
|
||||||
private fun SearchHistorySheet(
|
private fun SearchHistorySheet(
|
||||||
history: List<String>,
|
history: List<String>,
|
||||||
isSearchBarAtBottom: Boolean,
|
|
||||||
topBarBottomPx: Int,
|
|
||||||
onDismiss: () -> Unit,
|
onDismiss: () -> Unit,
|
||||||
onEntryClick: (String) -> Unit,
|
onEntryClick: (String) -> Unit,
|
||||||
onEntryRemove: (String) -> Unit,
|
onEntryRemove: (String) -> Unit,
|
||||||
@@ -297,24 +287,23 @@ private fun SearchHistorySheet(
|
|||||||
enabledValues = setOf(SheetValue.Hidden, SheetValue.Expanded),
|
enabledValues = setOf(SheetValue.Hidden, SheetValue.Expanded),
|
||||||
)
|
)
|
||||||
val density = LocalDensity.current
|
val density = LocalDensity.current
|
||||||
val windowHeightPx = LocalWindowInfo.current.containerSize.height
|
val sheetTopInset = with(density) {
|
||||||
val navigationBarsBottom = WindowInsets.navigationBars.getBottom(density)
|
WindowInsets.safeDrawing.only(WindowInsetsSides.Top).getTop(this).toDp()
|
||||||
val maxContentHeight = remember {
|
|
||||||
with(density) {
|
|
||||||
val screenHeight = windowHeightPx.toDp()
|
|
||||||
if (isSearchBarAtBottom) {
|
|
||||||
screenHeight - HistorySheetChrome - navigationBarsBottom.toDp()
|
|
||||||
} else {
|
|
||||||
screenHeight - topBarBottomPx.toDp()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
val sheetMaxHeight = with(density) {
|
||||||
|
LocalWindowInfo.current.containerSize.height.toDp()
|
||||||
|
} - sheetTopInset
|
||||||
|
|
||||||
ModalBottomSheet(
|
ModalBottomSheet(
|
||||||
onDismissRequest = onDismiss,
|
onDismissRequest = onDismiss,
|
||||||
sheetState = sheetState,
|
sheetState = sheetState,
|
||||||
contentWindowInsets = { WindowInsets(0, 0, 0, 0) },
|
contentWindowInsets = {
|
||||||
modifier = Modifier.testTag("search_history_sheet")
|
WindowInsets.safeDrawing.only(WindowInsetsSides.Bottom)
|
||||||
|
},
|
||||||
|
modifier = Modifier
|
||||||
|
.testTag("search_history_sheet")
|
||||||
|
.padding(top = sheetTopInset)
|
||||||
|
.heightIn(min = HistorySheetMinHeight, max = sheetMaxHeight)
|
||||||
) {
|
) {
|
||||||
SearchHistoryList(
|
SearchHistoryList(
|
||||||
history = history,
|
history = history,
|
||||||
@@ -324,7 +313,6 @@ private fun SearchHistorySheet(
|
|||||||
contentPadding = PaddingValues(bottom = HistorySheetBottomPadding),
|
contentPadding = PaddingValues(bottom = HistorySheetBottomPadding),
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
.fillMaxWidth()
|
.fillMaxWidth()
|
||||||
.heightIn(min = HistorySheetMinHeight, max = maxContentHeight)
|
|
||||||
.padding(horizontal = 24.dp)
|
.padding(horizontal = 24.dp)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,7 +90,7 @@
|
|||||||
<string name="action_add">Add</string>
|
<string name="action_add">Add</string>
|
||||||
<string name="action_add_another_url">Add another URL</string>
|
<string name="action_add_another_url">Add another URL</string>
|
||||||
<string name="action_remove">Remove</string>
|
<string name="action_remove">Remove</string>
|
||||||
<string name="dictionaries_empty_description">Add download links or import dictionary files to start searching.</string>
|
<string name="dictionaries_empty_description">Add download links or import dictionary files to start searching</string>
|
||||||
<plurals name="dictionary_entries_count">
|
<plurals name="dictionary_entries_count">
|
||||||
<item quantity="one">• %1$d entry</item>
|
<item quantity="one">• %1$d entry</item>
|
||||||
<item quantity="other">• %1$d entries</item>
|
<item quantity="other">• %1$d entries</item>
|
||||||
|
|||||||
Reference in new issue
Block a user