03-StyleStringBuilder:类型安全的样式构建系统
目录
概述
StyleStringBuilder 是 Hikari 的内联样式构建器,提供了类型安全的 CSS 属性设置方式。它通过 CssProperty 枚举和便捷方法,完全替换了传统的 style 字符串拼接,实现了编译时属性名检查和运行时零开销。
更新 (Phase 2):StyleStringBuilder 和 CssProperty 现在从 tairitsu-style re-export,提供 403 个 W3C 标准 CSS 属性。
设计理念
核心原则
- 1类型安全 - 编译时检查 CSS 属性名
- 2像素值优化 - 自动 px 单位转换
- 3紧凑输出 - 去除冗余空格
- 4CSS 变量支持 - 完美集成主题系统
与 ClassesBuilder 的区别
| 特性 | ClassesBuilder | StyleStringBuilder |
|---|---|---|
| 输出 | class 属性 | style 属性 |
| 使用场景 | 静态布局工具类 | 动态计算值、覆盖全局样式 |
| 类型安全 | 工具类枚举 | CSS 属性枚举 |
| 运行时开销 | 零(编译时) | 零(字符串连接) |
| 示例 | hi-p-4 | padding:16px |
架构层次
mermaid
核心架构
1. StyleStringBuilder 结构
定义位置:packages/animation/src/style.rs
rust
1
2
3
核心方法:
| 方法 | 职责 | 返回值 |
|---|---|---|
| new() | 创建 builder | StyleStringBuilder |
| add(property, value) | 添加 CSS 属性 | StyleStringBuilder |
| add_px(property, pixels) | 添加像素值(自动加 px) | StyleStringBuilder |
| build() | 构建样式字符串(带空格) | String |
| build_clean() | 构建紧凑样式字符串(无空格) | String |
2. CssProperty 枚举
定义位置:packages/animation/src/style.rs
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
属性映射:
rust
1
2
3
4
5
6
7
8
9
10
3. 像素值自动转换
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
工作机制
构建流程
mermaid
紧凑输出机制
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
类型检查机制
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// no 编译错误:属性名拼写错误
let style = new
.add // 没有这个变体
.build;
// no 编译错误:参数类型错误
let style = new
.add_px // 应该是 i32
.build;
// yes 编译通过:IDE 自动补全
let style = new
.add // IDE 提示 Width 变体
.build;
CSS 属性枚举
完整属性列表
布局属性
| 枚举变体 | CSS 属性 | 示例值 |
|---|---|---|
| Display | display | flex, block, none |
| Position | position | relative, absolute, fixed |
| Top | top | 10px, 50% |
| Right | right | 10px, 50% |
| Bottom | bottom | 10px, 50% |
| Left | left | 10px, 50% |
| ZIndex | z-index | 10, 100 |
盒模型属性
| 枚举变体 | CSS 属性 | 示例值 |
|---|---|---|
| Width | width | 100px, 50%, auto |
| Height | height | 100px, 50%, auto |
| MinWidth | min-width | 100px |
| MaxWidth | max-width | 1000px |
| Padding | padding | 16px, 1rem |
| Margin | margin | 16px, 1rem |
| BorderRadius | border-radius | 8px, 50% |
弹性布局属性
| 枚举变体 | CSS 属性 | 示例值 |
|---|---|---|
| FlexDirection | flex-direction | row, column |
| AlignItems | align-items | center, flex-start |
| JustifyContent | justify-content | center, space-between |
| Gap | gap | 16px, 1rem |
| FlexGrow | flex-grow | 1, 0 |
视觉属性
| 枚举变体 | CSS 属性 | 示例值 |
|---|---|---|
| Opacity | opacity | 0.5, 1 |
| Transform | transform | scale(1.1), translate(10px) |
| TransformOrigin | transform-origin | center, top |
| BoxShadow | box-shadow | 0 2px 4px rgba(0,0,0,0.1) |
| Background | background | red, url(...) |
字体属性
| 枚举变体 | CSS 属性 | 示例值 |
|---|---|---|
| FontSize | font-size | 16px, 1rem |
| FontWeight | font-weight | 400, bold |
| LineHeight | line-height | 1.5, 2 |
| Color | color | red, #ff0000 |
性能优化
1. 零运行时开销
编译时确定:所有属性名在编译时确定
rust
1
2
3
4
// 编译后等同于:
let style = "width:100px;height:200px;opacity:0.5";
// 不需要运行时拼接属性名
2. 紧凑输出
rust
1
2
3
4
5
6
7
8
9
10
11
// yes 推荐:紧凑输出(减少字节)
let style = new
.add_px
.build_clean;
// 输出: "width:100px"
// no 避免:标准输出(带空格)
let style = new
.add_px
.build;
// 输出: "width: 100px"
3. 避免 clone
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// yes 推荐:使用 &str(零成本转换)
let style = new
.add
.build;
// yes 也支持:使用 String(会移动所有权)
let width = "100px".to_string;
let style = new
.add
.build;
使用示例
示例 1:基础样式
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
use ;
let style = new
.add_px
.add_px
.add
.build_clean;
// 输出: "width:100px;height:50px;background-color:red"
rsx!
示例 2:CSS 变量
rust
1
2
3
4
5
6
7
let style = new
.add
.add
.add
.build_clean;
// 输出: "opacity:var(--hi-dropdown-opacity);transform:scale(var(--hi-dropdown-scale));transform-origin:top center"
示例 3:覆盖全局样式
rust
1
2
3
4
5
6
7
8
9
10
11
// 覆盖 img { height: auto; } 全局样式
let img_style = new
.add_px
.add_px
.add
.add
.build_clean;
rsx!
示例 4:动态计算值
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
let width = use_memo;
let style = use_memo;
// 动态计算宽度
rsx!
示例 5:组合使用 ClassesBuilder 和 StyleStringBuilder
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
use ;
use ;
// ClassesBuilder 处理布局
let classes = new
.add
.add
.add
.build;
// StyleStringBuilder 处理动态值
let style = new
.add_px
.add
.build_clean;
rsx!
总结
StyleStringBuilder 通过类型安全的样式构建系统,实现了:
- 1编译时安全 - 防止 CSS 属性名拼写错误
- 2像素值优化 - 自动 px 单位转换
- 3紧凑输出 - 减少字节传输
- 4CSS 变量支持 - 完美集成主题系统
- 5零运行时开销 - 纯字符串连接
这套系统完全替换了传统的 style 字符串拼接,是 Hikari 动态样式体系的核心组件。
Phase 2 迁移
迁移概述
在 Hikari 到 Tairitsu 构建链迁移的 Phase 2 中,StyleStringBuilder 和 CssProperty 已从内部实现迁移到共享的 tairitsu-style 库。
迁移前后对比
Before (Phase 2 前):
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// packages/animation/src/properties.rs
After (Phase 2 后):
rust
1
2
3
4
5
// packages/animation/src/style/mod.rs
// Re-export from tairitsu_style
pub use ;
// Now provides 403 W3C standard properties
属性数量对比
| 指标 | Before | After | 提升 |
|---|---|---|---|
| CSS 属性数量 | ~50 | 403 | +706% |
| 手动维护代码行数 | 635 | 0 | -100% |
| W3C 标准覆盖率 | ~12% | 100% | +733% |
完整的 403 个 CSS 属性
迁移后,CssProperty 枚举现在包含以下完整类别的属性:
布局属性 (Layout)
- Display, Position, Top, Right, Bottom, Left, ZIndex
- Float, Clear, Overflow, OverflowX, OverflowY
- Visibility, Opacity, Visibility
盒模型属性 (Box Model)
- Width, MinWidth, MaxWidth, Height, MinHeight, MaxHeight
- Padding, PaddingTop, PaddingRight, PaddingBottom, PaddingLeft
- Margin, MarginTop, MarginRight, MarginBottom, MarginLeft
- Border, BorderTop, BorderRight, BorderBottom, BorderLeft
- BorderWidth, BorderStyle, BorderColor
- BorderRadius, BoxSizing
弹性布局属性 (Flexbox)
- Flex, FlexBasis, FlexDirection, FlexFlow, FlexGrow, FlexShrink, FlexWrap
- AlignContent, AlignItems, AlignSelf
- JustifyContent, JustifyItems, JustifySelf
- Gap, RowGap, ColumnGap
- Order
网格布局属性 (Grid)
- Grid, GridArea, GridAutoColumns, GridAutoFlow, GridAutoRows
- GridColumn, GridColumnEnd, GridColumnStart, GridRow
- GridRowEnd, GridRowStart, GridTemplate
- GridTemplateAreas, GridTemplateColumns, GridTemplateRows
排版属性 (Typography)
- Font, FontFamily, FontSize, FontSizeAdjust, FontStretch
- FontStyle, FontVariant, FontWeight
- LineHeight, LetterSpacing, WordSpacing
- TextAlign, TextAlignLast, TextDecoration, TextIndent
- TextOverflow, TextShadow, TextTransform
- VerticalAlign, WhiteSpace, WordBreak, WordWrap
颜色与背景属性 (Color & Background)
- Color, Background, BackgroundAttachment, BackgroundBlendMode
- BackgroundClip, BackgroundColor, BackgroundImage
- BackgroundOrigin, BackgroundPosition, BackgroundRepeat
- BackgroundSize
视觉效果属性 (Visual Effects)
- BoxShadow, Filter, BackdropFilter
- Transform, TransformOrigin, TransformStyle
- Perspective, PerspectiveOrigin
- MixBlendMode, Isolation
过渡与动画属性 (Transition & Animation)
- Transition, TransitionDelay, TransitionDuration
- TransitionProperty, TransitionTimingFunction
- Animation, AnimationDelay, AnimationDirection
- AnimationDuration, AnimationFillMode
- AnimationIterationCount, AnimationName
- AnimationPlayState, AnimationTimingFunction
列表属性 (Lists)
- ListStyle, ListStyleImage, ListStylePosition
- ListStyleType
表格属性 (Tables)
- BorderCollapse, BorderSpacing
- CaptionSide, EmptyCells
- TableLayout
用户界面属性 (User Interface)
- Appearance, Cursor
- Outline, OutlineColor, OutlineOffset
- OutlineStyle, OutlineWidth
- Resize, UserSelect
多列布局属性 (Multi-column)
- Columns, ColumnCount, ColumnFill
- ColumnGap, ColumnRule, ColumnRuleColor
- ColumnRuleStyle, ColumnRuleWidth
- ColumnSpan, ColumnWidth
其他属性 (Miscellaneous)
- Content, CounterIncrement, CounterReset
- Quotes, Orphans, Widows
使用变化
迁移后,使用方式保持不变(通过 re-export):
rust
1
2
3
4
5
6
7
// Before and After (same usage)
use ;
let style = new
.add_px
.add
.build_clean;
但你现在可以使用更多的 CSS 属性:
rust
1
2
3
4
5
6
7
8
// 新增的属性示例
let style = new
.add
.add
.add
.add
.add
.build_clean;
代码清理
迁移删除了以下文件:
text
1
packages/animation/src/properties.rs (635 lines)
并简化了 packages/animation/src/style/mod.rs:
rust
1
2
3
4
5
6
7
8
9
10
11
12
// Before
pub use CssProperty;
// ... manual property mapping
// After
pub use ;
兼容性
所有现有代码继续工作,无需修改:
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
// 所有现有的用法都继续工作
use CssProperty;
// yes 仍然有效
Width
Height
BackgroundColor
// yes 新增属性也可用
GridTemplateColumns
BackdropFilter
Filter
MixBlendMode
性能影响
迁移后性能提升:
- 编译时间:减少 635 行代码编译
- 二进制大小:删除重复代码
- 运行时:无变化(纯 re-export,零开销)
- 内存:无变化
测试覆盖
所有现有测试继续通过:
bash
1
2
3
; ;
升级指南
如果要在新组件中使用新增的 CSS 属性,只需正常导入和使用:
rust
1
2
3
4
5
6
7
8
9
use ;
// 使用任何 403 个 CSS 属性
let style = new
.add
.add
.add
.add
.build_clean;
更多信息
- 完整属性列表 - W3C CSS 规范
- tairitsu-style 文档 - 属性实现详情
- 07-Migration Guide - 完整迁移文档
- 02-ClassesBuilder 系统 - 类名构建系统