Skip to content

Commit

Permalink
Expect material3.SearchBar and material3.DockedSearchBar in common (
Browse files Browse the repository at this point in the history
#801)

* Move SearchBar to common

* Handle platform specifics

* Add search bar sample
  • Loading branch information
MatkovIvan authored and igordmn committed Jan 30, 2024
1 parent a92be48 commit 9294cce
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.material3

import androidx.activity.compose.BackHandler
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp

@Composable
internal actual fun getScreenHeight(): Dp =
LocalConfiguration.current.screenHeightDp.dp

@Composable
internal actual fun SearchBarCloseHandler(enabled: Boolean, onBack: () -> Unit) =
BackHandler(enabled, onBack)
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,10 @@ 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.key
import androidx.compose.ui.input.key.onKeyEvent
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 +290,7 @@ fun SearchBar(
}
}

BackHandler(enabled = active) {
SearchBarCloseHandler(enabled = active) {
onActiveChange(false)
}
}
Expand Down Expand Up @@ -388,7 +389,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 +414,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 +456,14 @@ private fun SearchBarInputField(
.fillMaxWidth()
.focusRequester(focusRequester)
.onFocusChanged { if (it.isFocused) onActiveChange(true) }
.onKeyEvent {
if (it.key == Key.Escape) {
onActiveChange(false)
true
} else {
false
}
}
.semantics {
contentDescription = searchSemantics
if (active) {
Expand Down Expand Up @@ -647,7 +662,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package androidx.compose.material3

import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalWindowInfo
import androidx.compose.ui.unit.Dp

@OptIn(ExperimentalComposeUiApi::class)
@Composable
internal actual fun getScreenHeight(): Dp {
val density = LocalDensity.current
val windowInfo = LocalWindowInfo.current
return with(density) {
windowInfo.containerSize.height.toDp()
}
}

@Composable
internal actual fun SearchBarCloseHandler(enabled: Boolean, onBack: () -> Unit) {
// Nothing
}

0 comments on commit 9294cce

Please sign in to comment.