feat: custom template actionToast (#1222)

This commit is contained in:
二刺螈
2026-03-10 23:50:02 +08:00
parent 47b308bc22
commit 3978cdb1e6
5 changed files with 46 additions and 16 deletions

View File

@@ -391,7 +391,7 @@ class A11yRuleEngine(val service: A11yCommonImpl) {
startQueryJob()
}
if (actionResult.action != ActionPerformer.None.action) {
showActionToast()
showActionToast(rule)
}
addActionLog(rule, topActivity, target, actionResult)
}

View File

@@ -7,7 +7,6 @@ import android.util.LruCache
import android.view.accessibility.AccessibilityNodeInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.updateAndGet
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import li.songe.gkd.META
@@ -134,7 +133,7 @@ val activityRuleFlow = MutableStateFlow(ActivityRule())
private var lastAppId = ""
sealed class ActivityScene() {
sealed class ActivityScene {
data object ScreenOn : ActivityScene()
data object A11y : ActivityScene()
data object TaskStack : ActivityScene()
@@ -291,7 +290,6 @@ fun addActionLog(
) = appScope.launchTry(Dispatchers.IO) {
val ctime = System.currentTimeMillis()
actionLogMutex.withLock {
val actionCount = actionCountFlow.updateAndGet { it + 1 }
val actionLog = ActionLog(
appId = topActivity.appId,
activityId = topActivity.activityId,
@@ -304,7 +302,7 @@ fun addActionLog(
ctime = ctime,
)
DbSet.actionLogDao.insert(actionLog)
if (actionCount % 100 == 0L) {
if (actionCountFlow.value % 100 == 0L) {
DbSet.actionLogDao.deleteKeepLatest()
}
}

View File

@@ -4,9 +4,11 @@ import android.view.accessibility.AccessibilityNodeInfo
import kotlinx.atomicfu.atomic
import kotlinx.atomicfu.update
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.updateAndGet
import li.songe.gkd.a11y.appChangeTime
import li.songe.gkd.a11y.lastTriggerRule
import li.songe.gkd.a11y.lastTriggerTime
import li.songe.gkd.store.actionCountFlow
import li.songe.selector.MatchOption
import li.songe.selector.Selector
@@ -79,9 +81,9 @@ sealed class ResolvedRule(
val selfGroupRules = field[group] ?: emptyList()
val othersGroupRules =
(group.scopeKeys ?: emptyList()).distinct().filter { k -> k != group.key }
.map { k ->
.flatMap { k ->
field.entries.find { e -> e.key.key == k }?.value ?: emptyList()
}.flatten()
}
val groupRules = selfGroupRules + othersGroupRules
// 共享次数
@@ -131,6 +133,7 @@ sealed class ResolvedRule(
actionCount.incrementAndGet()
lastTriggerTime = t
lastTriggerRule = this
actionCountFlow.updateAndGet { it + 1 }
}
private var actionCount = atomic(0)
@@ -234,7 +237,7 @@ fun getFixActivityIds(
appId: String,
activityIds: List<String>?,
): List<String> {
if (activityIds == null || activityIds.isEmpty()) return emptyList()
if (activityIds.isNullOrEmpty()) return emptyList()
return activityIds.map { activityId ->
if (activityId.startsWith('.')) { // .a.b.c -> com.x.y.x.a.b.c
appId + activityId

View File

@@ -108,10 +108,36 @@ fun useSettingsPage(): ScaffoldExt {
var value by remember {
mutableStateOf(store.actionToast)
}
val maxCharLen = 32
val maxCharLen = 64
AlertDialog(
properties = DialogProperties(dismissOnClickOutside = false),
title = { Text(text = "触发提示") },
title = {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier.fillMaxWidth(),
) {
Text(text = "触发提示")
PerfIconButton(
imageVector = PerfIcon.HelpOutline,
contentDescription = "文案规则",
onClickLabel = "打开文案规则弹窗",
onClick = throttle {
showToastInputDlg = false
val confirmAction = {
mainVm.dialogFlow.value = null
showToastInputDlg = true
}
mainVm.dialogFlow.updateDialogOptions(
title = "文案规则",
text = $$"触发文案支持变量替换,规则如下\n${1} 子规则名称\n${2} 规则组名称\n${3} 触发次数\n\n示例模板\n${1}/${2}/${3}\n\n替换结果\n子规则a/规则组A/3",
confirmAction = confirmAction,
onDismissRequest = confirmAction,
)
},
)
}
},
text = {
OutlinedTextField(
value = value,
@@ -121,7 +147,6 @@ fun useSettingsPage(): ScaffoldExt {
onValueChange = {
value = it.take(maxCharLen)
},
singleLine = true,
supportingText = {
Text(
text = "${value.length} / $maxCharLen",

View File

@@ -22,10 +22,12 @@ import androidx.core.graphics.toColorInt
import com.hjq.toast.Toaster
import com.hjq.toast.style.WhiteToastStyle
import li.songe.gkd.app
import li.songe.gkd.data.ResolvedRule
import li.songe.gkd.isActivityVisible
import li.songe.gkd.permission.canDrawOverlaysState
import li.songe.gkd.service.A11yService
import li.songe.gkd.service.OverlayWindowService
import li.songe.gkd.store.actionCountFlow
import li.songe.gkd.store.storeFlow
import li.songe.loc.Loc
@@ -118,18 +120,20 @@ private fun setReactiveToastStyle() {
private var triggerTime = 0L
private const val triggerInterval = 2000L
fun showActionToast() {
fun showActionToast(rule: ResolvedRule) {
if (!storeFlow.value.toastWhenClick) return
runMainPost {
val t = System.currentTimeMillis()
if (t - triggerTime > triggerInterval + 100) { // 100ms 保证二次显示的时候上一次已经完全消失
triggerTime = t
val text = storeFlow.value.actionToast
.replace($$"${1}", rule.rule.name.toString())
.replace($$"${2}", rule.g.group.name)
.replace($$"${3}", actionCountFlow.value.toString())
if (storeFlow.value.useSystemToast) {
showSystemToast(storeFlow.value.actionToast)
showSystemToast(text)
} else {
showA11yToast(
storeFlow.value.actionToast
)
showA11yToast(text)
}
}
}