y901-ui 0.2.5

Widget tree, layout, events and theming for Y901.
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
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
//! Widget trait 与三类上下文。
//!
//! 框架的全部"平台假设"都收敛在 `TextMetrics` 和 `RenderCtx` 两个 trait 里:
//! 框架自己不知道 D3D11、DirectWrite、Win32,也不知道任何具体业务。
//! 应用层提供实现,组件只依赖 trait。

use crate::color::{Color, TextStyle};
use crate::event::{EventResult, UiEvent};
use crate::geometry::{Edges, Pos, Rect, Size};

/// 一个"请求挂浮层"的工厂:组件在事件里只提交工厂,
/// 真正构建 + 挂载由框架在 `finish_dispatch` 统一做。
///
/// 为什么是工厂而不是现成 widget:事件处理期间组件只有 `&mut EventCtx`,
/// 拿不到 `UiTree`;工厂在分发结束后才调用,避开借用冲突,
/// 也避免了"弹出失败但已经构造了一半"的浪费。
pub struct PopupRequest {
    /// 上层提交的工厂(只调一次)。
    pub factory: Box<dyn FnOnce() -> Box<dyn Widget>>,
    /// 触发控件的屏幕矩形。
    pub anchor: Rect,
    /// 是否模态(阻塞底层事件)。
    pub modal: bool,
}

/// 鼠标光标形状。框架只声明"想要什么",具体映射到平台由应用层负责。
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Cursor {
    #[default]
    /// 默认输入类型:只声明动作名,不约定参数形状。
    Default,
    /// 可点击(按钮、链接)
    Pointer,
    /// 文本输入
    Text,
    /// 左右拖拽(竖向分割条)
    ResizeHorizontal,
    /// 上下拖拽(横向分割条)
    ResizeVertical,
    /// 抓取/拖动中
    Grab,
}

/// 挂载上下文:组件在 `on_mount` 里订阅响应式数据时用。
pub struct MountCtx {
    /// 本节点 id(订阅时用作 owner)。
    pub id: String,
    /// 共享失效队列:Signal 变化时往里推 id。
    pub invalidation: crate::signal::Invalidation,
}

impl MountCtx {
    /// 建立挂载上下文。`invalidation` 是后台任务请求重绘的通道——组件挂载时拿到它,就能在不持有 `UiTree` 的情况下通知界面刷新。
    pub fn new(id: impl Into<String>, invalidation: crate::signal::Invalidation) -> Self {
        Self {
            id: id.into(),
            invalidation,
        }
    }
}
/// 组件的语义角色。给外部工具看的那一层。
///
/// **不是枚举**:框架只提供一组约定常量(panel / button / text …),
/// 业务角色(例如终端、文件树)由组件库或应用自己定义:
///
/// ```
/// use y901_ui::Role;
/// pub const ROLE_TIMELINE: Role = Role("timeline");
/// ```
///
/// 这样框架永远不会因为"多了一种业务"而需要改代码。
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Role(pub &'static str);

impl Role {
    /// 容器面板;也是未知角色的回退槽位。
    pub const PANEL: Role = Role("panel");
    /// 窗口背景(与面板分开:窗口色通常是整屏底色)。
    pub const WINDOW: Role = Role("window");
    /// 普通文本。
    pub const TEXT: Role = Role("text");
    /// 按钮;同时被用作"可交互块"的通用外观。
    pub const BUTTON: Role = Role("button");
    /// 文本输入框。
    pub const TEXT_INPUT: Role = Role("text_input");
    /// 列表容器。
    pub const LIST: Role = Role("list");
    /// 列表项(与容器分开:选中态通常只作用于项)。
    pub const LIST_ITEM: Role = Role("list_item");
    /// 滚动条。
    pub const SCROLLBAR: Role = Role("scrollbar");

    /// 角色名;语义树与主题查表都用它作为稳定标识。
    pub const fn as_str(self) -> &'static str {
        self.0
    }
}

impl Default for Role {
    fn default() -> Self {
        Role::PANEL
    }
}

impl std::fmt::Display for Role {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.0)
    }
}

/// 主轴方向。只有这两种——够了,也是刻意保持简单。
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Axis {
    #[default]
    /// 水平主轴(子控件从左到右排列)。
    Horizontal,
    /// 垂直主轴(从上到下)。
    Vertical,
}

/// 单个维度的尺寸约束。
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum Dimension {
    /// 由内容决定。
    #[default]
    Auto,
    /// 固定逻辑像素。
    Px(f32),
    /// 占父容器剩余空间的权重(flex-grow)。
    Flex(f32),
    /// 父容器尺寸的百分比。
    Percent(f32),
}

impl Dimension {
    /// 把"可用空间 + 内容尺寸"解析成最终长度。为什么需要它:`Auto` 要知道内容多大、`Fill` 要吃掉剩余、`Fixed` 无视两者——三者必须用同一处逻辑,否则父容器各写一遍就会不一致。
    pub fn resolve(&self, available: f32, content: f32) -> f32 {
        match *self {
            Dimension::Auto => content,
            Dimension::Px(v) => v,
            Dimension::Flex(_) => content,
            Dimension::Percent(p) => available * (p / 100.0),
        }
    }

    /// 弹性权重(`Flex(f)` 返回 `Some(f)`,其余 `None`)。父容器据此分配剩余空间。
    pub fn flex_factor(&self) -> Option<f32> {
        match *self {
            Dimension::Flex(f) if f > 0.0 => Some(f),
            _ => None,
        }
    }

    /// 是否是弹性尺寸(`Fill` 或 `Flex`)。
    pub fn is_flex(&self) -> bool {
        matches!(self, Dimension::Flex(f) if *f > 0.0)
    }
}

/// **交叉轴对齐**(Flex 的 `align-items`)。
///
/// 默认 `Stretch` = **加这个能力之前的行为**(Auto 子项拿满交叉轴),
/// 所以不显式设置的布局**逐像素不变**。
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Align {
    /// 交叉轴起点(`Stretch` 之外的子项按内容尺寸)。
    Start,
    /// 交叉轴居中。
    Center,
    /// 交叉轴终点。
    End,
    /// 占满交叉轴(默认;`Dimension::Auto` 子项拿满)。
    #[default]
    Stretch,
}

/// **主轴分布**(Flex 的 `justify-content`)。
///
/// 默认 `Start` = **加这个能力之前的行为**(从起点依次紧排,剩余空间留在尾部)。
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Justify {
    /// 起点紧排(默认)。
    #[default]
    Start,
    /// 居中。
    Center,
    /// 靠终点。
    End,
    /// 首尾贴边,间距均分(`n == 1` 时退化为 `Start`)。
    SpaceBetween,
    /// 每项两侧等间距(首尾也有半份间距)。
    SpaceAround,
}

/// 组件在父容器里的伸缩参数。
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct FlexItem {
    /// 宽度约束。
    pub width: Dimension,
    /// 高度约束。
    pub height: Dimension,
    /// 宽度下界;`Px(0.0)`(默认)= 无下界。
    pub min_width: Dimension,
    /// 宽度上界;`Auto`(默认)= 无上界。
    pub max_width: Dimension,
    /// 高度下界;`Px(0.0)`(默认)= 无下界。
    pub min_height: Dimension,
    /// 高度上界;`Auto`(默认)= 无上界。
    pub max_height: Dimension,
}

impl FlexItem {
    /// 无 min/max 限制的默认值(供各构造器 `..` 展开)。
    pub const NO_LIMITS: Self = Self {
        width: Dimension::Auto,
        height: Dimension::Auto,
        min_width: Dimension::Px(0.0),
        max_width: Dimension::Auto,
        min_height: Dimension::Px(0.0),
        max_height: Dimension::Auto,
    };

    /// 两个方向都填满可用空间(最常用)。
    pub const fn fill() -> Self {
        Self {
            width: Dimension::Flex(1.0),
            height: Dimension::Flex(1.0),
            ..Self::NO_LIMITS
        }
    }

    /// 固定尺寸。任一轴传 0.0 表示"该轴不强制",交给内容或父容器决定。
    pub const fn fixed(w: f32, h: f32) -> Self {
        Self {
            width: if w > 0.0 {
                Dimension::Px(w)
            } else {
                Dimension::Auto
            },
            height: if h > 0.0 {
                Dimension::Px(h)
            } else {
                Dimension::Auto
            },
            ..Self::NO_LIMITS
        }
    }

    /// 固定宽度,高度按内容。
    pub const fn width(w: f32) -> Self {
        Self {
            width: Dimension::Px(w),
            height: Dimension::Auto,
            ..Self::NO_LIMITS
        }
    }

    /// 固定高度,宽度按内容。
    pub const fn height(h: f32) -> Self {
        Self {
            width: Dimension::Auto,
            height: Dimension::Px(h),
            ..Self::NO_LIMITS
        }
    }

