velox-engine 0.2.0

Velox core UI engine: arena, pod state, 1D fixed-point layout solver, damage tracker, SDF scanline rasterizer, and cross-platform GPU/software backends
Documentation
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
//! 弹性盒容器构造器 `Div<'a>` 与链式样式、交互及动画配置 API。

use core::cell::Cell;
use core::time::Duration;

use crate::arena::StrRef;
use crate::paint::{
    BackdropFilter, BlendMode, ColorMatrix, ColorStop, CornerRadii, DropShadow, ExtendMode,
    GradientSpace, LayerFilter, Paint, PaintEffects, Transform2D,
};
use crate::pod::{ActionScope, CursorShape, DragEvent, FrameScope};
use crate::style::{AlignItems, Color, Easing, Edges, LoopMode, Style, StyleRefinement, Styled};

use super::div_node::{
    duration_to_ms, pack_visual_flags, DivExtPtr, DivExtended, DivInteractivity, DivNode,
    DivRareHandlers, DivStateStyles, DivVisual, EnterFrom, TransitionSpec, FOCUS_BORDER_NONE,
    NO_FOCUS_BORDER_DIV_VISUAL,
};
use super::{Element, ElementNode, IntoElement};

impl<'a> IntoElement<'a> for Div<'a> {
    #[inline(always)]
    fn into_element(mut self, cx: &'a FrameScope) -> Element<'a> {
        let has_state_styles = self.text_color.is_some()
            || self.text_size.is_some()
            || self.hover_style.is_some()
            || self.active_style.is_some()
            || self.focus_style.is_some();
        let has_effects = !self.effects.is_empty() || self.external_texture.is_some();

        let alloc_visual = |extended: DivExtPtr<'a>,

                            transition: Option<&'a TransitionSpec>|

         -> &'a DivVisual<'a> {
            let (pos_x, pos_y) = self.pos.unwrap_or((0, 0));
            let (bg_grad_to, bg_grad_horiz) = self.bg_grad.unwrap_or((Color::TRANSPARENT, false));
            let (focus_border_w, focus_border_color) = match self.focus_border {
                Some((w, c)) => (w.min(254), c),
                None => (FOCUS_BORDER_NONE, Color::TRANSPARENT),
            };
            let (shadow_blur, shadow_ox, shadow_oy, shadow_color) =
                self.shadow.unwrap_or((0, 0, 0, Color::TRANSPARENT));
            let extra_flags = pack_visual_flags(
                self.pos.is_some(),
                self.scrollable_max.is_some(),
                self.bg_grad.is_some(),
                bg_grad_horiz,
                self.items_baseline,
                self.align_self,
            );
            &*cx.arena().alloc_temp(DivVisual {
                extended,
                transition,
                margin: self.margin,
                extra_padding: self.extra_padding,
                bg_grad_to,
                focus_border_color,
                shadow_color,
                min_w: self.min_w,
                max_w: self.max_w,
                min_h: self.min_h,
                max_h: self.max_h,
                pos_x,
                pos_y,
                raw_scrollable_max: self.scrollable_max.unwrap_or(0),
                extra_flags,
                focus_border_w,
                shadow_blur,
                shadow_ox,
                shadow_oy,
                cursor: self.cursor,
            })
        };

        let visual = if self.has_non_focus_visual_extra() {
            let state_styles = if has_state_styles {
                let hover_style = self.hover_style.map(|r| &*cx.arena().alloc_temp(r));
                let active_style = self.active_style.map(|r| &*cx.arena().alloc_temp(r));
                let focus_style = self.focus_style.map(|r| &*cx.arena().alloc_temp(r));
                Some(&*cx.arena().alloc_temp(DivStateStyles {
                    hover_style,
                    active_style,
                    focus_style,
                    text_color: self.text_color.unwrap_or(Color::TRANSPARENT),
                    text_size: self.text_size.unwrap_or(0),
                    flags: if self.text_color.is_some() { 1 } else { 0 },
                }))
            } else {
                None
            };
            let effects = if has_effects {
                if let Some(ext_spec) = self.external_texture {
                    self.effects.external_texture = Some(&*cx.arena().alloc_temp(ext_spec));
                }
                Some(&*cx.arena().alloc_temp(self.effects))
            } else {
                None
            };
            let extended = match (state_styles, effects) {
                (None, None) => DivExtPtr::NONE,
                (Some(s), None) => DivExtPtr::from_state_styles(s),
                (None, Some(e)) => DivExtPtr::from_effects(e),
                (Some(s), Some(e)) => DivExtPtr::from_both(&*cx.arena().alloc_temp(DivExtended {
                    state_styles: Some(s),
                    effects: Some(e),
                })),
            };
            let transition = self.transition.map(|t| &*cx.arena().alloc_temp(t));
            Some(alloc_visual(extended, transition))
        } else if let Some((fw, fc)) = self.focus_border {
            if fw == 0 && fc == Color::TRANSPARENT {
                Some(&NO_FOCUS_BORDER_DIV_VISUAL)
            } else {
                Some(alloc_visual(DivExtPtr::NONE, None))
            }
        } else {
            None
        };

        let interact = if self.has_interactivity() {
            let rare = if self.has_rare_handlers() {
                Some(&*cx.arena().alloc(DivRareHandlers {
                    on_long_press: self.on_long_press,
                    on_drag: self.on_drag,
                    on_swipe: self.on_swipe,
                    on_key_input: self.on_key_input,
                    on_key_nav: self.on_key_nav,
                    on_navigate_back: self.on_navigate_back,
                    on_secondary_click: self.on_secondary_click,
                }))
            } else {
                None
            };
            Some(&*cx.arena().alloc(DivInteractivity {
                on_click: self.on_click,
                rare,
            }))
        } else {
            None
        };

        let mut flags = 0u8;
        if self.focusable {
            flags |= 1 << 0;
        }
        if self.is_overlay {
            flags |= 1 << 1;
        }
        if self.occlude_hit {
            flags |= 1 << 2;
        }
        if self.auto_repeat {
            flags |= 1 << 3;
        }
        if self.clip_children {
            flags |= 1 << 4;
        }
        if self.id.is_some() {
            flags |= 1 << 5;
        }
        if self.active_bg.is_some() {
            flags |= 1 << 6;
        }
        if self.hover_bg.is_some() {
            flags |= 1 << 7;
        }

        Element::Div(DivNode {
            style: self.style,
            first_child: self.first_child,
            visual,
            interact,
            raw_id: self.id.unwrap_or(0),
            raw_active_bg: self.active_bg.unwrap_or(Color::TRANSPARENT),
            raw_hover_bg: self.hover_bg.unwrap_or(Color::TRANSPARENT),
            opacity: self.opacity,
            z_index: self.z_index,
            grid_cols: self.grid_cols,
            flags,
        })
    }
}


