Conversation
📝 WalkthroughWalkthrough重构CustomKeyBtn的前景色逻辑,引入可复用的KeyFace()构建器来消除图标/文本渲染的重复代码;同时更新moonlight-common-c子项目的提交版本。 ChangesCustomKeyBtn前景色与渲染逻辑重构
moonlight-common-c子项目版本更新
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
entry/src/main/ets/components/CustomKeyBtn.ets (2)
93-108: ⚡ Quick win
getIconRes()在KeyFace()内被调用两次第 95 行用于条件判断,第 96 行再次用于传给
Image(),导致getButtonIconResource()被重复调用。建议在 builder 内缓存到局部变量,既消除冗余调用,也避免两次调用结果不一致的理论风险。♻️ 建议的重构
`@Builder` KeyFace() { - if (this.getIconRes()) { - Image(this.getIconRes()!) + const iconRes = this.getIconRes(); + if (iconRes) { + Image(iconRes) .width(this.keyDef.fontSize + 6) .height(this.keyDef.fontSize + 6) .fillColor(this.getForegroundColor()) .objectFit(ImageFit.Contain) } else {🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@entry/src/main/ets/components/CustomKeyBtn.ets` around lines 93 - 108, KeyFace() calls getIconRes() twice (once in the if condition and again when constructing Image), which is redundant and could yield inconsistent results; inside the KeyFace() builder, call getIconRes() once and store its result in a local variable (e.g., iconRes) then use that variable in the if check and when constructing Image(), leaving the Text branch unchanged (referencing KeyFace(), getIconRes(), Image(), and Text() to locate the code).
35-37: 考虑添加textColor的空值处理当
isActive为false时,函数直接返回textColor。虽然当前所有代码路径都为其赋予了有效值(默认'#E0FFFFFF'),但若某个地方意外传入空字符串,文字可能无法渲染。建议添加fallback:function resolveCustomKeyForegroundColor(isActive: boolean, textColor: string): ResourceColor { return isActive ? OV_TEXT_WHITE : (textColor || OV_TEXT_WHITE); }或在类型层面用 ResourceColor 约束,确保所有
textColor赋值都经过验证。🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@entry/src/main/ets/components/CustomKeyBtn.ets` around lines 35 - 37, The resolveCustomKeyForegroundColor function currently returns textColor when isActive is false which can propagate empty/null strings and break rendering; update resolveCustomKeyForegroundColor to fallback to a safe color (e.g., OV_TEXT_WHITE) when textColor is falsy, or tighten the parameter type to ResourceColor and validate callers to guarantee non-empty values, ensuring the function (resolveCustomKeyForegroundColor) always returns a valid ResourceColor.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@entry/src/main/ets/components/CustomKeyBtn.ets`:
- Around line 93-108: KeyFace() calls getIconRes() twice (once in the if
condition and again when constructing Image), which is redundant and could yield
inconsistent results; inside the KeyFace() builder, call getIconRes() once and
store its result in a local variable (e.g., iconRes) then use that variable in
the if check and when constructing Image(), leaving the Text branch unchanged
(referencing KeyFace(), getIconRes(), Image(), and Text() to locate the code).
- Around line 35-37: The resolveCustomKeyForegroundColor function currently
returns textColor when isActive is false which can propagate empty/null strings
and break rendering; update resolveCustomKeyForegroundColor to fallback to a
safe color (e.g., OV_TEXT_WHITE) when textColor is falsy, or tighten the
parameter type to ResourceColor and validate callers to guarantee non-empty
values, ensuring the function (resolveCustomKeyForegroundColor) always returns a
valid ResourceColor.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: a7a97779-dbd0-4231-a9ac-c71f8aaa3b58
📒 Files selected for processing (2)
entry/src/main/ets/components/CustomKeyBtn.etsnativelib/src/main/cpp/moonlight-common-c
修复内容
CustomKeyBtn中不可靠的private get fgColor访问器getForegroundColor()读取按下/切换态与keyDef.textColorresolveCustomKeyForegroundColor()纯函数表达前景色选择规则KeyFace()builder,统一普通按钮和六边形按钮的文字/图标渲染,减少重复代码根因
配置保存链路能正确更新
keyDef.textColor,但渲染端在 ArkUI@Component struct中通过private get fgColor再传给 UI modifier,实际表现为未稳定使用用户配置颜色,导致文字颜色回退为随系统深浅色变化。验证
CustomKeyBtn.ets:无编辑器错误hvigor assembleHap --mode module -p product=default --no-daemon:BUILD SUCCESSFUL注意
工作树中已有 native 子目录无关变更未包含在本提交中。
Summary by CodeRabbit
发布说明
重构
其他更新