Replace Modifier.composed with Composable Modifier (#1959)

Co-authored-by: AntsyLich <59261191+AntsyLich@users.noreply.github.com>
(cherry picked from commit a31b3b7bbf2c5164baf76ac4b36f1d27c5d43135)
This commit is contained in:
ArthurKun 2025-04-14 02:31:04 +08:00 committed by Jobobby04
parent b6409b05e7
commit 802b6508fa
3 changed files with 31 additions and 32 deletions

View File

@ -25,7 +25,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@ -86,7 +85,8 @@ internal fun BasePreferenceWidget(
} }
} }
internal fun Modifier.highlightBackground(highlighted: Boolean): Modifier = composed { @Composable
internal fun Modifier.highlightBackground(highlighted: Boolean): Modifier {
var highlightFlag by remember { mutableStateOf(false) } var highlightFlag by remember { mutableStateOf(false) }
LaunchedEffect(Unit) { LaunchedEffect(Unit) {
if (highlighted) { if (highlighted) {
@ -116,7 +116,7 @@ internal fun Modifier.highlightBackground(highlighted: Boolean): Modifier = comp
}, },
label = "highlight", label = "highlight",
) )
Modifier.background(color = highlight) return this.background(color = highlight)
} }
internal val TrailingWidgetBuffer = 16.dp internal val TrailingWidgetBuffer = 16.dp

View File