    /// 按权重瓜分剩余空间(`factor` 越大占比越高)。
    pub const fn grow(factor: f32) -> Self {
        Self {
            width: Dimension::Flex(factor),
            height: Dimension::Flex(factor),
            ..Self::NO_LIMITS
        }
    }

    /// 宽度下界(逻辑像素)。
    pub const fn min_width(mut self, w: f32) -> Self {
        self.min_width = Dimension::Px(w);
        self
    }

    /// 宽度上界(逻辑像素)。
    pub const fn max_width(mut self, w: f32) -> Self {
        self.max_width = Dimension::Px(w);
        self
    }

    /// 高度下界(逻辑像素)。
    pub const fn min_height(mut self, h: f32) -> Self {
        self.min_height = Dimension::Px(h);
        self
    }

    /// 高度上界(逻辑像素)。
    pub const fn max_height(mut self, h: f32) -> Self {
        self.max_height = Dimension::Px(h);
        self
    }

    /// 两个方向同一下界。
    pub const fn min(mut self, v: f32) -> Self {
        self.min_width = Dimension::Px(v);
        self.min_height = Dimension::Px(v);
        self
    }

    /// 两个方向同一上界。
    pub const fn max(mut self, v: f32) -> Self {
        self.max_width = Dimension::Px(v);
        self.max_height = Dimension::Px(v);
        self
    }
}

/// 轴掩码:用来表达"哪个方向无界"。
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Axis2(u8);

impl Axis2 {
    /// 无轴。
    pub const NONE: Axis2 = Axis2(0);
    /// 仅水平轴。
    pub const H: Axis2 = Axis2(1);
    /// 仅垂直轴。
    pub const V: Axis2 = Axis2(2);
    /// 两个轴。
    pub const BOTH: Axis2 = Axis2(3);

    /// 是否包含某个轴(位运算,用于约束传播)。
    pub const fn contains(self, other: Axis2) -> bool {
        self.0 & other.0 != 0
    }

    /// 合并两个轴集合。
    pub const fn union(self, other: Axis2) -> Axis2 {
        Axis2(self.0 | other.0)
    }

    /// 是否至少含一个轴。
    pub const fn any(self) -> bool {
        self.0 != 0
    }
}

/// 布局约束:父节点给子节点的可用空间上下限。
///
/// `unbounded` 显式标记哪个方向"没有上限"(ScrollView 的内容测量)。
/// 关键约定:**`measure` 返回的永远是有限的内容尺寸**,约束里的 `INFINITY`
/// 只表示"你可以任意高/宽",绝不允许出现在返回值里。
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Constraints {
    /// 尺寸上限。`f32::INFINITY` 表示该方向不设限(滚动内容常用)。
    pub max: Size,
    /// 尺寸下限;布局不会把子控件压到比它更小。
    pub min: Size,
    /// 哪些方向是无界的。为什么单独一个字段:`max` 里的 `INFINITY` 无法区分"父容器允许无限"与"测量时人为放开",布局需要知道是哪种。
    pub unbounded: Axis2,
}

impl Constraints {
    /// 宽松约束:上界给定,下界为 0(子控件可以更小)。
    pub const fn loose(max: Size) -> Self {
        Self {
            max,
            min: Size::ZERO,
            unbounded: Axis2::NONE,
        }
    }

    /// 紧约束:上下界都是 `size`(子控件必须正好这么大)。
    pub const fn tight(size: Size) -> Self {
        Self {
            max: size,
            min: size,
            unbounded: Axis2::NONE,
        }
    }

    /// 主轴无界:ScrollView 用它测量"内容到底多高/多宽"。
    ///
    /// `cross` 是交叉轴的有限上限。返回值里 `max` 对应轴是 `INFINITY`,
    /// 但 `constrain()` 会把它夹成内容尺寸,永不外泄。
    pub fn unbounded_main(axis: crate::Axis, cross: Size) -> Self {
        match axis {
            crate::Axis::Vertical => Self {
                max: Size::new(cross.w, f32::INFINITY),
                min: Size::ZERO,
                unbounded: Axis2::V,
            },
            crate::Axis::Horizontal => Self {
                max: Size::new(f32::INFINITY, cross.h),
                min: Size::ZERO,
                unbounded: Axis2::H,
            },
        }
    }

    /// 该方向是否无界(滚动容器据此决定要不要撑开)。
    pub fn is_unbounded(&self, axis: crate::Axis) -> bool {
        match axis {
            crate::Axis::Horizontal => self.unbounded.contains(Axis2::H),
            crate::Axis::Vertical => self.unbounded.contains(Axis2::V),
        }
    }

    /// 把内容尺寸夹进约束。
    ///
    /// 注意:`min` 可能是 0,`max` 可能是 ∞ —— 结果是**内容尺寸**,
    /// 不会因为 `∞` 而产生 `inf` / `NaN`。
    pub fn constrain(&self, size: Size) -> Size {
        // 先保证输入有限:万一组件的 measure 算飞了,这里兜底成 0。
        let safe = Size::new(
            if size.w.is_finite() { size.w } else { 0.0 },
            if size.h.is_finite() { size.h } else { 0.0 },
        );
        let clamped = safe.max(self.min).min(self.max);
        Size::new(
            if clamped.w.is_finite() {
                clamped.w
            } else {
                0.0
            },
            if clamped.h.is_finite() {
                clamped.h
            } else {
                0.0
            },
        )
    }
}

/// 文本度量:布局阶段需要的全部字体信息。
pub trait TextMetrics {
    /// 单行文本的墨迹外框尺寸。
    fn measure_text(&self, text: &str, style: &TextStyle) -> Size;
    /// 行高(基线间距)。
    ///
    /// **这是行距,不是墨迹深度**:它等于 1em 字身框(`round(px)`)。
    /// 真实墨迹深度是 `ascent + descent`,通常比它大 1~2px(下伸字与圆字母 overshoot),
    /// 需要"框必须装得下墨迹"的地方请用 [`TextMetrics::ink_bounds`]。
    fn line_height(&self, style: &TextStyle) -> f32;

    /// **墨迹相对文本原点的上下外扩**:`(原点以上, 原点以下)`,单位像素。
    ///
    /// 用途:定高控件(`TextLabel` 等)与脏区必须保证**装得下墨迹**,
    /// 否则下伸字/overshoot 会画到框外,局部重绘时那几行像素不会被清掉
    /// (`snapshot_harness::invalidate_rect_partial_equals_full` 当初就是靠
    /// 手工把测试脏区加宽 4px 才变绿的,根因就在这里)。
    ///
    /// 默认实现 = `(0, line_height)`:保持既有后端/测试度量的行为不变;
    /// 真实后端应覆盖为字体实际的 `ascent + descent`。
    fn ink_bounds(&self, style: &TextStyle) -> (f32, f32) {
        (0.0, self.line_height(style))
    }
    /// DPI 缩放,1.0 = 100%。
    fn scale(&self) -> f32;
}

/// 墨迹边距的唯一算法:`max(ink_depth − line_height, 2px)`,上限一行。
///
/// 为什么要有 2px 下限:名义度量常常"刚好等于行高"(实测 SimSun 16px 的
/// `ascent+descent` 就是 16),但真实墨迹仍会越过行框 1~2px(字形 overshoot +
/// 抗锯齿覆盖),名义度量预测不到。
///
/// 为什么上限是一行:异常字体度量(畸形 hhea)不该把脏区撑到整屏,
/// 那会让局部重绘退化成整窗重绘。
///
/// 这个函数是**树级默认**与**各控件 per-node 覆盖**的共同实现 ——
/// 两处各写一份就会出现"16px 对了、24px 漏了"那种分叉(本轮修的就是它)。
pub fn ink_margin_for(metrics: &dyn TextMetrics, style: &TextStyle) -> f32 {
    let (_, ink_depth) = metrics.ink_bounds(style);
    let line_h = metrics.line_height(style);
    const MIN_INK_MARGIN: f32 = 2.0;
    (ink_depth - line_h)
        .max(MIN_INK_MARGIN)
        .min(line_h.max(0.0))
}

/// 渲染上下文:框架唯一的出图接口。
///
/// 后端实现它(D3D11 / 软件 / 测试用的记录器),组件只往里画。
/// 计算一个点在渐变方向上的归一化投影 `t`(0..1)。
///
/// 与 GPU 着色器口径一致:四角投影求区间,再归一化。
fn gradient_t(rect: Rect, dir: (f32, f32), x: f32, y: f32) -> f32 {
    let (dx, dy) = dir;
    let len2 = dx * dx + dy * dy;
    if len2 <= f32::EPSILON {
        return 0.0;
    }
    let proj = |px: f32, py: f32| ((px - rect.x) * dx + (py - rect.y) * dy) / len2;
    let corners = [
        proj(rect.x, rect.y),
        proj(rect.right(), rect.y),
        proj(rect.x, rect.bottom()),
        proj(rect.right(), rect.bottom()),
    ];
    let t_min = corners.iter().cloned().fold(f32::MAX, f32::min);
    let t_max = corners.iter().cloned().fold(f32::MIN, f32::max);
    let span = t_max - t_min;
    if span <= f32::EPSILON {
        return 0.0;
    }
    ((proj(x, y) - t_min) / span).clamp(0.0, 1.0)
}

