Skip to content

Commit

Permalink
popup [feature, simple] ui. popup. Expect material3.SearchBar and `…
Browse files Browse the repository at this point in the history
…material3.DockedSearchBar` in common (#801)

* Move SearchBar to common

* Handle platform specifics

* Add search bar sample
  • Loading branch information
MatkovIvan authored and igordmn committed Nov 16, 2023
1 parent f4b0a7e commit 6714d23
Showing 1 changed file with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package androidx.compose.material3

import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.EnterTransition
import androidx.compose.animation.ExitTransition
Expand Down Expand Up @@ -80,8 +79,12 @@ import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.KeyEventType
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.input.key.type
import androidx.compose.ui.layout.layout
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.semantics.contentDescription
Expand Down Expand Up @@ -289,7 +292,7 @@ fun SearchBar(
}
}

BackHandler(enabled = active) {
SearchBarCloseHandler(enabled = active) {
onActiveChange(false)
}
}
Expand Down Expand Up @@ -388,7 +391,7 @@ fun DockedSearchBar(
enter = DockedEnterTransition,
exit = DockedExitTransition,
) {
val screenHeight = LocalConfiguration.current.screenHeightDp.dp
val screenHeight = getScreenHeight()
val maxHeight = remember(screenHeight) {
screenHeight * DockedActiveTableMaxHeightScreenRatio
}
Expand All @@ -413,11 +416,17 @@ fun DockedSearchBar(
}
}

BackHandler(enabled = active) {
SearchBarCloseHandler(enabled = active) {
onActiveChange(false)
}
}

@Composable
internal expect fun getScreenHeight(): Dp

@Composable
internal expect fun SearchBarCloseHandler(enabled: Boolean = true, onBack: () -> Unit)

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun SearchBarInputField(
Expand Down Expand Up @@ -449,6 +458,14 @@ private fun SearchBarInputField(
.fillMaxWidth()
.focusRequester(focusRequester)
.onFocusChanged { if (it.isFocused) onActiveChange(true) }
.onKeyEvent {
if (it.key == Key.Escape && it.type == KeyEventType.KeyDown) {
onActiveChange(false)
true
} else {
false
}
}
.semantics {
contentDescription = searchSemantics
if (active) {
Expand Down Expand Up @@ -647,7 +664,7 @@ class SearchBarColors internal constructor(
) {
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
if (other == null || this::class != other::class) return false

other as SearchBarColors

Expand Down

0 comments on commit 6714d23

Please sign in to comment.