MooTool Next-Compose 中 UA 解析操作条(P5 密度)与 SelectedFileName 所选文件组件的 DIFF-096 实现解析

原创2026-10-13 19:16:352,274 阅读
文章标签:开发工具桌面应用MCP 服务

MooTool Next-Compose 中 UA 解析操作条(P5 密度)与 SelectedFileName 所选文件组件的 DIFF-096 实现解析

导读

DIFF-096 是 MooTool 新一代 Compose 桌面客户端(next-compose)在向既有 Electron 版本做视觉与交互对齐过程中的一份样式差异记录,聚焦两个细节:UA(User-Agent)解析工具操作条如何以 Compose 的 p5Toolbar 按钮语义承接 Electron 的 .ua-actions 网格,以及新增的 SelectedFileName 组件如何替代加密摘要工具栏里原 Electron .selected-file-name 的展示逻辑。读完本文,你将掌握 Compose 版「工具操作条密度策略」的落地方式(按钮高度、字号、圆角、压缩布局联动),看懂 SelectedFileName 在加密摘要选文件场景下的接线,并能依据仓库中的 UI 捕获测试与领域单测定位对应实现与验收证据。

背景:从 Electron 到 Compose 的样式对齐

MooTool 仓库中并存多条产品线:Electron 版位于 next/(React + TypeScript),Compose 桌面版位于 next-compose/(Kotlin + Compose Multiplatform)。next-compose/docs/diff/ 目录下以 DIFF-xxx 编号记录 Compose 版相对 Electron 版的功能与样式差异,DIFF-096 正是这一系列中的一条:

  • 编号:DIFF-096
  • 影响范围:F12(UA 解析)、F14(加解密/随机)、SelectedFileName 组件
  • 记录日期:2026-09-15

文档以「原行为(Electron)」与「本产品行为(Compose)」两栏对比的方式约束实现,其核心结论可以归纳为下面这张对照表:

关注点 Electron 原行为 Compose 本产品行为
UA 操作条 .ua-actions 预设下拉占 1fr,其余为 panel-command / primary-command,P5 工具按钮约 30px 高 预设按钮 weight(1f),粘贴/清空/解析/结果复制全部使用 p5Toolbar 按钮语义
所选文件 .selected-file-name 11px faint 字色、超长省略,加密摘要工具栏选文件后显示 新增通用 SelectedFileName 组件;摘要 Tab 工具栏改用该组件

这条文档篇幅很短,但它在 next-compose 中对应着真实可读的实现,下面按「UA 操作条」与「SelectedFileName」两条主线逐一展开。

UA 操作条:p5Toolbar 按钮语义的落地

Electron 端原型:.ua-actions 网格

Electron 的 UA 工具在 next/src/features/ua/UaParseTool.tsx 中把输入区下方的操作条渲染为:

<div className="ua-actions">
  <select aria-label={t('ua.preset')} defaultValue="" ...>…</select>
  <button className="panel-command" …>{t('common.action.paste')}</button>
  <button className="panel-command" …>{t('common.action.clear')}</button>
  <button className="primary-command" …>{t('ua.parse')}</button>
</div>

对应的 next/src/shared/styles/global.css 将 .ua-actions 定义为一个四列网格:

.ua-actions { display: grid; grid-template-columns: minmax(0, 1fr) auto auto auto; gap: 8px; }

即预设(下拉)占据第一列的 1fr 弹性宽度,粘贴、清空、解析三个按钮按内容自适应;其中解析按钮使用 primary-command 强调样式。这与 DIFF-096 描述的「预设占 1fr,粘贴/清空/解析为 panel-command/primary-command」完全一致。窄窗(小于 825px)下该网格还会退化为两列,见 global.css。

Compose 端实现:weight(1f) 预设 + 全 p5Toolbar 按钮

Compose 版的 UA 屏幕实现在 next-compose/composeApp/src/desktopMain/kotlin/com/rememberber/mootool/next/compose/features/ua/UaParseScreen.kt,操作条直接放在输入文本框下方的 Row 中:

Row(
    Modifier.fillMaxWidth().mooUaParseBar(),
    horizontalArrangement = Arrangement.spacedBy(8.dp),
    verticalAlignment = Alignment.CenterVertically
) {
    Box(Modifier.weight(1f)) {
        MooButton(container.t("ua.preset"), onClick = { presetOpen = true },
            modifier = Modifier.fillMaxWidth(), p5Toolbar = true)
        MooMenu(expanded = presetOpen, onDismissRequest = { presetOpen = false }) {
            UaEngine.presets.forEach { (name, value) ->
                MooMenuItem(onClick = { session.source = value; session.error = ""; presetOpen = false; refresh() }) {
                    Text(name, fontSize = 13.sp) }
            }
        }
    }
    MooButton(container.t("common.action.paste"), onClick = { … }, p5Toolbar = true)
    MooButton(container.t("common.action.clear"), onClick = { … }, p5Toolbar = true)
    MooButton(container.t("ua.parse"), prominent = true,
        enabled = UaWiringPresentation.parseActionEnabled(session.source),
        onClick = { … }, p5Toolbar = true)
}

