Skip to content

Commit

Permalink
hide RuleSearchBox on unfocused
Browse files Browse the repository at this point in the history
  • Loading branch information
aj3423 committed Sep 20, 2024
1 parent 9067cb3 commit 86cc421
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
15 changes: 15 additions & 0 deletions app/src/main/java/spam/blocker/ui/setting/SettingScreen.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package spam.blocker.ui.setting

import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
Expand All @@ -20,6 +22,7 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -83,11 +86,23 @@ fun SettingScreen() {
}
) {

val focusManager = LocalFocusManager.current

NormalColumnScrollbar(state = scrollState) {
Column(
modifier = M
.verticalScroll(scrollState)
.padding(top = 8.dp)

// For hiding the RuleSearchBox when clicking around
.clickable(
// Disable the clicking ripple effect
interactionSource = remember { MutableInteractionSource() },
indication = null
) {
// The RuleSearchBox will be hidden when it loses focus
focusManager.clearFocus(true)
}
) {
// global
GloballyEnabled()
Expand Down
55 changes: 27 additions & 28 deletions app/src/main/java/spam/blocker/ui/setting/regex/RuleSearchBox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusEvent
import androidx.compose.ui.layout.onGloballyPositioned
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
Expand All @@ -19,6 +20,7 @@ import spam.blocker.ui.M
import spam.blocker.ui.widgets.LeftDeleteSwipeWrapper
import spam.blocker.ui.widgets.StrInputBox
import spam.blocker.ui.widgets.SwipeInfo
import spam.blocker.util.loge

@Composable
fun RuleSearchBox(
Expand All @@ -28,36 +30,33 @@ fun RuleSearchBox(

if (vm.searchEnabled.value) {

LeftDeleteSwipeWrapper(
left = SwipeInfo(
onSwipe = {
vm.searchEnabled.value = false
vm.filter = ""
vm.reload(ctx)
}
)
) {
val focusRequester = remember { FocusRequester() }
var textFieldLoaded by remember { mutableStateOf(false) }
val focusRequester = remember { FocusRequester() }
var textFieldLoaded by remember { mutableStateOf(false) }

StrInputBox(
text = "",
leadingIconId = R.drawable.ic_find,
onValueChange = {
vm.filter = it
vm.reload(ctx)
},
// Auto focus, and force scroll to input box.
modifier = M
.focusRequester(focusRequester)
.onGloballyPositioned {
if (!textFieldLoaded) {
focusRequester.requestFocus() // IMPORTANT
textFieldLoaded = true // stop cyclic recompositions
}
StrInputBox(
text = "",
leadingIconId = R.drawable.ic_find,
onValueChange = {
vm.filter = it
vm.reload(ctx)
},
// Auto focus, and force scroll to input box.
modifier = M
.focusRequester(focusRequester)
.onGloballyPositioned {
if (!textFieldLoaded) {
focusRequester.requestFocus() // IMPORTANT
textFieldLoaded = true // stop cyclic recompositions
}
}
.onFocusEvent { focusState ->
if (textFieldLoaded && !focusState.isFocused) {
vm.searchEnabled.value = false
vm.filter = ""
vm.reload(ctx)
}
)
}
}
)

Spacer(M.height(8.dp))
}
Expand Down

0 comments on commit 86cc421

Please sign in to comment.