/// 基础弹性盒容器原语(完全复刻 HTML/Tailwind 的 `div` 直觉)。
pub struct Div<'a> {
    /// 跨帧唯一 ID(驱动滚动位置记忆与 `.animate_xxx()` 过渡动画)。
    pub id: Option<u32>,
    /// 紧凑位域样式(严格 24 Bytes)。
    pub style: Style,
    /// 可选的四边独立外边距(配合 `style.margin_px()` 使用)。
    pub margin: Edges<i16>,
    /// 可选的大尺寸安全区或内边距扩展(像素,与 `style.padding` 叠加,避免移动端软键盘 > 127px 截断)。
    pub extra_padding: Edges<i16>,
    /// 最小宽度约束(0 表示不限)。
    pub min_w: i16,
    /// 最大宽度约束(0 表示不限)。
    pub max_w: i16,
    /// 最小高度约束(0 表示不限)。
    pub min_h: i16,
    /// 最大高度约束(0 表示不限)。
    pub max_h: i16,
    /// 容器与子树整体不透明度(`0..=255`,默认 `255`)。
    pub opacity: u8,
    /// 按下态(Active)背景色覆盖。
    pub active_bg: Option<Color>,
    /// 悬停态(Hover)背景色覆盖。
    pub hover_bg: Option<Color>,
    /// 线性渐变终点颜色与方向 `(to_color, is_horizontal)`。
    pub bg_grad: Option<(Color, bool)>,
    /// 键盘/旋钮聚焦态(Focus)边框 `(width, color)` 覆盖。
    pub focus_border: Option<(u8, Color)>,
    /// 是否可纳入物理按键/编码器 `Tab`/`Up`/`Down` 焦点链。
    pub focusable: bool,
    /// 是否将该子树提升至顶层 Z-Order 延迟渲染队列(用于 `Modal` / `Dropdown` / `Tooltip` 越过父裁剪框)。
    pub is_overlay: bool,
    /// 是否拦截并吞噬落在该容器内的指针命中事件(防止点击穿透 `Modal` 遮罩层)。
    pub occlude_hit: bool,
    /// 按住不放时是否自动连发 `on_click`(用于 `+/-` 连续调节按钮)。
    pub auto_repeat: bool,
    /// 可触摸拖拽滚动的最大垂直偏移量(`Some(max_px)` 表示启用触摸惯性滚动容器)。
    pub scrollable_max: Option<i16>,
    /// 可选的相对于父容器内容区左上角的叠层偏移坐标 `(x, y)`(用于表盘/徽标/浮层叠放)。
    pub pos: Option<(i16, i16)>,
    /// 第一个子节点引用(侵入式单向链表头)。
    pub first_child: Option<&'a ElementNode<'a>>,
    /// 最后一个子节点引用(用于 $O(1)$ 安全尾插,消除裸指针 UB)。
    pub last_child: Option<&'a ElementNode<'a>>,
    /// 点击事件响应闭包(分配于 `ScratchArena`)。
    pub on_click: Option<&'a dyn Fn(&mut ActionScope)>,
    /// 长按事件响应闭包(按住超过 `500ms` 触发,分配于 `ScratchArena`)。
    pub on_long_press: Option<&'a dyn Fn(&mut ActionScope)>,
    /// 拖拽事件响应闭包(分配于 `ScratchArena`)。
    pub on_drag: Option<&'a dyn Fn(&mut ActionScope)>,
    /// 四向快速滑动手势回调闭包(分配于 `ScratchArena`)。
    pub on_swipe: Option<&'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope)>,
    /// 字符与文本输入事件响应闭包(分配于 `ScratchArena`,供聚焦态 `TextInput` 编辑使用)。
    pub on_key_input: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
    /// 控件局部导航键(方向键、`Home`/`End`、`Enter`/`Space`)响应闭包(不抢占 `Tab`/`BackTab` 焦点切换,亦不触发文本 IME)。
    pub on_key_nav: Option<&'a dyn Fn(crate::pod::KeyCode, &mut ActionScope)>,
    /// 系统返回导航请求回调闭包(Android Back / 浏览器后退 / 左边缘返回手势 / 桌面 `Escape` 关闭浮层)。
    pub on_navigate_back: Option<&'a dyn Fn(&mut ActionScope)>,
    /// 鼠标右键(次键)点击回调闭包(分配于 `ScratchArena`)。
    pub on_secondary_click: Option<&'a dyn Fn(&mut ActionScope)>,
    /// 显式層级排序优先级(默认 `0`;用于控制同层或浮层间的绘制先后顺序)。
    pub z_index: i8,
    /// 解析式 SDF 软阴影 `(blur_radius, offset_x, offset_y, color)`(无需离屏模糊缓冲区)。
    pub shadow: Option<(u8, i8, i8, Color)>,
    /// 二维网格布局列数(`0` 表示常规 Flexbox,`>= 1` 表示按指定列数自动排列的二维 Grid)。
    pub grid_cols: u8,
    /// 声明式过渡动画配置。
    pub transition: Option<TransitionSpec>,
    /// 是否裁剪超出容器边界的子节点(如滚动列表视口)。
    pub clip_children: bool,
    /// 子节点独立交叉轴对齐方式覆盖(`align-self`)。
    pub align_self: Option<AlignItems>,
    /// 显式指定的鼠标光标形状(`None` 时按节点交互能力自动推断 `Pointer` / `Text` / `Grab` / `Default`)。
    pub cursor: Option<CursorShape>,
    /// 容器级可继承文本颜色(未显式设 `.color(...)` 的子 `Text` 自动继承)。
    pub text_color: Option<Color>,
    /// 容器级可继承字号(未显式设 `.size(...)` 的子 `Text` 自动继承)。
    pub text_size: Option<u8>,
    /// 悬停态样式覆写增量(由 `.hover(|s| ...)` 写入,支持背景/边框/文本色/不透明度/圆角)。
    pub hover_style: Option<StyleRefinement>,
    /// 按下态样式覆写增量(由 `.active(|s| ...)` 写入)。
    pub active_style: Option<StyleRefinement>,
    /// 聚焦态样式覆写增量(由 `.focus(|s| ...)` 写入)。
    pub focus_style: Option<StyleRefinement>,
    /// 行容器内是否按子元素文本基线对齐(`items-baseline`)。
    pub items_baseline: bool,
    /// 高级视觉特效包(多色标渐变、四角独立圆角、混合模式、模糊/色彩滤镜、毛玻璃、2D 变换与路径裁剪;未设置时零分配)。
    pub effects: PaintEffects<'a>,
    /// 外部零拷贝 GPU 纹理描述(仅在绑定时按需在帧内分配,对常规节点零内存开销)。
    pub external_texture: Option<crate::paint::ExternalTextureSpec>,
}

/// 构造一个基础弹性盒容器(完全复刻 HTML/Tailwind 的 `div` 直觉)。
#[inline(always)]
pub fn div<'a>() -> Div<'a> {
    Div {
        id: None,
        style: Style::default(),
        margin: Edges::all(0),
        extra_padding: Edges::all(0),
        min_w: 0,
        max_w: 0,
        min_h: 0,
        max_h: 0,
        opacity: 255,
        active_bg: None,
        hover_bg: None,
        bg_grad: None,
        focus_border: None,
        focusable: false,
        is_overlay: false,
        occlude_hit: false,
        auto_repeat: false,
        scrollable_max: None,
        pos: None,
        first_child: None,
        last_child: None,
        on_click: None,
        on_long_press: None,
        on_drag: None,
        on_swipe: None,
        on_key_input: None,
        on_key_nav: None,
        on_navigate_back: None,
        on_secondary_click: None,
        z_index: 0,
        shadow: None,
        grid_cols: 0,
        transition: None,
        clip_children: false,
        align_self: None,
        cursor: None,
        text_color: None,
        text_size: None,
        hover_style: None,
        active_style: None,
        focus_style: None,
        items_baseline: false,
        effects: PaintEffects::EMPTY,
        external_texture: None,
    }
}

impl<'a> Styled for Div<'a> {
    #[inline(always)]
    fn style_mut(&mut self) -> &mut Style {
        &mut self.style
    }
}

impl<'a> Div<'a> {
    #[inline(always)]
    fn has_non_focus_visual_extra(&self) -> bool {
        self.margin != Edges::all(0)
            || self.extra_padding != Edges::all(0)
            || self.min_w != 0
            || self.max_w != 0
            || self.min_h != 0
            || self.max_h != 0
            || self.bg_grad.is_some()
            || self.scrollable_max.is_some()
            || self.pos.is_some()
            || self.shadow.is_some()
            || self.transition.is_some()
            || self.align_self.is_some()
            || self.cursor.is_some()
            || self.text_color.is_some()
            || self.text_size.is_some()
            || self.hover_style.is_some()
            || self.active_style.is_some()
            || self.focus_style.is_some()
            || self.items_baseline
            || !self.effects.is_empty()
            || self.external_texture.is_some()
    }

    #[inline(always)]
    fn has_interactivity(&self) -> bool {
        self.on_click.is_some() || self.has_rare_handlers()
    }

    #[inline(always)]
    fn has_rare_handlers(&self) -> bool {
        self.on_long_press.is_some()
            || self.on_drag.is_some()
            || self.on_swipe.is_some()
            || self.on_key_input.is_some()
            || self.on_key_nav.is_some()
            || self.on_navigate_back.is_some()
            || self.on_secondary_click.is_some()
    }