/// 渐变背景的条带数(默认实现用)。
///
/// 为什么是常量而不是参数:条带数按像素切会让 draw 次数随控件高度线性增长,
/// 32 条在 200~1000px 高度上都足够平滑,且开销固定。
pub const GRADIENT_STEPS: usize = 32;

/// 圆角抗锯齿的竖直超采样数(每个像素行内部再切几份)。
pub const AA_SAMPLES: usize = 4;

/// 渲染上下文:框架唯一的出图接口。后端实现它(D3D11 / 软件 / 测试记录器),组件只往里画。为什么继承 `TextMetrics`:测量与绘制必须用**同一套字体度量**,否则文字会与布局错位。
pub trait RenderCtx: TextMetrics {
    /// 某个动画 key 的当前进度(0..=1)。
    ///
    /// 默认返回 `1.0`:"没有动画 → 就是终态"。这是现有控件不改一行也不受影响的原因。
    fn animation(&self, _key: u64) -> f32 {
        1.0
    }

    /// 注入本帧的动画快照(由 `UiTree::render` 调用)。
    ///
    /// 为什么是"注入快照"而不是传 `&UiTree`:`RenderCtx` 由各后端实现,
    /// 它不应认识 UiTree(否则 UI 层与后端反向耦合)。
    fn set_animations(&mut self, _snapshot: &crate::animation::AnimSnapshot) {}

    /// 压入一层坐标变换:以 `origin` 为中心、按 `scale` 缩放后续绘制。
    ///
    /// 默认空实现:不支持变换的后端(以及测试 mock)保持原样,绘制仍按绝对坐标。
    /// 为什么允许"不支持":变换是**视觉增强**,不是正确性前提——没有它界面也应完整可用。
    fn push_transform(&mut self, _origin: Pos, _scale: f32) {}

    /// 弹出最近一层变换(与 [`RenderCtx::push_transform`] 配对)。
    fn pop_transform(&mut self) {}

    /// 上传一张 RGBA 纹理,返回句柄。
    ///
    /// 默认返回 [`crate::image::TextureId::INVALID`](= 0):不支持纹理的后端
    /// 会得到一个无效句柄,随后 `draw_image` 什么都不画——**不崩、不报错**。
    /// 这是为什么现有后端与测试 mock 不改一行也不会坏。
    fn upload_texture(
        &mut self,
        _width: u32,
        _height: u32,
        _rgba: &[u8],
    ) -> crate::image::TextureId {
        crate::image::TextureId::INVALID
    }

    /// 按适配模式绘制纹理。
    ///
    /// 默认空实现(不画任何东西),与 [`RenderCtx::upload_texture`] 配套。
    fn draw_image(
        &mut self,
        _rect: Rect,
        _texture: crate::image::TextureId,
        _style: crate::image::ImageStyle,
    ) {
    }

    /// 增加一次引用计数。
    ///
    /// 配套 [`crate::image::TextureId`] 的共享场景:`Image::from_texture` 收养一张**已存在**的纹理时,
    /// 必须先 retain 一次,否则它卸载时会把别人还在用的纹理释放掉。
    /// 默认空实现(不支持纹理的后端无需实现)。
    fn retain_texture(&mut self, _id: crate::image::TextureId) {}

    /// 减少一次引用计数,归零时真正释放。
    ///
    /// 默认空实现(与 [`RenderCtx::retain_texture`] 配套)。
    fn release_texture(&mut self, _id: crate::image::TextureId) {}

    /// 某个节点当前是否被鼠标悬停。
    ///
    /// 默认 `false`:不支持悬停的后端(与测试 mock)不受影响。
    fn is_hovered(&self, _id: &str) -> bool {
        false
    }

    /// 注入本帧的悬停目标(由 `UiTree::render` 调用)。
    fn set_hovered(&mut self, _id: Option<&str>) {}

    /// 当前背景色(用于淡入/淡出遮罩)。
    ///
    /// 默认黑色:没有主题信息时至少能给出可用的中性遮罩色。
    fn background_color(&self) -> Color {
        Color::rgba(0.0, 0.0, 0.0, 1.0)
    }

    /// 视口尺寸(逻辑像素)。
    fn size(&self) -> Size;

    /// 填充一个轴对齐矩形(屏幕坐标,像素对齐)。alpha < 1 时与已有内容混合。
    fn fill_rect(&mut self, rect: Rect, color: Color);

    /// 画矩形边框。`width` 向内收:边框完全落在 `rect` 内,不会撑大占位。
    fn stroke_rect(&mut self, rect: Rect, color: Color, width: f32);

    /// 在 `origin`(左上角)画一行文本,返回前进宽度。
    fn draw_text(&mut self, origin: Pos, text: &str, style: &TextStyle) -> f32;

    /// 绘制单个字符,返回实际占用宽度(像素)。
    ///
    /// 默认实现用**栈上** `[u8; 4]` 编码成 UTF-8 后转调 [`RenderCtx::draw_text`],
    /// 零堆分配。终端这类"每格一个字符"的场景必须用它,否则每格一次 `String` 分配
    /// (80×24 = 每帧 1920 次)。
    ///
    /// 后端可以覆盖(例如直接查字形缓存),但默认实现对所有后端都够用。
    fn draw_char(&mut self, pos: Pos, ch: char, style: &TextStyle) -> f32 {
        let mut buf = [0u8; 4];
        let s = ch.encode_utf8(&mut buf);
        self.draw_text(pos, s, style)
    }

    /// 圆角矩形填充。
    ///
    /// 输入:`rect`(外接矩形)、`color`(含 alpha)、`radius`(像素,自动夹到半宽/半高)。
    /// 副作用:在 `rect` 内(含裁剪)绘制。
    /// 默认实现:逐扫描线用 [`RenderCtx::fill_rect`] 拼出圆角——对任何后端都可用,
    /// 不需要新增 GPU 图元;`radius <= 0` 时退化成普通矩形。
    /// 性能:中间部分合并成一整块,只有上下各 `radius` 行逐行绘制。
    fn fill_round_rect(&mut self, rect: Rect, color: Color, radius: f32) {
        let r = radius.clamp(0.0, rect.w.min(rect.h) * 0.5);
        if r <= 0.5 {
            self.fill_rect(rect, color);
            return;
        }
        self.fill_rect(
            Rect::new(rect.x, rect.y + r, rect.w, rect.h - 2.0 * r),
            color,
        );

        // 上下各 r 行:逐扫描线画,并在左右边界做**覆盖率混合**(Coverage AA)。
        // 把每个像素行在竖直方向再切 AA_SAMPLES 份,各自代入圆方程求
        // 内缩量,取平均;平均内缩的小数部分就是边界像素被覆盖的比例。
        let rows = r.ceil() as i32;
        for i in 0..rows {
            let mut inset_sum = 0.0f32;
            for k in 0..AA_SAMPLES {
                let dy = r - (i as f32 + (k as f32 + 0.5) / AA_SAMPLES as f32);
                inset_sum += r - (r * r - dy * dy).max(0.0).sqrt();
            }
            let inset = inset_sum / AA_SAMPLES as f32;
            self.fill_round_row(rect, rect.y + i as f32, inset, color);
            self.fill_round_row(rect, rect.bottom() - 1.0 - i as f32, inset, color);
        }
    }

