michiu_ui 0.0.1

The michiu library handles layout and rendering.
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
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
pub mod font;
pub mod from_into;
pub mod index;
pub mod layout_data;
pub mod string;

pub use font::*;
pub use from_into::*;
pub use index::*;
pub use layout_data::*;
pub use string::*;

use crate::{Context, Element, EntityId, ImeState, MichiuError, PropertyList, VirtualKey, rgba};
use bytemuck::{Pod, Zeroable};
use std::{path::PathBuf, sync::Arc, time::Duration};
use windows::Win32::{
    Graphics::Gdi::{
        BITMAPINFO, BITMAPINFOHEADER, CreateBitmap, CreateDIBSection, DIB_RGB_COLORS, DeleteObject,
        GetDC, HGDIOBJ, ReleaseDC,
    },
    UI::WindowsAndMessaging::{CreateIconIndirect, HCURSOR, ICONINFO},
};

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Pod, Zeroable)]
pub struct Color {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}

impl Default for Color {
    fn default() -> Self {
        Color::TRANSPARENT
    }
}

impl Color {
    pub const TRANSPARENT: Self = Self {
        r: 0.0,
        g: 0.0,
        b: 0.0,
        a: 0.0,
    };
    pub const WHITE: Self = Self {
        r: 1.0,
        g: 1.0,
        b: 1.0,
        a: 1.0,
    };
    pub const BLACK: Self = Self {
        r: 0.0,
        g: 0.0,
        b: 0.0,
        a: 1.0,
    };

    pub const RED: Self = Self {
        r: 1.0,
        g: 0.0,
        b: 0.0,
        a: 1.0,
    };
    pub const GREEN: Self = Self {
        r: 0.0,
        g: 1.0,
        b: 0.0,
        a: 1.0,
    };
    pub const BLUE: Self = Self {
        r: 0.0,
        g: 0.0,
        b: 1.0,
        a: 1.0,
    };
    pub const YELLOW: Self = Self {
        r: 1.0,
        g: 1.0,
        b: 0.0,
        a: 1.0,
    };
    pub const ORANGE: Self = Self {
        r: 1.0,
        g: 0.5,
        b: 0.0,
        a: 1.0,
    };
    pub const PURPLE: Self = Self {
        r: 0.5,
        g: 0.0,
        b: 0.5,
        a: 1.0,
    };
    pub const CYAN: Self = Self {
        r: 0.0,
        g: 1.0,
        b: 1.0,
        a: 1.0,
    };
    pub const MAGENTA: Self = Self {
        r: 1.0,
        g: 0.0,
        b: 1.0,
        a: 1.0,
    };
    pub const GRAY: Self = Self {
        r: 0.5,
        g: 0.5,
        b: 0.5,
        a: 1.0,
    };
    pub const LIGHT_GRAY: Self = Self {
        r: 0.75,
        g: 0.75,
        b: 0.75,
        a: 1.0,
    };
    pub const DARK_GRAY: Self = Self {
        r: 0.25,
        g: 0.25,
        b: 0.25,
        a: 1.0,
    };

    /// GPU/シェーダー用の 0.0~1.0 (f32) 値から直接生成します
    #[inline]
    #[must_use]
    pub(crate) const fn rgb_f32(r: f32, g: f32, b: f32) -> Self {
        Self { r, g, b, a: 1.0 }
    }

    /// GPU/シェーダー用の 0.0~1.0 (f32) 値から直接生成します
    #[inline]
    #[must_use]
    pub(crate) const fn rgba_f32(r: f32, g: f32, b: f32, a: f32) -> Self {
        Self { r, g, b, a }
    }

    /// I'll only change the opacity.
    #[inline]
    #[must_use]
    pub const fn with_alpha(self, a: f32) -> Self {
        Self { a, ..self }
    }

    /// Generates a [`Color`] from the HSL (Hue: 0..360, Saturation: 0..100%, Lightness: 0..100%).
    #[inline]
    #[must_use]
    pub fn hsl(h: f32, s: f32, l: f32) -> Self {
        Self::hsla(h, s, l, 1.0)
    }

    /// Generate a [`Color`] by applying an alpha value (0.0..1.0) to HSL
    #[must_use]
    #[allow(clippy::many_single_char_names)]
    pub fn hsla(h: f32, s: f32, l: f32, a: f32) -> Self {
        // 色相(h)を 0..360 の範囲に正規化
        let h_mod = (h % 360.0 + 360.0) % 360.0;

        // s, l を 0.0..100.0 (%) から 0.0..1.0 の比率へ安全変換
        let s = (s / 100.0).clamp(0.0, 1.0);
        let l = (l / 100.0).clamp(0.0, 1.0);

        let c = (1.0 - (2.0 * l - 1.0).abs()) * s;
        let x = c * (1.0 - ((h_mod / 60.0) % 2.0 - 1.0).abs());
        let m = l - c / 2.0;

        let (r, g, b) = if h_mod < 60.0 {
            (c, x, 0.0)
        } else if h_mod < 120.0 {
            (x, c, 0.0)
        } else if h_mod < 180.0 {
            (0.0, c, x)
        } else if h_mod < 240.0 {
            (0.0, x, c)
        } else if h_mod < 300.0 {
            (x, 0.0, c)
        } else {
            (c, 0.0, x)
        };

        Self {
            r: r + m,
            g: g + m,
            b: b + m,
            a,
        }
    }
}

pub trait IntoHexColor {
    fn into_hex_color(self) -> Color;
}

impl IntoHexColor for &str {
    #[inline]
    fn into_hex_color(self) -> Color {
        let s = self.trim_start_matches('#').trim_start_matches("0x");
        if let Ok(num) = u32::from_str_radix(s, 16) {
            parse_u32_to_color(num, s.len() > 6)
        } else {
            Color::TRANSPARENT
        }
    }
}

impl IntoHexColor for String {
    #[inline]
    fn into_hex_color(self) -> Color {
        self.as_str().into_hex_color()
    }
}

