Skip to main content

velox_engine/element/
div.rs

1//! 弹性盒容器构造器 `Div<'a>` 与链式样式、交互及动画配置 API。
2
3use core::cell::Cell;
4use core::time::Duration;
5
6use crate::pod::{ActionScope, CursorShape, DragEvent, FrameScope};
7use crate::style::{AlignItems, Color, Easing, Edges, LoopMode, Style, StyleRefinement, Styled};
8
9use super::div_node::{
10    duration_to_ms, pack_visual_flags, DivInteractivity, DivNode, DivRareHandlers, DivStateStyles,
11    DivVisual, EnterFrom, TransitionSpec, FOCUS_BORDER_NONE, NO_FOCUS_BORDER_DIV_VISUAL,
12};
13use super::{Element, ElementNode, IntoElement};
14
15impl<'a> IntoElement<'a> for Div<'a> {
16    #[inline(always)]
17    fn into_element(self, cx: &'a FrameScope) -> Element<'a> {
18        let has_state_styles = self.text_color.is_some()
19            || self.text_size.is_some()
20            || self.hover_style.is_some()
21            || self.active_style.is_some()
22            || self.focus_style.is_some();
23
24        let alloc_visual = |state_styles: Option<&'a DivStateStyles<'a>>,
25                            transition: Option<&'a TransitionSpec>|
26         -> &'a DivVisual<'a> {
27            let (pos_x, pos_y) = self.pos.unwrap_or((0, 0));
28            let (bg_grad_to, bg_grad_horiz) = self.bg_grad.unwrap_or((Color::TRANSPARENT, false));
29            let (focus_border_w, focus_border_color) = match self.focus_border {
30                Some((w, c)) => (w.min(254), c),
31                None => (FOCUS_BORDER_NONE, Color::TRANSPARENT),
32            };
33            let (shadow_blur, shadow_ox, shadow_oy, shadow_color) =
34                self.shadow.unwrap_or((0, 0, 0, Color::TRANSPARENT));
35            let extra_flags = pack_visual_flags(
36                self.pos.is_some(),
37                self.scrollable_max.is_some(),
38                self.bg_grad.is_some(),
39                bg_grad_horiz,
40                self.items_baseline,
41                self.align_self,
42            );
43            &*cx.arena().alloc_temp(DivVisual {
44                state_styles,
45                transition,
46                margin: self.margin,
47                extra_padding: self.extra_padding,
48                bg_grad_to,
49                focus_border_color,
50                shadow_color,
51                min_w: self.min_w,
52                max_w: self.max_w,
53                min_h: self.min_h,
54                max_h: self.max_h,
55                pos_x,
56                pos_y,
57                raw_scrollable_max: self.scrollable_max.unwrap_or(0),
58                extra_flags,
59                focus_border_w,
60                shadow_blur,
61                shadow_ox,
62                shadow_oy,
63                cursor: self.cursor,
64            })
65        };
66
67        let visual = if self.has_non_focus_visual_extra() {
68            let state_styles = if has_state_styles {
69                let hover_style = self.hover_style.map(|r| &*cx.arena().alloc_temp(r));
70                let active_style = self.active_style.map(|r| &*cx.arena().alloc_temp(r));
71                let focus_style = self.focus_style.map(|r| &*cx.arena().alloc_temp(r));
72                Some(&*cx.arena().alloc_temp(DivStateStyles {
73                    hover_style,
74                    active_style,
75                    focus_style,
76                    text_color: self.text_color.unwrap_or(Color::TRANSPARENT),
77                    text_size: self.text_size.unwrap_or(0),
78                    flags: if self.text_color.is_some() { 1 } else { 0 },
79                }))
80            } else {
81                None
82            };
83            let transition = self.transition.map(|t| &*cx.arena().alloc_temp(t));
84            Some(alloc_visual(state_styles, transition))
85        } else if let Some((fw, fc)) = self.focus_border {
86            if fw == 0 && fc == Color::TRANSPARENT {
87                Some(&NO_FOCUS_BORDER_DIV_VISUAL)
88            } else {
89                Some(alloc_visual(None, None))
90            }
91        } else {
92            None
93        };
94
95        let interact = if self.has_interactivity() {
96            let rare = if self.has_rare_handlers() {
97                Some(&*cx.arena().alloc(DivRareHandlers {
98                    on_long_press: self.on_long_press,
99                    on_drag: self.on_drag,
100                    on_swipe: self.on_swipe,
101                    on_key_input: self.on_key_input,
102                    on_key_nav: self.on_key_nav,
103                    on_navigate_back: self.on_navigate_back,
104                    on_secondary_click: self.on_secondary_click,
105                }))
106            } else {
107                None
108            };
109            Some(&*cx.arena().alloc(DivInteractivity {
110                on_click: self.on_click,
111                rare,
112            }))
113        } else {
114            None
115        };
116
117        let mut flags = 0u8;
118        if self.focusable {
119            flags |= 1 << 0;
120        }
121        if self.is_overlay {
122            flags |= 1 << 1;
123        }
124        if self.occlude_hit {
125            flags |= 1 << 2;
126        }
127        if self.auto_repeat {
128            flags |= 1 << 3;
129        }
130        if self.clip_children {
131            flags |= 1 << 4;
132        }
133        if self.id.is_some() {
134            flags |= 1 << 5;
135        }
136        if self.active_bg.is_some() {
137            flags |= 1 << 6;
138        }
139        if self.hover_bg.is_some() {
140            flags |= 1 << 7;
141        }
142
143        Element::Div(DivNode {
144            style: self.style,
145            first_child: self.first_child,
146            visual,
147            interact,
148            raw_id: self.id.unwrap_or(0),
149            raw_active_bg: self.active_bg.unwrap_or(Color::TRANSPARENT),
150            raw_hover_bg: self.hover_bg.unwrap_or(Color::TRANSPARENT),
151            opacity: self.opacity,
152            z_index: self.z_index,
153            grid_cols: self.grid_cols,
154            flags,
155        })
156    }
157}
158
159
160/// 基础弹性盒容器原语(完全复刻 HTML/Tailwind 的 `div` 直觉)。
161pub struct Div<'a> {
162    /// 跨帧唯一 ID(驱动滚动位置记忆与 `.animate_xxx()` 过渡动画)。
163    pub id: Option<u32>,
164    /// 紧凑位域样式(严格 24 Bytes)。
165    pub style: Style,
166    /// 可选的四边独立外边距(配合 `style.margin_px()` 使用)。
167    pub margin: Edges<i16>,
168    /// 可选的大尺寸安全区或内边距扩展(像素,与 `style.padding` 叠加,避免移动端软键盘 > 127px 截断)。
169    pub extra_padding: Edges<i16>,
170    /// 最小宽度约束(0 表示不限)。
171    pub min_w: i16,
172    /// 最大宽度约束(0 表示不限)。
173    pub max_w: i16,
174    /// 最小高度约束(0 表示不限)。
175    pub min_h: i16,
176    /// 最大高度约束(0 表示不限)。
177    pub max_h: i16,
178    /// 容器与子树整体不透明度(`0..=255`,默认 `255`)。
179    pub opacity: u8,
180    /// 按下态(Active)背景色覆盖。
181    pub active_bg: Option<Color>,
182    /// 悬停态(Hover)背景色覆盖。
183    pub hover_bg: Option<Color>,
184    /// 线性渐变终点颜色与方向 `(to_color, is_horizontal)`。
185    pub bg_grad: Option<(Color, bool)>,
186    /// 键盘/旋钮聚焦态(Focus)边框 `(width, color)` 覆盖。
187    pub focus_border: Option<(u8, Color)>,
188    /// 是否可纳入物理按键/编码器 `Tab`/`Up`/`Down` 焦点链。
189    pub focusable: bool,
190    /// 是否将该子树提升至顶层 Z-Order 延迟渲染队列(用于 `Modal` / `Dropdown` / `Tooltip` 越过父裁剪框)。
191    pub is_overlay: bool,
192    /// 是否拦截并吞噬落在该容器内的指针命中事件(防止点击穿透 `Modal` 遮罩层)。
193    pub occlude_hit: bool,
194    /// 按住不放时是否自动连发 `on_click`(用于 `+/-` 连续调节按钮)。
195    pub auto_repeat: bool,
196    /// 可触摸拖拽滚动的最大垂直偏移量(`Some(max_px)` 表示启用触摸惯性滚动容器)。
197    pub scrollable_max: Option<i16>,
198    /// 可选的相对于父容器内容区左上角的叠层偏移坐标 `(x, y)`(用于表盘/徽标/浮层叠放)。
199    pub pos: Option<(i16, i16)>,
200    /// 第一个子节点引用(侵入式单向链表头)。
201    pub first_child: Option<&'a ElementNode<'a>>,
202    /// 最后一个子节点引用(用于 $O(1)$ 安全尾插,消除裸指针 UB)。
203    pub last_child: Option<&'a ElementNode<'a>>,
204    /// 点击事件响应闭包(分配于 `ScratchArena`)。
205    pub on_click: Option<&'a dyn Fn(&mut ActionScope)>,
206    /// 长按事件响应闭包(按住超过 `500ms` 触发,分配于 `ScratchArena`)。
207    pub on_long_press: Option<&'a dyn Fn(&mut ActionScope)>,
208    /// 拖拽事件响应闭包(分配于 `ScratchArena`)。
209    pub on_drag: Option<&'a dyn Fn(&mut ActionScope)>,
210    /// 四向快速滑动手势回调闭包(分配于 `ScratchArena`)。
211    pub on_swipe: Option<&'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope)>,
212    /// 字符与文本输入事件响应闭包(分配于 `ScratchArena`,供聚焦态 `TextInput` 编辑使用)。
213    pub on_key_input: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
214    /// 控件局部导航键(方向键、`Home`/`End`、`Enter`/`Space`)响应闭包(不抢占 `Tab`/`BackTab` 焦点切换,亦不触发文本 IME)。
215    pub on_key_nav: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
216    /// 系统返回导航请求回调闭包(Android Back / 浏览器后退 / 左边缘返回手势 / 桌面 `Escape` 关闭浮层)。
217    pub on_navigate_back: Option<&'a dyn Fn(&mut ActionScope)>,
218    /// 鼠标右键(次键)点击回调闭包(分配于 `ScratchArena`)。
219    pub on_secondary_click: Option<&'a dyn Fn(&mut ActionScope)>,
220    /// 显式層级排序优先级(默认 `0`;用于控制同层或浮层间的绘制先后顺序)。
221    pub z_index: i8,
222    /// 解析式 SDF 软阴影 `(blur_radius, offset_x, offset_y, color)`(无需离屏模糊缓冲区)。
223    pub shadow: Option<(u8, i8, i8, Color)>,
224    /// 二维网格布局列数(`0` 表示常规 Flexbox,`>= 1` 表示按指定列数自动排列的二维 Grid)。
225    pub grid_cols: u8,
226    /// 声明式过渡动画配置。
227    pub transition: Option<TransitionSpec>,
228    /// 是否裁剪超出容器边界的子节点(如滚动列表视口)。
229    pub clip_children: bool,
230    /// 子节点独立交叉轴对齐方式覆盖(`align-self`)。
231    pub align_self: Option<AlignItems>,
232    /// 显式指定的鼠标光标形状(`None` 时按节点交互能力自动推断 `Pointer` / `Text` / `Grab` / `Default`)。
233    pub cursor: Option<CursorShape>,
234    /// 容器级可继承文本颜色(未显式设 `.color(...)` 的子 `Text` 自动继承)。
235    pub text_color: Option<Color>,
236    /// 容器级可继承字号(未显式设 `.size(...)` 的子 `Text` 自动继承)。
237    pub text_size: Option<u8>,
238    /// 悬停态样式覆写增量(由 `.hover(|s| ...)` 写入,支持背景/边框/文本色/不透明度/圆角)。
239    pub hover_style: Option<StyleRefinement>,
240    /// 按下态样式覆写增量(由 `.active(|s| ...)` 写入)。
241    pub active_style: Option<StyleRefinement>,
242    /// 聚焦态样式覆写增量(由 `.focus(|s| ...)` 写入)。
243    pub focus_style: Option<StyleRefinement>,
244    /// 行容器内是否按子元素文本基线对齐(`items-baseline`)。
245    pub items_baseline: bool,
246}
247
248/// 构造一个基础弹性盒容器(完全复刻 HTML/Tailwind 的 `div` 直觉)。
249#[inline(always)]
250pub fn div<'a>() -> Div<'a> {
251    Div {
252        id: None,
253        style: Style::default(),
254        margin: Edges::all(0),
255        extra_padding: Edges::all(0),
256        min_w: 0,
257        max_w: 0,
258        min_h: 0,
259        max_h: 0,
260        opacity: 255,
261        active_bg: None,
262        hover_bg: None,
263        bg_grad: None,
264        focus_border: None,
265        focusable: false,
266        is_overlay: false,
267        occlude_hit: false,
268        auto_repeat: false,
269        scrollable_max: None,
270        pos: None,
271        first_child: None,
272        last_child: None,
273        on_click: None,
274        on_long_press: None,
275        on_drag: None,
276        on_swipe: None,
277        on_key_input: None,
278        on_key_nav: None,
279        on_navigate_back: None,
280        on_secondary_click: None,
281        z_index: 0,
282        shadow: None,
283        grid_cols: 0,
284        transition: None,
285        clip_children: false,
286        align_self: None,
287        cursor: None,
288        text_color: None,
289        text_size: None,
290        hover_style: None,
291        active_style: None,
292        focus_style: None,
293        items_baseline: false,
294    }
295}
296
297impl<'a> Styled for Div<'a> {
298    #[inline(always)]
299    fn style_mut(&mut self) -> &mut Style {
300        &mut self.style
301    }
302}
303
304impl<'a> Div<'a> {
305    #[inline(always)]
306    fn has_non_focus_visual_extra(&self) -> bool {
307        self.margin != Edges::all(0)
308            || self.extra_padding != Edges::all(0)
309            || self.min_w != 0
310            || self.max_w != 0
311            || self.min_h != 0
312            || self.max_h != 0
313            || self.bg_grad.is_some()
314            || self.scrollable_max.is_some()
315            || self.pos.is_some()
316            || self.shadow.is_some()
317            || self.transition.is_some()
318            || self.align_self.is_some()
319            || self.cursor.is_some()
320            || self.text_color.is_some()
321            || self.text_size.is_some()
322            || self.hover_style.is_some()
323            || self.active_style.is_some()
324            || self.focus_style.is_some()
325            || self.items_baseline
326    }
327
328    #[inline(always)]
329    fn has_interactivity(&self) -> bool {
330        self.on_click.is_some() || self.has_rare_handlers()
331    }
332
333    #[inline(always)]
334    fn has_rare_handlers(&self) -> bool {
335        self.on_long_press.is_some()
336            || self.on_drag.is_some()
337            || self.on_swipe.is_some()
338            || self.on_key_input.is_some()
339            || self.on_key_nav.is_some()
340            || self.on_navigate_back.is_some()
341            || self.on_secondary_click.is_some()
342    }
343
344    /// 设置元素的跨帧唯一标识符(用于过渡动画与滚动位置记忆)。
345    #[inline(always)]
346    pub fn id(mut self, id: u32) -> Self {
347        self.id = Some(id);
348        self
349    }
350
351    /// 基于当前 [`FrameScope`] 作用域 ID 栈派生全局唯一的元素标识符(避免复用组件时 ID 撞车)。
352    #[inline(always)]
353    pub fn scoped_id(mut self, cx: &FrameScope, local_id: u32) -> Self {
354        self.id = Some(cx.scoped_id(local_id));
355        self
356    }
357
358    /// 设置相对于父容器内容区左上角的叠层偏移坐标 `(x, y)`(脱离常规线性流,用于叠层拼装)。
359    #[inline(always)]
360    pub fn pos(mut self, x: i16, y: i16) -> Self {
361        self.pos = Some((x, y));
362        self
363    }
364
365    /// 自动叠加移动端/异形屏安全区域内边距(状态栏、刘海屏、底部 Home Indicator 与软键盘高度,支持 >127px 软键盘不截断)。
366    #[inline(always)]
367    pub fn safe_area(mut self, cx: &FrameScope) -> Self {
368        let sa = cx.safe_area();
369        let kb = cx.keyboard_inset();
370        self.extra_padding.top = self.extra_padding.top.saturating_add(sa.top.max(0));
371        self.extra_padding.right = self.extra_padding.right.saturating_add(sa.right.max(0));
372        self.extra_padding.bottom = self
373            .extra_padding
374            .bottom
375            .saturating_add(sa.bottom.saturating_add(kb).max(0));
376        self.extra_padding.left = self.extra_padding.left.saturating_add(sa.left.max(0));
377        self
378    }
379
380    /// 自动叠加顶部安全区域内边距(用于顶部导航栏 `NavBar` 避开刘海屏/状态栏)。
381    #[inline(always)]
382    pub fn pt_safe(mut self, cx: &FrameScope) -> Self {
383        let sa = cx.safe_area();
384        self.extra_padding.top = self.extra_padding.top.saturating_add(sa.top.max(0));
385        self
386    }
387
388    /// 自动叠加底部安全区域与软键盘内边距(用于底部 `TabBar` 或 `BottomSheet` 避开 Home Indicator 与软键盘)。
389    #[inline(always)]
390    pub fn pb_safe(mut self, cx: &FrameScope) -> Self {
391        let sa = cx.safe_area();
392        let kb = cx.keyboard_inset();
393        self.extra_padding.bottom = self
394            .extra_padding
395            .bottom
396            .saturating_add(sa.bottom.saturating_add(kb).max(0));
397        self
398    }
399
400    /// 设置水平外边距(左右)。
401    #[inline(always)]
402    pub fn mx(mut self, px: i16) -> Self {
403        self.margin.left = px;
404        self.margin.right = px;
405        self
406    }
407
408    /// 设置垂直外边距(上下)。
409    #[inline(always)]
410    pub fn my(mut self, px: i16) -> Self {
411        self.margin.top = px;
412        self.margin.bottom = px;
413        self
414    }
415
416    /// 设置上外边距。
417    #[inline(always)]
418    pub fn mt(mut self, px: i16) -> Self {
419        self.margin.top = px;
420        self
421    }
422
423    /// 设置下外边距。
424    #[inline(always)]
425    pub fn mb(mut self, px: i16) -> Self {
426        self.margin.bottom = px;
427        self
428    }
429
430    /// 设置左外边距。
431    #[inline(always)]
432    pub fn ml(mut self, px: i16) -> Self {
433        self.margin.left = px;
434        self
435    }
436
437    /// 设置右外边距。
438    #[inline(always)]
439    pub fn mr(mut self, px: i16) -> Self {
440        self.margin.right = px;
441        self
442    }
443
444    /// 设置最小宽度像素约束。
445    #[inline(always)]
446    pub fn min_w(mut self, px: i16) -> Self {
447        self.min_w = px.max(0);
448        self
449    }
450
451    /// 设置最大宽度像素约束。
452    #[inline(always)]
453    pub fn max_w(mut self, px: i16) -> Self {
454        self.max_w = px.max(0);
455        self
456    }
457
458    /// 设置最小高度像素约束。
459    #[inline(always)]
460    pub fn min_h(mut self, px: i16) -> Self {
461        self.min_h = px.max(0);
462        self
463    }
464
465    /// 设置最大高度像素约束。
466    #[inline(always)]
467    pub fn max_h(mut self, px: i16) -> Self {
468        self.max_h = px.max(0);
469        self
470    }
471
472    /// 设置容器及其子树的整体不透明度(`0..=255`)。
473    #[inline(always)]
474    pub fn opacity(mut self, alpha: u8) -> Self {
475        self.opacity = alpha;
476        self
477    }
478
479    /// 设置指针按下(Active)时的背景色。
480    #[inline(always)]
481    pub fn active_bg(mut self, color: Color) -> Self {
482        self.active_bg = Some(color);
483        self
484    }
485
486    /// 设置指针悬停(Hover)时的背景色。
487    #[inline(always)]
488    pub fn hover_bg(mut self, color: Color) -> Self {
489        self.hover_bg = Some(color);
490        self
491    }
492
493    /// GPUI 风格悬停态样式闭包(如 `.hover(|s| s.bg(rgb(0xf5f5f5)).border_color(rgb(0x171717)))`)。
494    ///
495    /// 若闭包仅覆写背景色,自动退化为零分配 `hover_bg` 快速路径;否则完整保留边框、文本色、不透明度与圆角覆写。
496    #[inline(always)]
497    pub fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
498        let base = self.hover_style.unwrap_or_else(|| {
499            let mut r = StyleRefinement::new();
500            r.bg = self.hover_bg;
501            r
502        });
503        let refined = f(base);
504        if refined.is_empty() {
505            return self;
506        }
507        if refined.is_bg_only() {
508            self.hover_bg = refined.bg;
509            self.hover_style = None;
510        } else {
511            if let Some(bg) = refined.bg {
512                self.hover_bg = Some(bg);
513            }
514            self.hover_style = Some(refined);
515        }
516        self
517    }
518
519    /// GPUI 风格按下态(Active)样式闭包(如 `.active(|s| s.bg(rgb(0xe5e5e5)))`)。
520    #[inline(always)]
521    pub fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
522        let base = self.active_style.unwrap_or_else(|| {
523            let mut r = StyleRefinement::new();
524            r.bg = self.active_bg;
525            r
526        });
527        let refined = f(base);
528        if refined.is_empty() {
529            return self;
530        }
531        if refined.is_bg_only() {
532            self.active_bg = refined.bg;
533            self.active_style = None;
534        } else {
535            if let Some(bg) = refined.bg {
536                self.active_bg = Some(bg);
537            }
538            self.active_style = Some(refined);
539        }
540        self
541    }
542
543    /// GPUI 风格聚焦态(Focus)样式闭包(如 `.focus(|s| s.border_1().border_color(rgb(0x3b82f6)))`),自动开启 `focusable`。
544    #[inline(always)]
545    pub fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
546        self.focusable = true;
547        let refined = f(self.focus_style.unwrap_or_default());
548        if !refined.is_empty() {
549            self.focus_style = Some(refined);
550        }
551        self
552    }
553
554    /// 设置容器级可继承文本颜色(子树中未显式调用 `.color(...)` 的 `text(...)` 自动继承)。
555    #[inline(always)]
556    pub fn text_color(mut self, color: Color) -> Self {
557        self.text_color = Some(color);
558        self
559    }
560
561    /// 设置容器级可继承字号像素(子树中未显式调用 `.size(...)` 的 `text(...)` 自动继承)。
562    #[inline(always)]
563    pub fn text_size(mut self, px: u8) -> Self {
564        self.text_size = Some(px);
565        self
566    }
567
568    /// 容器级可继承字号 Tailwind 简写(`11px` / `12px` / `14px` / `16px` / `18px` / `22px` / `28px`)。
569    #[inline(always)] pub fn text_xs(self) -> Self { self.text_size(11) }
570    #[inline(always)] pub fn text_sm(self) -> Self { self.text_size(12) }
571    #[inline(always)] pub fn text_base(self) -> Self { self.text_size(14) }
572    #[inline(always)] pub fn text_lg(self) -> Self { self.text_size(16) }
573    #[inline(always)] pub fn text_xl(self) -> Self { self.text_size(18) }
574    #[inline(always)] pub fn text_2xl(self) -> Self { self.text_size(22) }
575    #[inline(always)] pub fn text_3xl(self) -> Self { self.text_size(28) }
576
577    /// 浮点不透明度简写(`0.0..=1.0`,映射到 `0..=255`)。
578    #[inline(always)]
579    pub fn opacity_f(mut self, alpha: f32) -> Self {
580        let c = if alpha <= 0.0 {
581            0.0
582        } else if alpha >= 1.0 {
583            1.0
584        } else {
585            alpha
586        };
587        self.opacity = (c * 255.0 + 0.5) as u8;
588        self
589    }
590
591    // --- 外边距 Tailwind 档位简写 ---
592    #[inline(always)] pub fn mx_0(self) -> Self { self.mx(0) }
593    #[inline(always)] pub fn mx_1(self) -> Self { self.mx(4) }
594    #[inline(always)] pub fn mx_2(self) -> Self { self.mx(8) }
595    #[inline(always)] pub fn mx_3(self) -> Self { self.mx(12) }
596    #[inline(always)] pub fn mx_4(self) -> Self { self.mx(16) }
597    #[inline(always)] pub fn my_0(self) -> Self { self.my(0) }
598    #[inline(always)] pub fn my_1(self) -> Self { self.my(4) }
599    #[inline(always)] pub fn my_2(self) -> Self { self.my(8) }
600    #[inline(always)] pub fn my_3(self) -> Self { self.my(12) }
601    #[inline(always)] pub fn my_4(self) -> Self { self.my(16) }
602    #[inline(always)] pub fn mt_1(self) -> Self { self.mt(4) }
603    #[inline(always)] pub fn mt_2(self) -> Self { self.mt(8) }
604    #[inline(always)] pub fn mt_3(self) -> Self { self.mt(12) }
605    #[inline(always)] pub fn mt_4(self) -> Self { self.mt(16) }
606    #[inline(always)] pub fn mb_1(self) -> Self { self.mb(4) }
607    #[inline(always)] pub fn mb_2(self) -> Self { self.mb(8) }
608    #[inline(always)] pub fn mb_3(self) -> Self { self.mb(12) }
609    #[inline(always)] pub fn mb_4(self) -> Self { self.mb(16) }
610    #[inline(always)] pub fn ml_1(self) -> Self { self.ml(4) }
611    #[inline(always)] pub fn ml_2(self) -> Self { self.ml(8) }
612    #[inline(always)] pub fn ml_3(self) -> Self { self.ml(12) }
613    #[inline(always)] pub fn ml_4(self) -> Self { self.ml(16) }
614    #[inline(always)] pub fn mr_1(self) -> Self { self.mr(4) }
615    #[inline(always)] pub fn mr_2(self) -> Self { self.mr(8) }
616    #[inline(always)] pub fn mr_3(self) -> Self { self.mr(12) }
617    #[inline(always)] pub fn mr_4(self) -> Self { self.mr(16) }
618
619    // --- 定位、显示、光标与阴影 Tailwind / GPUI 简写 ---
620    /// 语义化定位标记(Velox 子节点 `.pos(x, y)` 天然相对父容器左上角定位)。
621    #[inline(always)] pub fn relative(self) -> Self { self }
622    /// 块级垂直容器简写(等价 `.flex_col()`)。
623    #[inline(always)] pub fn block(self) -> Self { self.flex_col() }
624    /// 行内弹性容器简写(等价 `.flex_row()`)。
625    #[inline(always)] pub fn inline_flex(self) -> Self { self.flex_row() }
626
627    #[inline(always)] pub fn cursor_pointer(self) -> Self { self.cursor(CursorShape::Pointer) }
628    #[inline(always)] pub fn cursor_default(self) -> Self { self.cursor(CursorShape::Default) }
629    #[inline(always)] pub fn cursor_text(self) -> Self { self.cursor(CursorShape::Text) }
630    #[inline(always)] pub fn cursor_not_allowed(self) -> Self { self.cursor(CursorShape::NotAllowed) }
631    #[inline(always)] pub fn cursor_grab(self) -> Self { self.cursor(CursorShape::Grab) }
632    #[inline(always)] pub fn cursor_grabbing(self) -> Self { self.cursor(CursorShape::Grabbing) }
633
634    #[inline(always)] pub fn shadow_none(mut self) -> Self { self.shadow = None; self }
635    #[inline(always)] pub fn shadow_sm(self) -> Self { self.shadow(4, 0, 1, Color::rgba(0, 0, 0, 48)) }
636    #[inline(always)] pub fn shadow_md(self) -> Self { self.shadow(10, 0, 3, Color::rgba(0, 0, 0, 72)) }
637    #[inline(always)] pub fn shadow_lg(self) -> Self { self.shadow(18, 0, 6, Color::rgba(0, 0, 0, 96)) }
638    #[inline(always)] pub fn shadow_xl(self) -> Self { self.shadow(28, 0, 10, Color::rgba(0, 0, 0, 120)) }
639
640    /// 设置自顶向下的垂直线性渐变终点颜色(起点颜色为 `.bg(...)`)。
641    #[inline(always)]
642    pub fn bg_gradient(mut self, to_color: Color) -> Self {
643        self.bg_grad = Some((to_color, false));
644        self
645    }
646
647    /// 设置自左向右的水平线性渐变终点颜色(起点颜色为 `.bg(...)`)。
648    #[inline(always)]
649    pub fn bg_gradient_x(mut self, to_color: Color) -> Self {
650        self.bg_grad = Some((to_color, true));
651        self
652    }
653
654    /// 将当前节点及子树提升至顶层 Z-Order 渲染队列(脱离父级裁剪并覆盖在普通层之上,用于 `Modal` / `Dropdown`)。
655    #[inline(always)]
656    pub fn overlay(mut self) -> Self {
657        self.is_overlay = true;
658        self
659    }
660
661    /// 拦截并吞噬落在该容器区域内的指针命中事件,阻断点击穿透至底层控件(如 `Modal` 遮罩层)。
662    #[inline(always)]
663    pub fn occlude_hit(mut self) -> Self {
664        self.occlude_hit = true;
665        self
666    }
667
668    /// 开启按住不放自动连发(按住超过 `400ms` 后每 `100ms` 自动触发一次 `on_click`)。
669    #[inline(always)]
670    pub fn auto_repeat(mut self) -> Self {
671        self.auto_repeat = true;
672        self
673    }
674
675    /// 绑定原始长按回调闭包(按住 `>= 500ms` 触发)。
676    #[inline(always)]
677    pub fn on_long_press_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
678        self.on_long_press = Some(handler);
679        self.focusable = true;
680        self
681    }
682
683    /// 绑定与 `cx.listener(...)` 兼容的长按回调闭包(按住 `>= 500ms` 触发)。
684    #[inline(always)]
685    pub fn on_long_press(
686        mut self,
687        cx: &'a FrameScope,
688        handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
689    ) -> Self {
690        let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
691            let (x, y) = acx.click_position();
692            handler(crate::pod::ClickEvent { x, y }, acx);
693        });
694        self.on_long_press = Some(wrapped);
695        self.focusable = true;
696        self
697    }
698
699    /// 设置物理按键/旋钮焦点命中时的边框高亮样式,并自动开启 `focusable`。
700    #[inline(always)]
701    pub fn focus_border(mut self, width: u8, color: Color) -> Self {
702        self.focus_border = Some((width, color));
703        self.focusable = true;
704        self
705    }
706
707    /// 显式禁止在聚焦态绘制外围焦点边框(适用于 `Slider`、透明容器、内嵌滚动视口及 `.unstyled()` 控件)。
708    #[inline(always)]
709    pub fn no_focus_border(mut self) -> Self {
710        self.focus_border = Some((0, Color::TRANSPARENT));
711        self
712    }
713
714    /// 显式指定鼠标悬停在此节点时的光标形状(如 `CursorShape::Pointer` / `Grab` / `Text` / `NotAllowed` / `ResizeEW`)。
715    #[inline(always)]
716    pub fn cursor(mut self, cursor: CursorShape) -> Self {
717        self.cursor = Some(cursor);
718        self
719    }
720
721    /// 显式将该节点加入物理按键/旋钮焦点导航链。
722    #[inline(always)]
723    pub fn focusable(mut self) -> Self {
724        self.focusable = true;
725        self
726    }
727
728    /// 显式禁止该节点抢占键盘焦点(适用于右键上下文菜单项、清空按钮与点击外部关闭遮罩,点击后保持底层输入框焦点不丢失)。
729    #[inline(always)]
730    pub fn unfocusable(mut self) -> Self {
731        self.focusable = false;
732        self
733    }
734
735    /// 将当前容器标记为可触摸拖拽/滚轮滚动的视口,并指定最大可滚动偏移量 `max_scroll`(自动开启 `overflow_hidden`)。
736    #[inline(always)]
737    pub fn scrollable(mut self, max_scroll: i16) -> Self {
738        self.scrollable_max = Some(max_scroll.max(0));
739        self.clip_children = true;
740        self
741    }
742
743    /// 将当前容器标记为按内容实际尺寸自动测量最大滚动范围的视口(自动开启 `overflow_hidden`,同时支持垂直与水平滚动)。
744    #[inline(always)]
745    pub fn scrollable_auto(mut self) -> Self {
746        self.scrollable_max = Some(i16::MAX);
747        self.clip_children = true;
748        self
749    }
750
751    /// 独立覆盖当前节点在父容器交叉轴上的对齐方式(`align-self`)。
752    #[inline(always)]
753    pub fn align_self(mut self, align: AlignItems) -> Self {
754        self.align_self = Some(align);
755        self
756    }
757
758    /// 交叉轴自身起始对齐(`self-start`)。
759    #[inline(always)]
760    pub fn self_start(self) -> Self {
761        self.align_self(AlignItems::Start)
762    }
763
764    /// 交叉轴自身居中对齐(`self-center`)。
765    #[inline(always)]
766    pub fn self_center(self) -> Self {
767        self.align_self(AlignItems::Center)
768    }
769
770    /// 交叉轴自身末尾对齐(`self-end`)。
771    #[inline(always)]
772    pub fn self_end(self) -> Self {
773        self.align_self(AlignItems::End)
774    }
775
776    /// 交叉轴自身拉伸对齐(`self-stretch`)。
777    #[inline(always)]
778    pub fn self_stretch(self) -> Self {
779        self.align_self(AlignItems::Stretch)
780    }
781
782    /// 在水平行容器(`flex_row`)中启用子元素按文本基线对齐(`items-baseline`)。
783    #[inline(always)]
784    pub fn items_baseline(mut self) -> Self {
785        self.items_baseline = true;
786        self
787    }
788
789    /// 启用裁剪子元素(等价 CSS `overflow: hidden`)。
790    #[inline(always)]
791    pub fn overflow_hidden(mut self) -> Self {
792        self.clip_children = true;
793        self
794    }
795
796    /// 关闭子元素裁剪(等价 CSS `overflow: visible`)。
797    #[inline(always)]
798    pub fn overflow_visible(mut self) -> Self {
799        self.clip_children = false;
800        self
801    }
802
803    /// 启用自动测量边界的滚动视口(等价 CSS `overflow: auto/scroll`)。
804    #[inline(always)]
805    pub fn overflow_scroll(self) -> Self {
806        self.scrollable_auto()
807    }
808
809    /// 启用垂直方向自动测量滚动视口(等价 CSS `overflow-y: auto/scroll`)。
810    #[inline(always)]
811    pub fn overflow_y_scroll(self) -> Self {
812        self.flex_col().scrollable_auto()
813    }
814
815    /// 启用水平方向自动测量滚动视口(等价 CSS `overflow-x: auto/scroll`)。
816    #[inline(always)]
817    pub fn overflow_x_scroll(self) -> Self {
818        self.flex_row().scrollable_auto()
819    }
820
821    /// Tailwind / GPUI 风格默认过渡动画(150ms 颜色 + 不透明度 + 几何边界,EaseOut)。
822    #[inline(always)]
823    pub fn transition(self) -> Self {
824        self.animate_all(Duration::from_millis(150))
825    }
826
827    /// Tailwind / GPUI 风格颜色过渡简写(150ms 背景/边框/文本颜色平滑过渡)。
828    #[inline(always)]
829    pub fn transition_colors(self) -> Self {
830        self.animate_color(Duration::from_millis(150))
831    }
832
833    /// Tailwind / GPUI 风格不透明度过渡简写(150ms 淡入淡出)。
834    #[inline(always)]
835    pub fn transition_opacity(self) -> Self {
836        self.animate_opacity(Duration::from_millis(150))
837    }
838
839    /// Tailwind / GPUI 风格全属性过渡简写(150ms 颜色 + 边界 + 不透明度)。
840    #[inline(always)]
841    pub fn transition_all(self) -> Self {
842        self.animate_all(Duration::from_millis(150))
843    }
844
845    /// 显式禁用该节点上的过渡动画(等价 CSS `transition: none`)。
846    #[inline(always)]
847    pub fn transition_none(mut self) -> Self {
848        self.transition = None;
849        self
850    }
851
852    #[inline(always)] pub fn duration_75(self) -> Self { self.duration(Duration::from_millis(75)) }
853    #[inline(always)] pub fn duration_100(self) -> Self { self.duration(Duration::from_millis(100)) }
854    #[inline(always)] pub fn duration_150(self) -> Self { self.duration(Duration::from_millis(150)) }
855    #[inline(always)] pub fn duration_200(self) -> Self { self.duration(Duration::from_millis(200)) }
856    #[inline(always)] pub fn duration_300(self) -> Self { self.duration(Duration::from_millis(300)) }
857    #[inline(always)] pub fn duration_500(self) -> Self { self.duration(Duration::from_millis(500)) }
858
859    #[inline(always)] pub fn ease_linear(self) -> Self { self.ease(Easing::Linear) }
860    #[inline(always)] pub fn ease_in(self) -> Self { self.ease(Easing::EaseIn) }
861    #[inline(always)] pub fn ease_out(self) -> Self { self.ease(Easing::EaseOut) }
862    #[inline(always)] pub fn ease_in_out(self) -> Self { self.ease(Easing::EaseInOut) }
863    #[inline(always)] pub fn ease_spring(self) -> Self { self.ease(Easing::Spring) }
864
865    /// 声明背景色与边框/子文本颜色的跨帧平滑过渡动画。
866    #[inline(always)]
867    pub fn animate_color(mut self, duration: Duration) -> Self {
868        let ms = duration.as_millis().min(u16::MAX as u128) as u16;
869        let mut spec = self.transition.unwrap_or(TransitionSpec {
870            duration_ms: ms,
871            ..TransitionSpec::default()
872        });
873        spec.duration_ms = ms;
874        spec.animate_color = true;
875        self.transition = Some(spec);
876        self
877    }
878
879    /// 声明背景色的跨帧平滑过渡动画。
880    #[inline(always)]
881    pub fn animate_bg(self, duration: Duration) -> Self {
882        self.animate_color(duration)
883    }
884
885    /// 声明几何边界框(位置/尺寸)的跨帧平滑过渡动画。
886    #[inline(always)]
887    pub fn animate_bounds(mut self, duration: Duration) -> Self {
888        let ms = duration.as_millis().min(u16::MAX as u128) as u16;
889        let mut spec = self.transition.unwrap_or(TransitionSpec {
890            duration_ms: ms,
891            ..TransitionSpec::default()
892        });
893        spec.duration_ms = ms;
894        spec.animate_bounds = true;
895        self.transition = Some(spec);
896        self
897    }
898
899    /// 设置过渡动画的缓动曲线(`Linear` / `EaseIn` / `EaseOut` / `EaseInOut` / `Spring`)。
900    #[inline(always)]
901    pub fn ease(mut self, easing: Easing) -> Self {
902        let mut spec = self.transition.unwrap_or_default();
903        spec.easing = easing;
904        self.transition = Some(spec);
905        self
906    }
907
908    /// 设置动画为无限循环模式。
909    #[inline(always)]
910    pub fn repeat_infinite(mut self) -> Self {
911        let mut spec = self.transition.unwrap_or_default();
912        spec.loop_mode = LoopMode::Infinite;
913        self.transition = Some(spec);
914        self
915    }
916
917    /// 设置动画为无限往复(`0 -> 1 -> 0`)循环模式。
918    #[inline(always)]
919    pub fn repeat_ping_pong(mut self) -> Self {
920        let mut spec = self.transition.unwrap_or_default();
921        spec.loop_mode = LoopMode::PingPong;
922        self.transition = Some(spec);
923        self
924    }
925
926    /// 修改当前过渡动画的时长(不改变已声明的插值属性)。
927    #[inline(always)]
928    pub fn duration(mut self, duration: Duration) -> Self {
929        let mut spec = self.transition.unwrap_or_default();
930        spec.duration_ms = duration_to_ms(duration);
931        self.transition = Some(spec);
932        self
933    }
934
935    /// 设置过渡/入场动画开始前的延迟(用于列表项交错 `stagger` 入场等编排)。
936    #[inline(always)]
937    pub fn delay(mut self, delay: Duration) -> Self {
938        let mut spec = self.transition.unwrap_or_default();
939        spec.delay_ms = duration_to_ms(delay);
940        self.transition = Some(spec);
941        self
942    }
943
944    /// 声明容器整体不透明度(`.opacity(...)`)的跨帧淡入淡出过渡,作用于整个子树。
945    #[inline(always)]
946    pub fn animate_opacity(mut self, duration: Duration) -> Self {
947        let mut spec = self.transition.unwrap_or_default();
948        spec.duration_ms = duration_to_ms(duration);
949        spec.animate_opacity = true;
950        self.transition = Some(spec);
951        self
952    }
953
954    /// 同时声明颜色、几何边界与不透明度的跨帧过渡(等价 CSS `transition: all`)。
955    #[inline(always)]
956    pub fn animate_all(mut self, duration: Duration) -> Self {
957        let mut spec = self.transition.unwrap_or_default();
958        spec.duration_ms = duration_to_ms(duration);
959        spec.animate_color = true;
960        spec.animate_bounds = true;
961        spec.animate_opacity = true;
962        self.transition = Some(spec);
963        self
964    }
965
966    /// 声明首次出现时的入场动画:从不透明度 `opacity` 与偏移 `(dx, dy)` 平滑过渡到布局目标状态。
967    #[inline(always)]
968    pub fn enter_from(mut self, opacity: u8, dx: i16, dy: i16, duration: Duration) -> Self {
969        let mut spec = self.transition.unwrap_or_default();
970        spec.duration_ms = duration_to_ms(duration);
971        spec.enter = Some(EnterFrom { opacity, dx, dy });
972        self.transition = Some(spec);
973        self
974    }
975
976    /// 首次出现时从完全透明淡入(入场动画快捷方式)。
977    #[inline(always)]
978    pub fn fade_in(self, duration: Duration) -> Self {
979        self.enter_from(0, 0, 0, duration)
980    }
981
982    /// 首次出现时从偏移 `(dx, dy)` 处滑入并同时淡入(入场动画快捷方式)。
983    #[inline(always)]
984    pub fn slide_in(self, dx: i16, dy: i16, duration: Duration) -> Self {
985        self.enter_from(0, dx, dy, duration)
986    }
987
988    /// 声明在 `from_bg` 与当前 `.bg(...)` 之间无限往复呼吸的循环动画(如呼吸指示灯)。
989    #[inline(always)]
990    pub fn ping_pong_bg(mut self, from_bg: Color, duration: Duration) -> Self {
991        let ms = duration.as_millis().min(u16::MAX as u128) as u16;
992        let mut spec = self.transition.unwrap_or_default();
993        spec.duration_ms = ms;
994        spec.animate_color = true;
995        spec.loop_mode = LoopMode::PingPong;
996        spec.easing = Easing::EaseInOut;
997        spec.pulse_from_bg = Some(from_bg);
998        self.transition = Some(spec);
999        self
1000    }
1001
1002    /// 绑定原始点击回调闭包。
1003    #[inline(always)]
1004    pub fn on_click_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
1005        self.on_click = Some(handler);
1006        self.focusable = true;
1007        self
1008    }
1009
1010    /// 绑定与 `cx.listener(...)` 兼容的点击回调闭包。
1011    #[inline(always)]
1012    pub fn on_click(
1013        mut self,
1014        cx: &'a FrameScope,
1015        handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
1016    ) -> Self {
1017        let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
1018            let (x, y) = acx.click_position();
1019            handler(crate::pod::ClickEvent { x, y }, acx);
1020        });
1021        self.on_click = Some(wrapped);
1022        self.focusable = true;
1023        self
1024    }
1025
1026    /// 绑定原始鼠标右键(次键)点击回调闭包。
1027    #[inline(always)]
1028    pub fn on_secondary_click_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
1029        self.on_secondary_click = Some(handler);
1030        self
1031    }
1032
1033    /// 绑定与 `cx.listener(...)` 兼容的鼠标右键(次键)点击回调闭包。
1034    #[inline(always)]
1035    pub fn on_secondary_click(
1036        mut self,
1037        cx: &'a FrameScope,
1038        handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
1039    ) -> Self {
1040        let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
1041            let (x, y) = acx.click_position();
1042            handler(crate::pod::ClickEvent { x, y }, acx);
1043        });
1044        self.on_secondary_click = Some(wrapped);
1045        self
1046    }
1047
1048    /// 绑定原始拖拽回调闭包。
1049    #[inline(always)]
1050    pub fn on_drag_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
1051        self.on_drag = Some(handler);
1052        self
1053    }
1054
1055    /// 绑定与 `cx.listener(...)` 兼容的触摸/鼠标拖拽回调闭包(如 `Slider` 连续滑动)。
1056    #[inline(always)]
1057    pub fn on_drag(
1058        mut self,
1059        cx: &'a FrameScope,
1060        handler: &'a dyn Fn(DragEvent, &mut ActionScope),
1061    ) -> Self {
1062        let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
1063            let ev = acx.drag_event();
1064            handler(ev, acx);
1065        });
1066        self.on_drag = Some(wrapped);
1067        self
1068    }
1069
1070    /// 绑定与 `cx.listener(...)` 兼容的四向快速滑动手势(`SwipeDir`)回调闭包。
1071    #[inline(always)]
1072    pub fn on_swipe(
1073        mut self,
1074        _cx: &'a FrameScope,
1075        handler: &'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope),
1076    ) -> Self {
1077        self.on_swipe = Some(handler);
1078        self
1079    }
1080
1081    /// 设置列表项或动态节点的稳定键值(自动哈希为 `u32` 元素 ID,消除手动管理魔法数字冲突)。
1082    #[inline(always)]
1083    pub fn key(mut self, key: impl AsRef<[u8]>) -> Self {
1084        let mut h: u32 = 0x811c_9dc5;
1085        for &b in key.as_ref() {
1086            h ^= b as u32;
1087            h = h.wrapping_mul(0x0100_0193);
1088        }
1089        self.id = Some(h | 1);
1090        self
1091    }
1092
1093    /// 设置元素的 Z-Index 层级(用于在同父容器兄弟节点间调整前后绘制顺序;若需脱离父容器裁剪并提升至全屏最顶层,请显式调用 `.overlay()`)。
1094    #[inline(always)]
1095    pub fn z_index(mut self, z: i8) -> Self {
1096        self.z_index = z;
1097        self
1098    }
1099
1100    /// 绑定键盘字符/编辑键回调闭包(用于聚焦态 `TextInput` 接收 `Char(c)`、`Backspace`、`Delete`、`Left`/`Right` 等)。
1101    #[inline(always)]
1102    pub fn on_key_input(
1103        mut self,
1104        handler: &'a dyn Fn(crate::pod::KeyCode, &mut ActionScope),
1105    ) -> Self {
1106        self.on_key_input = Some(handler);
1107        self.focusable = true;
1108        self
1109    }
1110
1111    /// 绑定非文本控件的局部键盘导航回调闭包(供 `Slider` / `Tabs` / `RadioGroup` / `Select` 在聚焦或浮层打开时响应方向键、`Home`/`End`、`Enter`/`Space`,绝不吞掉 `Tab`/`BackTab` 焦点切换)。
1112    #[inline(always)]
1113    pub fn on_key_nav(
1114        mut self,
1115        handler: &'a dyn Fn(crate::pod::KeyCode, &mut ActionScope),
1116    ) -> Self {
1117        self.on_key_nav = Some(handler);
1118        self
1119    }
1120
1121    /// 绑定系统级返回请求(Android Back / 浏览器后退 / 左边缘返回手势)回调闭包。
1122    #[inline(always)]
1123    pub fn on_navigate_back(
1124        mut self,
1125        handler: &'a dyn Fn(&mut ActionScope),
1126    ) -> Self {
1127        self.on_navigate_back = Some(handler);
1128        self
1129    }
1130
1131    /// 设置解析式 SDF 外阴影(`blur`: 模糊扩散半径 `1..=64px`,`offset_x`/`offset_y`: 阴影偏移,`color`: 阴影颜色)。
1132    #[inline(always)]
1133    pub fn shadow(mut self, blur: u8, offset_x: i8, offset_y: i8, color: Color) -> Self {
1134        if blur > 0 && color.a() > 0 {
1135            self.shadow = Some((blur.min(64), offset_x, offset_y, color));
1136        }
1137        self
1138    }
1139
1140    /// 将当前容器启用为指定列数 `cols` 的二维网格布局。
1141    #[inline(always)]
1142    pub fn grid(mut self, cols: u8) -> Self {
1143        self.grid_cols = cols.max(1);
1144        self
1145    }
1146
1147    /// 链式追加单个子节点(通过 `ScratchArena` + `Cell` 侵入式链表连接,$O(1)$ 耗时,100% 遵循 Stacked Borrows 安全规范)。
1148    pub fn child(mut self, cx: &'a FrameScope, child: impl IntoElement<'a>) -> Self {
1149        let elem = child.into_element(cx);
1150        let node: &'a ElementNode<'a> = cx.arena().alloc_temp(ElementNode {
1151            element: elem,
1152            next_sibling: Cell::new(None),
1153        });
1154        if let Some(last) = self.last_child {
1155            last.next_sibling.set(Some(node));
1156        } else {
1157            self.first_child = Some(node);
1158        }
1159        self.last_child = Some(node);
1160        self
1161    }
1162
1163    /// 可选子节点追加(当 `child` 为 `Some(c)` 时追加,否则无操作)。
1164    #[inline(always)]
1165    pub fn child_opt(self, cx: &'a FrameScope, child: Option<impl IntoElement<'a>>) -> Self {
1166        match child {
1167            Some(c) => self.child(cx, c),
1168            None => self,
1169        }
1170    }
1171
1172    /// 批量追加任意迭代器生成的子节点序列。
1173    pub fn children<I, E>(mut self, cx: &'a FrameScope, iter: I) -> Self
1174    where
1175        I: IntoIterator<Item = E>,
1176        E: IntoElement<'a>,
1177    {
1178        for item in iter {
1179            self = self.child(cx, item);
1180        }
1181        self
1182    }
1183
1184    /// 按当前语言的书写方向设置主轴“逻辑起始端对齐”(LTR 为 `Start`,RTL 自动镜像为 `End`)。
1185    #[inline(always)]
1186    pub fn justify_start_localized(mut self, cx: &FrameScope) -> Self {
1187        if cx.is_rtl() {
1188            self.style.set_justify(crate::style::Justify::End);
1189        } else {
1190            self.style.set_justify(crate::style::Justify::Start);
1191        }
1192        self
1193    }
1194
1195    /// 按当前语言的书写方向设置主轴“逻辑末端对齐”(LTR 为 `End`,RTL 自动镜像为 `Start`)。
1196    #[inline(always)]
1197    pub fn justify_end_localized(mut self, cx: &FrameScope) -> Self {
1198        if cx.is_rtl() {
1199            self.style.set_justify(crate::style::Justify::Start);
1200        } else {
1201            self.style.set_justify(crate::style::Justify::End);
1202        }
1203        self
1204    }
1205
1206    /// 按当前语言方向设置逻辑起始端内边距(CSS `padding-inline-start`:LTR 为 `pl`,RTL 自动镜像为 `pr`)。
1207    #[inline(always)]
1208    pub fn ps(self, cx: &FrameScope, px: i8) -> Self {
1209        if cx.is_rtl() {
1210            self.pr(px)
1211        } else {
1212            self.pl(px)
1213        }
1214    }
1215
1216    /// 按当前语言方向设置逻辑末端内边距(CSS `padding-inline-end`:LTR 为 `pr`,RTL 自动镜像为 `pl`)。
1217    #[inline(always)]
1218    pub fn pe(self, cx: &FrameScope, px: i8) -> Self {
1219        if cx.is_rtl() {
1220            self.pl(px)
1221        } else {
1222            self.pr(px)
1223        }
1224    }
1225
1226    /// 按当前语言方向设置逻辑起始端外边距(CSS `margin-inline-start`:LTR 为 `ml`,RTL 自动镜像为 `mr`)。
1227    #[inline(always)]
1228    pub fn ms(self, cx: &FrameScope, px: i16) -> Self {
1229        if cx.is_rtl() {
1230            self.mr(px)
1231        } else {
1232            self.ml(px)
1233        }
1234    }
1235
1236    /// 按当前语言方向设置逻辑末端外边距(CSS `margin-inline-end`:LTR 为 `mr`,RTL 自动镜像为 `ml`)。
1237    #[inline(always)]
1238    pub fn me(self, cx: &FrameScope, px: i16) -> Self {
1239        if cx.is_rtl() {
1240            self.ml(px)
1241        } else {
1242            self.mr(px)
1243        }
1244    }
1245}
1246