    /// 圆角 + 线性渐变("苹果风按钮"底座)。
    ///
    /// 输入:`rect`、`from`/`to`、`dir`(任意方向向量)、`radius`。
    /// 默认实现(CPU 兜底):按圆角逐行算左右内缩,
    /// 在每一行里用水平条带近似渐变(对竖直渐变是精确的,对斜向是近似)。
    /// GPU 后端应覆盖为 SDF + 渐变单实例。
    fn fill_gradient_round_rect(
        &mut self,
        rect: Rect,
        from: Color,
        to: Color,
        dir: (f32, f32),
        radius: f32,
    ) {
        let r = radius.clamp(0.0, rect.w.min(rect.h) * 0.5);
        if r <= 0.5 {
            self.fill_gradient_rect_dir(rect, from, to, dir);
            return;
        }
        // 中间整块(不受圆角影响):直接用渐变
        self.fill_gradient_rect_dir(
            Rect::new(rect.x, rect.y + r, rect.w, rect.h - 2.0 * r),
            from,
            to,
            dir,
        );
        // 上下圆角行:每行只画 [x0, x1] 范围内的渐变条带
        let steps = (r.ceil() as i32).max(1);
        for i in 0..steps {
            let dy = r - (i as f32 + 0.5);
            let inset = r - (r * r - dy * dy).max(0.0).sqrt();
            let row = Rect::new(rect.x + inset, rect.y + i as f32, rect.w - 2.0 * inset, 1.0);
            // 1px 高的行里按 x 切 8 条(CPU 兜底的精度折衷)
            let n = 8;
            for k in 0..n {
                let t0 = k as f32 / n as f32;
                let t1 = (k + 1) as f32 / n as f32;
                let x0 = row.x + row.w * t0;
                let x1 = row.x + row.w * t1;
                // 用整个矩形的投影算 t(与 GPU 口径一致)
                let t = gradient_t(rect, dir, (x0 + x1) * 0.5, row.y + 0.5);
                self.fill_rect(Rect::new(x0, row.y, x1 - x0, 1.0), from.lerp(to, t));
            }
            let y = rect.bottom() - 1.0 - i as f32;
            let row = Rect::new(rect.x + inset, y, rect.w - 2.0 * inset, 1.0);
            for k in 0..n {
                let t0 = k as f32 / n as f32;
                let t1 = (k + 1) as f32 / n as f32;
                let x0 = row.x + row.w * t0;
                let x1 = row.x + row.w * t1;
                let t = gradient_t(rect, dir, (x0 + x1) * 0.5, y + 0.5);
                self.fill_rect(Rect::new(x0, y, x1 - x0, 1.0), from.lerp(to, t));
            }
        }
    }

    /// 圆角描边(只画轮廓,不填内部)。
    ///
    /// 输入:`rect`、`color`、`width`(描边宽度,居中于边界)、`radius`。
    /// 默认实现(CPU 兜底):先画外圆角再用背景色挖内部是不行的(会覆盖下层),
    /// 所以这里抽象为"上下左右四条带 + 四个圆角环":直边用 `fill_rect`,
    /// 圆角用逐行弦长差异画出带宽度的弧。后端(D3D)应覆盖为 SDF 单实例。
    fn stroke_round_rect(&mut self, rect: Rect, color: Color, width: f32, radius: f32) {
        let w = width.max(0.0);
        if w <= 0.0 || rect.is_empty() {
            return;
        }
        let r = radius.clamp(0.0, rect.w.min(rect.h) * 0.5);
        if r <= 0.5 {
            self.stroke_rect(rect, color, w);
            return;
        }
        // 直边(上下各一条、左右两条只到圆角起点)
        self.fill_rect(Rect::new(rect.x + r, rect.y, rect.w - 2.0 * r, w), color);
        self.fill_rect(
            Rect::new(rect.x + r, rect.bottom() - w, rect.w - 2.0 * r, w),
            color,
        );
        self.fill_rect(Rect::new(rect.x, rect.y + r, w, rect.h - 2.0 * r), color);
        self.fill_rect(
            Rect::new(rect.right() - w, rect.y + r, w, rect.h - 2.0 * r),
            color,
        );
        // 四个圆角:逐行画出弦长差(外弧 - 内弧)
        let steps = (r.ceil() as i32).max(1);
        for i in 0..steps {
            let dy = r - (i as f32 + 0.5);
            let outer = (r * r - dy * dy).max(0.0).sqrt();
            let inner_r = (r - w).max(0.0);
            let inner = if dy.abs() <= inner_r {
                (inner_r * inner_r - dy * dy).max(0.0).sqrt()
            } else {
                0.0
            };
            let band = (outer - inner).max(0.0);
            if band <= 0.0 {
                continue;
            }
            // 左上 / 右上
            self.fill_rect(
                Rect::new(rect.x + r - outer, rect.y + i as f32, band, 1.0),
                color,
            );
            self.fill_rect(
                Rect::new(rect.right() - r + inner, rect.y + i as f32, band, 1.0),
                color,
            );
            // 左下 / 右下
            let y = rect.bottom() - 1.0 - i as f32;
            self.fill_rect(Rect::new(rect.x + r - outer, y, band, 1.0), color);
            self.fill_rect(Rect::new(rect.right() - r + inner, y, band, 1.0), color);
        }
    }

    /// 圆角单行:左/右边界像素按覆盖率给 alpha,中间实色。
    fn fill_round_row(&mut self, rect: Rect, y: f32, inset: f32, color: Color) {
        let x0 = rect.x + inset;
        let x1 = rect.right() - inset;
        if x1 <= x0 {
            return;
        }
        let left_px = x0.floor();
        let right_px = x1.ceil();
        let left_frac = (x0 - left_px).clamp(0.0, 1.0);
        let right_frac = (right_px - x1).clamp(0.0, 1.0);

        if left_frac > 0.001 {
            self.fill_rect(
                Rect::new(left_px, y, 1.0, 1.0),
                color.with_alpha(color.a * (1.0 - left_frac)),
            );
        }
        if right_frac > 0.001 {
            self.fill_rect(
                Rect::new(right_px - 1.0, y, 1.0, 1.0),
                color.with_alpha(color.a * (1.0 - right_frac)),
            );
        }
        let body_x0 = left_px + if left_frac > 0.001 { 1.0 } else { 0.0 };
        let body_x1 = right_px - if right_frac > 0.001 { 1.0 } else { 0.0 };
        if body_x1 > body_x0 {
            self.fill_rect(Rect::new(body_x0, y, body_x1 - body_x0, 1.0), color);
        }
    }

    /// 全方向线性渐变:`dir = (dx, dy)`(任意向量,不需归一化)。
    ///
    /// 默认实现:沿主导轴切 1px 条带,每条按其中心在 `dir` 上的投影取色。
    /// 对轴向渐变是精确的;对斜向渐变是“沿主导轴逐行/逐列变色”的近似
    /// (`fill_rect` 只能画轴对齐矩形,真正的任意角渐变需要 GPU 图元)。
    fn fill_gradient_rect_dir(&mut self, rect: Rect, from: Color, to: Color, dir: (f32, f32)) {
        if rect.is_empty() || (from.a <= 0.0 && to.a <= 0.0) {
            return;
        }
        let (dx, dy) = dir;
        let len2 = dx * dx + dy * dy;
        if len2 <= f32::EPSILON {
            self.fill_rect(rect, from);
            return;
        }
        let proj = |x: f32, y: f32| -> f32 { ((x - rect.x) * dx + (y - rect.y) * dy) / len2 };
        let corners = [
            proj(rect.x, rect.y),
            proj(rect.right(), rect.y),
            proj(rect.x, rect.bottom()),
            proj(rect.right(), rect.bottom()),
        ];
        let t_min = corners.iter().cloned().fold(f32::MAX, f32::min);
        let t_max = corners.iter().cloned().fold(f32::MIN, f32::max);
        let span = t_max - t_min;
        if span <= f32::EPSILON {
            self.fill_rect(rect, from);
            return;
        }

        let vertical = dx.abs() >= dy.abs();
        let extent = if vertical { rect.w } else { rect.h };
        let count = (extent.round() as i32).clamp(1, 512);
        let steps = (GRADIENT_STEPS as i32).max(1);
        // 颜色按 steps 档量化:既保证平滑,又避免每个 1px 条带都算一次 lerp
        let _ = steps;
        for i in 0..count {
            let a0 = i as f32 / count as f32;
            let a1 = (i + 1) as f32 / count as f32;
            let t = if vertical {
                let cx = rect.x + rect.w * (a0 + a1) * 0.5;
                ((proj(cx, rect.y + rect.h * 0.5) - t_min) / span).clamp(0.0, 1.0)
            } else {
                let cy = rect.y + rect.h * (a0 + a1) * 0.5;
                ((proj(rect.x + rect.w * 0.5, cy) - t_min) / span).clamp(0.0, 1.0)
            };
            let c = from.lerp(to, t);
            if vertical {
                let x0 = (rect.w * a0).round();
                let x1 = (rect.w * a1).round();
                if x1 > x0 {
                    self.fill_rect(Rect::new(rect.x + x0, rect.y, x1 - x0, rect.h), c);
                }
            } else {
                let y0 = (rect.h * a0).round();
                let y1 = (rect.h * a1).round();
                if y1 > y0 {
                    self.fill_rect(Rect::new(rect.x, rect.y + y0, rect.w, y1 - y0), c);
                }
            }
        }
    }

    /// 一维渐变(兼容旧接口):等价于 dir = (1,0) 或 (0,1)。
    fn fill_gradient_rect(&mut self, rect: Rect, from: Color, to: Color, horizontal: bool) {
        let dir = if horizontal { (1.0, 0.0) } else { (0.0, 1.0) };
        self.fill_gradient_rect_dir(rect, from, to, dir);
    }

