Skip to content

Commit

Permalink
focus clearing on tapping outside text fields
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed May 4, 2024
1 parent 86fe92e commit 2d3ab82
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 5 deletions.
Expand Up @@ -41,6 +41,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -78,12 +79,14 @@ fun EnhancedButton(
) {
val settingsState = LocalSettingsState.current
val haptics = LocalHapticFeedback.current
val focus = LocalFocusManager.current

LocalMinimumInteractiveComponentSize.ProvidesValue(Dp.Unspecified) {
Box {
OutlinedButton(
onClick = {
onClick()
focus.clearFocus()
haptics.performHapticFeedback(
HapticFeedbackType.LongPress
)
Expand Down Expand Up @@ -135,11 +138,13 @@ fun EnhancedIconButton(
) {
val settingsState = LocalSettingsState.current
val haptics = LocalHapticFeedback.current
val focus = LocalFocusManager.current

LocalMinimumInteractiveComponentSize.ProvidesValue(Dp.Unspecified) {
OutlinedIconButton(
onClick = {
onClick()
focus.clearFocus()
haptics.performHapticFeedback(
HapticFeedbackType.LongPress
)
Expand Down
Expand Up @@ -39,6 +39,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.takeOrElse
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.platform.LocalViewConfiguration
import androidx.compose.ui.unit.Dp
Expand Down Expand Up @@ -66,6 +67,7 @@ fun EnhancedFloatingActionButton(
val settingsState = LocalSettingsState.current
val size by animateDpAsState(type.size)
val haptics = LocalHapticFeedback.current
val focus = LocalFocusManager.current

LocalMinimumInteractiveComponentSize.ProvidesValue(Dp.Unspecified) {
if (onLongClick != null) {
Expand All @@ -82,6 +84,7 @@ fun EnhancedFloatingActionButton(
delay(viewConfiguration.longPressTimeoutMillis)
isLongClick = true
onLongClick()
focus.clearFocus()
haptics.performHapticFeedback(
HapticFeedbackType.LongPress
)
Expand All @@ -90,6 +93,7 @@ fun EnhancedFloatingActionButton(
is PressInteraction.Release -> {
if (!isLongClick) {
onClick()
focus.clearFocus()
haptics.performHapticFeedback(
HapticFeedbackType.TextHandleMove
)
Expand All @@ -108,6 +112,7 @@ fun EnhancedFloatingActionButton(
onClick = {
if (onLongClick == null) {
onClick()
focus.clearFocus()
haptics.performHapticFeedback(
HapticFeedbackType.LongPress
)
Expand Down
Expand Up @@ -44,6 +44,7 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.compositeOver
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
Expand Down Expand Up @@ -180,10 +181,12 @@ fun ToggleGroupButton(
MaterialTheme.colorScheme.surfaceContainer
}
val selected = index == selectedIndex
val focus = LocalFocusManager.current

SegmentedButton(
enabled = enabled,
onClick = {
focus.clearFocus()
haptics.performHapticFeedback(
HapticFeedbackType.LongPress
)
Expand Down
Expand Up @@ -47,6 +47,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -102,6 +103,13 @@ fun EnhancedSlider(

val thumb: @Composable (SliderState) -> Unit = {
val interaction by interactionSource.interactions.collectAsState(initial = null)
val focus = LocalFocusManager.current

LaunchedEffect(interaction) {
if (interaction is PressInteraction.Press) {
focus.clearFocus()
}
}

val elevation = if (interaction is PressInteraction.Press) {
6.dp
Expand Down
Expand Up @@ -40,6 +40,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.compositeOver
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalHapticFeedback
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -70,6 +71,7 @@ fun EnhancedSwitch(
)
val settingsState = LocalSettingsState.current
val haptics = LocalHapticFeedback.current
val focus = LocalFocusManager.current

LocalMinimumInteractiveComponentSize.ProvidesValue(Dp.Unspecified) {
val switchModifier = modifier
Expand All @@ -92,6 +94,7 @@ fun EnhancedSwitch(
haptics.performHapticFeedback(
HapticFeedbackType.LongPress
)
focus.clearFocus()
}
}
val thumbContent: (@Composable () -> Unit)? = thumbIcon?.let {
Expand Down
Expand Up @@ -24,13 +24,15 @@ import androidx.compose.foundation.layout.size
import androidx.compose.material3.BasicAlertDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.unit.dp

@OptIn(ExperimentalMaterial3Api::class)
Expand All @@ -40,7 +42,13 @@ fun LoadingDialog(
canCancel: Boolean = true
) {
var showWantDismissDialog by remember(canCancel) { mutableStateOf(false) }
BasicAlertDialog(onDismissRequest = { showWantDismissDialog = canCancel }) {
BasicAlertDialog(
onDismissRequest = { showWantDismissDialog = canCancel }
) {
val focus = LocalFocusManager.current
LaunchedEffect(focus) {
focus.clearFocus()
}
Box(
modifier = Modifier
.fillMaxSize()
Expand Down Expand Up @@ -71,7 +79,13 @@ fun LoadingDialog(
canCancel: Boolean = true,
) {
var showWantDismissDialog by remember(canCancel) { mutableStateOf(false) }
BasicAlertDialog(onDismissRequest = { showWantDismissDialog = canCancel }) {
BasicAlertDialog(
onDismissRequest = { showWantDismissDialog = canCancel }
) {
val focus = LocalFocusManager.current
LaunchedEffect(focus) {
focus.clearFocus()
}
Box(
Modifier
.fillMaxSize()
Expand Down
Expand Up @@ -23,6 +23,7 @@ import androidx.activity.ComponentActivity
import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -73,8 +74,10 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalLayoutDirection
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -214,6 +217,14 @@ fun CropScreen(
)
)

val focus = LocalFocusManager.current

LaunchedEffect(scaffoldState.bottomSheetState.currentValue) {
if (scaffoldState.bottomSheetState.currentValue != SheetValue.Expanded) {
focus.clearFocus()
}
}

val controls: @Composable () -> Unit = {
Column(Modifier.verticalScroll(rememberScrollState())) {
Spacer(modifier = Modifier.height(16.dp))
Expand Down Expand Up @@ -394,7 +405,11 @@ fun CropScreen(
}

Column(
Modifier.weight(0.5f)
Modifier
.weight(0.5f)
.pointerInput(Unit) {
detectTapGestures { focus.clearFocus() }
}
) {
controls()
}
Expand Down Expand Up @@ -516,7 +531,12 @@ fun CropScreen(
sheetDragHandle = null,
sheetShape = RectangleShape,
sheetContent = {
Column(Modifier.heightIn(max = screenHeight * 0.7f)) {
Column(
Modifier
.heightIn(max = screenHeight * 0.7f)
.pointerInput(Unit) {
detectTapGestures { focus.clearFocus() }
}) {
BottomAppBar(
modifier = Modifier.drawHorizontalStroke(true),
actions = {
Expand Down
Expand Up @@ -34,8 +34,10 @@ import androidx.compose.material.icons.rounded.Done
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.SheetValue
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -44,6 +46,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.smarttoolfactory.cropper.model.AspectRatio
Expand All @@ -52,6 +55,7 @@ import com.smarttoolfactory.cropper.settings.CropProperties
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import ru.tech.imageresizershrinker.core.domain.utils.notNullAnd
import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.resources.icons.CropSmall
import ru.tech.imageresizershrinker.core.settings.domain.model.DomainAspectRatio
Expand Down Expand Up @@ -88,7 +92,15 @@ fun CropEditOption(
visible = visible,
onDismiss = onDismiss,
useScaffold = useScaffold,
controls = {
controls = { scaffoldState ->
val focus = LocalFocusManager.current
LaunchedEffect(scaffoldState?.bottomSheetState?.currentValue, focus) {
val current = scaffoldState?.bottomSheetState?.currentValue
if (current.notNullAnd { it != SheetValue.Expanded }) {
focus.clearFocus()
}
}

Spacer(modifier = Modifier.height(16.dp))
AspectRatioSelection(
modifier = Modifier
Expand Down

0 comments on commit 2d3ab82

Please sign in to comment.