几个值得注意的对应关系:

  1. 预设按钮的 weight(1f):Electron 的网格第一列 1fr 被 Compose 用 Box(Modifier.weight(1f)) 等价承接——预设按钮始终占满剩余行宽,与「粘贴/清空/解析」三按钮在同一行靠右排列。预设内容通过 MooMenu 弹出,菜单项遍历 UaEngine.presets。
  2. 全部操作按钮统一 p5Toolbar = true:DIFF-096 明确定义「粘贴/清空/解析/结果复制均 p5Toolbar」,因此不只是操作条上三个按钮,结果面板右上角的「复制结果」按钮同样带 p5Toolbar = true(见 UaParseScreen.kt)。
  3. mooUaParseBar() 修饰器:操作条行还套了 mooUaParseBar(),在 Controls.kt 中实现为 heightIn(min = 46.dp),保证 P5 工具壳内操作条的最小高度。

p5Toolbar 的密度语义:尺寸与字体的策略化计算

p5Toolbar 并不是简单地把按钮变小,而是一套可配置的「P5 工具工具栏按钮」密度策略。其尺寸逻辑集中在 next-compose/composeApp/src/desktopMain/kotlin/com/rememberber/mootool/next/compose/ui/workbench/LayoutPolicy.kt,对应注释明确对齐 Electron 的 .tool-page .toolbar-button(--tool-control-height 34px、modern 风格 13px/500):

/** DIFF-496 follow-up: tool `p5Toolbar` buttons shrink when layout compact navigation is on. */
fun p5ToolbarDense(compactNavigation: Boolean): Boolean = compactNavigation

/** Electron `.tool-page .toolbar-button` uses `--tool-control-height` (34px) and modern `font-size: 13px` /
 *  `font-weight: 500`. Compact navigation keeps DIFF-497 dense 26px / 11sp. */
fun p5ToolbarButtonHeightDp(dense: Boolean, controlHeightDp: Float): Float =
    if (dense) 26f else controlHeightDp

fun p5ToolbarFontSp(dense: Boolean, interfaceStyle: String): Float = when {
    dense -> 11f
    interfaceStyle == "modern" || interfaceStyle == "quiet" -> 13f
    else -> 12f
}

fun p5ToolbarFontWeightMedium(dense: Boolean, interfaceStyle: String): Boolean =
    dense || interfaceStyle == "modern" || interfaceStyle == "quiet"

MooButton 在 Controls.kt 中消费这些策略:

  • 圆角固定为 RoundedCornerShape(6.dp)(p5Toolbar 分支,L110);
  • 高度取 LayoutPolicy.p5ToolbarButtonHeightDp(...):默认即工具 controlHeight(对照 Electron 34px),开启紧凑导航(DIFF-496/497 的 dense 模式)时收缩到 26dp;
  • 内边距为水平 9dp、垂直 4dp;
  • 字号经 p5ToolbarFontSp 计算:常规主题 12sp,modern/quiet 主题 13sp,dense 时 11sp;
  • 字重经 p5ToolbarFontWeightMedium:dense 或 modern/quiet 主题使用 FontWeight.Medium。

因此 DIFF-096 里「P5 工具约 30px 高」这一表述在 Compose 端实际展开为:默认取 MooTheme.dimens.controlHeight(约 30px 量级),紧凑布局下缩至 26dp,且字号/字重随界面风格(interfaceStyle)与是否紧凑导航联动。这套策略由 DIFF-096 与 DIFF-496、DIFF-497、DIFF-501 等系列文档共同演进而来。

UA 预设与解析状态门控

操作条背后还挂着两层领域逻辑:

SelectedFileName:通用「所选文件名」组件

组件定义:11sp 弱化字色 + 省略号

DIFF-096 的第二项产物是新增通用组件 SelectedFileName,定义在 Controls.kt,代码注释直接标注了它的 Electron 原型:

/** Electron `.selected-file-name`:有文件名时 11sp faint/muted 省略。 */
@Composable
fun SelectedFileName(
    fileName: String,
    modifier: Modifier = Modifier
) {
    if (fileName.isEmpty()) return
    val colors = MooTheme.colors
    Text(
        text = fileName,
        color = colors.textMuted,
        fontSize = 11.sp,
        maxLines = 1,
        overflow = TextOverflow.Ellipsis,
        modifier = modifier
    )
}

行为要点:

  • 空文件名不渲染:if (fileName.isEmpty()) return,因此组件天然可「等用户选文件后再出现」,与 Electron 端「加密摘要工具栏在选文件后显示」的行为一致;
  • 字色 textMuted:对应 Electron .selected-file-name 的 var(--text-faint) 弱化样式(见 next/src/shared/styles/global.css);
  • 11sp + 单行 + Ellipsis:长文件名在工具栏剩余空间内省略展示,避免撑破工具壳。