    /// 裁剪栈:滚动容器由框架自动压栈,组件不需要手动调用。
    fn push_clip(&mut self, rect: Rect);
    /// 弹出最近一次 `push_clip`。必须与 push 严格配对——不配对会让后续绘制被错误裁剪。
    fn pop_clip(&mut self);

    /// 压入一层不透明度。
    ///
    /// 默认空实现:旧后端(以及测试用的 mock)不必实现就能编译。
    /// 真正支持的后端应将其与栈上其他层**乘积**,
    /// 并在最终像素输出时作用于 alpha。
    fn push_opacity(&mut self, _opacity: f32) {}

    /// 弹出栈顶不透明度(默认空实现)。
    fn pop_opacity(&mut self) {}

    /// 当前主题。默认返回内置暗色主题,应用可注入自己的。
    fn theme(&self) -> &crate::theme::Theme {
        &crate::theme::DARK_THEME
    }
}

/// 事件上下文:组件通过它向框架提请"焦点/重绘/重排"。
///
/// 刻意不借用树的字段:只有值拷贝,组件无法从事件里偷改树结构。
pub struct EventCtx {
    /// 当前焦点节点 id(快照)。
    pub focus: Option<String>,
    /// 组件请求把焦点移交给谁(分发结束后由框架统一执行)。
    pub focus_request: Option<String>,
    #[doc(hidden)]
    pub redraw: bool,
    #[doc(hidden)]
    pub relayout: bool,
    /// 本帧的视口尺寸;事件坐标换算与边界判断都用它。
    pub viewport: Size,
    /// 本次分发中组件产生的意图。框架只负责搬运,不解释。
    #[doc(hidden)]
    pub commands: Vec<crate::command::Command>,

    /// 当前处于哪个路由阶段。
    pub phase: crate::event::EventPhase,
    /// 本次事件命中/捕获的目标节点 id。
    pub target: String,
    /// 置 true 后,框架立刻停止后续阶段(捕获拦截 / 冒泡截断)。
    pub stop_propagation: bool,
    /// 本次分发里**真正要求重排**的节点 id(框架内部记账,组件不要读写)。
    ///
    /// 用途:`Relayout` 的脏区要标在"请求者"身上,而不是命中目标身上
    /// (滚动时命中目标是行,请求者是外层滚动容器)。
    #[doc(hidden)]
    pub relayout_from: Option<String>,
    /// 置 true 时,即使返回 `Handled` 也继续冒泡(罕见,需显式请求)。
    #[doc(hidden)]
    pub propagate: bool,
    /// 指针捕获请求,分发结束后由框架落实。
    pub pointer_capture: Option<crate::event::PointerCapture>,
    /// 焦点守卫:在处理 `Focus` 事件时置 true 可以拒绝这次焦点变更。
    #[doc(hidden)]
    pub focus_vetoed: bool,
    /// 请求把 IME 候选窗钉在某个屏幕坐标(组件算好光标位置后填)。
    #[doc(hidden)]
    pub ime_candidate: Option<Pos>,
    /// 本次分发中组件提交的浮层请求(分发结束后由 `UiTree` 统一挂载)。
    #[doc(hidden)]
    pub popup_requests: Vec<PopupRequest>,
    /// 请求关闭的浮层 id(分发结束后由树执行)。
    pub close_popup_requests: Vec<String>,
    /// 组件请求启动的属性动画(`key`, `Animate`)。
    ///
    /// 为何是"请求队列"而不是直接调 `UiTree::animate`:`EventCtx` 刻意不持有树
    /// (只有值拷贝,见结构体文档),所以用与 `popup_requests` 完全同构的方式交给 UiTree 消费。
    #[doc(hidden)]
    pub anim_requests: Vec<(u64, crate::animation::Animate)>,
    /// **DPI 缩放**(96 = 100%)。
    ///
    /// 为何要在事件里有它:命中测试必须与绘制用**同一个**尺寸。
    /// 在 125% DPI 下,菜单项高 26×1.25=32.5px;若命中还按 26px 算,
    /// 就会出现"点得到但画在别处"。
    ///
    /// 由 `UiTree` 在 `layout()` 时从 `metrics.scale()` 写入,分发前拷贝到每个 ctx。
    pub scale: f32,
}

impl EventCtx {
    /// 建立事件上下文。`focus` 是当前焦点节点(组件据此判断自己是否持有键盘),`viewport` 用于把点击位置换算成布局坐标。
    pub fn new(focus: Option<String>, viewport: Size) -> Self {
        Self {
            focus,
            focus_request: None,
            redraw: false,
            relayout: false,
            viewport,
            commands: Vec::new(),
            phase: crate::event::EventPhase::Target,
            target: String::new(),
            stop_propagation: false,
            relayout_from: None,
            propagate: false,
            pointer_capture: None,
            focus_vetoed: false,
            ime_candidate: None,
            popup_requests: Vec::new(),
            close_popup_requests: Vec::new(),
            anim_requests: Vec::new(),
            // 默认 1.0:测试与非布局路径行为不变;真实值由 `UiTree` 在 `layout()` 后写入。
            scale: 1.0,
        }
    }

    /// 请求把 IME 候选窗钉到这个屏幕坐标(由应用层落实)。
    pub fn set_ime_candidate(&mut self, pos: Pos) {
        self.ime_candidate = Some(pos);
    }

    /// 拒绝本次焦点变更(只能在 `UiEvent::Focus` 的处理中调用)。
    pub fn veto_focus(&mut self) {
        self.focus_vetoed = true;
    }

    /// 立刻终止后续阶段(Capture 里拦事件、Bubble 里截断都用它)。
    pub fn stop_propagation(&mut self) {
        self.stop_propagation = true;
    }

    /// 即使 `Handled` 也继续冒泡。
    pub fn allow_propagation(&mut self) {
        self.propagate = true;
    }

    /// 请求指针捕获:直到 `release_pointer_capture` 之前,
    /// 所有 `MouseMove` / `MouseUp` 都强制派发给这个节点(无视坐标)。
    pub fn set_pointer_capture(&mut self, id: impl Into<String>) {
        self.pointer_capture = Some(crate::event::PointerCapture::Capture(id.into()));
    }

    /// 释放指针捕获。
    pub fn release_pointer_capture(&mut self) {
        self.pointer_capture = Some(crate::event::PointerCapture::Release);
    }

    /// **当前 DPI 缩放**(96 = 100%)。
    ///
    /// 用途:命中测试里计算"随 scale 变化的尺寸"(如菜单项高、标签栏高),
    /// 保证与 `render` 用的 `ctx.scale()`(同一 trait 方法)**一致**。
    pub fn scale(&self) -> f32 {
        self.scale
    }

    /// 当前是否处于目标阶段。
    pub fn is_target(&self) -> bool {
        self.phase == crate::event::EventPhase::Target
    }

    /// 请应用层做一件事。框架不理解 `name` 的含义。
    pub fn emit_app(&mut self, name: impl Into<String>, args: crate::json::Json) {
        self.commands.push(crate::command::Command::app(name, args));
    }

    /// 请某个节点执行一个动作(组件不需要认识对方)。
    pub fn emit_node(
        &mut self,
        target: impl Into<String>,
        action: impl Into<String>,
        args: crate::json::Json,
    ) {
        self.commands
            .push(crate::command::Command::node(target, action, args));
    }

    /// 直接投递一条命令。
    pub fn emit(&mut self, cmd: crate::command::Command) {
        self.commands.push(cmd);
    }

    /// 请求把键盘焦点交给某个节点。为什么要"请求"而不是直接改:焦点是**树的状态**,必须在分发结束后统一落实,否则同一次事件里两个组件会各自以为自己拿到了焦点。
    pub fn request_focus(&mut self, id: impl Into<String>) {
        self.focus_request = Some(id.into());
    }

    /// 请求重绘(脏区由树在分发后统一标)。
    pub fn request_redraw(&mut self) {
        self.redraw = true;
    }

    /// 请求重排。只有尺寸/结构真的变了才该用它——它会触发整棵子树的测量。
    pub fn request_relayout(&mut self) {
        self.relayout = true;
        self.redraw = true;
    }

    /// 某个节点是否持有键盘焦点。
    pub fn is_focused(&self, id: &str) -> bool {
        self.focus.as_deref() == Some(id)
    }

    /// 把组件的返回值与已累积的标志合并。
    /// 请求打开一个浮层(非模态)。
    ///
    /// 输入:`factory`(分发结束后才调用)、`anchor`(触发控件屏幕矩形)。
    /// 副作用:追加一条请求并标记重绘。
    pub fn request_popup(&mut self, factory: Box<dyn FnOnce() -> Box<dyn Widget>>, anchor: Rect) {
        self.popup_requests.push(PopupRequest {
            factory,
            anchor,
            modal: false,
        });
        self.redraw = true;
    }

