perf(article): hoist accessibility listener to screen level

Each article block registered its own assistive-accessibility listeners,
so a long article accumulated one listener pair per block. Register the
listeners once per article screen and share the state through a
CompositionLocal so the block count no longer grows the listener set.
This commit is contained in:
OneWay committed 2026-11-22 18:47:27 +08:00
1 parent e733a2f3c0
commit 983e28fcf4
2 files changed
+14 -6

No files matched your search

@@ -31,6 +31,8 @@ import com.example.research.BuildConfig
internal val LocalForceStockArticleLinks = compositionLocalOf { false } internal val LocalForceStockArticleLinks = compositionLocalOf { false }
internal val LocalAssistiveAccessibilityEnabled = compositionLocalOf { false }
internal val ArticleReferenceAtOffsetAction = internal val ArticleReferenceAtOffsetAction =
SemanticsPropertyKey<(Int) -> Boolean>("ArticleReferenceAtOffsetAction") SemanticsPropertyKey<(Int) -> Boolean>("ArticleReferenceAtOffsetAction")
@@ -58,7 +60,7 @@ internal fun ArticleLinkText(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
preferDirectHitTesting: Boolean = false, preferDirectHitTesting: Boolean = false,
) { ) {
val accessibilityEnabled = rememberAssistiveAccessibilityEnabled() val accessibilityEnabled = LocalAssistiveAccessibilityEnabled.current
val inputMode = LocalInputModeManager.current.inputMode val inputMode = LocalInputModeManager.current.inputMode
val forceStockLinks = LocalForceStockArticleLinks.current val forceStockLinks = LocalForceStockArticleLinks.current
val useDirectLinkHitTesting = val useDirectLinkHitTesting =
@@ -136,7 +138,7 @@ internal fun ArticleLinkText(
} }
@Composable @Composable
private fun rememberAssistiveAccessibilityEnabled(): Boolean { internal fun rememberAssistiveAccessibilityEnabled(): Boolean {
val context = LocalContext.current val context = LocalContext.current
val manager = remember(context) { context.getSystemService<AccessibilityManager>() } val manager = remember(context) { context.getSystemService<AccessibilityManager>() }
var enabled by remember(manager) { var enabled by remember(manager) {
@@ -24,6 +24,7 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface import androidx.compose.material3.Surface
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.key import androidx.compose.runtime.key
@@ -134,6 +135,7 @@ fun ArticleContent(
} }
} }
val accessibilityEnabled = rememberAssistiveAccessibilityEnabled()
val isHeavyArticle = articleState.isHeavy val isHeavyArticle = articleState.isHeavy
val darkTheme = MaterialTheme.colorScheme.background.luminance() < 0.5f val darkTheme = MaterialTheme.colorScheme.background.luminance() < 0.5f
@@ -193,10 +195,14 @@ fun ArticleContent(
.onSizeChanged { viewportSize = it } .onSizeChanged { viewportSize = it }
.windowInsetsPadding(AppWindowInsets.horizontalSafeDrawing) .windowInsetsPadding(AppWindowInsets.horizontalSafeDrawing)
.padding(end = 16.dp) .padding(end = 16.dp)
if (isHeavyArticle) { CompositionLocalProvider(
articleList(articleListModifier) LocalAssistiveAccessibilityEnabled provides accessibilityEnabled
} else { ) {
SelectionContainer { articleList(articleListModifier) } if (isHeavyArticle) {
articleList(articleListModifier)
} else {
SelectionContainer { articleList(articleListModifier) }
}
} }
} }
is ArticleState.Error -> { is ArticleState.Error -> {