impl IntoHexColor for u32 {
    #[inline]
    fn into_hex_color(self) -> Color {
        parse_u32_to_color(self, self > 0xFF_FFFF)
    }
}

#[inline]
fn parse_u32_to_color(num: u32, is_8digit: bool) -> Color {
    if is_8digit {
        let r = ((num >> 24) & 0xFF) as f32 / 255.0;
        let g = ((num >> 16) & 0xFF) as f32 / 255.0;
        let b = ((num >> 8) & 0xFF) as f32 / 255.0;
        let a = (num & 0xFF) as f32 / 255.0;
        Color { r, g, b, a }
    } else {
        let r = ((num >> 16) & 0xFF) as f32 / 255.0;
        let g = ((num >> 8) & 0xFF) as f32 / 255.0;
        let b = (num & 0xFF) as f32 / 255.0;
        Color { r, g, b, a: 1.0 }
    }
}

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Pod, Zeroable)]
pub struct LayoutPoint {
    pub x: f32,
    pub y: f32,
}

impl Default for LayoutPoint {
    fn default() -> Self {
        LayoutPoint::ZERO
    }
}

impl LayoutPoint {
    pub const ZERO: Self = Self { x: 0.0, y: 0.0 };

    pub const ORIGIN: Self = Self { x: 0.5, y: 0.5 };

    #[inline]
    #[must_use]
    pub const fn new(x: f32, y: f32) -> Self {
        Self { x, y }
    }
}

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Pod, Zeroable)]
pub struct LayoutSize {
    pub width: f32,
    pub height: f32,
}

impl Default for LayoutSize {
    fn default() -> Self {
        LayoutSize::ZERO
    }
}

impl LayoutSize {
    pub const ZERO: Self = Self {
        width: 0.0,
        height: 0.0,
    };

    #[inline]
    #[must_use]
    pub const fn new(width: f32, height: f32) -> Self {
        Self { width, height }
    }
}

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Pod, Zeroable)]
pub struct LayoutRect {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

impl Default for LayoutRect {
    fn default() -> Self {
        LayoutRect::ZERO
    }
}

impl LayoutRect {
    pub const ZERO: Self = Self {
        x: 0.0,
        y: 0.0,
        width: 0.0,
        height: 0.0,
    };

    #[inline]
    #[must_use]
    pub const fn new(x: f32, y: f32, width: f32, height: f32) -> Self {
        Self {
            x,
            y,
            width,
            height,
        }
    }

    /// Determine if the mouse coordinates, etc., are included within this rectangle.
    #[inline]
    #[must_use]
    pub(crate) fn contains(&self, point: LayoutPoint) -> bool {
        // 幅または高さが 0 以下の場合は、当たり判定を即座に却下する
        if self.width <= 0.0 || self.height <= 0.0 {
            return false;
        }

        point.x >= self.x
            && point.x <= self.x + self.width
            && point.y >= self.y
            && point.y <= self.y + self.height
    }

    /// 2つの矩形が交差する領域(共通部分)を計算して返す。
    ///
    /// 共通部分が存在しない(はみ出している・離れている)場合は、
    /// 幅(width)と高さ(height)が `0.0` の空の `Rect` を返す。
    #[inline]
    #[must_use]
    pub(crate) fn intersect(&self, other: &Self) -> Self {
        // 交差領域の左上座標(最大値をとる)
        let x1 = self.x.max(other.x);
        let y1 = self.y.max(other.y);

        // 交差領域の右下座標(最小値をとる)
        let x2 = (self.x + self.width).min(other.x + other.width);
        let y2 = (self.y + self.height).min(other.y + other.height);

        // 幅と高さが負数(離れている状態)になった場合は max(0.0) で 0.0 に丸める
        let width = (x2 - x1).max(0.0);
        let height = (y2 - y1).max(0.0);

        Self {
            x: x1,
            y: y1,
            width,
            height,
        }
    }
}

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Pod, Zeroable)]
pub struct EdgeInsets {
    pub top: f32,
    pub right: f32,
    pub bottom: f32,
    pub left: f32,
}

impl Default for EdgeInsets {
    fn default() -> Self {
        EdgeInsets::ZERO
    }
}

impl EdgeInsets {
    pub const ZERO: Self = Self {
        top: 0.0,
        right: 0.0,
        bottom: 0.0,
        left: 0.0,
    };

    /// Generate images by specifying each of the four directions individually in physical pixels.
    #[inline]
    #[must_use]
    pub const fn px(top: f32, right: f32, bottom: f32, left: f32) -> Self {
        Self {
            top,
            right,
            bottom,
            left,
        }
    }

    /// Set all four directions to the same physical pixel at once.
    #[inline]
    #[must_use]
    pub const fn px_all(value: f32) -> Self {
        Self {
            top: value,
            right: value,
            bottom: value,
            left: value,
        }
    }

    /// Specify symmetry for the top/bottom and left/right axes in physical pixels.
    #[inline]
    #[must_use]
    pub const fn px_sym(vertical: f32, horizontal: f32) -> Self {
        Self {
            top: vertical,
            right: horizontal,
            bottom: vertical,
            left: horizontal,
        }
    }
}

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default, Pod, Zeroable)]
pub struct CornerRadius {
    pub top_left: f32,
    pub top_right: f32,
    pub bottom_right: f32,
    pub bottom_left: f32,
}

impl CornerRadius {
    pub const ZERO: Self = Self {
        top_left: 0.0,
        top_right: 0.0,
        bottom_right: 0.0,
        bottom_left: 0.0,
    };

    #[inline]
    #[must_use]
    pub const fn radius(
        top_left: f32,
        top_right: f32,
        bottom_right: f32,
        bottom_left: f32,
    ) -> Self {
        Self {
            top_left,
            top_right,
            bottom_right,
            bottom_left,
        }
    }

