breeze-shell icon indicating copy to clipboard operation
breeze-shell copied to clipboard

0.2.0 起菜单内容全部显示却出现滚动条,滚动时选项轻微位移

Open Z2549 opened this issue 1 month ago • 0 comments

0.2.0 起菜单"内容全部显示却出现滚动条 + 滚动时轻微位移"

环境

  • breeze-shell 0.2.0(9529969)
  • Windows 11,屏幕 2560×1440 @ 125% 缩放
  • 另一台 2160×1440 也复现(甚至一级菜单就有)

现象

  1. 二级菜单内容明明全部显示、底部还有余量,右侧却出现一条几乎满高的滚动条。
  2. 在菜单上滚轮,选项会轻微上下移动(零点几像素的抖动)。
  3. 0.1.34 完全正常,升级到 0.2.0 后才出现。
  4. 与右键位置、菜单展开方向无关,部分二级菜单触发、部分不触发。

实测

在 menu_controller 的 listener 里打印菜单容器的高度:

[diag] n=13 native=9 itemH=23 H=247.1999969482422 maxH=undefined scrolling=undefined

四次调用(top_right @ y=1040、bottom_right @ y=587 / 382 / 596)读到的 H 完全相同, 所以不是 mouse_menu_widget_main::calculate_position() 里 max_height = max(room,1)/dpi 的限高(那个会随锚点变化)。

247.2 × 1.25 = 309,正好是整数设备像素 → height 已是吸附后的值;而未被吸附的原始内容 高度落在 (247.2, 247.6) 之间。

根因

breeze-ui 624696f("按钮标签居中并修掉量测与半像素问题")在 ui::widget::apply_layout_tree() 里新增了整设备像素吸附:

const float scale = owner_rt && owner_rt->dpi_scale > 0 ? owner_rt->dpi_scale : 1.f;
auto align_to_pixel = [scale](float v) {
    return std::round(v * scale) / scale;
};
...
place(width,  YGNodeLayoutGetWidth(node));
place(height, YGNodeLayoutGetHeight(node));   // ← height 被吸附

但同一帧里计算"内容实际高度"的 ui::flex_widget::after_layout() 仍在用未吸附的原始 yoga 值:

for (auto &c : children) {
    ...
    content_bottom = std::max(content_bottom,
        YGNodeLayoutGetTop(c->node) + YGNodeLayoutGetHeight(c->node));
}
actual_height = content_bottom + YGNodeLayoutGetPadding(node, YGEdgeBottom);

而 render_scrollbar() 的判据没有容差:

if (!enable_scrolling || actual_height <= height->dest())
    return;

于是:只要 align_to_pixel 是向下取整(125% 下每 0.8 逻辑 px 就有一次机会), actual_height 就会比 height->dest() 大最多 0.5/dpi(125% 时 0.4px)逻辑像素, 判据成立 → 画出滚动条。

  • 滚动条长度 = h * h / actual_height,配 0.2px 的差 → 几乎满高;
  • max_scroll() = actual_height - height->dest() = 0.2px → 滚轮时内容轻微位移。

两个症状是同一个因。

为什么 0.1.34 没有:旧版 flex_widget::update() 里是 actual_height = height->dest();, 两者恒等,判据永远不成立;且旧版是自写 flex 布局 + std::floor,压根不存在半像素问题。

建议修复(任选一条)

方案 1(推荐,从源头对齐):把 align_to_pixel 提成 widget 的成员函数, apply_layout_tree() 与 after_layout() 共用,让 actual_height 走同一套吸附:

float ui::widget::align_to_pixel(float v) const {
    const float scale = owner_rt && owner_rt->dpi_scale > 0 ? owner_rt->dpi_scale : 1.f;
    return std::round(v * scale) / scale;
}

// flex_widget::after_layout()
actual_height = align_to_pixel(
    content_bottom + YGNodeLayoutGetPadding(node, YGEdgeBottom));

方案 2(改动更小,但要一起改 max_scroll):判据加亚像素容差,否则滚动条不画了、 滚轮仍能滚 0.2px:

const float eps = 0.5f / (owner_rt && owner_rt->dpi_scale > 0 ? owner_rt->dpi_scale : 1.f);

// render_scrollbar()
if (!enable_scrolling || actual_height <= height->dest() + eps)
    return;

// max_scroll()
return std::max(0.f, actual_height - height->dest() - eps);

方案 3(另一条思路):align_to_pixel 改用 std::ceil 而不是 std::round。 吸附只会变大不会变小,天然不可能出现 height < 内容;代价是整体偏移不超过 1 设备像素, 视觉上无差别,而且"向下吸附"更符合"保证内容不被切"的原始意图。

临时规避(给其他受影响用户)

在 config.json 里把滚动条宽度设为 0(menu_widget.cc 直接读这个值), 滚动条不再绘制,剩下 0.2px 的位移肉眼基本无感:

"context_menu": {
  "theme": {
    "scrollbar_width": 0
  }
}

Z2549 avatar Oct 20 '26 04:10 Z2549