    /// 设置元素的跨帧唯一标识符(用于过渡动画与滚动位置记忆)。
    #[inline(always)]
    pub fn id(mut self, id: u32) -> Self {
        self.id = Some(id);
        self
    }

    /// 基于当前 [`FrameScope`] 作用域 ID 栈派生全局唯一的元素标识符(避免复用组件时 ID 撞车)。
    #[inline(always)]
    pub fn scoped_id(mut self, cx: &FrameScope, local_id: u32) -> Self {
        self.id = Some(cx.scoped_id(local_id));
        self
    }

    /// 设置相对于父容器内容区左上角的叠层偏移坐标 `(x, y)`(脱离常规线性流,用于叠层拼装)。
    #[inline(always)]
    pub fn pos(mut self, x: i16, y: i16) -> Self {
        self.pos = Some((x, y));
        self
    }

    /// 自动叠加移动端/异形屏安全区域内边距(状态栏、刘海屏、底部 Home Indicator 与软键盘高度,支持 >127px 软键盘不截断)。
    #[inline(always)]
    pub fn safe_area(mut self, cx: &FrameScope) -> Self {
        let sa = cx.safe_area();
        let kb = cx.keyboard_inset();
        self.extra_padding.top = self.extra_padding.top.saturating_add(sa.top.max(0));
        self.extra_padding.right = self.extra_padding.right.saturating_add(sa.right.max(0));
        self.extra_padding.bottom = self
            .extra_padding
            .bottom
            .saturating_add(sa.bottom.saturating_add(kb).max(0));
        self.extra_padding.left = self.extra_padding.left.saturating_add(sa.left.max(0));
        self
    }

    /// 自动叠加顶部安全区域内边距(用于顶部导航栏 `NavBar` 避开刘海屏/状态栏)。
    #[inline(always)]
    pub fn pt_safe(mut self, cx: &FrameScope) -> Self {
        let sa = cx.safe_area();
        self.extra_padding.top = self.extra_padding.top.saturating_add(sa.top.max(0));
        self
    }

    /// 自动叠加底部安全区域与软键盘内边距(用于底部 `TabBar` 或 `BottomSheet` 避开 Home Indicator 与软键盘)。
    #[inline(always)]
    pub fn pb_safe(mut self, cx: &FrameScope) -> Self {
        let sa = cx.safe_area();
        let kb = cx.keyboard_inset();
        self.extra_padding.bottom = self
            .extra_padding
            .bottom
            .saturating_add(sa.bottom.saturating_add(kb).max(0));
        self
    }

    /// 设置水平外边距(左右)。
    #[inline(always)]
    pub fn mx(mut self, px: i16) -> Self {
        self.margin.left = px;
        self.margin.right = px;
        self
    }

    /// 设置垂直外边距(上下)。
    #[inline(always)]
    pub fn my(mut self, px: i16) -> Self {
        self.margin.top = px;
        self.margin.bottom = px;
        self
    }

    /// 设置上外边距。
    #[inline(always)]
    pub fn mt(mut self, px: i16) -> Self {
        self.margin.top = px;
        self
    }

    /// 设置下外边距。
    #[inline(always)]
    pub fn mb(mut self, px: i16) -> Self {
        self.margin.bottom = px;
        self
    }

    /// 设置左外边距。
    #[inline(always)]
    pub fn ml(mut self, px: i16) -> Self {
        self.margin.left = px;
        self
    }

    /// 设置右外边距。
    #[inline(always)]
    pub fn mr(mut self, px: i16) -> Self {
        self.margin.right = px;
        self
    }

    /// 设置最小宽度像素约束。
    #[inline(always)]
    pub fn min_w(mut self, px: i16) -> Self {
        self.min_w = px.max(0);
        self
    }

    /// 设置最大宽度像素约束。
    #[inline(always)]
    pub fn max_w(mut self, px: i16) -> Self {
        self.max_w = px.max(0);
        self
    }

    /// 设置最小高度像素约束。
    #[inline(always)]
    pub fn min_h(mut self, px: i16) -> Self {
        self.min_h = px.max(0);
        self
    }

    /// 设置最大高度像素约束。
    #[inline(always)]
    pub fn max_h(mut self, px: i16) -> Self {
        self.max_h = px.max(0);
        self
    }

    /// 设置容器及其子树的整体不透明度(`0..=255`)。
    #[inline(always)]
    pub fn opacity(mut self, alpha: u8) -> Self {
        self.opacity = alpha;
        self
    }

    /// 设置指针按下(Active)时的背景色。
    #[inline(always)]
    pub fn active_bg(mut self, color: Color) -> Self {
        self.active_bg = Some(color);
        self
    }

    /// 设置指针悬停(Hover)时的背景色。
    #[inline(always)]
    pub fn hover_bg(mut self, color: Color) -> Self {
        self.hover_bg = Some(color);
        self
    }

    /// GPUI 风格悬停态样式闭包(如 `.hover(|s| s.bg(rgb(0xf5f5f5)).border_color(rgb(0x171717)))`)。
    ///
    /// 若闭包仅覆写背景色,自动退化为零分配 `hover_bg` 快速路径;否则完整保留边框、文本色、不透明度与圆角覆写。
    #[inline(always)]
    pub fn hover(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let base = self.hover_style.unwrap_or_else(|| {
            let mut r = StyleRefinement::new();
            r.bg = self.hover_bg;
            r
        });
        let refined = f(base);
        if refined.is_empty() {
            return self;
        }
        if refined.is_bg_only() {
            self.hover_bg = refined.bg;
            self.hover_style = None;
        } else {
            if let Some(bg) = refined.bg {
                self.hover_bg = Some(bg);
            }
            self.hover_style = Some(refined);
        }
        self
    }

    /// GPUI 风格按下态(Active)样式闭包(如 `.active(|s| s.bg(rgb(0xe5e5e5)))`)。
    #[inline(always)]
    pub fn active(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        let base = self.active_style.unwrap_or_else(|| {
            let mut r = StyleRefinement::new();
            r.bg = self.active_bg;
            r
        });
        let refined = f(base);
        if refined.is_empty() {
            return self;
        }
        if refined.is_bg_only() {
            self.active_bg = refined.bg;
            self.active_style = None;
        } else {
            if let Some(bg) = refined.bg {
                self.active_bg = Some(bg);
            }
            self.active_style = Some(refined);
        }
        self
    }

    /// GPUI 风格聚焦态(Focus)样式闭包(如 `.focus(|s| s.border_1().border_color(rgb(0x3b82f6)))`),自动开启 `focusable`。
    #[inline(always)]
    pub fn focus(mut self, f: impl FnOnce(StyleRefinement) -> StyleRefinement) -> Self {
        self.focusable = true;
        let refined = f(self.focus_style.unwrap_or_default());
        if !refined.is_empty() {
            self.focus_style = Some(refined);
        }
        self
    }

    /// 设置容器级可继承文本颜色(子树中未显式调用 `.color(...)` 的 `text(...)` 自动继承)。
    #[inline(always)]
    pub fn text_color(mut self, color: Color) -> Self {
        self.text_color = Some(color);
        self
    }

    /// 设置容器级可继承字号像素(子树中未显式调用 `.size(...)` 的 `text(...)` 自动继承)。
    #[inline(always)]
    pub fn text_size(mut self, px: u8) -> Self {
        self.text_size = Some(px);
        self
    }

    /// 容器级可继承字号 Tailwind 简写(`11px` / `12px` / `14px` / `16px` / `18px` / `22px` / `28px`)。
    #[inline(always)] pub fn text_xs(self) -> Self { self.text_size(11) }
    #[inline(always)] pub fn text_sm(self) -> Self { self.text_size(12) }
    #[inline(always)] pub fn text_base(self) -> Self { self.text_size(14) }
    #[inline(always)] pub fn text_lg(self) -> Self { self.text_size(16) }
    #[inline(always)] pub fn text_xl(self) -> Self { self.text_size(18) }
    #[inline(always)] pub fn text_2xl(self) -> Self { self.text_size(22) }
    #[inline(always)] pub fn text_3xl(self) -> Self { self.text_size(28) }