    /// Creates rounded corners that are vertically symmetrical or horizontally symmetrical.
    #[inline]
    #[must_use]
    pub const fn symmetric(vertical: f32, horizontal: f32) -> Self {
        Self {
            top_left: vertical,
            top_right: vertical,
            bottom_right: horizontal,
            bottom_left: horizontal,
        }
    }

    /// All four corners have the same rounded shape.
    #[inline]
    #[must_use]
    pub const fn all(radius: f32) -> Self {
        Self {
            top_left: radius,
            top_right: radius,
            bottom_right: radius,
            bottom_left: radius,
        }
    }
}

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Pod, Zeroable)]
pub struct BoxShadow {
    pub offset: LayoutPoint,
    pub blur: f32,
    pub spread: f32,
    pub color: Color,
}

impl Default for BoxShadow {
    #[inline]
    fn default() -> Self {
        Self {
            offset: LayoutPoint::ZERO,
            blur: 0.0,
            spread: 0.0,
            color: Color::BLACK, // デフォルトは黒
        }
    }
}

impl BoxShadow {
    #[inline]
    #[must_use]
    pub const fn new() -> Self {
        Self {
            offset: LayoutPoint::ZERO,
            blur: 0.0,
            spread: 0.0,
            color: Color::BLACK,
        }
    }

    /// Set the shadow offset (x, y).
    #[inline]
    #[must_use]
    pub fn offset(mut self, value: impl IntoLayoutPoint) -> Self {
        self.offset = value.into_layout_point();
        self
    }

    /// Set the shadow blur width.
    #[inline]
    #[must_use]
    pub fn blur(mut self, value: impl Convert<f32>) -> Self {
        self.blur = value.convert();
        self
    }

    /// Set the shadow spread width.
    #[inline]
    #[must_use]
    pub fn spread(mut self, value: impl Convert<f32>) -> Self {
        self.spread = value.convert();
        self
    }

    /// Set the shadow color width.
    ///
    /// Default is [`Color::BLACK`].
    #[inline]
    #[must_use]
    pub fn color(mut self, value: Color) -> Self {
        self.color = value;
        self
    }

    /// A subtle, ultra-fine soft shadow
    #[must_use]
    pub fn sm() -> Self {
        BoxShadow::new()
            .blur(2)
            .color(rgba(0, 0, 0, 0.05))
            .offset((0, 1))
    }

    /// Standard Medium Soft Shadow
    #[must_use]
    pub fn md() -> Self {
        BoxShadow::new()
            .blur(6)
            .spread(-1)
            .color(rgba(0, 0, 0, 0.1))
            .offset((0, 4))
    }

    /// A large, soft shadow that appears to be slightly raised
    #[must_use]
    pub fn lg() -> Self {
        BoxShadow::new()
            .blur(15)
            .spread(-3)
            .color(rgba(0, 0, 0, 0.1))
            .offset((0, 10))
    }