@ -5,6 +5,7 @@ import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.WindowInsets import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.isImeVisible import androidx.compose.foundation.layout.isImeVisible
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
@ -12,7 +13,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.alpha import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.drawBehind import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.focus.FocusRequester import androidx.compose.ui.focus.FocusRequester
@ -24,16 +24,12 @@ import androidx.compose.ui.input.key.onPreviewKeyEvent
import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.platform.LocalFocusManager
import tachiyomi.presentation.core.components.material.SECONDARY_ALPHA import tachiyomi.presentation.core.components.material.SECONDARY_ALPHA
fun Modifier.selectedBackground(isSelected: Boolean): Modifier = if (isSelected) { @Composable
composed { fun Modifier.selectedBackground(isSelected: Boolean): Modifier {
if (!isSelected) return this
val alpha = if (isSystemInDarkTheme()) 0.16f else 0.22f val alpha = if (isSystemInDarkTheme()) 0.16f else 0.22f
val color = MaterialTheme.colorScheme.secondary.copy(alpha = alpha) val color = MaterialTheme.colorScheme.secondary.copy(alpha = alpha)
Modifier.drawBehind { return this.drawBehind { drawRect(color) }
drawRect(color)
}
}
} else {
this
} }
fun Modifier.secondaryItemAlpha(): Modifier = this.alpha(SECONDARY_ALPHA) fun Modifier.secondaryItemAlpha(): Modifier = this.alpha(SECONDARY_ALPHA)
@ -60,6 +56,7 @@ fun Modifier.runOnEnterKeyPressed(action: () -> Unit): Modifier = this.onPreview
action() action()
true true
} }
else -> false else -> false
} }
} }
@ -68,8 +65,9 @@ fun Modifier.runOnEnterKeyPressed(action: () -> Unit): Modifier = this.onPreview
* For TextField on AppBar, this modifier will request focus * For TextField on AppBar, this modifier will request focus
* to the element the first time it's composed. * to the element the first time it's composed.
*/ */
fun Modifier.showSoftKeyboard(show: Boolean): Modifier = if (show) { @Composable
composed { fun Modifier.showSoftKeyboard(show: Boolean): Modifier {
if (!show) return this
val focusRequester = remember { FocusRequester() } val focusRequester = remember { FocusRequester() }
var openKeyboard by rememberSaveable { mutableStateOf(show) } var openKeyboard by rememberSaveable { mutableStateOf(show) }
LaunchedEffect(focusRequester) { LaunchedEffect(focusRequester) {
@ -78,20 +76,17 @@ fun Modifier.showSoftKeyboard(show: Boolean): Modifier = if (show) {
openKeyboard = false openKeyboard = false
} }
} }
return this.focusRequester(focusRequester)
Modifier.focusRequester(focusRequester)
}
} else {
this
} }
/** /**
* For TextField, this modifier will clear focus when soft * For TextField, this modifier will clear focus when soft
* keyboard is hidden. * keyboard is hidden.
*/ */
@Composable
fun Modifier.clearFocusOnSoftKeyboardHide( fun Modifier.clearFocusOnSoftKeyboardHide(
onFocusCleared: (() -> Unit)? = null, onFocusCleared: (() -> Unit)? = null,
): Modifier = composed { ): Modifier {
var isFocused by remember { mutableStateOf(false) } var isFocused by remember { mutableStateOf(false) }
var keyboardShowedSinceFocused by remember { mutableStateOf(false) } var keyboardShowedSinceFocused by remember { mutableStateOf(false) }
if (isFocused) { if (isFocused) {
@ -107,7 +102,7 @@ fun Modifier.clearFocusOnSoftKeyboardHide(
} }
} }
Modifier.onFocusChanged { return this.onFocusChanged {
if (isFocused != it.isFocused) { if (isFocused != it.isFocused) {
if (isFocused) { if (isFocused) {
keyboardShowedSinceFocused = false keyboardShowedSinceFocused = false

View File

@ -47,7 +47,6 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.draw.drawWithContent import androidx.compose.ui.draw.drawWithContent
import androidx.compose.ui.geometry.Offset import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size import androidx.compose.ui.geometry.Size
@ -75,6 +74,7 @@ import tachiyomi.presentation.core.components.Scroller.STICKY_HEADER_KEY_PREFIX
* *
* Set key with [STICKY_HEADER_KEY_PREFIX] prefix to any sticky header item in the list. * Set key with [STICKY_HEADER_KEY_PREFIX] prefix to any sticky header item in the list.
*/ */
@Composable
fun Modifier.drawHorizontalScrollbar( fun Modifier.drawHorizontalScrollbar(
state: LazyListState, state: LazyListState,
reverseScrolling: Boolean = false, reverseScrolling: Boolean = false,
@ -87,6 +87,7 @@ fun Modifier.drawHorizontalScrollbar(
* *
* Set key with [STICKY_HEADER_KEY_PREFIX] prefix to any sticky header item in the list. * Set key with [STICKY_HEADER_KEY_PREFIX] prefix to any sticky header item in the list.
*/ */
@Composable
fun Modifier.drawVerticalScrollbar( fun Modifier.drawVerticalScrollbar(
state: LazyListState, state: LazyListState,
reverseScrolling: Boolean = false, reverseScrolling: Boolean = false,
@ -94,6 +95,7 @@ fun Modifier.drawVerticalScrollbar(
positionOffsetPx: Float = 0f, positionOffsetPx: Float = 0f,
): Modifier = drawScrollbar(state, Orientation.Vertical, reverseScrolling, positionOffsetPx) ): Modifier = drawScrollbar(state, Orientation.Vertical, reverseScrolling, positionOffsetPx)
@Composable
private fun Modifier.drawScrollbar( private fun Modifier.drawScrollbar(
state: LazyListState, state: LazyListState,
orientation: Orientation, orientation: Orientation,
@ -178,6 +180,7 @@ private fun ContentDrawScope.onDrawScrollbar(
} }
} }
@Composable
private fun Modifier.drawScrollbar( private fun Modifier.drawScrollbar(
orientation: Orientation, orientation: Orientation,
reverseScrolling: Boolean, reverseScrolling: Boolean,
@ -188,7 +191,7 @@ private fun Modifier.drawScrollbar(
color: Color, color: Color,
alpha: () -> Float, alpha: () -> Float,
) -> Unit, ) -> Unit,
): Modifier = composed { ): Modifier {
val scrolled = remember { val scrolled = remember {
MutableSharedFlow<Unit>( MutableSharedFlow<Unit>(
extraBufferCapacity = 1, extraBufferCapacity = 1,
@ -230,7 +233,8 @@ private fun Modifier.drawScrollbar(
val context = LocalContext.current val context = LocalContext.current
val thickness = remember { ViewConfiguration.get(context).scaledScrollBarSize.toFloat() } val thickness = remember { ViewConfiguration.get(context).scaledScrollBarSize.toFloat() }
val color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.364f) val color = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.364f)
Modifier
return this
.nestedScroll(nestedScrollConnection) .nestedScroll(nestedScrollConnection)
.drawWithContent { .drawWithContent {
onDraw(reverseDirection, atEnd, thickness, color, alpha::value) onDraw(reverseDirection, atEnd, thickness, color, alpha::value)