    /// 请求关闭一个浮层。
    ///
    /// 为什么需要它:控件可以通过 `request_popup` **开**浮层,
    /// 但没有回手就只能等外部点击关闭——Tooltip 必须"鼠标离开就消失"。
    /// 传入不存在的 id 不会 panic(树侧返回 false)。
    pub fn request_close_popup(&mut self, id: impl Into<String>) {
        self.close_popup_requests.push(id.into());
        self.redraw = true;
    }

    /// 请求启动一个**属性动画**:分发结束后由 `UiTree` 提交到 `animate()`。
    ///
    /// 输入:`key`(用 `animation::key_of(&format!("{id}-xxx"))` 生成)与 `anim`。
    /// 副作用:只压入请求;**同 key 重复请求 = 覆盖**(与 `UiTree::animate` 一致)。
    pub fn start_animation(&mut self, key: u64, anim: crate::animation::Animate) {
        self.anim_requests.push((key, anim));
        self.redraw = true;
    }

    /// 发送一条**意图命令**:`App` 走应用层(`Host::command`),`Node` 由树路由到目标节点
    /// 并转成 `UiEvent::Action`。这是组件联动的唯一通道(互不认识对方)。
    pub fn emit_command(&mut self, cmd: crate::command::Command) {
        match cmd {
            crate::command::Command::App { name, args } => self.emit_app(name, args),
            node @ crate::command::Command::Node { .. } => self.commands.push(node),
        }
    }

    /// 请求打开一个**模态**浮层(阻塞底层事件)。
    pub fn request_modal_popup(
        &mut self,
        factory: Box<dyn FnOnce() -> Box<dyn Widget>>,
        anchor: Rect,
    ) {
        self.popup_requests.push(PopupRequest {
            factory,
            anchor,
            modal: true,
        });
        self.redraw = true;
    }

    /// 把组件返回值与本次累积的"请求重绘/重排"合并。组件不需要自己拼 `EventResult`。
    pub fn finish(&mut self, result: EventResult) -> EventResult {
        let mut out = result;
        if self.relayout {
            out = out.merge(EventResult::Relayout);
        } else if self.redraw {
            out = out.merge(EventResult::Redraw);
        }
        out
    }
}

/// 动作参数的形态。网关据此做类型校验,外部工具据此知道该传什么。
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum InputKind {
    /// 无参数(纯动作,例如"关闭")。
    None,
    /// 文本参数。
    Text,
    /// 数值参数。
    Number,
    /// 布尔参数。
    Bool,
}

impl InputKind {
    /// 参数名(约定:每个动作最多一个入参)。
    pub fn arg_name(self) -> &'static str {
        match self {
            InputKind::None => "",
            InputKind::Text => "text",
            InputKind::Number => "value",
            InputKind::Bool => "flag",
        }
    }

    /// 校验一个 JSON 值是否匹配。
    pub fn accepts(self, value: &crate::json::Json) -> bool {
        use crate::json::Json;
        match self {
            InputKind::None => matches!(value, Json::Null),
            InputKind::Text => matches!(value, Json::Str(_)),
            InputKind::Number => matches!(value, Json::Num(_)),
            InputKind::Bool => matches!(value, Json::Bool(_)),
        }
    }
}

/// 一个可被外部工具调用的动作。
#[derive(Clone, Debug, PartialEq)]
pub struct ActionSpec {
    /// 动作名。
    pub name: String,
    /// 参数形状;网关据此做类型校验,避免把字符串传进数值参数。
    pub input: InputKind,
    /// 自定义参数名;`None` 时回退到 [`InputKind::arg_name`](text/value/flag)。
    ///
    /// 用于参数名不落俗套的动作(例如 `set_color` 的参数叫 `color` 而非 `text`)。
    pub arg_name: Option<&'static str>,
}

impl ActionSpec {
    /// 用默认参数名(由 `InputKind` 决定)构造。
    pub fn new(name: impl Into<String>, input: InputKind) -> Self {
        Self {
            name: name.into(),
            input,
            arg_name: None,
        }
    }

    /// 用自定义参数名构造(参数名固定为 `'static str`,避免运行期分配)。
    pub fn named(name: impl Into<String>, input: InputKind, arg_name: &'static str) -> Self {
        Self {
            name: name.into(),
            input,
            arg_name: Some(arg_name),
        }
    }

    /// 网关从 `args` 里取参数时用的键名。
    ///
    /// 返回:自定义名(若有),否则 `input.arg_name()`。
    pub fn arg_name(&self) -> &str {
        self.arg_name.unwrap_or_else(|| self.input.arg_name())
    }

    /// 语义树里的紧凑表示:`{"name":"click","input":"none"}`。
    ///
    /// `arg_name` 为 `Some` 时额外输出 `"arg"` 字段(外部工具据此知道参数名);
    /// 默认参数名的动作**不输出**该字段——保持既有 JSON 形状不变。
    pub fn to_json(&self) -> crate::json::Json {
        let obj = crate::json::Json::obj().set("name", self.name.clone()).set(
            "input",
            match self.input {
                InputKind::None => "none",
                InputKind::Text => "text",
                InputKind::Number => "number",
                InputKind::Bool => "bool",
            },
        );
        match self.arg_name {
            Some(name) => obj.set("arg", name),
            None => obj,
        }
    }
}
/// 语义描述:给外部工具读的节点信息。
#[derive(Clone, Debug, PartialEq)]
pub struct Semantics {
    /// 给人和外部工具看的文字标签。
    pub label: String,
    /// 当前值的字符串形式(外部工具直接消费,不做类型推断)。
    pub value: String,
    /// 角色(query 用;由框架在遍历时从 Widget::role() 自动注入)。
    pub role: Role,
    /// 该节点支持的动作名,例如 ["click", "set_text"]。
    pub actions: Vec<ActionSpec>,
    /// 额外键值(终端会填 cols/rows/scroll_offset)。
    pub props: Vec<(String, String)>,
    /// 结构化状态:外部工具直接消费(文本、光标、禁用…)。
    /// 是否被禁用(不参与 Tab 遍历,也不接收事件)。
    pub disabled: bool,
    /// 是否对外部开放(网关据此过滤,false 的节点外部不可见也不可操作)。
    pub exposed_to_ai: bool,
    /// 是否敏感(网关返回 JSON 时把 value 打码)。
    pub sensitive: bool,
    /// 是否持有键盘焦点。**框架拥有**:由 `UiTree::focus_path` 注入,
    /// 组件不得自行设置(组件自己的 focused 字段只用于渲染/按键门控)。
    pub focused: bool,
    /// 结构化状态:外部工具直接消费。
    pub state: crate::json::Json,
}

impl Default for Semantics {
    fn default() -> Self {
        Self {
            label: String::new(),
            value: String::new(),
            role: Role(""),
            actions: Vec::new(),
            props: Vec::new(),
            disabled: false,
            // 默认封禁:组件必须显式 .exposed(true) 才对外部可见
            exposed_to_ai: false,
            sensitive: false,
            focused: false,
            state: crate::json::Json::Null,
        }
    }
}

impl Semantics {
    /// 设置标签。
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.label = label.into();
        self
    }

    /// 设置当前值。
    pub fn value(mut self, value: impl Into<String>) -> Self {
        self.value = value.into();
        self
    }

    /// 标记为禁用;网关据此拒绝在其上执行动作。
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.disabled = disabled;
        self
    }

    /// 是否向外部暴露这个节点(网关据此过滤)。
    pub fn exposed(mut self, exposed: bool) -> Self {
        self.exposed_to_ai = exposed;
        self
    }

    /// 标记敏感:网关返回 JSON 时会把 value 打码。
    pub fn sensitive(mut self, sensitive: bool) -> Self {
        self.sensitive = sensitive;
        self
    }

    /// 写一个结构化状态字段(保留类型:数字还是数字、布尔还是布尔)。
    pub fn state(mut self, key: impl Into<String>, value: impl Into<crate::json::Json>) -> Self {
        let key = key.into();
        self.state = self.state.clone().set(&key, value.into());
        self
    }

    /// 声明一个动作(无参数)。
    pub fn action(mut self, name: impl Into<String>) -> Self {
        self.actions.push(ActionSpec::new(name, InputKind::None));
        self
    }

    /// 声明一个带参数类型的动作(参数名由 `input` 决定:text / value / flag)。
    pub fn action_spec(mut self, name: impl Into<String>, input: InputKind) -> Self {
        self.actions.push(ActionSpec::new(name, input));
        self
    }

    /// 声明一个**参数名自定义**的带参动作。
    ///
    /// 输入:动作名、参数类型、参数键名(`'static`,不分配)。
    /// 返回:自身。
    /// 用在哪:参数名不落俗套时(如 `set_color` 用 `color` 而不是 `text`)。
    /// 影响:网关按这个键从 `args` 取值,`to_json` 会额外输出 `"arg"` 字段。
    pub fn action_spec_named(
        mut self,
        name: impl Into<String>,
        input: InputKind,
        arg_name: &'static str,
    ) -> Self {
        self.actions.push(ActionSpec::named(name, input, arg_name));
        self
    }

    /// 追加一项自定义属性。为什么用扁平键值而不是结构体:组件自定义的属性无法事先枚举。
    pub fn prop(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.props.push((key.into(), value.into()));
        self
    }
}