    /// 浮点不透明度简写(`0.0..=1.0`,映射到 `0..=255`)。
    #[inline(always)]
    pub fn opacity_f(mut self, alpha: f32) -> Self {
        let c = if alpha <= 0.0 {
            0.0
        } else if alpha >= 1.0 {
            1.0
        } else {
            alpha
        };
        self.opacity = (c * 255.0 + 0.5) as u8;
        self
    }

    // --- 外边距 Tailwind 档位简写 ---
    #[inline(always)] pub fn mx_0(self) -> Self { self.mx(0) }
    #[inline(always)] pub fn mx_1(self) -> Self { self.mx(4) }
    #[inline(always)] pub fn mx_2(self) -> Self { self.mx(8) }
    #[inline(always)] pub fn mx_3(self) -> Self { self.mx(12) }
    #[inline(always)] pub fn mx_4(self) -> Self { self.mx(16) }
    #[inline(always)] pub fn my_0(self) -> Self { self.my(0) }
    #[inline(always)] pub fn my_1(self) -> Self { self.my(4) }
    #[inline(always)] pub fn my_2(self) -> Self { self.my(8) }
    #[inline(always)] pub fn my_3(self) -> Self { self.my(12) }
    #[inline(always)] pub fn my_4(self) -> Self { self.my(16) }
    #[inline(always)] pub fn mt_1(self) -> Self { self.mt(4) }
    #[inline(always)] pub fn mt_2(self) -> Self { self.mt(8) }
    #[inline(always)] pub fn mt_3(self) -> Self { self.mt(12) }
    #[inline(always)] pub fn mt_4(self) -> Self { self.mt(16) }
    #[inline(always)] pub fn mb_1(self) -> Self { self.mb(4) }
    #[inline(always)] pub fn mb_2(self) -> Self { self.mb(8) }
    #[inline(always)] pub fn mb_3(self) -> Self { self.mb(12) }
    #[inline(always)] pub fn mb_4(self) -> Self { self.mb(16) }
    #[inline(always)] pub fn ml_1(self) -> Self { self.ml(4) }
    #[inline(always)] pub fn ml_2(self) -> Self { self.ml(8) }
    #[inline(always)] pub fn ml_3(self) -> Self { self.ml(12) }
    #[inline(always)] pub fn ml_4(self) -> Self { self.ml(16) }
    #[inline(always)] pub fn mr_1(self) -> Self { self.mr(4) }
    #[inline(always)] pub fn mr_2(self) -> Self { self.mr(8) }
    #[inline(always)] pub fn mr_3(self) -> Self { self.mr(12) }
    #[inline(always)] pub fn mr_4(self) -> Self { self.mr(16) }

    // --- 定位、显示、光标与阴影 Tailwind / GPUI 简写 ---
    /// 语义化定位标记(Velox 子节点 `.pos(x, y)` 天然相对父容器左上角定位)。
    #[inline(always)] pub fn relative(self) -> Self { self }
    /// 块级垂直容器简写(等价 `.flex_col()`)。
    #[inline(always)] pub fn block(self) -> Self { self.flex_col() }
    /// 行内弹性容器简写(等价 `.flex_row()`)。
    #[inline(always)] pub fn inline_flex(self) -> Self { self.flex_row() }

    #[inline(always)] pub fn cursor_pointer(self) -> Self { self.cursor(CursorShape::Pointer) }
    #[inline(always)] pub fn cursor_default(self) -> Self { self.cursor(CursorShape::Default) }
    #[inline(always)] pub fn cursor_text(self) -> Self { self.cursor(CursorShape::Text) }
    #[inline(always)] pub fn cursor_not_allowed(self) -> Self { self.cursor(CursorShape::NotAllowed) }
    #[inline(always)] pub fn cursor_grab(self) -> Self { self.cursor(CursorShape::Grab) }
    #[inline(always)] pub fn cursor_grabbing(self) -> Self { self.cursor(CursorShape::Grabbing) }

    #[inline(always)] pub fn shadow_none(mut self) -> Self { self.shadow = None; self }
    #[inline(always)] pub fn shadow_sm(self) -> Self { self.shadow(4, 0, 1, Color::rgba(0, 0, 0, 48)) }
    #[inline(always)] pub fn shadow_md(self) -> Self { self.shadow(10, 0, 3, Color::rgba(0, 0, 0, 72)) }
    #[inline(always)] pub fn shadow_lg(self) -> Self { self.shadow(18, 0, 6, Color::rgba(0, 0, 0, 96)) }
    #[inline(always)] pub fn shadow_xl(self) -> Self { self.shadow(28, 0, 10, Color::rgba(0, 0, 0, 120)) }

    /// 设置自顶向下的垂直线性渐变终点颜色(起点颜色为 `.bg(...)`)。
    #[inline(always)]
    pub fn bg_gradient(mut self, to_color: Color) -> Self {
        self.bg_grad = Some((to_color, false));
        self
    }

    /// 设置自左向右的水平线性渐变终点颜色(起点颜色为 `.bg(...)`)。
    #[inline(always)]
    pub fn bg_gradient_x(mut self, to_color: Color) -> Self {
        self.bg_grad = Some((to_color, true));
        self
    }

    /// 将当前节点及子树提升至顶层 Z-Order 渲染队列(脱离父级裁剪并覆盖在普通层之上,用于 `Modal` / `Dropdown`)。
    #[inline(always)]
    pub fn overlay(mut self) -> Self {
        self.is_overlay = true;
        self
    }

    /// 拦截并吞噬落在该容器区域内的指针命中事件,阻断点击穿透至底层控件(如 `Modal` 遮罩层)。
    #[inline(always)]
    pub fn occlude_hit(mut self) -> Self {
        self.occlude_hit = true;
        self
    }

    /// 开启按住不放自动连发(按住超过 `400ms` 后每 `100ms` 自动触发一次 `on_click`)。
    #[inline(always)]
    pub fn auto_repeat(mut self) -> Self {
        self.auto_repeat = true;
        self
    }