    #[must_use]
    pub fn none() -> Self {
        Self {
            offset: LayoutPoint::ZERO,
            blur: 0.0,
            spread: 0.0,
            color: Color::TRANSPARENT,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
pub struct Size<T> {
    pub width: T,
    pub height: T,
}

impl<T> Size<T> {
    #[inline]
    pub const fn new(width: T, height: T) -> Self {
        Self { width, height }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
pub struct Rect<T> {
    pub top: T,
    pub right: T,
    pub bottom: T,
    pub left: T,
}

impl<T> Rect<T> {
    #[inline]
    pub const fn new(top: T, right: T, bottom: T, left: T) -> Self {
        Self {
            top,
            right,
            bottom,
            left,
        }
    }
}

impl<T: Clone> Rect<T> {
    #[inline]
    pub fn all(value: T) -> Self {
        Self {
            top: value.clone(),
            right: value.clone(),
            bottom: value.clone(),
            left: value,
        }
    }

    #[inline]
    pub fn symmetric(vertical: T, horizontal: T) -> Self {
        Self {
            top: vertical.clone(),
            right: horizontal.clone(),
            bottom: vertical,
            left: horizontal,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
pub struct Point<T> {
    pub x: T,
    pub y: T,
}

impl Point<f32> {
    pub const ORIGIN: Self = Self { x: 0.5, y: 0.5 };
}

impl<T> Point<T> {
    #[inline]
    pub const fn new(x: T, y: T) -> Self {
        Self { x, y }
    }
}

impl<T: Clone> Point<T> {
    #[inline]
    pub fn all(value: T) -> Self {
        Self {
            x: value.clone(),
            y: value,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Length {
    Px(f32),
    Percent(f32),
}

impl Length {
    #[inline]
    #[must_use]
    pub fn px(px: f32) -> Self {
        Self::Px(px)
    }

    #[inline]
    #[must_use]
    pub fn pct(percent: f32) -> Self {
        Self::Percent(percent)
    }

    /// Returns that value if it is a Px; otherwise, returns 0.0.
    #[inline]
    #[must_use]
    pub fn to_px_or_zero(&self) -> f32 {
        match *self {
            Length::Px(v) => v,
            Length::Percent(_) => 0.0,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Val {
    Auto,
    Px(f32),
    Percent(f32),
}

impl Val {
    #[inline]
    #[must_use]
    pub fn auto() -> Self {
        Self::Auto
    }

    #[inline]
    #[must_use]
    pub fn px(px: f32) -> Self {
        Self::Px(px)
    }

    #[inline]
    #[must_use]
    pub fn pct(percent: f32) -> Self {
        Self::Percent(percent)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Display {
    #[default]
    Flex,
    Grid,
    Block,
    None,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Position {
    #[default]
    Relative,
    Absolute,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum BoxSizing {
    #[default]
    BorderBox,
    ContentBox,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Direction {
    #[default]
    Ltr,
    Rtl,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum Overflow {
    #[default]
    Visible,
    Hidden,
    Scroll,
    Clip,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct LayoutOverflow {
    pub x: Overflow,
    pub y: Overflow,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum FlexDirection {
    #[default]
    Row,
    Column,
    RowReverse,
    ColumnReverse,
}

impl FlexDirection {
    #[inline]
    pub(crate) fn is_row(self) -> bool {
        self == FlexDirection::Row || self == FlexDirection::RowReverse
    }

    #[allow(unused)]
    #[inline]
    pub(crate) fn is_col(self) -> bool {
        self == FlexDirection::Column || self == FlexDirection::ColumnReverse
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum FlexWrap {
    #[default]
    NoWrap,
    Wrap,
    WrapReverse,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum AlignItems {
    Start,
    End,
    FlexStart,
    FlexEnd,
    Center,
    Baseline,
    #[default]
    Stretch,
    SafeStart,
    SafeEnd,
    SafeFlexStart,
    SafeFlexEnd,
    SafeCenter,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum AlignSelf {
    Start,
    End,
    FlexStart,
    FlexEnd,
    Center,
    Baseline,
    #[default]
    Stretch,
    SafeStart,
    SafeEnd,
    SafeFlexStart,
    SafeFlexEnd,
    SafeCenter,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum JustifyContent {
    Start,
    End,
    Center,
    #[default]
    Stretch,
    SpaceBetween,
    SpaceAround,
    SpaceEvenly,
    FlexStart,
    FlexEnd,
    SafeStart,
    SafeEnd,
    SafeFlexStart,
    SafeFlexEnd,
    SafeCenter,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum AlignContent {
    Start,
    End,
    Center,
    #[default]
    Stretch,
    SpaceBetween,
    SpaceAround,
    SpaceEvenly,
    FlexStart,
    FlexEnd,
    SafeStart,
    SafeEnd,
    SafeFlexStart,
    SafeFlexEnd,
    SafeCenter,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum TextAlign {
    #[default]
    Auto,
    Left,
    Right,
    Center,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum GridAutoFlow {
    #[default]
    Row,
    Column,
    RowDense,
    ColumnDense,
}

#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum GridPlacement<S>
where
    S: ToString,
{
    #[default]
    Auto,
    Line(S),
    NamedLine(S),
    Span(S),
    NamedSpan(S),
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GridLine<T> {
    pub start: T,
    pub end: T,
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Transform {
    pub(crate) matrix: [[f32; 4]; 4],
}

impl Default for Transform {
    fn default() -> Self {
        Self::new()
    }
}

impl Transform {
    #[inline]
    #[must_use]
    pub fn new() -> Self {
        Self {
            matrix: [
                [1.0, 0.0, 0.0, 0.0],
                [0.0, 1.0, 0.0, 0.0],
                [0.0, 0.0, 1.0, 0.0],
                [0.0, 0.0, 0.0, 1.0],
            ],
        }
    }

    #[inline]
    #[must_use]
    pub fn translate(self, x: f32, y: f32) -> Self {
        let mut t = Self::new();
        t.matrix[3][0] = x;
        t.matrix[3][1] = y;
        self.mul(&t)
    }

    #[inline]
    #[must_use]
    pub fn scale(self, x: f32, y: f32) -> Self {
        let mut s = Self::new();
        s.matrix[0][0] = x;
        s.matrix[1][1] = y;
        self.mul(&s)
    }

    #[inline]
    #[must_use]
    pub fn rotate(self, radians: f32) -> Self {
        let mut r = Self::new();
        let cos = radians.cos();
        let sin = radians.sin();
        r.matrix[0][0] = cos;
        r.matrix[0][1] = sin;
        r.matrix[1][0] = -sin;
        r.matrix[1][1] = cos;
        self.mul(&r)
    }

    /// 4x4 行列の乗算処理(列優先 / Column-Major 対応)
    #[allow(clippy::needless_range_loop)]
    fn mul(&self, other: &Self) -> Self {
        let mut out = [[0.0; 4]; 4];
        for i in 0..4 {
            for j in 0..4 {
                out[i][j] = self.matrix[i][0] * other.matrix[0][j]
                    + self.matrix[i][1] * other.matrix[1][j]
                    + self.matrix[i][2] * other.matrix[2][j]
                    + self.matrix[i][3] * other.matrix[3][j];
            }
        }
        Self { matrix: out }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Transition {
    pub property_list: PropertyList,
    pub duration: Duration,
    pub curve: AnimationCurve,
}

impl Transition {
    #[must_use]
    pub fn new(property_list: PropertyList, duration: Duration, curve: AnimationCurve) -> Self {
        Self {
            property_list,
            duration,
            curve,
        }
    }

    #[must_use]
    pub fn property_list(mut self, property_list: PropertyList) -> Self {
        self.property_list = property_list;
        self
    }

    #[must_use]
    pub fn duration(mut self, duration: Duration) -> Self {
        self.duration = duration;
        self
    }

    #[must_use]
    pub fn curve(mut self, curve: AnimationCurve) -> Self {
        self.curve = curve;
        self
    }
}

/// Define the easing curve for the animation.
// TODO: バネ物理シミュレーション Spring Physics
// 摩擦(Damping)とバネの強さ(Stiffness)のパラメータから毎フレーム物理演算
#[derive(Debug, Clone, Copy)]
pub enum AnimationCurve {
    /// No easing (linear)
    Linear,
    /// Standard quadratic easing for acceleration and deceleration
    EaseInOutQuad,
    /// Accelerate
    EaseInQuad,
    /// Deceleration
    EaseOutQuad,
    /// An escape hatch capable of performing custom easing calculations
    /// (accepts values between 0.0 and 1.0 and returns values between 0.0 and 1.0)
    Custom(fn(f32) -> f32),
}

impl AnimationCurve {
    /// Evaluate the smoothed value based on the progress ratio t (0.0 <= t <= 1.0).
    #[must_use]
    pub fn evaluate(&self, t: f32) -> f32 {
        let t = t.clamp(0.0, 1.0);
        match *self {
            AnimationCurve::Linear => t,
            AnimationCurve::EaseInQuad => t * t,
            AnimationCurve::EaseOutQuad => t * (2.0 - t),
            AnimationCurve::EaseInOutQuad => {
                if t < 0.5 {
                    2.0 * t * t
                } else {
                    -1.0 + (4.0 - 2.0 * t) * t
                }
            }
            AnimationCurve::Custom(f) => f(t),
        }
    }
}

impl PartialEq for AnimationCurve {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Linear, Self::Linear)
            | (Self::EaseInOutQuad, Self::EaseInOutQuad)
            | (Self::EaseInQuad, Self::EaseInQuad)
            | (Self::EaseOutQuad, Self::EaseOutQuad) => true,
            (Self::Custom(f1), Self::Custom(f2)) => std::ptr::fn_addr_eq(*f1, *f2),
            _ => false,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PlaybackCount {
    Infinite,
    Count(u32),
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct KeyframeAnimation {
    /// What to Animate
    pub property: PropertyList,
    /// Time per lap (loop)
    pub duration: Duration,
    /// Number of loops
    pub iteration_count: PlaybackCount,
    /// Easing Curve
    pub curve: AnimationCurve,
}

impl KeyframeAnimation {
    #[inline]
    #[must_use]
    pub fn new(
        property: PropertyList,
        duration: Duration,
        iteration_count: PlaybackCount,
        curve: AnimationCurve,
    ) -> Self {
        Self {
            property,
            duration,
            iteration_count,
            curve,
        }
    }
}

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default, bytemuck::Pod, bytemuck::Zeroable)]
pub struct LinearGradient {
    pub start_color: Color,
    pub end_color: Color,
    pub angle: f32,                // ラジアン単位の角度
    pub(crate) _padding: [f32; 3], // アライメント
}

impl LinearGradient {
    #[inline]
    #[must_use]
    pub fn new(start_color: Color, end_color: Color, angle_degrees: f32) -> Self {
        Self {
            start_color,
            end_color,
            angle: angle_degrees.to_radians(),
            _padding: [0.0; 3],
        }
    }
}

/// Transparency Control for Pointer Events
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PointerEvents {
    /// Receive pointer events and prevent them from being displayed on the elements below
    #[default]
    Auto,
    /// Ignore pointer events and make the element transparent to the element below it
    None,
}

/// Interaction definitions that can be resolved by propagating from child elements
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum InteractionName {
    Hover,
    Focus,
    FocusVisible,
    Press,
    Disable,
    Active,
    Select,
    Drag,
    All,
}

macro_rules! define_event_dispatchers {
    (
        $(
            $fn_name:ident, $field_name:ident $(, $arg_name:ident : $arg_type:ty)*;
        )*
    ) => {
        $(
            #[inline]
            #[allow(dead_code)]
            pub(crate) fn $fn_name(
                cx: &mut Context,
                id: EntityId,
                $($arg_name : $arg_type),*
            ) {
                // events.evt_listeners から該当のハンドラを一時的に take する
                if let Some(mut handler) = cx
                    .events
                    .evt_listeners
                    .get_mut(id)
                    .and_then(|l| l.$field_name.take())
                {
                    let _guard = crate::ActiveElementGuard::new(id);

                    // コールバックを安全に実行
                    handler(cx, $($arg_name),*);

                    // 実行後コールバックを書き戻す
                    if let Some(l) = cx.events.evt_listeners.get_mut(id) {
                        l.$field_name = Some(handler);
                    }
                }
            }
        )*
    };
}

pub type ClickCallback = Box<dyn FnMut(&mut Context) + 'static>;
pub type MouseCallback =
    Box<dyn FnMut(&mut Context, MouseButton, Modifiers, ElementState) + 'static>;
pub type CursorMovedCallback = Box<dyn FnMut(&mut Context, LayoutPoint) + 'static>;
pub type MouseWheelCallback = Box<dyn FnMut(&mut Context, f32, f32) + 'static>;
pub type DragCallback = Box<dyn FnMut(&mut Context, LayoutPoint) + 'static>;
pub type KeyCallback = Box<dyn FnMut(&mut Context, VirtualKey, Modifiers, ElementState) + 'static>;
pub type CharCallback = Box<dyn FnMut(&mut Context, char) + 'static>;
pub type ImeCallback = Box<dyn FnMut(&mut Context, ImeState) + 'static>;
pub type FileDropCallback = Box<dyn FnMut(&mut Context, Vec<PathBuf>) + 'static>;
pub type FileDragCallback = Box<dyn FnMut(&mut Context) + 'static>;
pub type SimpleCallback = Box<dyn FnMut(&mut Context) + 'static>;

// 各コールバックの引数: (context, ドラッグ元のElement, 現在ホバーまたはドロップされた対象のElement)
// 失敗時は対象が None
pub type EntityDragCallback = Box<dyn FnMut(&mut Context, Element, Option<Element>) + 'static>;
pub type IdDragCallback = Box<dyn FnMut(&mut Context, EntityId, Option<EntityId>) + 'static>;
pub type EntityDropCallback = Box<dyn FnMut(&mut Context, Element, Option<Element>) + 'static>;
pub type IdDropCallback = Box<dyn FnMut(&mut Context, EntityId, Option<EntityId>) + 'static>;
// ドラッグ開始時のコールバック型。生成されたプレースホルダーの Element を受け取れます。
// 引数: (context, ドラッグ元のオリジナル要素, 生成されたプレースホルダー要素)
pub type DragStartCallback = Box<dyn FnMut(&mut Context, Element, Element) + 'static>;

/// Events bound to individual elements.
#[derive(Default)]
#[allow(clippy::struct_field_names)]
pub struct EventListeners {
    pub on_click: Option<ClickCallback>,
    pub on_right_click: Option<SimpleCallback>,
    pub on_mouse_input: Option<MouseCallback>,
    pub on_mouse_enter: Option<SimpleCallback>,
    pub on_mouse_leave: Option<SimpleCallback>,
    pub on_cursor_moved: Option<CursorMovedCallback>,
    pub on_mouse_wheel: Option<MouseWheelCallback>,
    pub on_drag: Option<DragCallback>,
    pub on_hover: Option<SimpleCallback>,
    pub on_focus: Option<SimpleCallback>,
    pub on_blur: Option<SimpleCallback>,
    pub on_disable: Option<SimpleCallback>,
    pub on_active: Option<SimpleCallback>,
    pub on_select: Option<SimpleCallback>,
    pub on_keyboard_input: Option<KeyCallback>,
    pub on_char_input: Option<CharCallback>,
    pub on_ime: Option<ImeCallback>,
    pub on_file_dropped: Option<FileDropCallback>,
    pub on_file_drag_enter: Option<FileDragCallback>,
    pub on_file_drag_leave: Option<FileDragCallback>,
    pub on_dnd_entity_drag: Option<EntityDragCallback>,
    pub on_dnd_id_drag: Option<IdDragCallback>,
    pub on_dnd_entity_drop: Option<EntityDropCallback>,
    pub on_dnd_id_drop: Option<IdDropCallback>,
    pub on_dnd_drag_start: Option<DragStartCallback>,
}

define_event_dispatchers! {
    // マウス・クリック
    handle_on_click, on_click;
    handle_on_right_click, on_right_click;
    handle_on_mouse_input, on_mouse_input, button: MouseButton, modifiers: Modifiers, state: ElementState;
    handle_on_mouse_enter, on_mouse_enter;
    handle_on_mouse_leave, on_mouse_leave;
    handle_on_cursor_moved, on_cursor_moved, pos: LayoutPoint;
    handle_on_mouse_wheel, on_mouse_wheel, delta_x: f32, delta_y: f32;
    handle_on_drag, on_drag, delta: LayoutPoint;

    // ステート変化
    handle_on_hover, on_hover;
    handle_on_focus, on_focus;
    handle_on_blur, on_blur;
    handle_on_disable, on_disable;
    handle_on_active, on_active;
    handle_on_select, on_select;

    // キーボード・入力
    handle_on_keyboard_input, on_keyboard_input, key: VirtualKey, modifiers: Modifiers, state: ElementState;
    handle_on_char_input, on_char_input, ch: char;
    handle_on_ime, on_ime, info: ImeState;

    // ファイルドロップ
    handle_on_file_dropped, on_file_dropped, paths: Vec<PathBuf>;
    handle_on_file_drag_enter, on_file_drag_enter;
    handle_on_file_drag_leave, on_file_drag_leave;

    // ドラッグ&ドロップ
    handle_on_dnd_entity_drag, on_dnd_entity_drag, origin: Element, target: Option<Element>;
    handle_on_dnd_id_drag, on_dnd_id_drag, origin_id: EntityId, target_id: Option<EntityId>;
    handle_on_dnd_entity_drop, on_dnd_entity_drop, origin: Element, target: Option<Element>;
    handle_on_dnd_id_drop, on_dnd_id_drop, origin_id: EntityId, target_id: Option<EntityId>;
    handle_on_dnd_drag_start, on_dnd_drag_start, origin: Element, placeholder: Element;
}

impl std::fmt::Debug for EventListeners {
    #[allow(clippy::too_many_lines)]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("EventListeners")
            .field("on_click", &self.on_click.as_ref().map(|_| "FnMut"))
            .field(
                "on_right_click",
                &self.on_right_click.as_ref().map(|_| "FnMut"),
            )
            .field(
                "on_mouse_input",
                &self.on_mouse_input.as_ref().map(|_| "MouseCallback"),
            )
            .field(
                "on_mouse_enter",
                &self.on_mouse_enter.as_ref().map(|_| "FnMut"),
            )
            .field(
                "on_mouse_leave",
                &self.on_mouse_leave.as_ref().map(|_| "FnMut"),
            )
            .field(
                "on_cursor_moved",
                &self.on_cursor_moved.as_ref().map(|_| "FnMut(Point)"),
            )
            .field(
                "on_mouse_wheel",
                &self.on_mouse_wheel.as_ref().map(|_| "FnMut(f32)"),
            )
            .field("on_drag", &self.on_drag.as_ref().map(|_| "FnMut(Point)"))
            .field(
                "on_keyboard_input",
                &self.on_keyboard_input.as_ref().map(|_| "KeyCallback"),
            )
            .field(
                "on_char_input",
                &self.on_char_input.as_ref().map(|_| "FnMut(char)"),
            )
            .field("on_ime", &self.on_ime.as_ref().map(|_| "FnMut(ImeState)"))
            .field(
                "on_file_dropped",
                &self.on_file_dropped.as_ref().map(|_| "FnMut(Vec<PathBuf>)"),
            )
            .field(
                "on_file_drag_enter",
                &self.on_file_drag_enter.as_ref().map(|_| "FnMut"),
            )
            .field(
                "on_file_drag_leave",
                &self.on_file_drag_leave.as_ref().map(|_| "FnMut"),
            )
            .field("on_hover", &self.on_hover.as_ref().map(|_| "FnMut"))
            .field("on_focus", &self.on_focus.as_ref().map(|_| "FnMut"))
            .field("on_blur", &self.on_blur.as_ref().map(|_| "FnMut"))
            .field("on_disable", &self.on_disable.as_ref().map(|_| "FnMut"))
            .field("on_active", &self.on_active.as_ref().map(|_| "FnMut"))
            .field("on_select", &self.on_select.as_ref().map(|_| "FnMut"))
            .field(
                "on_dnd_entity_drag",
                &self
                    .on_dnd_entity_drag
                    .as_ref()
                    .map(|_| "FnMut(EntityId, Option<EntityId>)"),
            )
            .field(
                "on_dnd_id_drag",
                &self
                    .on_dnd_id_drag
                    .as_ref()
                    .map(|_| "FnMut(EntityId, Option<EntityId>)"),
            )
            .field(
                "on_dnd_entity_drop",
                &self
                    .on_dnd_entity_drop
                    .as_ref()
                    .map(|_| "FnMut(EntityId, Option<EntityId>)"),
            )
            .field(
                "on_dnd_id_drop",
                &self
                    .on_dnd_id_drop
                    .as_ref()
                    .map(|_| "FnMut(EntityId, Option<EntityId>)"),
            )
            .field(
                "on_dnd_drag_start",
                &self
                    .on_dnd_drag_start
                    .as_ref()
                    .map(|_| "FnMut(EntityId, Element)"),
            )
            .finish()
    }
}

/// Global Cursor Types for Propagation
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GlobalCursorIcon {
    Default(Option<HCURSOR>),
    Pointer(Option<HCURSOR>),
    Text(Option<HCURSOR>),
    Grab(Option<HCURSOR>),
    Grabbing(Option<HCURSOR>),
    NotAllowed(Option<HCURSOR>),
    ResizeNs(Option<HCURSOR>),
    ResizeEw(Option<HCURSOR>),
    ResizeNesw(Option<HCURSOR>),
    ResizeNwse(Option<HCURSOR>),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CursorIcon {
    Default(Option<HCURSOR>),
    Pointer(Option<HCURSOR>),
    Text(Option<HCURSOR>),
    Grab(Option<HCURSOR>),
    Grabbing(Option<HCURSOR>),
    NotAllowed(Option<HCURSOR>),
    ResizeNs(Option<HCURSOR>),
    ResizeEw(Option<HCURSOR>),
    ResizeNesw(Option<HCURSOR>),
    ResizeNwse(Option<HCURSOR>),
    Global(GlobalCursorIcon),
}

unsafe impl Send for CursorIcon {}
unsafe impl Sync for CursorIcon {}

unsafe impl Send for GlobalCursorIcon {}
unsafe impl Sync for GlobalCursorIcon {}

impl Default for CursorIcon {
    fn default() -> Self {
        CursorIcon::Default(None)
    }
}

impl CursorIcon {
    /// Loads and returns the Windows `HCURSOR` physical handle.
    /// If a custom `HCURSOR` is specified, it takes precedence; otherwise, the OS's default is loaded.
    #[must_use]
    pub fn to_hcursor(self) -> crate::Result<HCURSOR> {
        use windows::Win32::UI::WindowsAndMessaging::{
            IDC_ARROW, IDC_HAND, IDC_IBEAM, IDC_NO, IDC_SIZEALL, IDC_SIZENESW, IDC_SIZENS,
            IDC_SIZENWSE, IDC_SIZEWE, LoadCursorW,
        };
        unsafe {
            let idc = match self {
                // 独自カーソル指定時は即座にそのハンドルを返却
                CursorIcon::Default(Some(h))
                | CursorIcon::Pointer(Some(h))
                | CursorIcon::Text(Some(h))
                | CursorIcon::Grab(Some(h))
                | CursorIcon::Grabbing(Some(h))
                | CursorIcon::NotAllowed(Some(h))
                | CursorIcon::ResizeNs(Some(h))
                | CursorIcon::ResizeEw(Some(h))
                | CursorIcon::ResizeNesw(Some(h))
                | CursorIcon::ResizeNwse(Some(h)) => return Ok(h),
                CursorIcon::Global(global_icon) => {
                    match global_icon {
                        GlobalCursorIcon::Default(Some(h))
                        | GlobalCursorIcon::Pointer(Some(h))
                        | GlobalCursorIcon::Text(Some(h))
                        | GlobalCursorIcon::Grab(Some(h))
                        | GlobalCursorIcon::Grabbing(Some(h))
                        | GlobalCursorIcon::NotAllowed(Some(h))
                        | GlobalCursorIcon::ResizeNs(Some(h))
                        | GlobalCursorIcon::ResizeEw(Some(h))
                        | GlobalCursorIcon::ResizeNesw(Some(h))
                        | GlobalCursorIcon::ResizeNwse(Some(h)) => return Ok(h),
                        _ => {}
                    }
                    // None 時は標準システムカーソルにフォールバック
                    match global_icon {
                        GlobalCursorIcon::Default(_) => IDC_ARROW,
                        GlobalCursorIcon::Pointer(_) => IDC_HAND,
                        GlobalCursorIcon::Text(_) => IDC_IBEAM,
                        GlobalCursorIcon::Grab(_) | GlobalCursorIcon::Grabbing(_) => IDC_SIZEALL,
                        GlobalCursorIcon::NotAllowed(_) => IDC_NO,
                        GlobalCursorIcon::ResizeNs(_) => IDC_SIZENS,
                        GlobalCursorIcon::ResizeEw(_) => IDC_SIZEWE,
                        GlobalCursorIcon::ResizeNesw(_) => IDC_SIZENESW,
                        GlobalCursorIcon::ResizeNwse(_) => IDC_SIZENWSE,
                    }
                }

                // 独自カーソル未指定(None)時は、Windows 標準カーソルからロード
                CursorIcon::Default(None) => IDC_ARROW,
                CursorIcon::Pointer(None) => IDC_HAND,
                CursorIcon::Text(None) => IDC_IBEAM,
                CursorIcon::Grab(None) | CursorIcon::Grabbing(None) => IDC_SIZEALL,
                CursorIcon::NotAllowed(None) => IDC_NO,
                CursorIcon::ResizeNs(None) => IDC_SIZENS,
                CursorIcon::ResizeEw(None) => IDC_SIZEWE,
                CursorIcon::ResizeNesw(None) => IDC_SIZENESW,
                CursorIcon::ResizeNwse(None) => IDC_SIZENWSE,
            };

            LoadCursorW(None, idc).map_err(|e| MichiuError::WindowsApiError { source: e })
        }
    }

    /// Generates a custom `HCURSOR` with the specified hotspot coordinates from the RGBA8 pixel data in memory.
    pub fn create_from_rgba(
        rgba_pixels: &[u8],
        width: u32,
        height: u32,
        hotspot_x: u32,
        hotspot_y: u32,
    ) -> crate::Result<HCURSOR> {
        let len = rgba_pixels.len();
        if len != (width * height * 4) as usize {
            return Err(MichiuError::CursorCreationFailed(
                "The pixel buffer size ({len}) does not match the resolution ({width} * {height} * 4)."
                    .into()
            ));
        }

        unsafe {
            let h_dc = GetDC(None);
            if h_dc.is_invalid() {
                return Err(MichiuError::CursorCreationFailed(
                    "Failed to obtain device context (DC).".into(),
                ));
            }

            let bmi = BITMAPINFO {
                bmiHeader: BITMAPINFOHEADER {
                    biSize: std::mem::size_of::<BITMAPINFOHEADER>() as u32,
                    biWidth: width as i32,
                    biHeight: -(height as i32),
                    biPlanes: 1,
                    biBitCount: 32,
                    biCompression: 0,
                    ..Default::default()
                },
                ..Default::default()
            };

            let mut pv_bits = std::ptr::null_mut();
            let hbm_color = CreateDIBSection(
                Some(h_dc),
                &raw const bmi,
                DIB_RGB_COLORS,
                &raw mut pv_bits,
                None,
                0,
            )
            .map_err(|e| {
                MichiuError::CursorCreationFailed(format!("Failed to create DIBSection.: {e}"))
            })?;

            if pv_bits.is_null() {
                let _ = ReleaseDC(None, h_dc);
                let _ = DeleteObject(HGDIOBJ(hbm_color.0));
                return Err(MichiuError::CursorCreationFailed(
                    "DIBSection memory allocation failed.".into(),
                ));
            }

            let dest_slice =
                std::slice::from_raw_parts_mut(pv_bits.cast::<u8>(), (width * height * 4) as usize);
            for i in (0..(width * height * 4) as usize).step_by(4) {
                dest_slice[i] = rgba_pixels[i + 2]; // B
                dest_slice[i + 1] = rgba_pixels[i + 1]; // G
                dest_slice[i + 2] = rgba_pixels[i]; // R
                dest_slice[i + 3] = rgba_pixels[i + 3]; // A
            }

            let hbm_mask = CreateBitmap(width as i32, height as i32, 1, 1, None);

            let icon_info = ICONINFO {
                fIcon: false.into(),
                xHotspot: hotspot_x,
                yHotspot: hotspot_y,
                hbmMask: hbm_mask,
                hbmColor: hbm_color,
            };

            let h_icon = CreateIconIndirect(&raw const icon_info).map_err(|e| {
                MichiuError::CursorCreationFailed(format!("IconIndirect generation failed.: {e}"))
            })?;
            let h_cursor = windows::Win32::UI::WindowsAndMessaging::HCURSOR(h_icon.0);

            let _ = DeleteObject(HGDIOBJ(hbm_color.0));
            let _ = DeleteObject(HGDIOBJ(hbm_mask.0));
            let _ = ReleaseDC(None, h_dc);

            Ok(h_cursor)
        }
    }

    /// Generates a custom `HCURSOR` with the specified hotspot coordinates based on the image file path.
    pub fn create_from_path(
        path: impl AsRef<std::path::Path>,
        hotspot_x: u32,
        hotspot_y: u32,
    ) -> crate::Result<HCURSOR> {
        let p_clone = path.as_ref().to_path_buf();
        let img = image::open(path).map_err(|e| MichiuError::ImageLoadFailed {
            path: p_clone,
            source: Arc::new(e),
        })?;
        let rgba_img = img.to_rgba8();
        let (width, height) = rgba_img.dimensions();

        Self::create_from_rgba(rgba_img.as_raw(), width, height, hotspot_x, hotspot_y)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum MouseButton {
    Left,
    Right,
    Middle,
    X1,
    X2,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ElementState {
    Pressed,
    Released,
}

#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Hash)]
pub struct Modifiers {
    pub shift: bool,
    pub ctrl: bool,
    pub alt: bool,
    pub logo: bool, // Windowsキー
}

/// Not Implemented
#[derive(Debug, Clone, PartialEq)]
pub enum UiaValue {
    String(String),
    Bool(bool),
    Int(i32),
    Double(f64),
}

/// Windows 11's Native System Background Blur Effect
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(i32)]
pub enum Backdrop {
    #[default]
    None = 0,
    Mica = 2,
    Acrylic = 3,
    MicaAlt = 4,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum UserSelect {
    #[default]
    None,
    Text,
    All,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u32)]
pub enum BorderStyle {
    #[default]
    Solid = 0,
    Dotted = 1,
    Dashed = 2,
    Double = 3,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[repr(u32)]
pub enum BorderAlignment {
    #[default]
    Start = 0, // 左・上が基準 (左から右、上から下へ伸びる)
    End = 1,    // 右・下が基準 (右から左、下から上へ伸びる)
    Center = 2, // 中心が基準 (中心から両方向へ対称に広がる)
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum InteractionState {
    Hovered,
    Focused,
    Pressed,
    Dragged,
}

/// Trait for dynamically supplying textures
pub trait ExternalTexture: Send + Sync {
    /// Called immediately before rendering,
    /// this function returns the latest [`wgpu::TextureView`] that should be rendered in this frame.
    fn resolve_view(&self, device: &wgpu::Device, queue: &wgpu::Queue) -> wgpu::TextureView;

    /// Retrieve the metadata that controls the rendering method.
    fn metadata(&self) -> ExternalTextureMetadata;
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ExternalTextureMetadata {
    pub size: LayoutSize,
    pub alpha_mode: ExternalTextureAlphaMode,
    pub y_flip: bool,
    /// Whether the texture is an -Srgb-based automatic color space conversion format
    pub is_srgb: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExternalTextureAlphaMode {
    /// Standard alpha. Automatically converted to PMA (multiplied alpha) within the shader.
    Straight,
    /// Multiplied alpha. Composited directly within the shader.
    Premultiplied,
}

#[cfg(test)]
mod tests;