/// 组件。
///
/// 生命周期:`measure` → `arrange` → `render`;事件随时来。
/// 只实现这一个 trait,就能被框架当普通节点对待。
///
/// 布局是**框架驱动**的:容器在 `measure`/`arrange` 里通过
/// [`crate::layout::LayoutCache`] 访问子节点,缓存、去重、脏标记都由框架负责。
pub trait Widget {
    /// 树内唯一 id,例如 `"panel.list"`。
    ///
    /// 返回:稳定字符串引用(框架会大量比较它,别每次返回新分配的 `String`)。
    /// 约束:**同一棵树内必须唯一**——重复 id 会让 `add_node` 拒绝插入,
    /// 也会让 `find` / `dispatch_to` / 语义查询指向错误的节点。
    /// 一旦挂载就不应改变(`step` 会按 id 缓存矩形与 revision)。
    fn id(&self) -> &str;

    /// 语义角色:给外部工具与 `query` 用的分类标签。
    ///
    /// 返回:`Role`(不是枚举,业务可自定义常量)。默认 `PANEL`。
    /// 副作用:无。`role` 会参与语义面哈希,改动它会让 revision +1。
    fn role(&self) -> Role {
        Role::PANEL
    }

    /// 自己在父容器里的伸缩参数(宽/高的 `Dimension`)。
    ///
    /// 返回:`FlexItem`;默认 `Auto`(由内容决定)。
    /// 副作用:无。容器在 `measure`/`arrange` 里读取它决定分配,**不写回**组件。
    fn flex(&self) -> FlexItem {
        FlexItem::default()
    }

    /// 是否可以获得键盘焦点(Tab 遍历、Modal 自动聚焦都用它)。
    ///
    /// 返回:`true` 表示愿意接收焦点。默认 `false`。
    /// 约束:返回 true **不等于**当前持有焦点——焦点由 `UiTree::focus_path` 决定,
    /// 组件应通过 `UiEvent::Focus(bool)` 得知自己拿到/失去焦点。
    fn focusable(&self) -> bool {
        false
    }

    /// 语义版本:组件自己维护的单调计数器,状态变化时 +1。
    ///
    /// 返回:当前版本;`0` 表示组件不自己维护。
    /// 副作用:无。框架的 `effective_revision = 本值 + 框架计数器`,
    /// 因此返回 0 只是"框架计数器独立生效",**不是**关闭版本锁。
    fn revision(&self) -> u64 {
        0
    }

    /// 挂载:申请资源(订阅 Signal、启动后台任务)。
    ///
    /// 输入:`ctx.id`(本节点 id)、`ctx.invalidation`(共享失效队列)。
    /// 时机:树构建时与 `add_node` / `insert_node` 插入后立即调用,**先于首次布局**。
    /// 副作用:允许任意(这是唯一可以"申请资源"的地方)。
    /// 失败:无返回值;资源申请失败请自己 `eprintln!` 并降级,不要 panic。
    fn on_mount(&mut self, _ctx: &mut MountCtx) {}

    /// 卸载:释放资源(取消订阅、关闭 PTY)。
    ///
    /// 时机:`remove_node` 时对整棵被摘掉的子树**自底向上**调用,随后节点被 drop。
    /// 副作用:允许任意,但**不得**再访问树(此时树已不包含该节点)。
    /// 注意:关闭浮层(`is_open=false`)**不会**触发卸载——只是不画不命中。
    fn on_unmount(&mut self) {}

    /// 在卸载前申报"我占用了哪些纹理"。
    ///
    /// 为什么单独一个钩子而不是在 `on_unmount` 里直接释放:
    /// 卸载时**手里没有 `RenderCtx`**(资源在后端手里),
    /// 所以只能先记下来,等下一帧渲染时由树统一转交后端。
    fn release_textures(&mut self, _out: &mut Vec<crate::image::TextureId>) {}

    /// 是否处于已打开状态。关闭的浮层不参与绘制、命中与索引。
    ///
    /// 返回:`true` 默认(普通节点永远"打开");浮层组件按自身状态返回。
    /// 副作用:无(框架把它当纯查询)。
    /// 重要:翻转它必须返回 `EventResult::Relayout`,否则索引不会重建,
    /// 关闭的节点会残留在 `paint_order` 里(既占绘制又可能被误命中)。
    fn is_open(&self) -> bool {
        true
    }

    /// 是否自己消费 Tab 键(而不是让框架切换焦点)。
    ///
    /// 返回:`true` = Tab 会派发给本组件(终端用它做补全);默认 `false`。
    /// 副作用:无。框架在 `UiTree::dispatch` 里先问焦点节点,`true` 才放行。
    /// 注意:只有**当前焦点**组件会被询问,非焦点组件的返回值无意义。
    fn wants_tab(&self) -> bool {
        false
    }

    /// 布局缓存键(可选)。返回 `Some` 时框架直接用它,避免对 `id()` 做字符串哈希。
    ///
    /// 返回:`Some(key)` = 调用方保证在同一 widget 生命周期内稳定且全局唯一;
    /// `None` = 框架退回 `id()` 的 FNV-1a 哈希。
    /// 用途:容器(`Flex`/`Stack`)在构造时算一次,`measure`/`arrange` 就不再碰字符串。
    /// 注意:不要用会随布局变化的值(尺寸、约束)做键,否则缓存会失效或串味。
    fn cache_key(&self) -> Option<u64> {
        None
    }

    /// 是否订阅 `UiEvent::Tick`(每帧心跳)。
    ///
    /// 返回:`true` = 广播 `Tick` 时调用本组件的 `event`;默认 `false`。
    /// 副作用:无。框架据此把 `Tick` 广播从"遍历全树"降为"只发给订阅者"。
    /// 用途:需要周期性拉取外设数据的组件(终端读 PTY、动画)。
    /// 注意:不订阅就永远收不到 `Tick`——需要心跳的组件必须覆写它。
    fn wants_tick(&self) -> bool {
        false
    }

    /// 是否处于"模态"状态(模态时焦点与点击都被限制在该子树内)。
    ///
    /// 返回:`true` = 此时它是模态层;默认 `false`。
    /// 它描述的是**交互语义**(是否阻塞底层),与绘制层级无关——
    /// 想表达"画在上层但不阻塞"的浮层,`is_open` 为真、`is_modal` 为假即可。
    fn is_modal(&self) -> bool {
        false
    }

    /// 是否被禁用。禁用节点会被排除在 Tab 遍历之外。
    ///
    /// 返回:`true` 表示不接收键盘焦点、不参与 Tab。
    /// 副作用:无。注意它**不**阻止鼠标事件——组件应在 `event` 里自己判 `disabled`。
    /// 与 `semantics().disabled` 不一致会导致外部工具看到的状态与行为不符。
    fn is_disabled(&self) -> bool {
        false
    }

    /// 子控件的裁剪矩形(`Some` 时每个子控件的绘制被限制在**它自己的格子**里)。
    ///
    /// 输入:该子控件的格子矩形(`arrange` 给它的那个)。
    /// 输出:`None` = 不裁剪(默认,现有控件零影响)。
    /// 用途:`Grid` 用它把"固定列宽"变成硬约束——内容太大就裁掉,而不是撑破布局。
    ///
    /// 为什么不能用 `clip()` 代替:`clip()` 裁的是**节点自己及其整棵子树**(滚动容器的视口),
    /// 而这里要的是**逐个子控件**各裁各的格子,两者作用域不同。
    fn children_clip(&self, _cell_rect: Rect) -> Option<Rect> {
        None
    }

    /// 本节点及其子树的裁剪矩形(与 `rect()` 同一坐标系)。
    ///
    /// 返回:`Some(rect)` 表示裁剪到该矩形(滚动容器声明视口);`None` 不裁剪。
    /// 副作用:无。框架在渲染时自动 `push_clip` / `pop_clip`,组件无需手写。
    /// 注意:命中测试用 `rect ∩ clip` —— 看不见的区域点不到。
    fn clip(&self) -> Option<Rect> {
        None
    }

