Skip to main content

velox_engine/
style_ext.rs

1//! 组件通用外层样式扩展包([`StyleExt`])与 `gpui-kit` 风格统一链式修饰 Trait([`StyledExt`])。
2//!
3//! 搭配 24 字节紧凑核心 [`Style`] / [`Styled`] 使用:
4//! - 任何实现了 [`Styled`] + [`StyledExt`] 的组件(涵盖全部 `velox-base` 无样式原语与 `velox-component` 主题组件)
5//!   均可直接使用与 `Div` 100% 一致的 `.hover(|s| ...)`、`.active(|s| ...)`、`.focus(|s| ...)`、
6//!   `.overflow_hidden()`、`.transition_colors()`、`.duration_150()`、`.ease_out()`、`.text_xs()`、
7//!   `.text_color(...)`、`.shadow_md()`、`.cursor_pointer()`、`.opacity_f(...)`、`.mx_2()` 等语法;
8//! - 默认未调用任何扩展修饰符时 `StyleExt::is_empty() == true`,渲染热路径零额外 `DivVisual` / `DivStateStyles` 分配。
9
10use core::time::Duration;
11
12use crate::element::{div, Div, Element, EnterFrom, IntoElement, TransitionSpec};
13use crate::pod::{CursorShape, FrameScope};
14use crate::style::{
15    AlignItems, Color, Easing, Edges, FlexDir, Length, LoopMode, Style, StyleRefinement, Styled,
16};
17
18/// 除 24B 核心 [`Style`] 以外的组件外层扩展修饰符集合(零堆分配、`const` 可构造)。
19#[derive(Copy, Clone, Debug)]
20pub struct StyleExt {
21    pub margin: Edges<i16>,
22    pub min_w: i16,
23    pub max_w: i16,
24    pub min_h: i16,
25    pub max_h: i16,
26    pub opacity: Option<u8>,
27    pub clip_children: Option<bool>,
28    pub scrollable_max: Option<i16>,
29    pub scroll_dir: Option<FlexDir>,
30    pub shadow: Option<(u8, i8, i8, Color)>,
31    pub shadow_cleared: bool,
32    pub cursor: Option<CursorShape>,
33    pub transition: Option<TransitionSpec>,
34    pub transition_cleared: bool,
35    pub text_color: Option<Color>,
36    pub text_size: Option<u8>,
37    pub hover_style: Option<StyleRefinement>,
38    pub active_style: Option<StyleRefinement>,
39    pub focus_style: Option<StyleRefinement>,
40    pub z_index: Option<i8>,
41    pub align_self: Option<AlignItems>,
42    pub items_baseline: bool,
43    dirty: bool,
44}
45
46impl Default for StyleExt {
47    #[inline(always)]
48    fn default() -> Self {
49        Self::new()
50    }
51}
52
53impl StyleExt {
54    /// 空扩展样式常量(供所有组件 `const fn new` 零成本初始化)。
55    pub const DEFAULT: Self = Self::new();
56
57    /// 构造一个未设置任何扩展修饰符的空 `StyleExt`。
58    #[inline(always)]
59    pub const fn new() -> Self {
60        Self {
61            margin: Edges::all(0),
62            min_w: 0,
63            max_w: 0,
64            min_h: 0,
65            max_h: 0,
66            opacity: None,
67            clip_children: None,
68            scrollable_max: None,
69            scroll_dir: None,
70            shadow: None,
71            shadow_cleared: false,
72            cursor: None,
73            transition: None,
74            transition_cleared: false,
75            text_color: None,
76            text_size: None,
77            hover_style: None,
78            active_style: None,
79            focus_style: None,
80            z_index: None,
81            align_self: None,
82            items_baseline: false,
83            dirty: false,
84        }
85    }
86
87    /// 是否未调用过任何外层扩展修饰符(为 `true` 时渲染热路径零额外开销)。
88    #[inline(always)]
89    pub const fn is_empty(&self) -> bool {
90        !self.dirty
91    }
92
93    /// 便捷解析外部 `Styled` 宽度(若用户通过 `.w(...)` / `.w_64()` 设置了像素宽则优先使用,否则回退组件默认宽度)。
94    #[inline(always)]
95    pub const fn resolve_width(custom: Style, fallback: i16) -> i16 {
96        match custom.width {
97            Length::Px(w) => w,
98            _ => fallback,
99        }
100    }
101
102    /// 便捷解析外部 `Styled` 高度(若用户通过 `.h(...)` / `.h_10()` 设置了像素高则优先使用,否则回退组件默认高度)。
103    #[inline(always)]
104    pub const fn resolve_height(custom: Style, fallback: i16) -> i16 {
105        match custom.height {
106            Length::Px(h) => h,
107            _ => fallback,
108        }
109    }
110
111    /// 将用户通过 [`Styled`] 在外层显式设置的非默认 [`Style`] 属性合并到组件内部根 [`Style`] 上。
112    #[inline]
113    pub fn merge_style(mut base: Style, custom: Style) -> Style {
114        if custom == Style::default() {
115            return base;
116        }
117        if !matches!(custom.width, Length::Auto) {
118            base.width = custom.width;
119        }
120        if !matches!(custom.height, Length::Auto) {
121            base.height = custom.height;
122        }
123        if custom.padding != Edges::all(0) {
124            base.padding = custom.padding;
125        }
126        if custom.bg != Color::TRANSPARENT {
127            base.bg = custom.bg;
128        }
129        if custom.border_px() > 0 {
130            base.set_border_px(custom.border_px());
131        }
132        if custom.border_color != Color::TRANSPARENT {
133            base.border_color = custom.border_color;
134        }
135        if custom.rounded_radius() > 0 {
136            base.set_rounded(custom.rounded_radius());
137        }
138        if custom.gap != 0 {
139            base.gap = custom.gap;
140        }
141        if custom.margin_px() != 0 {
142            base.set_margin_px(custom.margin_px());
143        }
144        if custom.flex_wrap() {
145            base.set_flex_wrap(true);
146        }
147        if custom.flex_flags != 0 {
148            if (custom.flex_flags & 0b0000_0001) != 0 {
149                base.set_col();
150            }
151            if (custom.flex_flags & 0b0000_1110) != 0 {
152                base.set_justify(custom.justify());
153            }
154            if (custom.flex_flags & 0b0011_0000) != 0 {
155                base.set_items_align(custom.items_align());
156            }
157            if (custom.flex_flags & 0b1100_0000) != 0 {
158                base.set_grow(custom.flex_grow());
159            }
160        }
161        base
162    }
163
164    /// 仅将 `StyleExt` 扩展属性叠加到已构造的根 [`Div`] 上(适用于组件已自行合并 `self.style` 的场景)。
165    #[inline]
166    pub fn apply_ext_only<'a>(self, mut target: Div<'a>) -> Div<'a> {
167        if !self.dirty {
168            return target;
169        }
170        if self.margin != Edges::all(0) {
171            target.margin = self.margin;
172        }
173        if self.min_w != 0 {
174            target.min_w = self.min_w;
175        }
176        if self.max_w != 0 {
177            target.max_w = self.max_w;
178        }
179        if self.min_h != 0 {
180            target.min_h = self.min_h;
181        }
182        if self.max_h != 0 {
183            target.max_h = self.max_h;
184        }
185        if let Some(op) = self.opacity {
186            target.opacity = op;
187        }
188        if let Some(clip) = self.clip_children {
189            target.clip_children = clip;
190        }
191        if let Some(scroll_max) = self.scrollable_max {
192            target.scrollable_max = Some(scroll_max);
193            target.clip_children = true;
194            if let Some(dir) = self.scroll_dir {
195                match dir {
196                    FlexDir::Row => target.style.set_row(),
197                    FlexDir::Col => target.style.set_col(),
198                }
199            }
200        }
201        if self.shadow_cleared {
202            target.shadow = None;
203        } else if let Some(sh) = self.shadow {
204            target.shadow = Some(sh);
205        }
206        if let Some(c) = self.cursor {
207            target.cursor = Some(c);
208        }
209        if self.transition_cleared {
210            target.transition = None;
211        } else if let Some(tr) = self.transition {
212            target.transition = Some(tr);
213        }
214        if let Some(tc) = self.text_color {
215            target.text_color = Some(tc);
216        }
217        if let Some(ts) = self.text_size {
218            target.text_size = Some(ts);
219        }
220        if let Some(hr) = self.hover_style {
221            target = target.hover(|base| merge_refinement(base, hr));
222        }
223        if let Some(ar) = self.active_style {
224            target = target.active(|base| merge_refinement(base, ar));
225        }
226        if let Some(fr) = self.focus_style {
227            target = target.focus(|base| merge_refinement(base, fr));
228        }
229        if let Some(z) = self.z_index {
230            target.z_index = z;
231        }
232        if let Some(a) = self.align_self {
233            target.align_self = Some(a);
234        }
235        if self.items_baseline {
236            target.items_baseline = true;
237        }
238        target
239    }
240
241    /// 将外层 [`Style`] 与 [`StyleExt`] 一次性精准叠加到组件根 [`Div`] 上(无自定义修饰时零额外分配)。
242    #[inline]
243    pub fn apply_to_div<'a>(self, mut target: Div<'a>, custom_style: Style) -> Div<'a> {
244        if custom_style != Style::default() {
245            target.style = Self::merge_style(target.style, custom_style);
246        }
247        self.apply_ext_only(target)
248    }
249
250    /// 供输出非 `Div` 元素(如 `Element::Img` / `Element::Text`)的原子组件使用:
251    /// 未设置外层样式时零包装直接透传,设置任意外层样式时自动用 `Div` 包裹并完整应用。
252    #[inline]
253    pub fn wrap_element<'a>(
254        self,
255        cx: &'a FrameScope,
256        elem: Element<'a>,
257        custom_style: Style,
258    ) -> Element<'a> {
259        if custom_style == Style::default() && !self.dirty {
260            return elem;
261        }
262        self.apply_to_div(div(), custom_style)
263            .child(cx, elem)
264            .into_element(cx)
265    }
266}
267
268#[inline(always)]
269fn merge_refinement(mut base: StyleRefinement, overlay: StyleRefinement) -> StyleRefinement {
270    if overlay.bg.is_some() {
271        base.bg = overlay.bg;
272    }
273    if overlay.border_width.is_some() {
274        base.border_width = overlay.border_width;
275    }
276    if overlay.border_color.is_some() {
277        base.border_color = overlay.border_color;
278    }
279    if overlay.text_color.is_some() {
280        base.text_color = overlay.text_color;
281    }
282    if overlay.opacity.is_some() {
283        base.opacity = overlay.opacity;
284    }
285    if overlay.rounded.is_some() {
286        base.rounded = overlay.rounded;
287    }
288    base
289}
290
291#[inline(always)]
292fn duration_ms_u16(d: Duration) -> u16 {
293    d.as_millis().min(u16::MAX as u128) as u16
294}
295
296/// 统一 `gpui-kit` / Tailwind 风格扩展修饰符 Trait(覆盖交互态闭包、过渡动画、溢出裁剪、容器文本继承、阴影、光标与外边距)。
297///
298/// 所有 `velox-base` 无样式原语与 `velox-component` 主题化组件均实现此 Trait,
299/// 使任意组件均可像 `div()` 一样自由编写 `.hover(|s| ...)`、`.transition_colors()`、`.overflow_hidden()`、`.text_xs()` 等链式语法。
300pub trait StyledExt: Styled + Sized {
301    /// 获取内部 [`StyleExt`] 可变引用。
302    fn style_ext_mut(&mut self) -> &mut StyleExt;
303
304    // --- 交互态闭包(Hover / Active / Focus) ---
305
306    /// GPUI 风格悬停态样式闭包(如 `.hover(|s| s.bg(rgb(0xf5f5f5)).border_color(rgb(0x171717)))`)。
307    #[inline(always)]
308    fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
309        let ext = self.style_ext_mut();
310        let refined = f(ext.hover_style.unwrap_or_default());
311        if !refined.is_empty() {
312            ext.hover_style = Some(refined);
313            ext.dirty = true;
314        }
315        self
316    }
317
318    /// GPUI 风格按下态样式闭包(如 `.active(|s| s.bg(rgb(0xe5e5e5)))`)。
319    #[inline(always)]
320    fn active(self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
321        self.active_style(f)
322    }
323
324    /// 按下态样式闭包显式别名(避免与少数自带 `.active(id)` 固有方法的导航组件冲突)。
325    #[inline(always)]
326    fn active_style(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
327        let ext = self.style_ext_mut();
328        let refined = f(ext.active_style.unwrap_or_default());
329        if !refined.is_empty() {
330            ext.active_style = Some(refined);
331            ext.dirty = true;
332        }
333        self
334    }
335
336    /// GPUI 风格聚焦态样式闭包(如 `.focus(|s| s.border_1().border_color(rgb(0x3b82f6)))`)。
337    #[inline(always)]
338    fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
339        let ext = self.style_ext_mut();
340        let refined = f(ext.focus_style.unwrap_or_default());
341        if !refined.is_empty() {
342            ext.focus_style = Some(refined);
343            ext.dirty = true;
344        }
345        self
346    }
347
348    // --- 溢出与滚动控制 ---
349
350    /// 启用溢出裁剪(等价 CSS `overflow: hidden`)。
351    #[inline(always)]
352    fn overflow_hidden(mut self) -> Self {
353        let ext = self.style_ext_mut();
354        ext.clip_children = Some(true);
355        ext.dirty = true;
356        self
357    }
358
359    /// 关闭溢出裁剪(等价 CSS `overflow: visible`)。
360    #[inline(always)]
361    fn overflow_visible(mut self) -> Self {
362        let ext = self.style_ext_mut();
363        ext.clip_children = Some(false);
364        ext.dirty = true;
365        self
366    }
367
368    /// 启用自动测量边界的滚动视口(等价 CSS `overflow: auto/scroll`)。
369    #[inline(always)]
370    fn overflow_scroll(mut self) -> Self {
371        let ext = self.style_ext_mut();
372        ext.scrollable_max = Some(i16::MAX);
373        ext.clip_children = Some(true);
374        ext.dirty = true;
375        self
376    }
377
378    /// 启用垂直方向自动滚动视口(等价 CSS `overflow-y: auto/scroll`)。
379    #[inline(always)]
380    fn overflow_y_scroll(mut self) -> Self {
381        let ext = self.style_ext_mut();
382        ext.scrollable_max = Some(i16::MAX);
383        ext.scroll_dir = Some(FlexDir::Col);
384        ext.clip_children = Some(true);
385        ext.dirty = true;
386        self
387    }
388
389    /// 启用水平方向自动滚动视口(等价 CSS `overflow-x: auto/scroll`)。
390    #[inline(always)]
391    fn overflow_x_scroll(mut self) -> Self {
392        let ext = self.style_ext_mut();
393        ext.scrollable_max = Some(i16::MAX);
394        ext.scroll_dir = Some(FlexDir::Row);
395        ext.clip_children = Some(true);
396        ext.dirty = true;
397        self
398    }
399
400    /// 启用指定最大滚动距离的可滚动视口。
401    #[inline(always)]
402    fn scrollable(mut self, max_scroll: i16) -> Self {
403        let ext = self.style_ext_mut();
404        ext.scrollable_max = Some(max_scroll.max(0));
405        ext.clip_children = Some(true);
406        ext.dirty = true;
407        self
408    }
409
410    // --- 过渡与缓动动画(Tailwind / GPUI 语法) ---
411
412    /// 默认过渡动画(150ms 颜色 + 不透明度 + 几何边界)。
413    #[inline(always)]
414    fn transition(self) -> Self {
415        self.animate_all(Duration::from_millis(150))
416    }
417
418    /// 颜色过渡简写(150ms 背景/边框/文本色平滑过渡)。
419    #[inline(always)]
420    fn transition_colors(self) -> Self {
421        self.animate_color(Duration::from_millis(150))
422    }
423
424    /// 不透明度过渡简写(150ms 淡入淡出)。
425    #[inline(always)]
426    fn transition_opacity(self) -> Self {
427        self.animate_opacity(Duration::from_millis(150))
428    }
429
430    /// 全属性过渡简写(150ms 颜色 + 边界 + 不透明度)。
431    #[inline(always)]
432    fn transition_all(self) -> Self {
433        self.animate_all(Duration::from_millis(150))
434    }
435
436    /// 显式禁用过渡动画(等价 CSS `transition: none`)。
437    #[inline(always)]
438    fn transition_none(mut self) -> Self {
439        let ext = self.style_ext_mut();
440        ext.transition = None;
441        ext.transition_cleared = true;
442        ext.dirty = true;
443        self
444    }
445
446    /// 设置颜色跨帧过渡时长。
447    #[inline(always)]
448    fn animate_color(mut self, duration: Duration) -> Self {
449        let ext = self.style_ext_mut();
450        let mut spec = ext.transition.unwrap_or_default();
451        spec.duration_ms = duration_ms_u16(duration);
452        spec.animate_color = true;
453        ext.transition = Some(spec);
454        ext.transition_cleared = false;
455        ext.dirty = true;
456        self
457    }
458
459    /// 设置背景色跨帧过渡时长。
460    #[inline(always)]
461    fn animate_bg(self, duration: Duration) -> Self {
462        self.animate_color(duration)
463    }
464
465    /// 设置几何边界跨帧过渡时长。
466    #[inline(always)]
467    fn animate_bounds(mut self, duration: Duration) -> Self {
468        let ext = self.style_ext_mut();
469        let mut spec = ext.transition.unwrap_or_default();
470        spec.duration_ms = duration_ms_u16(duration);
471        spec.animate_bounds = true;
472        ext.transition = Some(spec);
473        ext.transition_cleared = false;
474        ext.dirty = true;
475        self
476    }
477
478    /// 设置不透明度跨帧过渡时长。
479    #[inline(always)]
480    fn animate_opacity(mut self, duration: Duration) -> Self {
481        let ext = self.style_ext_mut();
482        let mut spec = ext.transition.unwrap_or_default();
483        spec.duration_ms = duration_ms_u16(duration);
484        spec.animate_opacity = true;
485        ext.transition = Some(spec);
486        ext.transition_cleared = false;
487        ext.dirty = true;
488        self
489    }
490
491    /// 设置全属性跨帧过渡时长。
492    #[inline(always)]
493    fn animate_all(mut self, duration: Duration) -> Self {
494        let ext = self.style_ext_mut();
495        let mut spec = ext.transition.unwrap_or_default();
496        spec.duration_ms = duration_ms_u16(duration);
497        spec.animate_color = true;
498        spec.animate_bounds = true;
499        spec.animate_opacity = true;
500        ext.transition = Some(spec);
501        ext.transition_cleared = false;
502        ext.dirty = true;
503        self
504    }
505
506    /// 调整过渡动画时长。
507    #[inline(always)]
508    fn duration(mut self, duration: Duration) -> Self {
509        let ext = self.style_ext_mut();
510        let mut spec = ext.transition.unwrap_or_default();
511        spec.duration_ms = duration_ms_u16(duration);
512        ext.transition = Some(spec);
513        ext.transition_cleared = false;
514        ext.dirty = true;
515        self
516    }
517
518    #[inline(always)] fn duration_75(self) -> Self { self.duration(Duration::from_millis(75)) }
519    #[inline(always)] fn duration_100(self) -> Self { self.duration(Duration::from_millis(100)) }
520    #[inline(always)] fn duration_150(self) -> Self { self.duration(Duration::from_millis(150)) }
521    #[inline(always)] fn duration_200(self) -> Self { self.duration(Duration::from_millis(200)) }
522    #[inline(always)] fn duration_300(self) -> Self { self.duration(Duration::from_millis(300)) }
523    #[inline(always)] fn duration_500(self) -> Self { self.duration(Duration::from_millis(500)) }
524
525    /// 调整过渡动画缓动曲线。
526    #[inline(always)]
527    fn ease(mut self, easing: Easing) -> Self {
528        let ext = self.style_ext_mut();
529        let mut spec = ext.transition.unwrap_or_default();
530        spec.easing = easing;
531        ext.transition = Some(spec);
532        ext.transition_cleared = false;
533        ext.dirty = true;
534        self
535    }
536
537    #[inline(always)] fn ease_linear(self) -> Self { self.ease(Easing::Linear) }
538    #[inline(always)] fn ease_in(self) -> Self { self.ease(Easing::EaseIn) }
539    #[inline(always)] fn ease_out(self) -> Self { self.ease(Easing::EaseOut) }
540    #[inline(always)] fn ease_in_out(self) -> Self { self.ease(Easing::EaseInOut) }
541    #[inline(always)] fn ease_spring(self) -> Self { self.ease(Easing::Spring) }
542
543    /// 设置动画延迟时间。
544    #[inline(always)]
545    fn delay(mut self, delay: Duration) -> Self {
546        let ext = self.style_ext_mut();
547        let mut spec = ext.transition.unwrap_or_default();
548        spec.delay_ms = duration_ms_u16(delay);
549        ext.transition = Some(spec);
550        ext.transition_cleared = false;
551        ext.dirty = true;
552        self
553    }
554
555    /// 声明动画循环模式。
556    #[inline(always)]
557    fn loop_mode(mut self, mode: LoopMode) -> Self {
558        let ext = self.style_ext_mut();
559        let mut spec = ext.transition.unwrap_or_default();
560        spec.loop_mode = mode;
561        ext.transition = Some(spec);
562        ext.transition_cleared = false;
563        ext.dirty = true;
564        self
565    }
566
567    /// 声明入场淡入动画。
568    #[inline(always)]
569    fn fade_in(mut self, duration: Duration) -> Self {
570        let ext = self.style_ext_mut();
571        let mut spec = ext.transition.unwrap_or_default();
572        spec.duration_ms = duration_ms_u16(duration);
573        spec.enter = Some(EnterFrom { opacity: 0, dx: 0, dy: 0 });
574        ext.transition = Some(spec);
575        ext.transition_cleared = false;
576        ext.dirty = true;
577        self
578    }
579
580    /// 声明入场滑入淡入动画。
581    #[inline(always)]
582    fn slide_in(mut self, dx: i16, dy: i16, duration: Duration) -> Self {
583        let ext = self.style_ext_mut();
584        let mut spec = ext.transition.unwrap_or_default();
585        spec.duration_ms = duration_ms_u16(duration);
586        spec.enter = Some(EnterFrom { opacity: 0, dx, dy });
587        ext.transition = Some(spec);
588        ext.transition_cleared = false;
589        ext.dirty = true;
590        self
591    }
592
593    // --- 容器级可继承文本样式 ---
594
595    /// 设置容器级可继承文本颜色。
596    #[inline(always)]
597    fn text_color(mut self, color: Color) -> Self {
598        let ext = self.style_ext_mut();
599        ext.text_color = Some(color);
600        ext.dirty = true;
601        self
602    }
603
604    /// 设置容器级可继承字号(像素)。
605    #[inline(always)]
606    fn text_size(mut self, px: u8) -> Self {
607        let ext = self.style_ext_mut();
608        ext.text_size = Some(px);
609        ext.dirty = true;
610        self
611    }
612
613    #[inline(always)] fn text_xs(self) -> Self { self.text_size(11) }
614    #[inline(always)] fn text_sm(self) -> Self { self.text_size(12) }
615    #[inline(always)] fn text_base(self) -> Self { self.text_size(14) }
616    #[inline(always)] fn text_lg(self) -> Self { self.text_size(16) }
617    #[inline(always)] fn text_xl(self) -> Self { self.text_size(18) }
618    #[inline(always)] fn text_2xl(self) -> Self { self.text_size(22) }
619    #[inline(always)] fn text_3xl(self) -> Self { self.text_size(28) }
620
621    // --- 不透明度、阴影与光标 ---
622
623    /// 设置组件整体不透明度(`0..=255`)。
624    #[inline(always)]
625    fn opacity(mut self, alpha: u8) -> Self {
626        let ext = self.style_ext_mut();
627        ext.opacity = Some(alpha);
628        ext.dirty = true;
629        self
630    }
631
632    /// 浮点不透明度简写(`0.0..=1.0`)。
633    #[inline(always)]
634    fn opacity_f(self, alpha: f32) -> Self {
635        let c = if alpha <= 0.0 {
636            0.0
637        } else if alpha >= 1.0 {
638            1.0
639        } else {
640            alpha
641        };
642        self.opacity((c * 255.0 + 0.5) as u8)
643    }
644
645    /// 设置解析式 SDF 外阴影。
646    #[inline(always)]
647    fn shadow(mut self, blur: u8, offset_x: i8, offset_y: i8, color: Color) -> Self {
648        let ext = self.style_ext_mut();
649        if blur > 0 && color.a() > 0 {
650            ext.shadow = Some((blur.min(64), offset_x, offset_y, color));
651            ext.shadow_cleared = false;
652            ext.dirty = true;
653        }
654        self
655    }
656
657    #[inline(always)]
658    fn shadow_none(mut self) -> Self {
659        let ext = self.style_ext_mut();
660        ext.shadow = None;
661        ext.shadow_cleared = true;
662        ext.dirty = true;
663        self
664    }
665
666    #[inline(always)] fn shadow_sm(self) -> Self { self.shadow(4, 0, 1, Color::rgba(0, 0, 0, 48)) }
667    #[inline(always)] fn shadow_md(self) -> Self { self.shadow(10, 0, 3, Color::rgba(0, 0, 0, 72)) }
668    #[inline(always)] fn shadow_lg(self) -> Self { self.shadow(18, 0, 6, Color::rgba(0, 0, 0, 96)) }
669    #[inline(always)] fn shadow_xl(self) -> Self { self.shadow(28, 0, 10, Color::rgba(0, 0, 0, 120)) }
670
671    /// 显式设置鼠标悬停光标形状。
672    #[inline(always)]
673    fn cursor(self, cursor: CursorShape) -> Self {
674        self.cursor_shape(cursor)
675    }
676
677    /// 鼠标光标形状显式别名(避免与 `TextInput::cursor(byte_offset)` 冲突)。
678    #[inline(always)]
679    fn cursor_shape(mut self, cursor: CursorShape) -> Self {
680        let ext = self.style_ext_mut();
681        ext.cursor = Some(cursor);
682        ext.dirty = true;
683        self
684    }
685
686    #[inline(always)] fn cursor_pointer(self) -> Self { self.cursor_shape(CursorShape::Pointer) }
687    #[inline(always)] fn cursor_default(self) -> Self { self.cursor_shape(CursorShape::Default) }
688    #[inline(always)] fn cursor_text(self) -> Self { self.cursor_shape(CursorShape::Text) }
689    #[inline(always)] fn cursor_not_allowed(self) -> Self { self.cursor_shape(CursorShape::NotAllowed) }
690    #[inline(always)] fn cursor_grab(self) -> Self { self.cursor_shape(CursorShape::Grab) }
691    #[inline(always)] fn cursor_grabbing(self) -> Self { self.cursor_shape(CursorShape::Grabbing) }
692
693    // --- 外边距与最小/最大尺寸约束 ---
694
695    #[inline(always)]
696    fn mx(mut self, px: i16) -> Self {
697        let ext = self.style_ext_mut();
698        ext.margin.left = px;
699        ext.margin.right = px;
700        ext.dirty = true;
701        self
702    }
703
704    #[inline(always)]
705    fn my(mut self, px: i16) -> Self {
706        let ext = self.style_ext_mut();
707        ext.margin.top = px;
708        ext.margin.bottom = px;
709        ext.dirty = true;
710        self
711    }
712
713    #[inline(always)]
714    fn mt(mut self, px: i16) -> Self {
715        let ext = self.style_ext_mut();
716        ext.margin.top = px;
717        ext.dirty = true;
718        self
719    }
720
721    #[inline(always)]
722    fn mb(mut self, px: i16) -> Self {
723        let ext = self.style_ext_mut();
724        ext.margin.bottom = px;
725        ext.dirty = true;
726        self
727    }
728
729    #[inline(always)]
730    fn ml(mut self, px: i16) -> Self {
731        let ext = self.style_ext_mut();
732        ext.margin.left = px;
733        ext.dirty = true;
734        self
735    }
736
737    #[inline(always)]
738    fn mr(mut self, px: i16) -> Self {
739        let ext = self.style_ext_mut();
740        ext.margin.right = px;
741        ext.dirty = true;
742        self
743    }
744
745    #[inline(always)] fn mx_0(self) -> Self { self.mx(0) }
746    #[inline(always)] fn mx_1(self) -> Self { self.mx(4) }
747    #[inline(always)] fn mx_2(self) -> Self { self.mx(8) }
748    #[inline(always)] fn mx_3(self) -> Self { self.mx(12) }
749    #[inline(always)] fn mx_4(self) -> Self { self.mx(16) }
750    #[inline(always)] fn my_0(self) -> Self { self.my(0) }
751    #[inline(always)] fn my_1(self) -> Self { self.my(4) }
752    #[inline(always)] fn my_2(self) -> Self { self.my(8) }
753    #[inline(always)] fn my_3(self) -> Self { self.my(12) }
754    #[inline(always)] fn my_4(self) -> Self { self.my(16) }
755    #[inline(always)] fn mt_1(self) -> Self { self.mt(4) }
756    #[inline(always)] fn mt_2(self) -> Self { self.mt(8) }
757    #[inline(always)] fn mt_3(self) -> Self { self.mt(12) }
758    #[inline(always)] fn mt_4(self) -> Self { self.mt(16) }
759    #[inline(always)] fn mb_1(self) -> Self { self.mb(4) }
760    #[inline(always)] fn mb_2(self) -> Self { self.mb(8) }
761    #[inline(always)] fn mb_3(self) -> Self { self.mb(12) }
762    #[inline(always)] fn mb_4(self) -> Self { self.mb(16) }
763    #[inline(always)] fn ml_1(self) -> Self { self.ml(4) }
764    #[inline(always)] fn ml_2(self) -> Self { self.ml(8) }
765    #[inline(always)] fn ml_3(self) -> Self { self.ml(12) }
766    #[inline(always)] fn ml_4(self) -> Self { self.ml(16) }
767    #[inline(always)] fn mr_1(self) -> Self { self.mr(4) }
768    #[inline(always)] fn mr_2(self) -> Self { self.mr(8) }
769    #[inline(always)] fn mr_3(self) -> Self { self.mr(12) }
770    #[inline(always)] fn mr_4(self) -> Self { self.mr(16) }
771
772    #[inline(always)]
773    fn min_w(mut self, px: i16) -> Self {
774        let ext = self.style_ext_mut();
775        ext.min_w = px.max(0);
776        ext.dirty = true;
777        self
778    }
779
780    #[inline(always)]
781    fn max_w(mut self, px: i16) -> Self {
782        let ext = self.style_ext_mut();
783        ext.max_w = px.max(0);
784        ext.dirty = true;
785        self
786    }
787
788    #[inline(always)]
789    fn min_h(mut self, px: i16) -> Self {
790        let ext = self.style_ext_mut();
791        ext.min_h = px.max(0);
792        ext.dirty = true;
793        self
794    }
795
796    #[inline(always)]
797    fn max_h(mut self, px: i16) -> Self {
798        let ext = self.style_ext_mut();
799        ext.max_h = px.max(0);
800        ext.dirty = true;
801        self
802    }
803
804    // --- 自身对齐、层级与语义显示标记 ---
805
806    #[inline(always)]
807    fn align_self(mut self, align: AlignItems) -> Self {
808        let ext = self.style_ext_mut();
809        ext.align_self = Some(align);
810        ext.dirty = true;
811        self
812    }
813
814    #[inline(always)] fn self_start(self) -> Self { self.align_self(AlignItems::Start) }
815    #[inline(always)] fn self_center(self) -> Self { self.align_self(AlignItems::Center) }
816    #[inline(always)] fn self_end(self) -> Self { self.align_self(AlignItems::End) }
817    #[inline(always)] fn self_stretch(self) -> Self { self.align_self(AlignItems::Stretch) }
818
819    #[inline(always)]
820    fn items_baseline(mut self) -> Self {
821        let ext = self.style_ext_mut();
822        ext.items_baseline = true;
823        ext.dirty = true;
824        self
825    }
826
827    #[inline(always)]
828    fn z_index(mut self, z: i8) -> Self {
829        let ext = self.style_ext_mut();
830        ext.z_index = Some(z);
831        ext.dirty = true;
832        self
833    }
834
835    /// 语义化相对定位标记(Velox 子节点天然相对父容器定位)。
836    #[inline(always)]
837    fn relative(self) -> Self {
838        self
839    }
840
841    /// 块级垂直容器简写(等价 `.flex_col()`)。
842    #[inline(always)]
843    fn block(self) -> Self {
844        self.flex_col()
845    }
846
847    /// 行内弹性容器简写(等价 `.flex_row()`)。
848    #[inline(always)]
849    fn inline_flex(self) -> Self {
850        self.flex_row()
851    }
852}