同文件中还有一个近亲组件 FileDropRow(Controls.kt,注释为 Electron .file-drop-row),它把「选择按钮 + 11sp muted 省略文件名」封装成一行,可选支持系统文件拖放——DIFF-096 的「未做」清单里也明确提到 file-drop 拖放尚未接入 UA,这也是后续 DIFF-105/106 等文档继续演进的领域。

在加密摘要 Tab 中的接线

SelectedFileName 目前的主要消费方是加密/摘要工具的「文件摘要」行。在 CryptoScreen.kt 的 DigestPanel 中:

Row(
    Modifier.fillMaxWidth().desktopFileDropTarget { files ->
        digestPickedFile(container, session, scope, files.first(), onChanged)
    },
    horizontalArrangement = Arrangement.spacedBy(6.dp),
    verticalAlignment = Alignment.CenterVertically
) {
    Text(container.t("crypto.algorithm"), …)
    CompactChoice(current = session.digestAlgorithm, options = DigestAlgorithm.entries, …)
    MooButton(container.t("crypto.textDigest"), prominent = true, p5Toolbar = true, …)
    MooButton(container.t("crypto.fileDigest"), p5Toolbar = true, onClick = {
        val file = chooseFileWithExportDirectory(container, save = false, …) ?: return@MooButton
        digestPickedFile(container, session, scope, file, onChanged)
    })
    SelectedFileName(session.digestFileName, Modifier.weight(1f, fill = false))
}

这段代码完整呈现了 DIFF-096 所述「摘要 Tab 工具栏改用该组件」:

  • 摘要工具栏这一行的算法选择、文本摘要、文件摘要按钮均使用 p5Toolbar = true 密度;
  • 选择文件后,digestPickedFile(...) 会写入 session.digestFileName,随后由 SelectedFileName(session.digestFileName, Modifier.weight(1f, fill = false)) 在行尾以弱化、省略的方式展示所选文件;文件名会随文件摘要结果一起写入历史记录,与 Electron 端 hashFile() 里 setDigestFileName(result.name) 的行为对应(见 next/src/features/crypto/CryptoTool.tsx)。

会话持久化佐证

所选文件名还进入会话/设置持久化:LegacyJavaSettingsTest 中有一条断言 assertEquals("sample.txt", crypto.digestFileName)(next-compose/composeApp/src/desktopTest/kotlin/com/rememberber/mootool/next/compose/domain/LegacyJavaSettingsTest.kt),说明 digestFileName 作为加密工具会话字段会被持久化保存。

验收证据与测试定位

DIFF-096 的「证据」一节指出 desktopTest 见 next-compose/docs/acceptance.md。在该验收清单中,F12(UA)与 F14(加解密)两行分别记录了本差异的归属:F12 行明确写到「操作条 p5Toolbar 见 DIFF-096」(acceptance.md),F14 行则记录了摘要/加密相关样式的演进序列(acceptance.md)。

领域层的单测可以直接复现 DIFF-096 关联的按钮状态门控:UaWiringPresentationTest.kt 断言:

  • 空源码 parseActionEnabled("") == false,非空如 "Mozilla/5.0" 为 true;
  • 空结果 copyResultActionEnabled("") == false,非空 JSON 为 true;
  • runParse("Mozilla/5.0 (Windows NT 10.0; Win64; x64)") 返回 ParseOutcome.Success。

UI 层的「截图捕获测试」(如 CryptoGenerateKeyTabCaptureTest、CalculatorEvaluateTabCaptureTest 等)在 next-compose/composeApp/src/desktopTest/kotlin/com/rememberber/mootool/next/compose/features/ 下以 p5Toolbar = true 构造 MooButton 并渲染到面板,用于核对最终视觉密度。

未做项:明确的边界清单

DIFF-096 最后列出本差异不覆盖的边界,这些仍是 UA 工具后续演进(如 DIFF-105/106 的文件拖放、DIFF-497 的紧凑导航等)的输入:

  • UA 预设改原生 select 栅格:Compose 版以 MooButton + MooMenu 弹层实现预设,而非 Electron 的原生 <select> 栅格;
  • file-drop 拖放:UA 输入区尚未接入系统文件拖放(FileDropRow 的可选 desktopFileDropTarget 尚未用于 UA);
  • 系统 IME:系统输入法在 UA 场景下的行为未在本差异中测试;
  • 产品窗 Tab 帧、三平台安装(Windows/macOS/Linux)仍未在本差异范围内验证。

小结

DIFF-096 是一条「小而精」的样式差异记录,但它串起了三层实现:Electron 原型(.ua-actions 网格、.selected-file-name)、Compose 通用组件(MooButton.p5Toolbar 与 SelectedFileName)、以及策略层密度计算(LayoutPolicy.p5Toolbar*)。对开发者而言,改动或验证 UA/加密摘要工具栏时,只需沿 UaParseScreen.kt → Controls.kt → LayoutPolicy.kt 这条链路即可定位全部逻辑,并以 UaWiringPresentationTest.kt 与 acceptance.md 作为回归验收入口。

登录后查看全文
MooTool