    /// 绑定原始长按回调闭包(按住 `>= 500ms` 触发)。
    #[inline(always)]
    pub fn on_long_press_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
        self.on_long_press = Some(handler);
        self.focusable = true;
        self
    }

    /// 绑定与 `cx.listener(...)` 兼容的长按回调闭包(按住 `>= 500ms` 触发)。
    #[inline(always)]
    pub fn on_long_press(
        mut self,
        cx: &'a FrameScope,
        handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
    ) -> Self {
        let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
            let (x, y) = acx.click_position();
            handler(crate::pod::ClickEvent { x, y }, acx);
        });
        self.on_long_press = Some(wrapped);
        self.focusable = true;
        self
    }

    /// 设置物理按键/旋钮焦点命中时的边框高亮样式,并自动开启 `focusable`。
    #[inline(always)]
    pub fn focus_border(mut self, width: u8, color: Color) -> Self {
        self.focus_border = Some((width, color));
        self.focusable = true;
        self
    }

    /// 显式禁止在聚焦态绘制外围焦点边框(适用于 `Slider`、透明容器、内嵌滚动视口及 `.unstyled()` 控件)。
    #[inline(always)]
    pub fn no_focus_border(mut self) -> Self {
        self.focus_border = Some((0, Color::TRANSPARENT));
        self
    }

    /// 显式指定鼠标悬停在此节点时的光标形状(如 `CursorShape::Pointer` / `Grab` / `Text` / `NotAllowed` / `ResizeEW`)。
    #[inline(always)]
    pub fn cursor(mut self, cursor: CursorShape) -> Self {
        self.cursor = Some(cursor);
        self
    }

    /// 显式将该节点加入物理按键/旋钮焦点导航链。
    #[inline(always)]
    pub fn focusable(mut self) -> Self {
        self.focusable = true;
        self
    }

    /// 显式禁止该节点抢占键盘焦点(适用于右键上下文菜单项、清空按钮与点击外部关闭遮罩,点击后保持底层输入框焦点不丢失)。
    #[inline(always)]
    pub fn unfocusable(mut self) -> Self {
        self.focusable = false;
        self
    }

    /// 将当前容器标记为可触摸拖拽/滚轮滚动的视口,并指定最大可滚动偏移量 `max_scroll`(自动开启 `overflow_hidden`)。
    #[inline(always)]
    pub fn scrollable(mut self, max_scroll: i16) -> Self {
        self.scrollable_max = Some(max_scroll.max(0));
        self.clip_children = true;
        self
    }

    /// 将当前容器标记为按内容实际尺寸自动测量最大滚动范围的视口(自动开启 `overflow_hidden`,同时支持垂直与水平滚动)。
    #[inline(always)]
    pub fn scrollable_auto(mut self) -> Self {
        self.scrollable_max = Some(i16::MAX);
        self.clip_children = true;
        self
    }

    /// 独立覆盖当前节点在父容器交叉轴上的对齐方式(`align-self`)。
    #[inline(always)]
    pub fn align_self(mut self, align: AlignItems) -> Self {
        self.align_self = Some(align);
        self
    }

    /// 交叉轴自身起始对齐(`self-start`)。
    #[inline(always)]
    pub fn self_start(self) -> Self {
        self.align_self(AlignItems::Start)
    }

    /// 交叉轴自身居中对齐(`self-center`)。
    #[inline(always)]
    pub fn self_center(self) -> Self {
        self.align_self(AlignItems::Center)
    }

    /// 交叉轴自身末尾对齐(`self-end`)。
    #[inline(always)]
    pub fn self_end(self) -> Self {
        self.align_self(AlignItems::End)
    }

    /// 交叉轴自身拉伸对齐(`self-stretch`)。
    #[inline(always)]
    pub fn self_stretch(self) -> Self {
        self.align_self(AlignItems::Stretch)
    }

    /// 在水平行容器(`flex_row`)中启用子元素按文本基线对齐(`items-baseline`)。
    #[inline(always)]
    pub fn items_baseline(mut self) -> Self {
        self.items_baseline = true;
        self
    }

    /// 启用裁剪子元素(等价 CSS `overflow: hidden`)。
    #[inline(always)]
    pub fn overflow_hidden(mut self) -> Self {
        self.clip_children = true;
        self
    }

    /// 关闭子元素裁剪(等价 CSS `overflow: visible`)。
    #[inline(always)]
    pub fn overflow_visible(mut self) -> Self {
        self.clip_children = false;
        self
    }

    /// 启用自动测量边界的滚动视口(等价 CSS `overflow: auto/scroll`)。
    #[inline(always)]
    pub fn overflow_scroll(self) -> Self {
        self.scrollable_auto()
    }

    /// 启用垂直方向自动测量滚动视口(等价 CSS `overflow-y: auto/scroll`)。
    #[inline(always)]
    pub fn overflow_y_scroll(self) -> Self {
        self.flex_col().scrollable_auto()
    }

    /// 启用水平方向自动测量滚动视口(等价 CSS `overflow-x: auto/scroll`)。
    #[inline(always)]
    pub fn overflow_x_scroll(self) -> Self {
        self.flex_row().scrollable_auto()
    }

    /// Tailwind / GPUI 风格默认过渡动画(150ms 颜色 + 不透明度 + 几何边界,EaseOut)。
    #[inline(always)]
    pub fn transition(self) -> Self {
        self.animate_all(Duration::from_millis(150))
    }

    /// Tailwind / GPUI 风格颜色过渡简写(150ms 背景/边框/文本颜色平滑过渡)。
    #[inline(always)]
    pub fn transition_colors(self) -> Self {
        self.animate_color(Duration::from_millis(150))
    }

    /// Tailwind / GPUI 风格不透明度过渡简写(150ms 淡入淡出)。
    #[inline(always)]
    pub fn transition_opacity(self) -> Self {
        self.animate_opacity(Duration::from_millis(150))
    }

    /// Tailwind / GPUI 风格全属性过渡简写(150ms 颜色 + 边界 + 不透明度)。
    #[inline(always)]
    pub fn transition_all(self) -> Self {
        self.animate_all(Duration::from_millis(150))
    }

    /// 显式禁用该节点上的过渡动画(等价 CSS `transition: none`)。
    #[inline(always)]
    pub fn transition_none(mut self) -> Self {
        self.transition = None;
        self
    }

    #[inline(always)] pub fn duration_75(self) -> Self { self.duration(Duration::from_millis(75)) }
    #[inline(always)] pub fn duration_100(self) -> Self { self.duration(Duration::from_millis(100)) }
    #[inline(always)] pub fn duration_150(self) -> Self { self.duration(Duration::from_millis(150)) }
    #[inline(always)] pub fn duration_200(self) -> Self { self.duration(Duration::from_millis(200)) }
    #[inline(always)] pub fn duration_300(self) -> Self { self.duration(Duration::from_millis(300)) }
    #[inline(always)] pub fn duration_500(self) -> Self { self.duration(Duration::from_millis(500)) }

    #[inline(always)] pub fn ease_linear(self) -> Self { self.ease(Easing::Linear) }
    #[inline(always)] pub fn ease_in(self) -> Self { self.ease(Easing::EaseIn) }
    #[inline(always)] pub fn ease_out(self) -> Self { self.ease(Easing::EaseOut) }
    #[inline(always)] pub fn ease_in_out(self) -> Self { self.ease(Easing::EaseInOut) }
    #[inline(always)] pub fn ease_spring(self) -> Self { self.ease(Easing::Spring) }

    /// 声明背景色与边框/子文本颜色的跨帧平滑过渡动画。
    #[inline(always)]
    pub fn animate_color(mut self, duration: Duration) -> Self {
        let ms = duration.as_millis().min(u16::MAX as u128) as u16;
        let mut spec = self.transition.unwrap_or(TransitionSpec {
            duration_ms: ms,
            ..TransitionSpec::default()
        });
        spec.duration_ms = ms;
        spec.animate_color = true;
        self.transition = Some(spec);
        self
    }

    /// 声明背景色的跨帧平滑过渡动画。
    #[inline(always)]
    pub fn animate_bg(self, duration: Duration) -> Self {
        self.animate_color(duration)
    }

    /// 声明几何边界框(位置/尺寸)的跨帧平滑过渡动画。
    #[inline(always)]
    pub fn animate_bounds(mut self, duration: Duration) -> Self {
        let ms = duration.as_millis().min(u16::MAX as u128) as u16;
        let mut spec = self.transition.unwrap_or(TransitionSpec {
            duration_ms: ms,
            ..TransitionSpec::default()
        });
        spec.duration_ms = ms;
        spec.animate_bounds = true;
        self.transition = Some(spec);
        self
    }

    /// 设置过渡动画的缓动曲线(`Linear` / `EaseIn` / `EaseOut` / `EaseInOut` / `Spring`)。
    #[inline(always)]
    pub fn ease(mut self, easing: Easing) -> Self {
        let mut spec = self.transition.unwrap_or_default();
        spec.easing = easing;
        self.transition = Some(spec);
        self
    }

    /// 设置动画为无限循环模式。
    #[inline(always)]
    pub fn repeat_infinite(mut self) -> Self {
        let mut spec = self.transition.unwrap_or_default();
        spec.loop_mode = LoopMode::Infinite;
        self.transition = Some(spec);
        self
    }

    /// 设置动画为无限往复(`0 -> 1 -> 0`)循环模式。
    #[inline(always)]
    pub fn repeat_ping_pong(mut self) -> Self {
        let mut spec = self.transition.unwrap_or_default();
        spec.loop_mode = LoopMode::PingPong;
        self.transition = Some(spec);
        self
    }

    /// 修改当前过渡动画的时长(不改变已声明的插值属性)。
    #[inline(always)]
    pub fn duration(mut self, duration: Duration) -> Self {
        let mut spec = self.transition.unwrap_or_default();
        spec.duration_ms = duration_to_ms(duration);
        self.transition = Some(spec);
        self
    }

    /// 设置过渡/入场动画开始前的延迟(用于列表项交错 `stagger` 入场等编排)。
    #[inline(always)]
    pub fn delay(mut self, delay: Duration) -> Self {
        let mut spec = self.transition.unwrap_or_default();
        spec.delay_ms = duration_to_ms(delay);
        self.transition = Some(spec);
        self
    }

    /// 声明容器整体不透明度(`.opacity(...)`)的跨帧淡入淡出过渡,作用于整个子树。
    #[inline(always)]
    pub fn animate_opacity(mut self, duration: Duration) -> Self {
        let mut spec = self.transition.unwrap_or_default();
        spec.duration_ms = duration_to_ms(duration);
        spec.animate_opacity = true;
        self.transition = Some(spec);
        self
    }

    /// 同时声明颜色、几何边界与不透明度的跨帧过渡(等价 CSS `transition: all`)。
    #[inline(always)]
    pub fn animate_all(mut self, duration: Duration) -> Self {
        let mut spec = self.transition.unwrap_or_default();
        spec.duration_ms = duration_to_ms(duration);
        spec.animate_color = true;
        spec.animate_bounds = true;
        spec.animate_opacity = true;
        self.transition = Some(spec);
        self
    }

    /// 声明首次出现时的入场动画:从不透明度 `opacity` 与偏移 `(dx, dy)` 平滑过渡到布局目标状态。
    #[inline(always)]
    pub fn enter_from(mut self, opacity: u8, dx: i16, dy: i16, duration: Duration) -> Self {
        let mut spec = self.transition.unwrap_or_default();
        spec.duration_ms = duration_to_ms(duration);
        spec.enter = Some(EnterFrom { opacity, dx, dy });
        self.transition = Some(spec);
        self
    }

    /// 首次出现时从完全透明淡入(入场动画快捷方式)。
    #[inline(always)]
    pub fn fade_in(self, duration: Duration) -> Self {
        self.enter_from(0, 0, 0, duration)
    }

    /// 首次出现时从偏移 `(dx, dy)` 处滑入并同时淡入(入场动画快捷方式)。
    #[inline(always)]
    pub fn slide_in(self, dx: i16, dy: i16, duration: Duration) -> Self {
        self.enter_from(0, dx, dy, duration)
    }

    /// 声明在 `from_bg` 与当前 `.bg(...)` 之间无限往复呼吸的循环动画(如呼吸指示灯)。
    #[inline(always)]
    pub fn ping_pong_bg(mut self, from_bg: Color, duration: Duration) -> Self {
        let ms = duration.as_millis().min(u16::MAX as u128) as u16;
        let mut spec = self.transition.unwrap_or_default();
        spec.duration_ms = ms;
        spec.animate_color = true;
        spec.loop_mode = LoopMode::PingPong;
        spec.easing = Easing::EaseInOut;
        spec.pulse_from_bg = Some(from_bg);
        self.transition = Some(spec);
        self
    }

    /// 绑定原始点击回调闭包。
    #[inline(always)]
    pub fn on_click_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
        self.on_click = Some(handler);
        self.focusable = true;
        self
    }

    /// 绑定与 `cx.listener(...)` 兼容的点击回调闭包。
    #[inline(always)]
    pub fn on_click(
        mut self,
        cx: &'a FrameScope,
        handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
    ) -> Self {
        let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
            let (x, y) = acx.click_position();
            handler(crate::pod::ClickEvent { x, y }, acx);
        });
        self.on_click = Some(wrapped);
        self.focusable = true;
        self
    }

    /// 绑定原始鼠标右键(次键)点击回调闭包。
    #[inline(always)]
    pub fn on_secondary_click_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
        self.on_secondary_click = Some(handler);
        self
    }

    /// 绑定与 `cx.listener(...)` 兼容的鼠标右键(次键)点击回调闭包。
    #[inline(always)]
    pub fn on_secondary_click(
        mut self,
        cx: &'a FrameScope,
        handler: &'a dyn Fn(crate::pod::ClickEvent, &mut ActionScope),
    ) -> Self {
        let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
            let (x, y) = acx.click_position();
            handler(crate::pod::ClickEvent { x, y }, acx);
        });
        self.on_secondary_click = Some(wrapped);
        self
    }

    /// 绑定原始拖拽回调闭包。
    #[inline(always)]
    pub fn on_drag_raw(mut self, handler: &'a dyn Fn(&mut ActionScope)) -> Self {
        self.on_drag = Some(handler);
        self
    }

    /// 绑定与 `cx.listener(...)` 兼容的触摸/鼠标拖拽回调闭包(如 `Slider` 连续滑动)。
    #[inline(always)]
    pub fn on_drag(
        mut self,
        cx: &'a FrameScope,
        handler: &'a dyn Fn(DragEvent, &mut ActionScope),
    ) -> Self {
        let wrapped = cx.arena().alloc(move |acx: &mut ActionScope| {
            let ev = acx.drag_event();
            handler(ev, acx);
        });
        self.on_drag = Some(wrapped);
        self
    }

    /// 绑定与 `cx.listener(...)` 兼容的四向快速滑动手势(`SwipeDir`)回调闭包。
    #[inline(always)]
    pub fn on_swipe(
        mut self,
        _cx: &'a FrameScope,
        handler: &'a dyn Fn(crate::pod::SwipeDir, &mut ActionScope),
    ) -> Self {
        self.on_swipe = Some(handler);
        self
    }

    /// 设置列表项或动态节点的稳定键值(自动哈希为 `u32` 元素 ID,消除手动管理魔法数字冲突)。
    #[inline(always)]
    pub fn key(mut self, key: impl AsRef<[u8]>) -> Self {
        let mut h: u32 = 0x811c_9dc5;
        for &b in key.as_ref() {
            h ^= b as u32;
            h = h.wrapping_mul(0x0100_0193);
        }
        self.id = Some(h | 1);
        self
    }

    /// 设置元素的 Z-Index 层级(用于在同父容器兄弟节点间调整前后绘制顺序;若需脱离父容器裁剪并提升至全屏最顶层,请显式调用 `.overlay()`)。
    #[inline(always)]
    pub fn z_index(mut self, z: i8) -> Self {
        self.z_index = z;
        self
    }

    /// 绑定键盘字符/编辑键回调闭包(用于聚焦态 `TextInput` 接收 `Char(c)`、`Backspace`、`Delete`、`Left`/`Right` 等)。
    #[inline(always)]
    pub fn on_key_input(
        mut self,
        handler: &'a dyn Fn(crate::pod::KeyCode, &mut ActionScope),
    ) -> Self {
        self.on_key_input = Some(handler);
        self.focusable = true;
        self
    }

    /// 绑定非文本控件的局部键盘导航回调闭包(供 `Slider` / `Tabs` / `RadioGroup` / `Select` 在聚焦或浮层打开时响应方向键、`Home`/`End`、`Enter`/`Space`,绝不吞掉 `Tab`/`BackTab` 焦点切换)。
    #[inline(always)]
    pub fn on_key_nav(
        mut self,
        handler: &'a dyn Fn(crate::pod::KeyCode, &mut ActionScope),
    ) -> Self {
        self.on_key_nav = Some(handler);
        self
    }

    /// 绑定系统级返回请求(Android Back / 浏览器后退 / 左边缘返回手势)回调闭包。
    #[inline(always)]
    pub fn on_navigate_back(
        mut self,
        handler: &'a dyn Fn(&mut ActionScope),
    ) -> Self {
        self.on_navigate_back = Some(handler);
        self
    }

    /// 设置解析式 SDF 外阴影(`blur`: 模糊扩散半径 `1..=64px`,`offset_x`/`offset_y`: 阴影偏移,`color`: 阴影颜色)。
    #[inline(always)]
    pub fn shadow(mut self, blur: u8, offset_x: i8, offset_y: i8, color: Color) -> Self {
        if blur > 0 && color.a() > 0 {
            self.shadow = Some((blur.min(64), offset_x, offset_y, color));
        }
        self
    }

    /// 将当前容器启用为指定列数 `cols` 的二维网格布局。
    #[inline(always)]
    pub fn grid(mut self, cols: u8) -> Self {
        self.grid_cols = cols.max(1);
        self
    }

    /// 链式追加单个子节点(通过 `ScratchArena` + `Cell` 侵入式链表连接,$O(1)$ 耗时,100% 遵循 Stacked Borrows 安全规范)。
    pub fn child(mut self, cx: &'a FrameScope, child: impl IntoElement<'a>) -> Self {
        let elem = child.into_element(cx);
        let node: &'a ElementNode<'a> = cx.arena().alloc_temp(ElementNode {
            element: elem,
            next_sibling: Cell::new(None),
        });
        if let Some(last) = self.last_child {
            last.next_sibling.set(Some(node));
        } else {
            self.first_child = Some(node);
        }
        self.last_child = Some(node);
        self
    }

    /// 可选子节点追加(当 `child` 为 `Some(c)` 时追加,否则无操作)。
    #[inline(always)]
    pub fn child_opt(self, cx: &'a FrameScope, child: Option<impl IntoElement<'a>>) -> Self {
        match child {
            Some(c) => self.child(cx, c),
            None => self,
        }
    }

    /// 批量追加任意迭代器生成的子节点序列。
    pub fn children<I, E>(mut self, cx: &'a FrameScope, iter: I) -> Self
    where
        I: IntoIterator<Item = E>,
        E: IntoElement<'a>,
    {
        for item in iter {
            self = self.child(cx, item);
        }
        self
    }

    /// 按当前语言的书写方向设置主轴“逻辑起始端对齐”(LTR 为 `Start`,RTL 自动镜像为 `End`)。
    #[inline(always)]
    pub fn justify_start_localized(mut self, cx: &FrameScope) -> Self {
        if cx.is_rtl() {
            self.style.set_justify(crate::style::Justify::End);
        } else {
            self.style.set_justify(crate::style::Justify::Start);
        }
        self
    }

    /// 按当前语言的书写方向设置主轴“逻辑末端对齐”(LTR 为 `End`,RTL 自动镜像为 `Start`)。
    #[inline(always)]
    pub fn justify_end_localized(mut self, cx: &FrameScope) -> Self {
        if cx.is_rtl() {
            self.style.set_justify(crate::style::Justify::Start);
        } else {
            self.style.set_justify(crate::style::Justify::End);
        }
        self
    }

    /// 按当前语言方向设置逻辑起始端内边距(CSS `padding-inline-start`:LTR 为 `pl`,RTL 自动镜像为 `pr`)。
    #[inline(always)]
    pub fn ps(self, cx: &FrameScope, px: i8) -> Self {
        if cx.is_rtl() {
            self.pr(px)
        } else {
            self.pl(px)
        }
    }

    /// 按当前语言方向设置逻辑末端内边距(CSS `padding-inline-end`:LTR 为 `pr`,RTL 自动镜像为 `pl`)。
    #[inline(always)]
    pub fn pe(self, cx: &FrameScope, px: i8) -> Self {
        if cx.is_rtl() {
            self.pl(px)
        } else {
            self.pr(px)
        }
    }

    /// 按当前语言方向设置逻辑起始端外边距(CSS `margin-inline-start`:LTR 为 `ml`,RTL 自动镜像为 `mr`)。
    #[inline(always)]
    pub fn ms(self, cx: &FrameScope, px: i16) -> Self {
        if cx.is_rtl() {
            self.mr(px)
        } else {
            self.ml(px)
        }
    }

    /// 按当前语言方向设置逻辑末端外边距(CSS `margin-inline-end`:LTR 为 `mr`,RTL 自动镜像为 `ml`)。
    #[inline(always)]
    pub fn me(self, cx: &FrameScope, px: i16) -> Self {
        if cx.is_rtl() {
            self.ml(px)
        } else {
            self.mr(px)
        }
    }

    // ========================================================================
    // 高级渲染能力:四角独立圆角 / 多色标渐变 / 混合模式 / 滤镜 / 毛玻璃 / 2D 变换 / 路径裁剪
    // ========================================================================

    /// 设置四角独立圆角 `[tl, tr, br, bl]`。
    #[inline(always)]
    pub fn rounded_corners(mut self, radii: CornerRadii) -> Self {
        self.effects.corner_radii = Some(radii);
        self
    }

    /// 仅设置顶部两角圆角(底部直角)。
    #[inline(always)]
    pub fn rounded_t(self, r: u8) -> Self {
        self.rounded_corners(CornerRadii::top(r))
    }

    /// 仅设置底部两角圆角(顶部直角)。
    #[inline(always)]
    pub fn rounded_b(self, r: u8) -> Self {
        self.rounded_corners(CornerRadii::bottom(r))
    }

    /// 仅设置左侧两角圆角(右侧直角)。
    #[inline(always)]
    pub fn rounded_l(self, r: u8) -> Self {
        self.rounded_corners(CornerRadii::left(r))
    }

    /// 仅设置右侧两角圆角(左侧直角)。
    #[inline(always)]
    pub fn rounded_r(self, r: u8) -> Self {
        self.rounded_corners(CornerRadii::right(r))
    }

    /// 设置任意完整填充画刷 [`Paint`](多色标 Linear / Radial / Conic 渐变或纯色)。
    #[inline(always)]
    pub fn bg_paint(mut self, paint: Paint<'a>) -> Self {
        self.effects.bg_paint = Some(paint);
        self
    }

    /// 设置多色标任意角度线性渐变(色标切片自动拷贝进帧级 `ScratchArena`,零堆分配)。
    #[inline]
    pub fn bg_linear_gradient(
        mut self,
        cx: &'a FrameScope,
        angle_deg: f32,
        stops: &[ColorStop],
    ) -> Self {
        let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
        self.effects.bg_paint = Some(Paint::linear_angle(angle_deg, arena_stops));
        self
    }

    /// 设置多色标居中径向渐变。
    #[inline]
    pub fn bg_radial_gradient(mut self, cx: &'a FrameScope, stops: &[ColorStop]) -> Self {
        let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
        self.effects.bg_paint = Some(Paint::radial(arena_stops));
        self
    }

    /// 设置多色标双圆焦点径向渐变。
    #[inline]
    pub fn bg_radial_focal_gradient(
        mut self,
        cx: &'a FrameScope,
        center: [f32; 2],
        radius: f32,
        focal: [f32; 2],
        focal_radius: f32,
        stops: &[ColorStop],
    ) -> Self {
        let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
        self.effects.bg_paint = Some(Paint::radial_focal(
            center,
            radius,
            focal,
            focal_radius,
            arena_stops,
        ));
        self
    }

    /// 设置多色标锥形/扫描渐变(`start_angle_deg` 以 12 点钟方向为 `0°` 顺时针)。
    #[inline]
    pub fn bg_conic_gradient(
        mut self,
        cx: &'a FrameScope,
        start_angle_deg: f32,
        stops: &[ColorStop],
    ) -> Self {
        let arena_stops = cx.arena().alloc_iter(stops.iter().copied());
        self.effects.bg_paint = Some(Paint::conic(start_angle_deg, arena_stops));
        self
    }

    /// 修改已配置的背景渐变延展模式(`Pad` / `Repeat` / `Reflect`)。
    #[inline]
    pub fn bg_gradient_extend(mut self, mode: ExtendMode) -> Self {
        if let Some(p) = self.effects.bg_paint {
            self.effects.bg_paint = Some(p.with_extend(mode));
        }
        self
    }

    /// 修改已配置的背景渐变插值色彩空间(`Srgb` / `LinearRgb` / `Oklab`)。
    #[inline]
    pub fn bg_gradient_space(mut self, space: GradientSpace) -> Self {
        if let Some(p) = self.effects.bg_paint {
            self.effects.bg_paint = Some(p.with_space(space));
        }
        self
    }

    /// 设置图元/图层合成与混合模式(完整 27 种 Porter-Duff + W3C 混合模式)。
    #[inline(always)]
    pub fn blend_mode(mut self, mode: BlendMode) -> Self {
        self.effects.blend_mode = mode;
        self
    }

    /// 显式开启隔离合成图层(使 `.opacity(...)` 作为真正的 Group Opacity 合成,重叠子元素互不透底)。
    #[inline(always)]
    pub fn isolate(mut self) -> Self {
        self.effects.isolate = true;
        self
    }

    /// 设置完整图层后处理滤镜 [`LayerFilter`]。
    #[inline(always)]
    pub fn filter(mut self, filter: LayerFilter) -> Self {
        self.effects.filter = filter;
        self
    }

    /// 设置子树高斯/Kawase 模糊半径(逻辑像素)。
    #[inline(always)]
    pub fn blur(mut self, radius: u8) -> Self {
        self.effects.filter.blur_radius = radius;
        self
    }

    /// 叠加 4×5 色彩矩阵滤镜(可多次链式调用,自动矩阵右乘复合)。
    #[inline]
    pub fn color_matrix(mut self, matrix: ColorMatrix) -> Self {
        self.effects.filter.color_matrix = Some(match self.effects.filter.color_matrix {
            Some(prev) => prev.then(matrix),
            None => matrix,
        });
        self
    }

    /// 灰度滤镜(`amount ∈ [0.0, 1.0]`)。
    #[inline]
    pub fn grayscale(self, amount: f32) -> Self {
        self.color_matrix(ColorMatrix::grayscale(amount))
    }

    /// 怀旧棕褐滤镜(`amount ∈ [0.0, 1.0]`)。
    #[inline]
    pub fn sepia(self, amount: f32) -> Self {
        self.color_matrix(ColorMatrix::sepia(amount))
    }

    /// 饱和度滤镜(`1.0` 不变,`0.0` 灰度,`> 1.0` 增艳)。
    #[inline]
    pub fn saturate(self, amount: f32) -> Self {
        self.color_matrix(ColorMatrix::saturate(amount))
    }

    /// 亮度滤镜(`1.0` 不变)。
    #[inline]
    pub fn brightness(self, amount: f32) -> Self {
        self.color_matrix(ColorMatrix::brightness(amount))
    }

    /// 对比度滤镜(`1.0` 不变)。
    #[inline]
    pub fn contrast(self, amount: f32) -> Self {
        self.color_matrix(ColorMatrix::contrast(amount))
    }

    /// 色相旋转滤镜(角度制 `deg`)。
    #[inline]
    pub fn hue_rotate(self, deg: f32) -> Self {
        self.color_matrix(ColorMatrix::hue_rotate(deg))
    }

    /// 反色滤镜(`amount ∈ [0.0, 1.0]`)。
    #[inline]
    pub fn invert(self, amount: f32) -> Self {
        self.color_matrix(ColorMatrix::invert(amount))
    }

    /// 任意子树轮廓投影滤镜(CSS `filter: drop-shadow(...)`)。
    #[inline(always)]
    pub fn drop_shadow(mut self, blur: u8, offset_x: i8, offset_y: i8, color: Color) -> Self {
        self.effects.filter.drop_shadow = Some(DropShadow::new(blur, offset_x, offset_y, color));
        self
    }

    /// 设置完整实时背景滤镜 [`BackdropFilter`]。
    #[inline(always)]
    pub fn backdrop_filter(mut self, filter: BackdropFilter) -> Self {
        self.effects.backdrop_filter = filter;
        self
    }

    /// 开启实时背景毛玻璃模糊(CSS `backdrop-filter: blur(...)`)。
    #[inline(always)]
    pub fn backdrop_blur(mut self, radius: u8) -> Self {
        self.effects.backdrop_filter.blur_radius = radius;
        self
    }

    /// 一键开启现代 Acrylic / Vibrancy 毛玻璃材质(背景模糊 + 饱和度提亮 + 微颗粒 + 磨砂着色)。
    #[inline]
    pub fn backdrop_acrylic(mut self, blur_radius: u8, tint: Color) -> Self {
        self.effects.backdrop_filter = BackdropFilter::acrylic(blur_radius, tint);
        self
    }

    /// 叠加 2D 仿射变换(以容器自身中心为原点;支持多次链式复合)。
    #[inline]
    pub fn transform(mut self, t: Transform2D) -> Self {
        self.effects.transform = Some(match self.effects.transform {
            Some(prev) => prev.concat(t),
            None => t,
        });
        self
    }

    /// 绕元素中心顺时针旋转(角度制 `deg`)。
    #[inline]
    pub fn rotate_deg(self, deg: f32) -> Self {
        self.transform(Transform2D::rotate(deg))
    }

    /// 绕元素中心缩放 `(sx, sy)`。
    #[inline]
    pub fn scale(self, sx: f32, sy: f32) -> Self {
        self.transform(Transform2D::scale(sx, sy))
    }

    /// 沿 `(tx, ty)` 平移视觉位置(不触发布局重排)。
    #[inline]
    pub fn translate(self, tx: f32, ty: f32) -> Self {
        self.transform(Transform2D::translate(tx, ty))
    }

    /// 错切变换(角度制 `ax`, `ay`)。
    #[inline]
    pub fn skew(self, ax_deg: f32, ay_deg: f32) -> Self {
        let mut s = self;
        if ax_deg != 0.0 {
            s = s.transform(Transform2D::skew_x(ax_deg));
        }
        if ay_deg != 0.0 {
            s = s.transform(Transform2D::skew_y(ay_deg));
        }
        s
    }

    /// 使用任意静态 SVG Path 字符串(`d="..."`)对容器及子树执行抗锯齿矢量裁剪。
    #[inline(always)]
    pub fn clip_path(mut self, path_d: &'static str) -> Self {
        self.effects.clip_path = Some(StrRef::Static(path_d));
        self
    }

    /// 使用帧内动态 SVG Path 字符串对容器及子树执行抗锯齿矢量裁剪。
    #[inline]
    pub fn clip_path_str(mut self, cx: &'a FrameScope, path_d: &str) -> Self {
        let buf = cx.arena().alloc_bytes(path_d.len());
        buf.copy_from_slice(path_d.as_bytes());
        // SAFETY: `buf` 刚刚完整拷贝自合法 UTF-8 `&str`
        let s = unsafe { core::str::from_utf8_unchecked(buf) };
        self.effects.clip_path = Some(StrRef::Scratch(s));
        self
    }

    /// 绑定外部零拷贝 GPU 纹理槽位(如海图/雷达/视频/3D 引擎共享的 `wgpu::TextureView`)。
    ///
    /// - 硬件 `wgpu` 后端直接零拷贝采样该纹理并支持四角圆角、混合与上方实时毛玻璃联动;
    /// - 软件光栅或无 GPU 模式下自动降级为 `fallback_bg` 圆角占位。
    #[inline(always)]
    pub fn external_texture(mut self, id: crate::paint::ExternalTextureId) -> Self {
        self.external_texture = Some(crate::paint::ExternalTextureSpec::new(id));
        self
    }

    /// 绑定完整配置的外部零拷贝纹理描述 [`ExternalTextureSpec`](crate::paint::ExternalTextureSpec)。
    #[inline(always)]
    pub fn external_texture_spec(mut self, spec: crate::paint::ExternalTextureSpec) -> Self {
        self.external_texture = Some(spec);
        self
    }

    /// 配置外部纹理的归一化采样 UV 矩形 `[u0, v0, u1, v1]`。
    #[inline(always)]
    pub fn external_texture_uv(mut self, uv_rect: [f32; 4]) -> Self {
        if let Some(ext) = &mut self.external_texture {
            ext.uv_rect = uv_rect;
        }
        self
    }

    /// 更新外部纹理内容版本号(外部引擎重绘同一纹理时递增此值,精准驱动脏区刷新)。
    #[inline(always)]
    pub fn external_texture_revision(mut self, revision: u64) -> Self {
        if let Some(ext) = &mut self.external_texture {
            ext.content_revision = revision;
        }
        self
    }

    /// 配置外部纹理的 Alpha 预乘约定与软件降级占位色。
    #[inline(always)]
    pub fn external_texture_format(mut self, premultiplied: bool, fallback_bg: Color) -> Self {
        if let Some(ext) = &mut self.external_texture {
            ext.premultiplied = premultiplied;
            ext.fallback_bg = fallback_bg;
        }
        self
    }
}