diff --git a/app/src/main/java/com/example/research/ui/main/components/MainTopBar.kt b/app/src/main/java/com/example/research/ui/main/components/MainTopBar.kt index 0b1a8cc..bc1ae38 100644 --- a/app/src/main/java/com/example/research/ui/main/components/MainTopBar.kt +++ b/app/src/main/java/com/example/research/ui/main/components/MainTopBar.kt @@ -5,13 +5,15 @@ import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.WindowInsets +import androidx.compose.foundation.layout.WindowInsetsSides import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.heightIn import androidx.compose.foundation.layout.ime -import androidx.compose.foundation.layout.navigationBars import androidx.compose.foundation.layout.offset +import androidx.compose.foundation.layout.only import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.safeDrawing import androidx.compose.foundation.layout.size import androidx.compose.foundation.layout.windowInsetsPadding import androidx.compose.foundation.shape.RoundedCornerShape @@ -34,7 +36,6 @@ import androidx.compose.material3.rememberBottomSheetState import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope @@ -45,11 +46,9 @@ import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color 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.LocalFocusManager +import androidx.compose.ui.platform.LocalWindowInfo import androidx.compose.ui.platform.testTag import androidx.compose.ui.semantics.semantics 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 kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.launch -import kotlin.math.roundToInt import kotlin.time.Duration.Companion.milliseconds private val TopBarHeight = Spacing.topBarHeight private val TopBarShape = RoundedCornerShape(28.dp) -private val HistorySheetChrome = 64.dp private val HistorySheetMinHeight = 300.dp private val HistorySheetBottomPadding = 16.dp @@ -97,16 +94,13 @@ fun MainTopBar( val focusManager = LocalFocusManager.current var showHistorySheet by remember { mutableStateOf(false) } - var topBarBottomPx by remember { mutableIntStateOf(0) } val coroutineScope = rememberCoroutineScope() val density = LocalDensity.current val imeInsets = WindowInsets.ime Surface( color = MaterialTheme.colorScheme.surface, - modifier = Modifier - .fillMaxWidth() - .onGloballyPositioned { topBarBottomPx = it.boundsInWindow().bottom.roundToInt() } + modifier = Modifier.fillMaxWidth() ) { Row( modifier = Modifier @@ -175,8 +169,6 @@ fun MainTopBar( if (showHistorySheet && searchHistory.isNotEmpty()) { SearchHistorySheet( history = searchHistory, - isSearchBarAtBottom = isSearchBarAtBottom, - topBarBottomPx = topBarBottomPx, onDismiss = { showHistorySheet = false }, onEntryClick = { entry -> focusManager.clearFocus(force = true) @@ -285,8 +277,6 @@ private fun SearchField( @Composable private fun SearchHistorySheet( history: List, - isSearchBarAtBottom: Boolean, - topBarBottomPx: Int, onDismiss: () -> Unit, onEntryClick: (String) -> Unit, onEntryRemove: (String) -> Unit, @@ -297,24 +287,23 @@ private fun SearchHistorySheet( enabledValues = setOf(SheetValue.Hidden, SheetValue.Expanded), ) val density = LocalDensity.current - val windowHeightPx = LocalWindowInfo.current.containerSize.height - val navigationBarsBottom = WindowInsets.navigationBars.getBottom(density) - val maxContentHeight = remember { - with(density) { - val screenHeight = windowHeightPx.toDp() - if (isSearchBarAtBottom) { - screenHeight - HistorySheetChrome - navigationBarsBottom.toDp() - } else { - screenHeight - topBarBottomPx.toDp() - } - } + val sheetTopInset = with(density) { + WindowInsets.safeDrawing.only(WindowInsetsSides.Top).getTop(this).toDp() } + val sheetMaxHeight = with(density) { + LocalWindowInfo.current.containerSize.height.toDp() + } - sheetTopInset ModalBottomSheet( onDismissRequest = onDismiss, sheetState = sheetState, - contentWindowInsets = { WindowInsets(0, 0, 0, 0) }, - modifier = Modifier.testTag("search_history_sheet") + contentWindowInsets = { + WindowInsets.safeDrawing.only(WindowInsetsSides.Bottom) + }, + modifier = Modifier + .testTag("search_history_sheet") + .padding(top = sheetTopInset) + .heightIn(min = HistorySheetMinHeight, max = sheetMaxHeight) ) { SearchHistoryList( history = history, @@ -324,7 +313,6 @@ private fun SearchHistorySheet( contentPadding = PaddingValues(bottom = HistorySheetBottomPadding), modifier = Modifier .fillMaxWidth() - .heightIn(min = HistorySheetMinHeight, max = maxContentHeight) .padding(horizontal = 24.dp) ) } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 64ce44e..eb3ba84 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -90,7 +90,7 @@ Add Add another URL Remove - Add download links or import dictionary files to start searching. + Add download links or import dictionary files to start searching • %1$d entry • %1$d entries