Skip to content

Commit

Permalink
fix: auto clear selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ZTFtrue committed Feb 2, 2024
1 parent af14e9c commit 76c32f4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
51 changes: 46 additions & 5 deletions app/src/main/java/com/ztftrue/music/ui/play/LyricsView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.content.Context
import android.content.Intent
import android.util.TypedValue
import android.view.MotionEvent
import android.view.View
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
Expand All @@ -25,6 +24,7 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.key
Expand All @@ -35,13 +35,19 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.input.pointer.motionEventSpy
import androidx.compose.ui.input.pointer.pointerInteropFilter
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.platform.LocalTextToolbar
import androidx.compose.ui.platform.LocalView
import androidx.compose.ui.platform.TextToolbarStatus
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
Expand Down Expand Up @@ -148,7 +154,11 @@ fun LyricsView(
var popupOffset by remember {
mutableStateOf(IntOffset(0, 0))
}
DisposableEffect(Unit) {
onDispose {

}
}
if (showMenu) {
val list = musicViewModel.dictionaryAppList
if (list.isEmpty()) {
Expand Down Expand Up @@ -282,16 +292,33 @@ fun LyricsView(
LocalTextToolbar provides CustomTextToolbar(
LocalView.current,
musicViewModel.dictionaryAppList
)
),
) {
val textToolbar = LocalTextToolbar.current
val focusManager = LocalFocusManager.current
SelectionContainer(
modifier = Modifier,
content = {
val nestedScrollConnection = remember {
object : NestedScrollConnection {
override fun onPreScroll(
available: Offset,
source: NestedScrollSource
): Offset {
if(textToolbar.status == TextToolbarStatus.Shown){
focusManager.clearFocus()
textToolbar.hide()
}
return super.onPreScroll(available, source)
}
}
}
LazyColumn(
state = listState,
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight()
.nestedScroll(nestedScrollConnection)
.pointerInteropFilter {
when (it.action) {
MotionEvent.ACTION_DOWN -> {
Expand All @@ -304,11 +331,23 @@ fun LyricsView(
popupOffset = IntOffset(0, a.toInt())
}
}

MotionEvent.ACTION_UP -> {
if (showMenu) {
showMenu = false
isSelected = false
selectedTag = ""
word = ""
}
}
}
false
}
.motionEventSpy {

if(it.action == MotionEvent.ACTION_DOWN&&textToolbar.status == TextToolbarStatus.Shown) {
textToolbar.hide()
focusManager.clearFocus()
}
}
.onSizeChanged { sizeIt ->
size.value = sizeIt
Expand Down Expand Up @@ -361,8 +400,10 @@ fun LyricsView(
.fillMaxWidth()
.padding(2.dp)
) { offset ->

if (showMenu) {
if(textToolbar.status == TextToolbarStatus.Shown) {
textToolbar.hide()
focusManager.clearFocus()
}else if (showMenu) {
showMenu = false
} else {
val annotations =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import android.view.ActionMode
import android.view.View
import androidx.annotation.DoNotInline
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.TextToolbar
import androidx.compose.ui.platform.TextToolbarStatus
import androidx.core.view.allViews
import com.ztftrue.music.sqlData.model.DictionaryApp


Expand Down Expand Up @@ -50,6 +48,7 @@ internal class CustomTextToolbar(
textActionModeCallback.onSelectAllRequested = onSelectAllRequested
textActionModeCallback.customProcessTextApp = customApp
textActionModeCallback.onProcessAppItemClick = {
view.tooltipText
var min = 0
// var max: Int = view.getText().length()
// if (mTextView.isFocused()) {
Expand All @@ -73,8 +72,8 @@ internal class CustomTextToolbar(
)
view.context.startActivity(intent)
}
status = TextToolbarStatus.Shown
if (actionMode == null) {
status = TextToolbarStatus.Shown
actionMode =
TextToolbarHelperMethods.startActionMode(
view,
Expand Down

0 comments on commit 76c32f4

Please sign in to comment.