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