docs: 逐模块细化中文逻辑注释并覆盖测试与示例
This commit is contained in:
@@ -13,8 +13,10 @@ use crate::{
|
||||
tabs::Tab,
|
||||
};
|
||||
|
||||
// 展开侧栏固定逻辑宽度;实际设备像素比例由后续显示列表缩放处理。
|
||||
pub const WIDTH: f32 = 256.0;
|
||||
pub const HEADER: f32 = 40.0;
|
||||
// macOS 标题行预留原生三键安全区域,其他平台无需这段横向留白。
|
||||
pub const CONTROLS: f32 = if cfg!(target_os = "macos") { 86.0 } else { 0.0 };
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
@@ -32,6 +34,7 @@ pub struct State {
|
||||
pub open: bool,
|
||||
pub interaction: Interaction,
|
||||
pub targets: Vec<Target<Action>>,
|
||||
// 只滚动普通页面列表,顶部工具入口、折叠按钮和底部工作区标签保持固定。
|
||||
pub scroll: f32,
|
||||
}
|
||||
|
||||
@@ -55,6 +58,8 @@ impl State {
|
||||
|
||||
/// 判断指针是否落入宿主侧栏/展开按钮区域,保护页面输入边界。
|
||||
pub fn covers(&self, point: Point) -> bool {
|
||||
// 展开时整个侧栏横向带均拦截页面输入,y 的窗口有效范围由外层事件入口保证。
|
||||
// 折叠时只覆盖顶部三键安全区与展开按钮,不继续挡住左侧整列页面。
|
||||
if self.open {
|
||||
point.x >= 0.0 && point.x < WIDTH
|
||||
} else {
|
||||
@@ -69,10 +74,12 @@ impl State {
|
||||
height: f32,
|
||||
accent: crate::display_list::Color,
|
||||
) -> DisplayList {
|
||||
// 每帧重新生成控件目标与显示列表,保留 Interaction 的稳定身份状态,而不是复用上帧屏幕矩形。
|
||||
let mut ui = Ui::new(Theme::new(accent), self.interaction.clone());
|
||||
if self.open {
|
||||
ui.fill(Rect::new(0.0, 0.0, WIDTH, height), theme::BAR);
|
||||
}
|
||||
// 展开时按钮靠侧栏右边,折叠时靠原生按钮安全区右侧,给页面释放左边宽度。
|
||||
let toggle_x = if self.open {
|
||||
WIDTH - 40.0
|
||||
} else {
|
||||
@@ -107,6 +114,7 @@ impl State {
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
{
|
||||
// 工具入口的选中状态来自已发布标签,而非仅凭最后点击动作推断,避免请求失败后显示错误选中。
|
||||
let selected = tabs
|
||||
.iter()
|
||||
.any(|tab| tab.active && tab.developer_tool.as_deref() == Some(name));
|
||||
@@ -118,10 +126,12 @@ impl State {
|
||||
}
|
||||
ui.label(20.0, 303.0, "页面", theme::MUTED, false);
|
||||
let area = Rect::new(8.0, 333.0, WIDTH - 16.0, (height - 383.0).max(0.0));
|
||||
// 开发工具已由固定入口展示,因此页面列表仅收集普通页,保持原注册表顺序。
|
||||
let pages: Vec<_> = tabs
|
||||
.iter()
|
||||
.filter(|tab| tab.developer_tool.is_none())
|
||||
.collect();
|
||||
// 页面关闭或窗口增高后重新钳制旧偏移,内容不足视口时回零,防止列表留在空白区域。
|
||||
self.scroll = self
|
||||
.scroll
|
||||
.clamp(0.0, (pages.len() as f32 * 36.0 - area.height).max(0.0));
|
||||
@@ -129,11 +139,13 @@ impl State {
|
||||
for (index, tab) in pages.into_iter().enumerate() {
|
||||
let y = area.y + index as f32 * 36.0 - self.scroll;
|
||||
// Register only fully visible rows: paint clipping must match input.
|
||||
// 当前实现完全跳过部分可见行,绘制与注册都不发生;Clip 仍保护其他原语不溢出列表区域。
|
||||
if y < area.y || y + 32.0 > area.y + area.height {
|
||||
continue;
|
||||
}
|
||||
ui.button(
|
||||
Rect::new(12.0, y, WIDTH - 56.0, 32.0),
|
||||
// 切换和关闭动作携带稳定页面 ID,滚动或标题变化不会改变动作身份。
|
||||
Action::Activate(tab.id),
|
||||
Button::new(&tab.title).selected(tab.active),
|
||||
);
|
||||
@@ -146,6 +158,7 @@ impl State {
|
||||
ui.list.push(DrawCommand::PopClip);
|
||||
ui.label(20.0, height - 32.0, "本地工作区", theme::MUTED, false);
|
||||
}
|
||||
// 重建目标后校正跨帧捕获/焦点;折叠会移除旧页面和工具目标,不能在释放时激活已隐藏控件。
|
||||
self.targets = ui.targets;
|
||||
self.interaction.reconcile(&self.targets);
|
||||
ui.list
|
||||
@@ -162,6 +175,7 @@ mod tests {
|
||||
assert!(sidebar.targets.len() > 1);
|
||||
sidebar.open = false;
|
||||
sidebar.draw(&[], 800.0, theme::PALETTE[0]);
|
||||
// 折叠后仅保留 Toggle,且页面不再让出左宽;顶部按钮区域之外的指针不被侧栏拦截。
|
||||
assert_eq!(sidebar.targets.len(), 1);
|
||||
assert_eq!(sidebar.targets[0].action, Some(Action::Toggle));
|
||||
assert_eq!(sidebar.left(), 0.0);
|
||||
|
||||
Reference in New Issue
Block a user