Skip to main content

velox_engine/
style.rs

1//! 24 字节紧凑样式结构体 `Style` 与 Tailwind 风格链式修饰符 `Styled`。
2
3/// ARGB8888 32 位颜色表示(`0xAARRGGBB`)。
4#[repr(transparent)]
5#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
6pub struct Color(pub u32);
7
8impl Color {
9    /// 完全透明色。
10    pub const TRANSPARENT: Self = Self(0x0000_0000);
11    /// 纯白色。
12    pub const WHITE: Self = Self(0xFFFF_FFFF);
13    /// 纯黑色。
14    pub const BLACK: Self = Self(0xFF00_0000);
15
16    /// 从不透明 RGB 分量构造颜色。
17    #[inline(always)]
18    pub const fn rgb(r: u8, g: u8, b: u8) -> Self {
19        Self::rgba(r, g, b, 255)
20    }
21
22    /// 从 RGBA 分量构造颜色。
23    #[inline(always)]
24    pub const fn rgba(r: u8, g: u8, b: u8, a: u8) -> Self {
25        Self(((a as u32) << 24) | ((r as u32) << 16) | ((g as u32) << 8) | (b as u32))
26    }
27
28    /// 从 24 位十六进制 RGB 整数构造不透明颜色(如 `Color::hex(0xd4d4d4)`)。
29    #[inline(always)]
30    pub const fn hex(rgb: u32) -> Self {
31        Self(0xFF00_0000 | (rgb & 0x00FF_FFFF))
32    }
33
34    /// 从 32 位十六进制 `0xRRGGBBAA` 整数构造颜色(如 `Color::hex_alpha(0x171717cc)`)。
35    #[inline(always)]
36    pub const fn hex_alpha(rgba: u32) -> Self {
37        let r = ((rgba >> 24) & 0xFF) as u8;
38        let g = ((rgba >> 16) & 0xFF) as u8;
39        let b = ((rgba >> 8) & 0xFF) as u8;
40        let a = (rgba & 0xFF) as u8;
41        Self::rgba(r, g, b, a)
42    }
43
44    /// Alpha 通道(`0..=255`)。
45    #[inline(always)]
46    pub const fn a(self) -> u8 {
47        ((self.0 >> 24) & 0xFF) as u8
48    }
49
50    /// Red 通道(`0..=255`)。
51    #[inline(always)]
52    pub const fn r(self) -> u8 {
53        ((self.0 >> 16) & 0xFF) as u8
54    }
55
56    /// Green 通道(`0..=255`)。
57    #[inline(always)]
58    pub const fn g(self) -> u8 {
59        ((self.0 >> 8) & 0xFF) as u8
60    }
61
62    /// Blue 通道(`0..=255`)。
63    #[inline(always)]
64    pub const fn b(self) -> u8 {
65        (self.0 & 0xFF) as u8
66    }
67
68    /// 定点数颜色线性插值(`t_256` 取值范围 `0..=256`)。
69    #[inline]
70    pub const fn lerp(self, other: Self, t_256: u16) -> Self {
71        let t = if t_256 > 256 { 256 } else { t_256 as u32 };
72        let inv = 256 - t;
73        let a = ((self.a() as u32 * inv + other.a() as u32 * t) >> 8) as u8;
74        let r = ((self.r() as u32 * inv + other.r() as u32 * t) >> 8) as u8;
75        let g = ((self.g() as u32 * inv + other.g() as u32 * t) >> 8) as u8;
76        let b = ((self.b() as u32 * inv + other.b() as u32 * t) >> 8) as u8;
77        Self::rgba(r, g, b, a)
78    }
79
80    /// 按给定不透明度 `opacity`(`0..=255`)调制当前颜色的 Alpha 通道。
81    #[inline(always)]
82    pub const fn scale_alpha(self, opacity: u8) -> Self {
83        if opacity == 255 {
84            return self;
85        }
86        if opacity == 0 {
87            return Self::TRANSPARENT;
88        }
89        let a = ((self.a() as u16 * opacity as u16) / 255) as u8;
90        Self::rgba(self.r(), self.g(), self.b(), a)
91    }
92}
93
94/// 动画缓动曲线类型。
95#[repr(u8)]
96#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
97pub enum Easing {
98    /// 线性匀速插值。
99    Linear = 0,
100    /// 三次方渐入(加速)。
101    EaseIn = 1,
102    /// 三次方渐出(减速,默认)。
103    #[default]
104    EaseOut = 2,
105    /// 三次方渐入渐出(S 型曲线)。
106    EaseInOut = 3,
107    /// 欠阻尼弹簧过冲曲线(带约 8% 物理回弹质感)。
108    Spring = 4,
109    /// 回拉过冲渐出(`easeOutBack`,末端约 10% 过冲后回落,适合弹窗/卡片入场)。
110    EaseOutBack = 5,
111    /// 落地弹跳渐出(`easeOutBounce`,模拟小球落地多次弹跳,结果不超出 `0..=256`)。
112    Bounce = 6,
113    /// 弹性振荡渐出(`easeOutElastic`,指数衰减正弦振荡,适合提醒/抖动效果)。
114    Elastic = 7,
115}
116
117impl Easing {
118    /// 从低 3 位编码还原缓动曲线(与 `Easing as u8` 互逆)。
119    #[inline(always)]
120    pub const fn from_bits(bits: u8) -> Self {
121        match bits & 0x07 {
122            0 => Self::Linear,
123            1 => Self::EaseIn,
124            2 => Self::EaseOut,
125            3 => Self::EaseInOut,
126            4 => Self::Spring,
127            5 => Self::EaseOutBack,
128            6 => Self::Bounce,
129            _ => Self::Elastic,
130        }
131    }
132
133    /// 纯整数定点缓动求值:输入归一化进度 `t_256`(`0..=256`),输出 `0..=256`
134    /// (`Spring` / `EaseOutBack` / `Elastic` 允许短暂过冲超出 `256` 或低于 `0`)。
135    #[inline]
136    pub fn apply_256(self, t_256: i32) -> i32 {
137        let t = t_256.clamp(0, 256);
138        if t >= 256 {
139            return 256;
140        }
141        match self {
142            Self::Linear => t,
143            Self::EaseIn => (t * t * t) >> 16,
144            Self::EaseOut => {
145                let inv = 256 - t;
146                256 - ((inv * inv * inv) >> 16)
147            }
148            Self::EaseInOut => {
149                if t < 128 {
150                    (4 * t * t * t) >> 16
151                } else {
152                    let inv = 256 - t;
153                    256 - ((4 * inv * inv * inv) >> 16)
154                }
155            }
156            Self::Spring => {
157                // 二阶欠阻尼弹簧拟合:快速上升并在 t ≈ 180..200 时产生约 +20..26/256 (8%~10%) 的过冲随后收敛至 256
158                let inv = 256 - t;
159                let base = 256 - ((inv * inv) >> 8);
160                let overshoot = ((9 * (t as i64) * (t as i64) * (inv as i64)) >> 19) as i32;
161                base + overshoot
162            }
163            Self::EaseOutBack => {
164                // 1 + c3·(t-1)³ + c1·(t-1)²,c1 ≈ 1.70158 (436/256),c3 ≈ 2.70158 (692/256)
165                let u = (t - 256) as i64;
166                let cubic = (692 * u * u * u) >> 24;
167                let quad = (436 * u * u) >> 16;
168                (256 + cubic + quad) as i32
169            }
170            Self::Bounce => {
171                // 标准 easeOutBounce 分段抛物线(n1 = 7.5625 = 121/16,d1 = 2.75)
172                let seg = |x: i32, base: i32| ((121 * x * x) >> 12) + base;
173                let v = if t < 93 {
174                    seg(t, 0)
175                } else if t < 186 {
176                    seg(t - 140, 192)
177                } else if t < 233 {
178                    seg(t - 209, 240)
179                } else {
180                    seg(t - 244, 252)
181                };
182                v.min(256)
183            }
184            Self::Elastic => {
185                // 2^(-10t) · sin((10t - 0.75) · 2π/3) + 1
186                let e = 10 * t; // 以 1/256 为单位的指数
187                let k = (e >> 8) as u32;
188                let frac = e & 0xFF;
189                // 2^-k 与 2^-(k+1) 之间线性插值近似 2^-(k + frac)
190                let hi = 256i32 >> k.min(16);
191                let decay = hi - ((hi >> 1) * frac >> 8);
192                let deg10 = (t * 12000) / 256 - 900;
193                let (sin_q10, _) = crate::sdf::sin_cos_deg10(deg10);
194                256 + ((decay * sin_q10) >> 10)
195            }
196        }
197    }
198
199    /// 按已流逝时间 `elapsed_ms` / 总时长 `duration_ms` 求值缓动输出(`0..=256` 定点数)。
200    #[inline]
201    pub fn sample_256(self, elapsed_ms: u32, duration_ms: u32) -> i32 {
202        if duration_ms == 0 || elapsed_ms >= duration_ms {
203            return 256;
204        }
205        self.apply_256(((elapsed_ms as u64 * 256) / duration_ms as u64) as i32)
206    }
207}
208
209/// 动画循环模式。
210#[repr(u8)]
211#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
212pub enum LoopMode {
213    /// 单次过渡动画(默认)。
214    #[default]
215    Once = 0,
216    /// 无限单向循环(`0 -> 1 -> 0 -> 1`)。
217    Infinite = 1,
218    /// 无限往复呼吸循环(`0 -> 1 -> 0`)。
219    PingPong = 2,
220}
221
222/// 文本水平对齐方式。
223#[repr(u8)]
224#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
225pub enum TextAlign {
226    /// 左对齐(默认)。
227    #[default]
228    Left = 0,
229    /// 水平居中对齐。
230    Center = 1,
231    /// 右对齐。
232    Right = 2,
233}
234
235/// 尺寸长度单位(严格 4 字节表示)。
236#[repr(C, i16)]
237#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
238pub enum Length {
239    /// 由内容自适应决定尺寸。
240    #[default]
241    Auto = 0,
242    /// 固定像素长度。
243    Px(i16) = 1,
244    /// 撑满父容器可用空间(100%)。
245    Full = 2,
246    /// 按父容器可用空间的百分比(`0..=100`)计算长度。
247    Pct(i16) = 3,
248}
249
250/// 四边边距(4 字节当 `T = i8`)。
251#[repr(C)]
252#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
253pub struct Edges<T> {
254    pub top: T,
255    pub right: T,
256    pub bottom: T,
257    pub left: T,
258}
259
260impl<T: Copy> Edges<T> {
261    /// 四边统一边距。
262    #[inline(always)]
263    pub const fn all(val: T) -> Self {
264        Self {
265            top: val,
266            right: val,
267            bottom: val,
268            left: val,
269        }
270    }
271
272    /// 水平与垂直对称边距。
273    #[inline(always)]
274    pub const fn xy(x: T, y: T) -> Self {
275        Self {
276            top: y,
277            right: x,
278            bottom: y,
279            left: x,
280        }
281    }
282}
283
284/// 主轴排列方向(1 bit)。
285#[derive(Copy, Clone, PartialEq, Eq, Debug)]
286pub enum FlexDir {
287    Row = 0,
288    Col = 1,
289}
290
291/// 主轴对齐方式(3 bits)。
292#[derive(Copy, Clone, PartialEq, Eq, Debug)]
293pub enum Justify {
294    Start = 0,
295    Center = 1,
296    End = 2,
297    Between = 3,
298    Around = 4,
299}
300
301/// 交叉轴对齐方式(2 bits,默认 `0 = Stretch` 遵循 CSS Flexbox 规范)。
302#[derive(Copy, Clone, PartialEq, Eq, Debug)]
303pub enum AlignItems {
304    Stretch = 0,
305    Center = 1,
306    Start = 2,
307    End = 3,
308}
309
310/// 紧凑位域样式结构体(严格 **24 字节**)。
311#[repr(C)]
312#[derive(Copy, Clone, PartialEq, Eq, Debug)]
313pub struct Style {
314    /// 宽度策略(4 Bytes)。
315    pub width: Length,
316    /// 高度策略(4 Bytes)。
317    pub height: Length,
318    /// 四周内边距(4 Bytes)。
319    pub padding: Edges<i8>,
320    /// 背景填充色(4 Bytes)。
321    pub bg: Color,
322    /// 边框颜色(4 Bytes)。
323    pub border_color: Color,
324    /// 子节点间距(1 Byte)。
325    pub gap: i8,
326    /// 圆角半径 `0..=127px`(1 Byte)。
327    pub rounded: u8,
328    /// 边框宽度(1 Byte)。
329    pub border_width: u8,
330    /// 位域:`flex_dir(1b) | justify(3b) | items_align(2b) | flex_grow(2b)`(1 Byte)。
331    pub flex_flags: u8,
332}
333
334const _: () = assert!(core::mem::size_of::<Style>() == 24);
335
336impl Default for Style {
337    #[inline(always)]
338    fn default() -> Self {
339        Self::DEFAULT
340    }
341}
342
343impl Style {
344    /// 全零默认值样式常量(供所有组件 `const fn new` 零成本初始化)。
345    pub const DEFAULT: Self = Self::new();
346
347    /// 构造全默认样式。
348    #[inline(always)]
349    pub const fn new() -> Self {
350        Self {
351            width: Length::Auto,
352            height: Length::Auto,
353            padding: Edges::all(0),
354            bg: Color::TRANSPARENT,
355            border_color: Color::TRANSPARENT,
356            gap: 0,
357            rounded: 0,
358            border_width: 0,
359            flex_flags: 0,
360        }
361    }
362
363    /// 设置水平主轴 `Row`。
364    #[inline(always)]
365    pub fn set_row(&mut self) {
366        self.flex_flags &= !0b0000_0001;
367    }
368
369    /// 设置垂直主轴 `Col`。
370    #[inline(always)]
371    pub fn set_col(&mut self) {
372        self.flex_flags |= 0b0000_0001;
373    }
374
375    /// 读取主轴方向。
376    #[inline(always)]
377    pub const fn flex_dir(&self) -> FlexDir {
378        if (self.flex_flags & 0b0000_0001) != 0 {
379            FlexDir::Col
380        } else {
381            FlexDir::Row
382        }
383    }
384
385    /// 设置主轴对齐方式。
386    #[inline(always)]
387    pub fn set_justify(&mut self, justify: Justify) {
388        self.flex_flags = (self.flex_flags & !0b0000_1110) | ((justify as u8 & 0b111) << 1);
389    }
390
391    /// 设置主轴居中对齐。
392    #[inline(always)]
393    pub fn set_justify_center(&mut self) {
394        self.set_justify(Justify::Center);
395    }
396
397    /// 设置主轴两端对齐。
398    #[inline(always)]
399    pub fn set_justify_between(&mut self) {
400        self.set_justify(Justify::Between);
401    }
402
403    /// 读取主轴对齐方式。
404    #[inline(always)]
405    pub const fn justify(&self) -> Justify {
406        match (self.flex_flags >> 1) & 0b111 {
407            1 => Justify::Center,
408            2 => Justify::End,
409            3 => Justify::Between,
410            4 => Justify::Around,
411            _ => Justify::Start,
412        }
413    }
414
415    /// 设置交叉轴对齐方式。
416    #[inline(always)]
417    pub fn set_items_align(&mut self, align: AlignItems) {
418        self.flex_flags = (self.flex_flags & !0b0011_0000) | ((align as u8 & 0b11) << 4);
419    }
420
421    /// 设置交叉轴居中对齐。
422    #[inline(always)]
423    pub fn set_items_center(&mut self) {
424        self.set_items_align(AlignItems::Center);
425    }
426
427    /// 读取交叉轴对齐方式。
428    #[inline(always)]
429    pub const fn items_align(&self) -> AlignItems {
430        match (self.flex_flags >> 4) & 0b11 {
431            1 => AlignItems::Center,
432            2 => AlignItems::Start,
433            3 => AlignItems::End,
434            _ => AlignItems::Stretch,
435        }
436    }
437
438    /// 设置弹性拉伸权重(`0..=3`)。
439    #[inline(always)]
440    pub fn set_grow(&mut self, weight: u8) {
441        let w = if weight > 3 { 3 } else { weight };
442        self.flex_flags = (self.flex_flags & !0b1100_0000) | (w << 6);
443    }
444
445    /// 读取弹性拉伸权重(`0..=3`)。
446    #[inline(always)]
447    pub const fn flex_grow(&self) -> u8 {
448        (self.flex_flags >> 6) & 0b11
449    }
450
451    /// 读取圆角半径(`0..=127px`,剥离最高位 `flex_wrap` 标志)。
452    #[inline(always)]
453    pub const fn rounded_radius(&self) -> u8 {
454        self.rounded & 0x7F
455    }
456
457    /// 设置圆角半径(`0..=127px`,保留最高位 `flex_wrap` 标志)。
458    #[inline(always)]
459    pub fn set_rounded(&mut self, radius: u8) {
460        self.rounded = (self.rounded & 0x80) | (radius.min(127) & 0x7F);
461    }
462
463    /// 是否启用多行自动折行(`flex_wrap`,存储在 `rounded` 的第 7 位)。
464    #[inline(always)]
465    pub const fn flex_wrap(&self) -> bool {
466        (self.rounded & 0x80) != 0
467    }
468
469    /// 设置多行自动折行(`flex_wrap`)。
470    #[inline(always)]
471    pub fn set_flex_wrap(&mut self, wrap: bool) {
472        if wrap {
473            self.rounded |= 0x80;
474        } else {
475            self.rounded &= 0x7F;
476        }
477    }
478
479    /// 读取边框宽度(`0..=15px`,存储在 `border_width` 低 4 位)。
480    #[inline(always)]
481    pub const fn border_px(&self) -> u8 {
482        self.border_width & 0x0F
483    }
484
485    /// 设置边框宽度(`0..=15px`)。
486    #[inline(always)]
487    pub fn set_border_px(&mut self, width: u8) {
488        self.border_width = (self.border_width & 0xF0) | (width.min(15) & 0x0F);
489    }
490
491    /// 读取四边统一外边距(`0..=15px`,存储在 `border_width` 高 4 位)。
492    #[inline(always)]
493    pub const fn margin_px(&self) -> i8 {
494        ((self.border_width >> 4) & 0x0F) as i8
495    }
496
497    /// 设置四边统一外边距(`0..=15px`)。
498    #[inline(always)]
499    pub fn set_margin_px(&mut self, px: i8) {
500        let m = px.clamp(0, 15) as u8;
501        self.border_width = (self.border_width & 0x0F) | (m << 4);
502    }
503}
504
505/// GPUI 风格像素单位辅助函数(四舍五入转换为 `i16`,兼容 `.w(px(250.))` 与 `.w(250)`)。
506#[inline(always)]
507pub const fn px(val: f32) -> i16 {
508    if val >= 0.0 {
509        (val + 0.5) as i16
510    } else {
511        (val - 0.5) as i16
512    }
513}
514
515/// GPUI 风格 24 位十六进制 RGB 颜色构造函数(如 `rgb(0xd4d4d4)`)。
516#[inline(always)]
517pub const fn rgb(hex: u32) -> Color {
518    Color::hex(hex)
519}
520
521/// GPUI 风格 32 位十六进制 `0xRRGGBBAA` 颜色构造函数(如 `rgba(0x171717cc)`)。
522#[inline(always)]
523pub const fn rgba(hex: u32) -> Color {
524    Color::hex_alpha(hex)
525}
526
527/// GPUI 风格条件链式构建 Trait(对所有 `Sized` 类型自动实现,零运行时开销)。
528///
529/// 支持在任意 `Div`、`Text`、组件或 `StyleRefinement` 上直接使用 `.when(...)` / `.when_some(...)`:
530/// ```ignore
531/// item.size_8()
532///     .when(state.is_active(), |s| s.bg(rgb(0x171717)).text_color(rgb(0xffffff)))
533///     .when(!state.is_disabled(), |s| s.hover(|h| h.bg(rgb(0xf5f5f5))))
534/// ```
535pub trait FluentBuilder: Sized {
536    /// 当 `condition` 为 `true` 时执行闭包修饰自身,否则原样返回。
537    #[inline(always)]
538    fn when(self, condition: bool, then: impl FnOnce(Self) -> Self) -> Self {
539        if condition {
540            then(self)
541        } else {
542            self
543        }
544    }
545
546    /// 根据 `condition` 在两个修饰分支之间二选一。
547    #[inline(always)]
548    fn when_else(
549        self,
550        condition: bool,
551        then: impl FnOnce(Self) -> Self,
552        else_fn: impl FnOnce(Self) -> Self,
553    ) -> Self {
554        if condition {
555            then(self)
556        } else {
557            else_fn(self)
558        }
559    }
560
561    /// 当 `option` 为 `Some(value)` 时执行闭包修饰自身,否则原样返回。
562    #[inline(always)]
563    fn when_some<O>(self, option: Option<O>, then: impl FnOnce(Self, O) -> Self) -> Self {
564        match option {
565            Some(value) => then(self, value),
566            None => self,
567        }
568    }
569
570    /// 当 `option` 为 `None` 时执行闭包修饰自身,否则原样返回。
571    #[inline(always)]
572    fn when_none<O>(self, option: &Option<O>, then: impl FnOnce(Self) -> Self) -> Self {
573        if option.is_none() {
574            then(self)
575        } else {
576            self
577        }
578    }
579
580    /// 通用管道映射变换(避免与 `Iterator::map` / `Option::map` 重名)。
581    #[inline(always)]
582    fn apply<U>(self, f: impl FnOnce(Self) -> U) -> U {
583        f(self)
584    }
585}
586
587impl<T: Sized> FluentBuilder for T {}
588
589/// 交互态样式覆写增量(供 `Div::hover(|s| ...)`、`Div::active(|s| ...)`、`Div::focus(|s| ...)` 使用,零堆分配)。
590#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
591pub struct StyleRefinement {
592    pub bg: Option<Color>,
593    pub border_width: Option<u8>,
594    pub border_color: Option<Color>,
595    pub text_color: Option<Color>,
596    pub opacity: Option<u8>,
597    pub rounded: Option<u8>,
598}
599
600impl StyleRefinement {
601    /// 创建空的交互态样式增量。
602    #[inline(always)]
603    pub const fn new() -> Self {
604        Self {
605            bg: None,
606            border_width: None,
607            border_color: None,
608            text_color: None,
609            opacity: None,
610            rounded: None,
611        }
612    }
613
614    /// 是否未配置任何视觉覆写。
615    #[inline(always)]
616    pub const fn is_empty(&self) -> bool {
617        self.bg.is_none()
618            && self.border_width.is_none()
619            && self.border_color.is_none()
620            && self.text_color.is_none()
621            && self.opacity.is_none()
622            && self.rounded.is_none()
623    }
624
625    /// 是否仅覆写了背景色(可直接走 `DivNode` 内联零分配快速路径)。
626    #[inline(always)]
627    pub const fn is_bg_only(&self) -> bool {
628        self.bg.is_some()
629            && self.border_width.is_none()
630            && self.border_color.is_none()
631            && self.text_color.is_none()
632            && self.opacity.is_none()
633            && self.rounded.is_none()
634    }
635
636    #[inline(always)]
637    pub const fn bg(mut self, color: Color) -> Self {
638        self.bg = Some(color);
639        self
640    }
641
642    #[inline(always)]
643    pub const fn border(mut self, width: u8, color: Color) -> Self {
644        self.border_width = Some(width);
645        self.border_color = Some(color);
646        self
647    }
648
649    #[inline(always)]
650    pub const fn border_0(mut self) -> Self {
651        self.border_width = Some(0);
652        self
653    }
654
655    #[inline(always)]
656    pub const fn border_1(mut self) -> Self {
657        self.border_width = Some(1);
658        self
659    }
660
661    #[inline(always)]
662    pub const fn border_2(mut self) -> Self {
663        self.border_width = Some(2);
664        self
665    }
666
667    #[inline(always)]
668    pub const fn border_4(mut self) -> Self {
669        self.border_width = Some(4);
670        self
671    }
672
673    #[inline(always)]
674    pub const fn border_color(mut self, color: Color) -> Self {
675        self.border_color = Some(color);
676        self
677    }
678
679    #[inline(always)]
680    pub const fn text_color(mut self, color: Color) -> Self {
681        self.text_color = Some(color);
682        self
683    }
684
685    #[inline(always)]
686    pub const fn opacity(mut self, alpha: u8) -> Self {
687        self.opacity = Some(alpha);
688        self
689    }
690
691    #[inline(always)]
692    pub const fn opacity_f(mut self, alpha: f32) -> Self {
693        let clamped = if alpha <= 0.0 {
694            0.0
695        } else if alpha >= 1.0 {
696            1.0
697        } else {
698            alpha
699        };
700        self.opacity = Some((clamped * 255.0 + 0.5) as u8);
701        self
702    }
703
704    #[inline(always)]
705    pub const fn rounded(mut self, radius: u8) -> Self {
706        self.rounded = Some(radius);
707        self
708    }
709
710    #[inline(always)]
711    pub const fn rounded_none(self) -> Self {
712        self.rounded(0)
713    }
714
715    #[inline(always)]
716    pub const fn rounded_sm(self) -> Self {
717        self.rounded(4)
718    }
719
720    #[inline(always)]
721    pub const fn rounded_md(self) -> Self {
722        self.rounded(6)
723    }
724
725    #[inline(always)]
726    pub const fn rounded_lg(self) -> Self {
727        self.rounded(8)
728    }
729
730    #[inline(always)]
731    pub const fn rounded_xl(self) -> Self {
732        self.rounded(12)
733    }
734
735    #[inline(always)]
736    pub const fn rounded_full(self) -> Self {
737        self.rounded(127)
738    }
739}
740
741/// 统一的 Tailwind 风格链式修饰符 Trait,`Div` 与自定义组件均可实现。
742pub trait Styled: Sized {
743    /// 获取内部 `Style` 的可变引用。
744    fn style_mut(&mut self) -> &mut Style;
745
746    /// 开启弹性布局(等价 CSS `display: flex`,默认水平主轴 `Row`)。
747    #[inline(always)]
748    fn flex(mut self) -> Self {
749        self.style_mut().set_row();
750        self
751    }
752
753    #[inline(always)]
754    fn flex_row(mut self) -> Self {
755        self.style_mut().set_row();
756        self
757    }
758
759    #[inline(always)]
760    fn flex_col(mut self) -> Self {
761        self.style_mut().set_col();
762        self
763    }
764
765    #[inline(always)]
766    fn flex_wrap(mut self) -> Self {
767        self.style_mut().set_flex_wrap(true);
768        self
769    }
770
771    #[inline(always)]
772    fn flex_nowrap(mut self) -> Self {
773        self.style_mut().set_flex_wrap(false);
774        self
775    }
776
777    #[inline(always)]
778    fn flex_1(mut self) -> Self {
779        self.style_mut().set_grow(1);
780        self
781    }
782
783    #[inline(always)]
784    fn flex_none(mut self) -> Self {
785        self.style_mut().set_grow(0);
786        self
787    }
788
789    #[inline(always)]
790    fn size_full(mut self) -> Self {
791        self.style_mut().width = Length::Full;
792        self.style_mut().height = Length::Full;
793        self
794    }
795
796    /// 同时设置等宽高正方形尺寸(像素)。
797    #[inline(always)]
798    fn size(mut self, px: i16) -> Self {
799        let s = self.style_mut();
800        s.width = Length::Px(px);
801        s.height = Length::Px(px);
802        self
803    }
804
805    #[inline(always)]
806    fn w_full(mut self) -> Self {
807        self.style_mut().width = Length::Full;
808        self
809    }
810
811    #[inline(always)]
812    fn h_full(mut self) -> Self {
813        self.style_mut().height = Length::Full;
814        self
815    }
816
817    #[inline(always)]
818    fn w_auto(mut self) -> Self {
819        self.style_mut().width = Length::Auto;
820        self
821    }
822
823    #[inline(always)]
824    fn h_auto(mut self) -> Self {
825        self.style_mut().height = Length::Auto;
826        self
827    }
828
829    #[inline(always)]
830    fn w(mut self, px: i16) -> Self {
831        self.style_mut().width = Length::Px(px);
832        self
833    }
834
835    #[inline(always)]
836    fn h(mut self, px: i16) -> Self {
837        self.style_mut().height = Length::Px(px);
838        self
839    }
840
841    /// 设置宽度占父容器可用宽度的百分比(`0..=100`)。
842    #[inline(always)]
843    fn w_pct(mut self, pct: i16) -> Self {
844        self.style_mut().width = Length::Pct(pct.clamp(0, 100));
845        self
846    }
847
848    /// 设置高度占父容器可用高度的百分比(`0..=100`)。
849    #[inline(always)]
850    fn h_pct(mut self, pct: i16) -> Self {
851        self.style_mut().height = Length::Pct(pct.clamp(0, 100));
852        self
853    }
854
855    #[inline(always)]
856    fn w_1_2(self) -> Self { self.w_pct(50) }
857    #[inline(always)]
858    fn w_1_3(self) -> Self { self.w_pct(33) }
859    #[inline(always)]
860    fn w_2_3(self) -> Self { self.w_pct(67) }
861    #[inline(always)]
862    fn w_1_4(self) -> Self { self.w_pct(25) }
863    #[inline(always)]
864    fn w_3_4(self) -> Self { self.w_pct(75) }
865    #[inline(always)]
866    fn h_1_2(self) -> Self { self.h_pct(50) }
867
868    // --- Tailwind 尺寸档位简写(1 unit = 4px) ---
869    #[inline(always)] fn size_0(self) -> Self { self.size(0) }
870    #[inline(always)] fn size_0_5(self) -> Self { self.size(2) }
871    #[inline(always)] fn size_1(self) -> Self { self.size(4) }
872    #[inline(always)] fn size_1_5(self) -> Self { self.size(6) }
873    #[inline(always)] fn size_2(self) -> Self { self.size(8) }
874    #[inline(always)] fn size_2_5(self) -> Self { self.size(10) }
875    #[inline(always)] fn size_3(self) -> Self { self.size(12) }
876    #[inline(always)] fn size_3_5(self) -> Self { self.size(14) }
877    #[inline(always)] fn size_4(self) -> Self { self.size(16) }
878    #[inline(always)] fn size_5(self) -> Self { self.size(20) }
879    #[inline(always)] fn size_6(self) -> Self { self.size(24) }
880    #[inline(always)] fn size_7(self) -> Self { self.size(28) }
881    #[inline(always)] fn size_8(self) -> Self { self.size(32) }
882    #[inline(always)] fn size_9(self) -> Self { self.size(36) }
883    #[inline(always)] fn size_10(self) -> Self { self.size(40) }
884    #[inline(always)] fn size_11(self) -> Self { self.size(44) }
885    #[inline(always)] fn size_12(self) -> Self { self.size(48) }
886    #[inline(always)] fn size_14(self) -> Self { self.size(56) }
887    #[inline(always)] fn size_16(self) -> Self { self.size(64) }
888    #[inline(always)] fn size_20(self) -> Self { self.size(80) }
889    #[inline(always)] fn size_24(self) -> Self { self.size(96) }
890    #[inline(always)] fn size_32(self) -> Self { self.size(128) }
891
892    #[inline(always)] fn w_0(self) -> Self { self.w(0) }
893    #[inline(always)] fn w_0_5(self) -> Self { self.w(2) }
894    #[inline(always)] fn w_1(self) -> Self { self.w(4) }
895    #[inline(always)] fn w_1_5(self) -> Self { self.w(6) }
896    #[inline(always)] fn w_2(self) -> Self { self.w(8) }
897    #[inline(always)] fn w_2_5(self) -> Self { self.w(10) }
898    #[inline(always)] fn w_3(self) -> Self { self.w(12) }
899    #[inline(always)] fn w_3_5(self) -> Self { self.w(14) }
900    #[inline(always)] fn w_4(self) -> Self { self.w(16) }
901    #[inline(always)] fn w_5(self) -> Self { self.w(20) }
902    #[inline(always)] fn w_6(self) -> Self { self.w(24) }
903    #[inline(always)] fn w_7(self) -> Self { self.w(28) }
904    #[inline(always)] fn w_8(self) -> Self { self.w(32) }
905    #[inline(always)] fn w_9(self) -> Self { self.w(36) }
906    #[inline(always)] fn w_10(self) -> Self { self.w(40) }
907    #[inline(always)] fn w_11(self) -> Self { self.w(44) }
908    #[inline(always)] fn w_12(self) -> Self { self.w(48) }
909    #[inline(always)] fn w_14(self) -> Self { self.w(56) }
910    #[inline(always)] fn w_16(self) -> Self { self.w(64) }
911    #[inline(always)] fn w_20(self) -> Self { self.w(80) }
912    #[inline(always)] fn w_24(self) -> Self { self.w(96) }
913    #[inline(always)] fn w_28(self) -> Self { self.w(112) }
914    #[inline(always)] fn w_32(self) -> Self { self.w(128) }
915    #[inline(always)] fn w_36(self) -> Self { self.w(144) }
916    #[inline(always)] fn w_40(self) -> Self { self.w(160) }
917    #[inline(always)] fn w_44(self) -> Self { self.w(176) }
918    #[inline(always)] fn w_48(self) -> Self { self.w(192) }
919    #[inline(always)] fn w_52(self) -> Self { self.w(208) }
920    #[inline(always)] fn w_56(self) -> Self { self.w(224) }
921    #[inline(always)] fn w_60(self) -> Self { self.w(240) }
922    #[inline(always)] fn w_64(self) -> Self { self.w(256) }
923    #[inline(always)] fn w_72(self) -> Self { self.w(288) }
924    #[inline(always)] fn w_80(self) -> Self { self.w(320) }
925    #[inline(always)] fn w_96(self) -> Self { self.w(384) }
926
927    #[inline(always)] fn h_0(self) -> Self { self.h(0) }
928    #[inline(always)] fn h_0_5(self) -> Self { self.h(2) }
929    #[inline(always)] fn h_1(self) -> Self { self.h(4) }
930    #[inline(always)] fn h_1_5(self) -> Self { self.h(6) }
931    #[inline(always)] fn h_2(self) -> Self { self.h(8) }
932    #[inline(always)] fn h_2_5(self) -> Self { self.h(10) }
933    #[inline(always)] fn h_3(self) -> Self { self.h(12) }
934    #[inline(always)] fn h_3_5(self) -> Self { self.h(14) }
935    #[inline(always)] fn h_4(self) -> Self { self.h(16) }
936    #[inline(always)] fn h_5(self) -> Self { self.h(20) }
937    #[inline(always)] fn h_6(self) -> Self { self.h(24) }
938    #[inline(always)] fn h_7(self) -> Self { self.h(28) }
939    #[inline(always)] fn h_8(self) -> Self { self.h(32) }
940    #[inline(always)] fn h_9(self) -> Self { self.h(36) }
941    #[inline(always)] fn h_10(self) -> Self { self.h(40) }
942    #[inline(always)] fn h_11(self) -> Self { self.h(44) }
943    #[inline(always)] fn h_12(self) -> Self { self.h(48) }
944    #[inline(always)] fn h_14(self) -> Self { self.h(56) }
945    #[inline(always)] fn h_16(self) -> Self { self.h(64) }
946    #[inline(always)] fn h_20(self) -> Self { self.h(80) }
947    #[inline(always)] fn h_24(self) -> Self { self.h(96) }
948    #[inline(always)] fn h_32(self) -> Self { self.h(128) }
949    #[inline(always)] fn h_40(self) -> Self { self.h(160) }
950    #[inline(always)] fn h_48(self) -> Self { self.h(192) }
951    #[inline(always)] fn h_56(self) -> Self { self.h(224) }
952    #[inline(always)] fn h_64(self) -> Self { self.h(256) }
953
954    #[inline(always)]
955    fn p(mut self, px: i8) -> Self {
956        self.style_mut().padding = Edges::all(px);
957        self
958    }
959
960    #[inline(always)]
961    fn px(mut self, px: i8) -> Self {
962        let p = &mut self.style_mut().padding;
963        p.left = px;
964        p.right = px;
965        self
966    }
967
968    #[inline(always)]
969    fn py(mut self, px: i8) -> Self {
970        let p = &mut self.style_mut().padding;
971        p.top = px;
972        p.bottom = px;
973        self
974    }
975
976    #[inline(always)]
977    fn pt(mut self, px: i8) -> Self {
978        self.style_mut().padding.top = px;
979        self
980    }
981
982    #[inline(always)]
983    fn pb(mut self, px: i8) -> Self {
984        self.style_mut().padding.bottom = px;
985        self
986    }
987
988    #[inline(always)]
989    fn pl(mut self, px: i8) -> Self {
990        self.style_mut().padding.left = px;
991        self
992    }
993
994    #[inline(always)]
995    fn pr(mut self, px: i8) -> Self {
996        self.style_mut().padding.right = px;
997        self
998    }
999
1000    // --- Tailwind 内边距档位简写 ---
1001    #[inline(always)] fn p_0(self) -> Self { self.p(0) }
1002    #[inline(always)] fn p_0p5(self) -> Self { self.p(2) }
1003    #[inline(always)] fn p_0_5(self) -> Self { self.p(2) }
1004    #[inline(always)] fn p_1(self) -> Self { self.p(4) }
1005    #[inline(always)] fn p_1p5(self) -> Self { self.p(6) }
1006    #[inline(always)] fn p_1_5(self) -> Self { self.p(6) }
1007    #[inline(always)] fn p_2(self) -> Self { self.p(8) }
1008    #[inline(always)] fn p_2p5(self) -> Self { self.p(10) }
1009    #[inline(always)] fn p_2_5(self) -> Self { self.p(10) }
1010    #[inline(always)] fn p_3(self) -> Self { self.p(12) }
1011    #[inline(always)] fn p_3p5(self) -> Self { self.p(14) }
1012    #[inline(always)] fn p_3_5(self) -> Self { self.p(14) }
1013    #[inline(always)] fn p_4(self) -> Self { self.p(16) }
1014    #[inline(always)] fn p_5(self) -> Self { self.p(20) }
1015    #[inline(always)] fn p_6(self) -> Self { self.p(24) }
1016    #[inline(always)] fn p_8(self) -> Self { self.p(32) }
1017    #[inline(always)] fn p_10(self) -> Self { self.p(40) }
1018    #[inline(always)] fn p_12(self) -> Self { self.p(48) }
1019
1020    #[inline(always)] fn px_0(self) -> Self { self.px(0) }
1021    #[inline(always)] fn px_0p5(self) -> Self { self.px(2) }
1022    #[inline(always)] fn px_0_5(self) -> Self { self.px(2) }
1023    #[inline(always)] fn px_1(self) -> Self { self.px(4) }
1024    #[inline(always)] fn px_1p5(self) -> Self { self.px(6) }
1025    #[inline(always)] fn px_1_5(self) -> Self { self.px(6) }
1026    #[inline(always)] fn px_2(self) -> Self { self.px(8) }
1027    #[inline(always)] fn px_2p5(self) -> Self { self.px(10) }
1028    #[inline(always)] fn px_2_5(self) -> Self { self.px(10) }
1029    #[inline(always)] fn px_3(self) -> Self { self.px(12) }
1030    #[inline(always)] fn px_3p5(self) -> Self { self.px(14) }
1031    #[inline(always)] fn px_3_5(self) -> Self { self.px(14) }
1032    #[inline(always)] fn px_4(self) -> Self { self.px(16) }
1033    #[inline(always)] fn px_5(self) -> Self { self.px(20) }
1034    #[inline(always)] fn px_6(self) -> Self { self.px(24) }
1035    #[inline(always)] fn px_8(self) -> Self { self.px(32) }
1036
1037    #[inline(always)] fn py_0(self) -> Self { self.py(0) }
1038    #[inline(always)] fn py_0p5(self) -> Self { self.py(2) }
1039    #[inline(always)] fn py_0_5(self) -> Self { self.py(2) }
1040    #[inline(always)] fn py_1(self) -> Self { self.py(4) }
1041    #[inline(always)] fn py_1p5(self) -> Self { self.py(6) }
1042    #[inline(always)] fn py_1_5(self) -> Self { self.py(6) }
1043    #[inline(always)] fn py_2(self) -> Self { self.py(8) }
1044    #[inline(always)] fn py_2p5(self) -> Self { self.py(10) }
1045    #[inline(always)] fn py_2_5(self) -> Self { self.py(10) }
1046    #[inline(always)] fn py_3(self) -> Self { self.py(12) }
1047    #[inline(always)] fn py_3p5(self) -> Self { self.py(14) }
1048    #[inline(always)] fn py_3_5(self) -> Self { self.py(14) }
1049    #[inline(always)] fn py_4(self) -> Self { self.py(16) }
1050    #[inline(always)] fn py_5(self) -> Self { self.py(20) }
1051    #[inline(always)] fn py_6(self) -> Self { self.py(24) }
1052    #[inline(always)] fn py_8(self) -> Self { self.py(32) }
1053
1054    #[inline(always)] fn pt_0(self) -> Self { self.pt(0) }
1055    #[inline(always)] fn pt_1(self) -> Self { self.pt(4) }
1056    #[inline(always)] fn pt_2(self) -> Self { self.pt(8) }
1057    #[inline(always)] fn pt_3(self) -> Self { self.pt(12) }
1058    #[inline(always)] fn pt_4(self) -> Self { self.pt(16) }
1059    #[inline(always)] fn pb_0(self) -> Self { self.pb(0) }
1060    #[inline(always)] fn pb_1(self) -> Self { self.pb(4) }
1061    #[inline(always)] fn pb_2(self) -> Self { self.pb(8) }
1062    #[inline(always)] fn pb_3(self) -> Self { self.pb(12) }
1063    #[inline(always)] fn pb_4(self) -> Self { self.pb(16) }
1064    #[inline(always)] fn pl_0(self) -> Self { self.pl(0) }
1065    #[inline(always)] fn pl_1(self) -> Self { self.pl(4) }
1066    #[inline(always)] fn pl_2(self) -> Self { self.pl(8) }
1067    #[inline(always)] fn pl_3(self) -> Self { self.pl(12) }
1068    #[inline(always)] fn pl_4(self) -> Self { self.pl(16) }
1069    #[inline(always)] fn pr_0(self) -> Self { self.pr(0) }
1070    #[inline(always)] fn pr_1(self) -> Self { self.pr(4) }
1071    #[inline(always)] fn pr_2(self) -> Self { self.pr(8) }
1072    #[inline(always)] fn pr_3(self) -> Self { self.pr(12) }
1073    #[inline(always)] fn pr_4(self) -> Self { self.pr(16) }
1074
1075    #[inline(always)]
1076    fn m(mut self, px: i8) -> Self {
1077        self.style_mut().set_margin_px(px);
1078        self
1079    }
1080
1081    #[inline(always)] fn m_0(self) -> Self { self.m(0) }
1082    #[inline(always)] fn m_0_5(self) -> Self { self.m(2) }
1083    #[inline(always)] fn m_1(self) -> Self { self.m(4) }
1084    #[inline(always)] fn m_1_5(self) -> Self { self.m(6) }
1085    #[inline(always)] fn m_2(self) -> Self { self.m(8) }
1086    #[inline(always)] fn m_2_5(self) -> Self { self.m(10) }
1087    #[inline(always)] fn m_3(self) -> Self { self.m(12) }
1088    #[inline(always)] fn m_4(self) -> Self { self.m(16) }
1089
1090    #[inline(always)]
1091    fn gap(mut self, px: i8) -> Self {
1092        self.style_mut().gap = px;
1093        self
1094    }
1095
1096    #[inline(always)] fn gap_0(self) -> Self { self.gap(0) }
1097    #[inline(always)] fn gap_0p5(self) -> Self { self.gap(2) }
1098    #[inline(always)] fn gap_0_5(self) -> Self { self.gap(2) }
1099    #[inline(always)] fn gap_1(self) -> Self { self.gap(4) }
1100    #[inline(always)] fn gap_1p5(self) -> Self { self.gap(6) }
1101    #[inline(always)] fn gap_1_5(self) -> Self { self.gap(6) }
1102    #[inline(always)] fn gap_2(self) -> Self { self.gap(8) }
1103    #[inline(always)] fn gap_2p5(self) -> Self { self.gap(10) }
1104    #[inline(always)] fn gap_2_5(self) -> Self { self.gap(10) }
1105    #[inline(always)] fn gap_3(self) -> Self { self.gap(12) }
1106    #[inline(always)] fn gap_3p5(self) -> Self { self.gap(14) }
1107    #[inline(always)] fn gap_3_5(self) -> Self { self.gap(14) }
1108    #[inline(always)] fn gap_4(self) -> Self { self.gap(16) }
1109    #[inline(always)] fn gap_5(self) -> Self { self.gap(20) }
1110    #[inline(always)] fn gap_6(self) -> Self { self.gap(24) }
1111    #[inline(always)] fn gap_8(self) -> Self { self.gap(32) }
1112
1113    #[inline(always)]
1114    fn flex_grow(mut self, weight: u8) -> Self {
1115        self.style_mut().set_grow(weight);
1116        self
1117    }
1118
1119    #[inline(always)]
1120    fn items_center(mut self) -> Self {
1121        self.style_mut().set_items_center();
1122        self
1123    }
1124
1125    #[inline(always)]
1126    fn items_start(mut self) -> Self {
1127        self.style_mut().set_items_align(AlignItems::Start);
1128        self
1129    }
1130
1131    #[inline(always)]
1132    fn items_end(mut self) -> Self {
1133        self.style_mut().set_items_align(AlignItems::End);
1134        self
1135    }
1136
1137    #[inline(always)]
1138    fn items_stretch(mut self) -> Self {
1139        self.style_mut().set_items_align(AlignItems::Stretch);
1140        self
1141    }
1142
1143    #[inline(always)]
1144    fn justify_start(mut self) -> Self {
1145        self.style_mut().set_justify(Justify::Start);
1146        self
1147    }
1148
1149    #[inline(always)]
1150    fn justify_center(mut self) -> Self {
1151        self.style_mut().set_justify_center();
1152        self
1153    }
1154
1155    #[inline(always)]
1156    fn justify_between(mut self) -> Self {
1157        self.style_mut().set_justify_between();
1158        self
1159    }
1160
1161    #[inline(always)]
1162    fn justify_end(mut self) -> Self {
1163        self.style_mut().set_justify(Justify::End);
1164        self
1165    }
1166
1167    #[inline(always)]
1168    fn justify_around(mut self) -> Self {
1169        self.style_mut().set_justify(Justify::Around);
1170        self
1171    }
1172
1173    #[inline(always)]
1174    fn bg(mut self, color: Color) -> Self {
1175        self.style_mut().bg = color;
1176        self
1177    }
1178
1179    #[inline(always)]
1180    fn rounded(mut self, radius: u8) -> Self {
1181        self.style_mut().set_rounded(radius);
1182        self
1183    }
1184
1185    #[inline(always)] fn rounded_none(self) -> Self { self.rounded(0) }
1186    #[inline(always)] fn rounded_xs(self) -> Self { self.rounded(2) }
1187    #[inline(always)] fn rounded_sm(self) -> Self { self.rounded(4) }
1188    #[inline(always)] fn rounded_md(self) -> Self { self.rounded(6) }
1189    #[inline(always)] fn rounded_lg(self) -> Self { self.rounded(8) }
1190    #[inline(always)] fn rounded_xl(self) -> Self { self.rounded(12) }
1191    #[inline(always)] fn rounded_2xl(self) -> Self { self.rounded(16) }
1192    #[inline(always)] fn rounded_3xl(self) -> Self { self.rounded(24) }
1193    #[inline(always)] fn rounded_full(self) -> Self { self.rounded(127) }
1194
1195    #[inline(always)]
1196    fn border(mut self, width: u8, color: Color) -> Self {
1197        let s = self.style_mut();
1198        s.set_border_px(width);
1199        s.border_color = color;
1200        self
1201    }
1202
1203    #[inline(always)]
1204    fn border_0(mut self) -> Self {
1205        self.style_mut().set_border_px(0);
1206        self
1207    }
1208
1209    #[inline(always)]
1210    fn border_1(mut self) -> Self {
1211        self.style_mut().set_border_px(1);
1212        self
1213    }
1214
1215    #[inline(always)]
1216    fn border_2(mut self) -> Self {
1217        self.style_mut().set_border_px(2);
1218        self
1219    }
1220
1221    #[inline(always)]
1222    fn border_4(mut self) -> Self {
1223        self.style_mut().set_border_px(4);
1224        self
1225    }
1226
1227    #[inline(always)]
1228    fn border_8(mut self) -> Self {
1229        self.style_mut().set_border_px(8);
1230        self
1231    }
1232
1233    #[inline(always)]
1234    fn border_color(mut self, color: Color) -> Self {
1235        self.style_mut().border_color = color;
1236        self
1237    }
1238
1239    #[inline(always)]
1240    fn flex_shrink_0(self) -> Self {
1241        self
1242    }
1243}
1244
1245pub use crate::style_ext::{StyleExt, StyledExt};