    /// 向下转型:容器需要就地修改子节点(虚拟化行复用)时用。
    ///
    /// 返回:`Some(self as &mut dyn Any)` 才能被 `downcast_mut`;默认 `None`(不支持)。
    /// 副作用:返回的是可变借用,调用方拿到的期间不能再借用树。
    /// 失败:`None` 表示该组件不支持向下转型,调用方应回退到"重建节点"。
    fn as_any_mut(&mut self) -> Option<&mut dyn std::any::Any> {
        None
    }

    /// 鼠标悬停时的光标形状(静态声明)。
    ///
    /// 返回:`Cursor`,由应用层映射到平台(框架不认识 `IDC_*`)。
    /// 副作用:无;但框会在**每次 MouseMove** 都问一次(实现要够快)。
    fn cursor(&self) -> Cursor {
        Cursor::Default
    }

    /// 按指针位置计算光标形状(需要几何判断的组件覆写它)。
    ///
    /// 输入:`pos` 为屏幕坐标(同一坐标系下的指针位置)。
    /// 返回:该位置对应的光标;默认退回 `cursor()`。
    /// 副作用:无。仅当节点被命中时调用。
    fn cursor_at(&self, _pos: Pos) -> Cursor {
        self.cursor()
    }

    /// 子节点(容器才有)。
    ///
    /// 返回:只读切片;默认空切片(叶子组件)。
    /// 副作用:无。框架用它建索引、算焦点顺序、递归查询语义。
    /// 约束:返回的顺序就是**树序**(影响绘制先后与 Tab 顺序)。
    fn children(&self) -> &[Box<dyn Widget>] {
        &[]
    }

    /// 可变子节点。返回 `None` 表示叶子组件。
    ///
    /// 返回:`Some(&mut Vec<..>)` 才能被 `add_node` / `insert_node` / `remove_node`
    /// 修改;默认 `None`(叶子)。
    /// 副作用:调用方拿到可变借用期间不能再借用树。
    /// 注意:直接改这个 Vec **不会**触发布局失效——请走 `UiTree` 的增删接口。
    fn children_mut(&mut self) -> Option<&mut Vec<Box<dyn Widget>>> {
        None
    }

    /// 内容版本号:**内在尺寸**可能变化时 +1(文本变了、行数变了…)。
    ///
    /// 返回:当前版本;默认 0 表示"尺寸永不变化"。
    /// 副作用:无。框架用它判断 `measure` 结果能否复用。
    /// 约束:只有**影响尺寸**的状态才 bump;纯颜色/悬停态不要 bump,
    /// 否则每帧都会白白失效重算。
    fn layout_version(&self) -> u64 {
        0
    }

    /// 上报期望尺寸。父容器据此做 flex 分配。
    ///
    /// 输入:`metrics`(字体度量)、`constraints`(父给的最小/最大尺寸)、`cache`(布局缓存)。
    /// 返回:受 `constraints` 约束后的尺寸。
    /// 副作用:允许缓存内部测量结果(通常由 `cache` 代劳),**不得**修改树结构。
    /// 约束:容器必须用 `cache.measure(child, …)` 而不是 `child.measure(…)`,
    /// 否则会绕过缓存与去重,大树上退化成指数级重复测量。
    fn measure(
        &mut self,
        metrics: &dyn TextMetrics,
        constraints: Constraints,
        cache: &mut crate::layout::LayoutCache,
    ) -> Size;

    /// 父容器算好绝对矩形后回传。
    ///
    /// 输入:`rect`(父坐标系里的绝对矩形)、`metrics`、`cache`。
    /// 返回:无。
    /// 副作用:**应当**把 `rect` 存下来(`rect()` 后续要靠它),容器还要给子节点排布。
    /// 约束:递归由容器驱动(`layout::flex_arrange`),框架不预设子节点怎么摆。
    fn arrange(
        &mut self,
        rect: Rect,
        metrics: &dyn TextMetrics,
        cache: &mut crate::layout::LayoutCache,
    );

    /// 自己当前的矩形(`arrange` 之后有效)。
    ///
    /// 返回:父坐标系矩形;布局前是 `Rect::ZERO`(因此 `rect_of` 会拒绝未布局的节点)。
    /// 副作用:无(框架把它当纯查询,会高频调用)。
    fn rect(&self) -> Rect;

    /// 事件。返回 `Ignored` 表示不关心,让框架继续找别人。
    ///
    /// 输入:`ctx`(可提请焦点/捕获/重绘/发命令)、`event`(三阶段之一)。
    /// 返回:`EventResult`;`Handled` 阻止继续冒泡,`Redraw`/`Relayout` 追加标志。
    /// 副作用:允许改自身状态(这是唯一的状态变更入口之一,另一个是 `on_mount`)。
    /// 约束:**Capture 阶段只拦截,不要在这里做响应**——否则响应会执行两次。
    fn event(&mut self, _ctx: &mut EventCtx, _event: &UiEvent) -> EventResult {
        EventResult::Ignored
    }

    /// 出图。只允许通过 `RenderCtx`。
    ///
    /// 输入:`ctx`(`fill_rect` / `draw_text` / `draw_char` / 裁剪栈 / 主题)。
    /// 返回:无。
    /// 副作用:**不得有任何副作用**——不能改自身状态、不能 dispatch、不能读写外部可变状态。
    /// 原因:revision 的哈希时机在 `layout()` 之前,`render` 里改的状态不会被捕获。
    fn render(&mut self, _ctx: &mut dyn RenderCtx);

    /// 语义信息。给外部工具看的**唯一**来源。
    ///
    /// 返回:`Semantics`;默认 `exposed_to_ai = false`(不对外部开放)。
    /// 副作用:无(但会被高频调用:查询、哈希、推送)。
    /// 约束:会参与语义面哈希(label/value/state/disabled/sensitive/exposed/focused),
    /// 因此**不要**把悬停、动画帧等瞬时状态放进去,否则 revision 会被淹没。
    fn semantics(&self) -> Semantics {
        Semantics::default()
    }

    /// 内边距:容器用它把子节点内缩。
    ///
    /// 返回:`Edges`(逻辑像素);默认 `ZERO`。
    /// 副作用:无。只影响容器的排布,不改变自身矩形。
    fn padding(&self) -> Edges {
        Edges::ZERO
    }

    /// 本节点墨迹越出 `rect()` 的量(像素,四边统一取最大值)。
    ///
    /// 返回:`Some(margin)` = 本节点自己的墨迹边距;`None` = 沿用框架的树级默认
    /// (用 `TextStyle::default()` 度量算出的那一份)。
    ///
    /// **为什么必须是 per-node**:树级默认只按 16px 算一次(实测边距 4.63px),
    /// 而 `TextLabel::size(24/32/48)` 的真实墨迹溢出随字号线性增长
    /// (6.95 / 9.27 / 13.90px)。用同一个常数套所有节点时,
    /// 大字号文本的局部重绘会留下残影 —— 实测 24px 差 72 像素、48px 差 312 像素。
    ///
    /// 约束:返回值应随字号单调(通常就是 `max(ink_depth − line_height, 2px)`),
    /// 且**不得超过一行**——异常字体度量不该把脏区撑到离谱。
    fn ink_overflow(&self, _metrics: &dyn TextMetrics) -> Option<f32> {
        None
    }

    /// 本帧是否必须全量重绘(例如全屏遮罩、动画)。
    ///
    /// 返回:`true` 表示忽略脏区、整棵子树都画;默认 `false`。
    /// 副作用:无。返回 true 会让脏区剪枝失效,**只给真正每帧都变的节点用**。
    fn always_paints(&self) -> bool {
        false
    }

    /// 脏区剪枝:与脏区不相交就可以整棵子树跳过。
    ///
    /// 输入:当前帧脏区。返回:是否需要绘制本节点。
    /// 副作用:无。默认实现是 `always_paints() || dirty.intersects(rect())`。
    /// 注意:节点矩形为空(未布局)时 `intersects` 为 false → 整棵跳过,
    /// 这是"未布局不绘制"的机制保证。
    fn needs_paint(&self, dirty: &crate::tree::DirtyRegion) -> bool {
        self.always_paints() || dirty.intersects(self.rect())
    }
}

#[cfg(test)]
mod event_ctx_scale_tests {
    use super::*;

    /// **`EventCtx::new` 默认 `scale = 1.0`**(测试与非布局路径行为不变)。
    #[test]
    fn event_ctx_scale_defaults_to_one() {
        let ctx = EventCtx::new(None, Size::new(100.0, 100.0));
        assert_eq!(
            ctx.scale(),
            1.0,
            "默认 scale 必须是 1.0,否则现有测试会变尺寸"
        );
    }

    /// **`scale` 可被 `UiTree` 写入并读回**(命中测试依赖它与绘制同源)。
    #[test]
    fn event_ctx_scale_is_settable() {
        let mut ctx = EventCtx::new(None, Size::new(100.0, 100.0));
        ctx.scale = 1.25;
        assert_eq!(ctx.scale(), 1.25);
    }
}