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
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
//! Base widget for every other widget in the crate. It contains layout-specific info, parent-child relations
//! visibility, various transforms, drag'n'drop-related data, etc. See [`Widget`] docs for more info.

#![warn(missing_docs)]

use crate::{
    brush::Brush,
    core::{algebra::Vector2, math::Rect, pool::Handle},
    define_constructor,
    message::{CursorIcon, KeyCode, MessageDirection, UiMessage},
    HorizontalAlignment, LayoutEvent, MouseButton, MouseState, RcUiNodeHandle, Thickness, UiNode,
    UserInterface, VerticalAlignment, BRUSH_FOREGROUND, BRUSH_PRIMARY,
};
use fyrox_core::{
    algebra::{Matrix3, Point2},
    uuid::Uuid,
};
use std::{
    any::Any,
    cell::{Cell, RefCell},
    rc::Rc,
    sync::mpsc::Sender,
};

/// A set of messages for any kind of widgets (including user controls). These messages provides basic
/// communication elements of the UI library.
#[derive(Debug, Clone, PartialEq)]
pub enum WidgetMessage {
    /// Initiated when user clicks on a widget's geometry.
    ///
    /// Direction: **From UI**.
    MouseDown {
        /// Position of cursor.
        pos: Vector2<f32>,
        /// A button that was pressed.
        button: MouseButton,
    },

    /// Initiated when user releases mouse button while cursor is over widget's geometry.
    ///
    /// Direction: **From UI**.
    MouseUp {
        /// Position of cursor.
        pos: Vector2<f32>,
        /// A button that was released.
        button: MouseButton,
    },

    /// Initiated when user moves cursor over widget's geometry.
    ///
    /// Direction: **From/To UI**.
    MouseMove {
        /// New position of cursor in screen coordinates.
        pos: Vector2<f32>,
        /// State of mouse buttons.
        state: MouseState,
    },

    /// Initiated when user scrolls mouse wheel while cursor is over widget's geometry.
    ///
    /// Direction: **From/To UI**.
    MouseWheel {
        /// Position of cursor.
        pos: Vector2<f32>,
        /// Amount of lines per mouse wheel turn.
        amount: f32,
    },

    /// Initiated when cursor leaves geometry of a widget.
    ///
    /// Direction: **From UI**.
    MouseLeave,

    /// Initiated when cursor enters geometry of a widget.
    ///
    /// Direction: **From UI**.
    MouseEnter,

    /// Initiated when widget is in focus and user types something on a keyboard.
    ///
    /// Direction: **From/To UI**.
    Text(String),

    /// Initiated when widget is in focus and user presses a button on a keyboard.
    ///
    /// Direction: **From UI**.
    KeyDown(KeyCode),

    /// Initiated when widget is in focus and user releases a button on a keyboard.
    ///
    /// Direction: **From UI**.
    KeyUp(KeyCode),

    /// Initiated when widget received focus (when direction is [`MessageDirection::FromWidget`]). In most cases focus is received
    /// by clicking on widget. You can request focus explicitly by sending this message to a widget with [`MessageDirection::ToWidget`]
    ///
    /// Direction: **From UI/To UI**.
    Focus,

    /// Initiated when widget has lost its focus (when direction is [`MessageDirection::FromWidget`]). Can be used to
    /// removed focus from widget if sent with [`MessageDirection::ToWidget`]
    ///
    /// Direction: **From UI/To UI**.
    Unfocus,

    /// Initiated when dragging of a widget has started.
    ///
    /// Direction: **From UI**.
    DragStarted(Handle<UiNode>),

    /// Initiated when user drags a widget over some other widget.
    ///
    /// Direction: **From UI**.
    DragOver(Handle<UiNode>),

    /// Initiated when user drops a widget onto some other widget.
    ///
    /// Direction: **From UI**.
    Drop(Handle<UiNode>),

    /// A request to make widget topmost. Widget can be made topmost only in the same hierarchy
    /// level only!
    ///
    /// Direction: **From/To UI**.
    Topmost,

    /// A request to make widget lowermost. Widget can be made lowermost only in the same hierarchy
    /// level only!
    ///
    /// Direction: **From/To UI**.
    Lowermost,

    /// A request to detach widget from its current parent and attach to root canvas.
    ///
    /// Direction: **From/To UI**.
    Unlink,

    /// A request to delete widget with all its children widgets. All handles to a node and its
    /// children will be invalid after processing such message!
    ///
    /// Direction: **From/To UI**.
    Remove,

    /// A request to link initiator with specified widget.
    ///
    /// Direction: **From/To UI**.
    LinkWith(Handle<UiNode>),

    /// A request to link initiator with specified widget and put it in front of children list.
    ///
    /// Direction: **From/To UI**.
    LinkWithReverse(Handle<UiNode>),

    /// A request to change background brush of a widget. Background brushes are used to fill volume of widgets.
    ///
    /// Direction: **From/To UI**
    Background(Brush),

    /// A request to change foreground brush of a widget. Foreground brushes are used for text, borders and so on.
    ///
    /// Direction: **From/To UI**
    Foreground(Brush),

    /// A request to change name of a widget. Name is given to widget mostly for debugging purposes.
    ///
    /// Direction: **From/To UI**
    Name(String),

    /// A request to set width of a widget. In most cases there is no need to explicitly set width of a widget,
    /// because fyrox-ui uses automatic layout engine which will correctly calculate desired width of a widget.
    ///
    /// Direction: **From/To UI**
    Width(f32),

    /// A request to set height of a widget. In most cases there is no need to explicitly set height of a widget,
    /// because fyrox-ui uses automatic layout engine which will correctly calculate desired height of a widget.
    ///
    /// Direction: **From/To UI**
    Height(f32),

    /// A request to set vertical alignment of a widget. Vertical alignment tells where to put widget in the parent
    /// widget's bounds in vertical direction.
    ///
    /// Direction: **From/To UI**
    VerticalAlignment(VerticalAlignment),

    /// A request to set horizontal alignment of a widget. Horizontal alignment tells where to put widget in the parent
    /// widget's bounds in horizontal direction.
    ///
    /// Direction: **From/To UI**
    HorizontalAlignment(HorizontalAlignment),

    /// A request to set maximum size of widget. Maximum size restricts size of a widget during layout pass. For example
    /// you can set maximum size to a button which was placed into a grid's cell, if maximum size wouldn't be set, button
    /// would be stretched to fill entire cell.
    ///
    /// Direction: **From/To UI**
    MaxSize(Vector2<f32>),

    /// A request to set minimum size of widget. Minimum size restricts size of a widget during layout pass. For example
    /// you can set minimum size to a button which was placed into a grid's cell, if minimum size wouldn't be set, button
    /// would be compressed to fill entire cell.
    ///
    /// Direction: **From/To UI**
    MinSize(Vector2<f32>),

    /// A request to set row number of a grid to which widget should belong to.
    ///
    /// Direction: **From/To UI**
    ///
    /// # Notes
    ///
    /// This is bad API and it should be changed in future. Grid should have explicit list of pairs (row, child) instead
    /// of this indirect attachment.
    Row(usize),

    /// A request to set column number of a grid to which widget should belong to.
    ///
    /// Direction: **From/To UI**
    ///
    /// # Notes
    ///
    /// This is bad API and it should be changed in future. Grid should have explicit list of pairs (column, child) instead
    /// of this indirect attachment.
    Column(usize),

    /// A request to set new margin of widget. Margin could be used to add some free space around widget to make UI look less
    /// dense.
    ///
    /// Direction: **From/To UI**
    Margin(Thickness),

    /// A request to set new state hit test visibility. If set to false, widget will become "non-clickable". It is useful for
    /// decorations which should be transparent for mouse events.
    ///
    /// Direction: **From/To UI**
    HitTestVisibility(bool),

    /// A request to set new visibility of a widget. Widget can be either visible or not. Invisible widgets does not take space
    /// in layout pass and collapsed to a point.
    ///
    /// Direction: **From/To UI**
    Visibility(bool),

    /// A request to set new z index of a widget. Z index is used to change drawing order of widgets. Please note that it works
    /// only in same hierarchy level, which means that it is impossible to set z index to 9999 (or similar huge value) to force
    /// widget to be drawn on top of everything.
    ///
    /// Direction: **From/To UI**
    ZIndex(usize),

    /// A request to set new desired position of a widget. It is called "desired" because layout system may ignore it and set
    /// some other position. Desired position works with a combination of a layout panel that supports direct coordinated
    /// (Canvas for example).
    ///
    /// Direction: **From/To UI**
    DesiredPosition(Vector2<f32>),

    /// A request to enable or disable widget. Disabled widget won't receive mouse events and may look differently (it is defined
    /// by internal styling).
    ///
    /// Direction: **From/To UI**
    Enabled(bool),

    /// A request to set desired position at center in local coordinates.
    ///
    /// Direction: **From/To UI**
    Center,

    /// A request to adjust widget's position to fit in parent's bounds.
    AdjustPositionToFit,

    /// A request to set new cursor icon for widget.
    ///
    /// Direction: **From/To UI**
    Cursor(Option<CursorIcon>),

    /// A request to set new opacity for widget.
    ///
    /// Direction: **From/To UI**
    Opacity(Option<f32>),

    /// A request to set new layout transform.
    LayoutTransform(Matrix3<f32>),

    /// A request to set new render transform.
    RenderTransform(Matrix3<f32>),

    /// A double click of a mouse button has occurred on a widget.
    DoubleClick {
        /// A button, that was double-clicked.
        button: MouseButton,
    },

    /// A request to set new context menu for a widget. Old context menu will be removed only if its
    /// reference counter was 1.
    ContextMenu(Option<RcUiNodeHandle>),

    /// A request to set new tooltip for a widget. Old tooltip will be removed only if its reference
    /// counter was 1.
    Tooltip(Option<RcUiNodeHandle>),
}

impl WidgetMessage {
    define_constructor!(
        /// Creates [`WidgetMessage::Remove`] message.
        WidgetMessage:Remove => fn remove(), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Unlink`] message.
        WidgetMessage:Unlink => fn unlink(), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::LinkWith`] message.
        WidgetMessage:LinkWith => fn link(Handle<UiNode>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::LinkWithReverse`] message.
        WidgetMessage:LinkWithReverse => fn link_reverse(Handle<UiNode>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Background`] message.
        WidgetMessage:Background => fn background(Brush), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Foreground`] message.
        WidgetMessage:Foreground => fn foreground(Brush), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Visibility`] message.
        WidgetMessage:Visibility => fn visibility(bool), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Width`] message.
        WidgetMessage:Width => fn width(f32), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Height`] message.
        WidgetMessage:Height => fn height(f32), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::DesiredPosition`] message.
        WidgetMessage:DesiredPosition => fn desired_position(Vector2<f32>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Center`] message.
        WidgetMessage:Center => fn center(), layout: true
    );

    define_constructor!(
        /// Creates [`WidgetMessage::AdjustPositionToFit`] message.
        WidgetMessage:AdjustPositionToFit => fn adjust_position_to_fit(), layout: true
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Topmost`] message.
        WidgetMessage:Topmost => fn topmost(), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Lowermost`] message.
        WidgetMessage:Lowermost => fn lowermost(), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Enabled`] message.
        WidgetMessage:Enabled => fn enabled(bool), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Name`] message.
        WidgetMessage:Name => fn name(String), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Row`] message.
        WidgetMessage:Row => fn row(usize), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Column`] message.
        WidgetMessage:Column => fn column(usize), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Cursor`] message.
        WidgetMessage:Cursor => fn cursor(Option<CursorIcon>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::ZIndex`] message.
        WidgetMessage:ZIndex => fn z_index(usize), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::HitTestVisibility`] message.
        WidgetMessage:HitTestVisibility => fn hit_test_visibility(bool), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Margin`] message.
        WidgetMessage:Margin => fn margin(Thickness), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::MinSize`] message.
        WidgetMessage:MinSize => fn min_size(Vector2<f32>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::MaxSize`] message.
        WidgetMessage:MaxSize => fn max_size(Vector2<f32>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::HorizontalAlignment`] message.
        WidgetMessage:HorizontalAlignment => fn horizontal_alignment(HorizontalAlignment), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::VerticalAlignment`] message.
        WidgetMessage:VerticalAlignment => fn vertical_alignment(VerticalAlignment), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Opacity`] message.
        WidgetMessage:Opacity => fn opacity(Option<f32>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::LayoutTransform`] message.
        WidgetMessage:LayoutTransform => fn layout_transform(Matrix3<f32>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::RenderTransform`] message.
        WidgetMessage:RenderTransform => fn render_transform(Matrix3<f32>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::ContextMenu`] message.
        WidgetMessage:ContextMenu => fn context_menu(Option<RcUiNodeHandle>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Tooltip`] message.
        WidgetMessage:Tooltip => fn tooltip(Option<RcUiNodeHandle>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Focus`] message.
        WidgetMessage:Focus => fn focus(), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Unfocus`] message.
        WidgetMessage:Unfocus => fn unfocus(), layout: false
    );

    // Internal messages. Do not use.
    define_constructor!(
        /// Creates [`WidgetMessage::MouseDown`] message. This method is for internal use only, and should not
        /// be used anywhere else.
        WidgetMessage:MouseDown => fn mouse_down(pos: Vector2<f32>, button: MouseButton), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::MouseUp`] message. This method is for internal use only, and should not
        /// be used anywhere else.
        WidgetMessage:MouseUp => fn mouse_up(pos: Vector2<f32>, button: MouseButton), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::MouseMove`] message. This method is for internal use only, and should not
        /// be used anywhere else.
        WidgetMessage:MouseMove => fn mouse_move(pos: Vector2<f32>, state: MouseState), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::MouseWheel`] message. This method is for internal use only, and should not
        /// be used anywhere else.
        WidgetMessage:MouseWheel => fn mouse_wheel(pos: Vector2<f32>, amount: f32), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::MouseLeave`] message. This method is for internal use only, and should not
        /// be used anywhere else.
        WidgetMessage:MouseLeave => fn mouse_leave(), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::MouseEnter`] message. This method is for internal use only, and should not
        /// be used anywhere else.
        WidgetMessage:MouseEnter => fn mouse_enter(), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Text`] message. This method is for internal use only, and should not
        /// be used anywhere else.
        WidgetMessage:Text => fn text(String), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::KeyDown`] message. This method is for internal use only, and should not
        /// be used anywhere else.
        WidgetMessage:KeyDown => fn key_down(KeyCode), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::KeyUp`] message. This method is for internal use only, and should not
        /// be used anywhere else.
        WidgetMessage:KeyUp => fn key_up(KeyCode), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::DragStarted`] message. This method is for internal use only, and should not
        /// be used anywhere else.
        WidgetMessage:DragStarted => fn drag_started(Handle<UiNode>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::DragOver`] message. This method is for internal use only, and should not
        /// be used anywhere else.
        WidgetMessage:DragOver => fn drag_over(Handle<UiNode>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::Drop`] message. This method is for internal use only, and should not
        /// be used anywhere else.
        WidgetMessage:Drop => fn drop(Handle<UiNode>), layout: false
    );

    define_constructor!(
        /// Creates [`WidgetMessage::DoubleClick`] message. This method is for internal use only, and should not
        /// be used anywhere else.
        WidgetMessage:DoubleClick => fn double_click(button: MouseButton), layout: false
    );
}

/// Widget is a base UI element, that is always used to build derived, more complex, widgets. In general, it is a container
/// for layout information, basic visual appearance, visibility options, parent-child information. It does almost nothing
/// on its own, instead, the user interface modifies its state accordingly.
#[derive(Debug, Clone)]
pub struct Widget {
    /// Self handle of the widget. It is valid **only**, if the widget is added to the user interface, in other
    /// cases it will most likely be [`Handle::NONE`].
    pub handle: Handle<UiNode>,
    /// Name of the widget. Could be useful for debugging purposes.
    pub name: String,
    /// Desired position relative to the parent node. It is just a recommendation for the layout system, actual position
    /// will be stored in the `actual_local_position` field and can be fetched using [`Widget::actual_local_position`]
    /// method.
    pub desired_local_position: Vector2<f32>,
    /// Explicit width for the widget, or automatic if [`f32::NAN`] (means the value is undefined). Default is [`f32::NAN`].
    pub width: f32,
    /// Explicit height for the widget, or automatic if [`f32::NAN`] (means the value is undefined). Default is [`f32::NAN`].
    pub height: f32,
    /// Minimum width and height. Default is 0.0 for both axes.
    pub min_size: Vector2<f32>,
    /// Maximum width and height. Default is [`f32::INFINITY`] for both axes.
    pub max_size: Vector2<f32>,
    /// Background brush of the widget.
    pub background: Brush,
    /// Foreground brush of the widget.
    pub foreground: Brush,
    /// Index of the row to which this widget belongs to. It is valid only in when used in [`crate::grid::Grid`] widget.
    pub row: usize,
    /// Index of the column to which this widget belongs to. It is valid only in when used in [`crate::grid::Grid`] widget.
    pub column: usize,
    /// Vertical alignment of the widget.
    pub vertical_alignment: VerticalAlignment,
    /// Horizontal alignment of the widget.
    pub horizontal_alignment: HorizontalAlignment,
    /// Margin for every sides of bounding rectangle. See [`Thickness`] docs for more info.
    pub margin: Thickness,
    /// Current, **local**, visibility state of the widget.
    pub visibility: bool,
    /// Current, **global** (including the chain of parent widgets), visibility state of the widget.
    pub global_visibility: bool,
    /// A set of handles to children nodes of this widget.
    pub children: Vec<Handle<UiNode>>,
    /// A handle to the parent node of this widget.
    pub parent: Handle<UiNode>,
    /// Indices of drawing commands in the drawing context emitted by this widget. It is used for picking.
    pub command_indices: RefCell<Vec<usize>>,
    /// A flag, that indicates that the mouse is directly over the widget. It will be raised only for top-most widget in the
    /// "stack" of widgets.
    pub is_mouse_directly_over: bool,
    /// A flag, that defines whether the widget is "visible" for hit testing (picking). Could be useful to prevent some widgets
    /// from any interactions with mouse.
    pub hit_test_visibility: bool,
    /// Index of the widget in parent's children list that defines its order in drawing and picking.
    pub z_index: usize,
    /// A flag, that defines whether the drag from drag'n'drop functionality can be started by the widget or not.
    pub allow_drag: bool,
    /// A flag, that defines whether the drop from drag'n'drop functionality can be accepted by the widget or not.
    pub allow_drop: bool,
    /// Optional, user-defined data.
    pub user_data: Option<Rc<dyn Any>>,
    /// A flag, that defines whether the widget should be drawn in a separate drawind pass after any other widget that draws
    /// normally.
    pub draw_on_top: bool,
    /// A flag, that defines whether the widget is enabled or not. Disabled widgets cannot be interacted by used and they're
    /// greyed out.
    pub enabled: bool,
    /// Optional cursor icon that will be used for mouse cursor when hovering over the widget.
    pub cursor: Option<CursorIcon>,
    /// Optional opacity of the widget. It should be in `[0.0..1.0]` range, where 0.0 - fully transparent, 1.0 - fully opaque.
    pub opacity: Option<f32>,
    /// An optional ref counted handle to a tooltip used by the widget.
    pub tooltip: Option<RcUiNodeHandle>,
    /// Maximum available time to show the tooltip after the cursor was moved away from the widget.
    pub tooltip_time: f32,
    /// An optional ref counted handle to a context menu used by the widget.
    pub context_menu: Option<RcUiNodeHandle>,
    /// A flag, that defines whether the widget should be clipped by the parent bounds or not.
    pub clip_to_bounds: bool,
    /// Current render transform of the node. It modifies layout information of the widget, as well as it affects visual transform
    /// of the widget.
    pub layout_transform: Matrix3<f32>,
    /// Current render transform of the node. It only modifies the widget at drawing stage, layout information remains unmodified.
    pub render_transform: Matrix3<f32>,
    /// Current visual transform of the node. It always contains a result of mixing the layout and render transformation matrices.
    pub visual_transform: Matrix3<f32>,
    /// A flag, that defines whether the widget will preview UI messages or not. Basically, it defines whether [crate::Control::preview_message]
    /// is called or not.
    pub preview_messages: bool,
    /// A flag, that defines whether the widget will receive any OS events or not. Basically, it defines whether [crate::Control::handle_os_event]
    /// is called or not.
    pub handle_os_events: bool,
    /// Internal sender for layout events.
    pub layout_events_sender: Option<Sender<LayoutEvent>>,
    /// Unique identifier of the widget.
    pub id: Uuid,
    //
    // Layout. Interior mutability is a must here because layout performed in a series of recursive calls.
    //
    /// A flag, that defines whether the measurement results are still valid or not.
    pub measure_valid: Cell<bool>,
    /// A flag, that defines whether the arrangement results are still valid or not.
    pub arrange_valid: Cell<bool>,
    /// Results or previous measurement.
    pub prev_measure: Cell<Vector2<f32>>,
    /// Results or previous arrangement.
    pub prev_arrange: Cell<Rect<f32>>,
    /// Desired size of the node after Measure pass.
    pub desired_size: Cell<Vector2<f32>>,
    /// Actual local position of the widget after Arrange pass.
    pub actual_local_position: Cell<Vector2<f32>>,
    /// Actual local size of the widget after Arrange pass.
    pub actual_local_size: Cell<Vector2<f32>>,
    /// Previous global visibility of the widget.
    pub prev_global_visibility: bool,
    /// Current clip bounds of the widget.
    pub clip_bounds: Cell<Rect<f32>>,
}

impl Widget {
    /// Returns self handle of the widget.
    #[inline]
    pub fn handle(&self) -> Handle<UiNode> {
        self.handle
    }

    /// Returns the name of the widget.
    #[inline]
    pub fn name(&self) -> &str {
        self.name.as_str()
    }

    /// Sets the new name of the widget.
    #[inline]
    pub fn set_name<P: AsRef<str>>(&mut self, name: P) -> &mut Self {
        self.name = name.as_ref().to_owned();
        self
    }

    /// Returns the actual size of the widget after the full layout cycle.
    #[inline]
    pub fn actual_local_size(&self) -> Vector2<f32> {
        self.actual_local_size.get()
    }

    /// Returns size of the widget without any layout or rendering transform applied.
    #[inline]
    pub fn actual_initial_size(&self) -> Vector2<f32> {
        Rect::new(
            0.0,
            0.0,
            self.actual_local_size.get().x,
            self.actual_local_size.get().y,
        )
        .transform(&self.visual_transform.try_inverse().unwrap_or_default())
        .size
    }

    /// Returns the actual global size of the widget after the full layout cycle.
    #[inline]
    pub fn actual_global_size(&self) -> Vector2<f32> {
        self.screen_bounds().size
    }

    /// Sets the new minimum size of the widget.
    #[inline]
    pub fn set_min_size(&mut self, value: Vector2<f32>) -> &mut Self {
        self.min_size = value;
        self
    }

    /// Sets the new minimum width of the widget.
    #[inline]
    pub fn set_min_width(&mut self, value: f32) -> &mut Self {
        self.min_size.x = value;
        self
    }

    /// Sets the new minimum height of the widget.
    #[inline]
    pub fn set_min_height(&mut self, value: f32) -> &mut Self {
        self.min_size.y = value;
        self
    }

    /// Sets the new minimum size of the widget.
    #[inline]
    pub fn min_size(&self) -> Vector2<f32> {
        self.min_size
    }

    /// Returns the minimum width of the widget.
    #[inline]
    pub fn min_width(&self) -> f32 {
        self.min_size.x
    }

    /// Returns the minimum height of the widget.
    #[inline]
    pub fn min_height(&self) -> f32 {
        self.min_size.y
    }

    /// Return `true` if the dragging of the widget is allowed, `false` - otherwise.
    #[inline]
    pub fn is_drag_allowed(&self) -> bool {
        self.allow_drag
    }

    /// Return `true` if the dropping of other widgets is allowed on this widget, `false` - otherwise.
    #[inline]
    pub fn is_drop_allowed(&self) -> bool {
        self.allow_drop
    }

    /// Maps the given point from screen to local widget's coordinates.
    #[inline]
    pub fn screen_to_local(&self, point: Vector2<f32>) -> Vector2<f32> {
        self.visual_transform
            .try_inverse()
            .unwrap_or_default()
            .transform_point(&Point2::from(point))
            .coords
    }

    /// Invalidates layout of the widget. **WARNING**: Do not use this method, unless you understand what you're doing,
    /// it will cause new layout pass for this widget which could be quite heavy and doing so on every frame for multiple
    /// widgets **will** cause severe performance issues.
    #[inline]
    pub fn invalidate_layout(&self) {
        self.invalidate_measure();
        self.invalidate_arrange();
    }

    /// Invalidates measurement results of the widget. **WARNING**: Do not use this method, unless you understand what you're
    /// doing, it will cause new measurement pass for this widget which could be quite heavy and doing so on every frame for
    /// multiple widgets **will** cause severe performance issues.
    #[inline]
    pub fn invalidate_measure(&self) {
        self.measure_valid.set(false);

        if let Some(layout_events_sender) = self.layout_events_sender.as_ref() {
            let _ = layout_events_sender.send(LayoutEvent::MeasurementInvalidated(self.handle));
        }
    }

    /// Invalidates arrangement results of the widget. **WARNING**: Do not use this method, unless you understand what you're
    /// doing, it will cause new arrangement pass for this widget which could be quite heavy and doing so on every frame for
    /// multiple widgets **will** cause severe performance issues.
    #[inline]
    pub fn invalidate_arrange(&self) {
        self.arrange_valid.set(false);

        if let Some(layout_events_sender) = self.layout_events_sender.as_ref() {
            let _ = layout_events_sender.send(LayoutEvent::ArrangementInvalidated(self.handle));
        }
    }

    /// Returns `true` if the widget is able to participate in hit testing, `false` - otherwise.
    #[inline]
    pub fn is_hit_test_visible(&self) -> bool {
        self.hit_test_visibility
    }

    /// Sets the new maximum size of the widget.
    #[inline]
    pub fn set_max_size(&mut self, value: Vector2<f32>) -> &mut Self {
        self.max_size = value;
        self
    }

    /// Returns current maximum size of the widget.
    #[inline]
    pub fn max_size(&self) -> Vector2<f32> {
        self.max_size
    }

    /// Returns maximum width of the widget.
    #[inline]
    pub fn max_width(&self) -> f32 {
        self.max_size.x
    }

    /// Return maximum height of the widget.
    #[inline]
    pub fn max_height(&self) -> f32 {
        self.max_size.y
    }

    /// Sets new Z index for the widget. Z index defines the sorting (stable) index which will be used to "arrange" widgets
    /// in the correct order.
    #[inline]
    pub fn set_z_index(&mut self, z_index: usize) -> &mut Self {
        self.z_index = z_index;
        self
    }

    /// Returns current Z index of the widget.
    #[inline]
    pub fn z_index(&self) -> usize {
        self.z_index
    }

    /// Sets the new background of the widget.
    #[inline]
    pub fn set_background(&mut self, brush: Brush) -> &mut Self {
        self.background = brush;
        self
    }

    /// Returns current background of the widget.
    #[inline]
    pub fn background(&self) -> Brush {
        self.background.clone()
    }

    /// Sets new foreground of the widget.
    #[inline]
    pub fn set_foreground(&mut self, brush: Brush) -> &mut Self {
        self.foreground = brush;
        self
    }

    /// Returns current foreground of the widget.
    #[inline]
    pub fn foreground(&self) -> Brush {
        self.foreground.clone()
    }

    /// Sets new width of the widget.
    #[inline]
    pub fn set_width(&mut self, width: f32) -> &mut Self {
        self.width = width.clamp(self.min_size.x, self.max_size.x);
        self
    }

    /// Returns current width of the widget.
    #[inline]
    pub fn width(&self) -> f32 {
        self.width
    }

    /// Return `true` if the widget is set to be drawn on top of every other, normally drawn, widgets, `false` - otherwise.
    pub fn is_draw_on_top(&self) -> bool {
        self.draw_on_top
    }

    /// Sets new height of the widget.
    #[inline]
    pub fn set_height(&mut self, height: f32) -> &mut Self {
        self.height = height.clamp(self.min_size.y, self.max_size.y);
        self
    }

    /// Returns current height of the widget.
    #[inline]
    pub fn height(&self) -> f32 {
        self.height
    }

    /// Sets the desired local position of the widget.
    #[inline]
    pub fn set_desired_local_position(&mut self, pos: Vector2<f32>) -> &mut Self {
        self.desired_local_position = pos;
        self
    }

    /// Returns current screen-space position of the widget.
    #[inline]
    pub fn screen_position(&self) -> Vector2<f32> {
        Vector2::new(self.visual_transform[6], self.visual_transform[7])
    }

    #[inline]
    pub(crate) fn add_child(&mut self, child: Handle<UiNode>, in_front: bool) {
        self.invalidate_layout();
        if in_front && !self.children.is_empty() {
            self.children.insert(0, child)
        } else {
            self.children.push(child)
        }
    }

    /// Returns a reference to the slice with the children widgets of this widget.
    #[inline(always)]
    pub fn children(&self) -> &[Handle<UiNode>] {
        &self.children
    }

    #[inline]
    pub(crate) fn clear_children(&mut self) {
        self.invalidate_layout();
        self.children.clear();
    }

    #[inline]
    pub(crate) fn remove_child(&mut self, child: Handle<UiNode>) {
        if let Some(i) = self.children.iter().position(|h| *h == child) {
            self.children.remove(i);
            self.invalidate_layout();
        }
    }

    /// Returns current parent handle of the widget.
    #[inline]
    pub fn parent(&self) -> Handle<UiNode> {
        self.parent
    }

    /// Sets new
    #[inline]
    pub(super) fn set_parent(&mut self, parent: Handle<UiNode>) {
        self.parent = parent;
    }

    /// Sets new column of the widget. Columns are used only by [`crate::grid::Grid`] widget.
    #[inline]
    pub fn set_column(&mut self, column: usize) -> &mut Self {
        self.column = column;
        self
    }

    /// Returns current column of the widget. Columns are used only by [`crate::grid::Grid`] widget.
    #[inline]
    pub fn column(&self) -> usize {
        self.column
    }

    /// Sets new row of the widget. Rows are used only by [`crate::grid::Grid`] widget.
    #[inline]
    pub fn set_row(&mut self, row: usize) -> &mut Self {
        self.row = row;
        self
    }

    /// Returns current row of the widget. Rows are used only by [`crate::grid::Grid`] widget.
    #[inline]
    pub fn row(&self) -> usize {
        self.row
    }

    /// Returns the desired size of the widget.
    #[inline]
    pub fn desired_size(&self) -> Vector2<f32> {
        self.desired_size.get()
    }

    /// Returns current desired local position of the widget.
    #[inline]
    pub fn desired_local_position(&self) -> Vector2<f32> {
        self.desired_local_position
    }

    /// Returns current screen-space bounds of the widget.
    #[inline]
    pub fn screen_bounds(&self) -> Rect<f32> {
        self.bounding_rect().transform(&self.visual_transform)
    }

    /// Returns local-space bounding rect of the widget.
    #[inline]
    pub fn bounding_rect(&self) -> Rect<f32> {
        Rect::new(
            0.0,
            0.0,
            self.actual_local_size.get().x,
            self.actual_local_size.get().y,
        )
    }

    /// Returns current visual transform of the widget.
    #[inline]
    pub fn visual_transform(&self) -> &Matrix3<f32> {
        &self.visual_transform
    }

    /// Returns current render transform of the widget.
    #[inline]
    pub fn render_transform(&self) -> &Matrix3<f32> {
        &self.render_transform
    }

    /// Returns current layout transform of the widget.
    #[inline]
    pub fn layout_transform(&self) -> &Matrix3<f32> {
        &self.layout_transform
    }

    /// Returns `true`, if the widget has a descendant widget with the specified handle, `false` - otherwise.
    pub fn has_descendant(&self, node_handle: Handle<UiNode>, ui: &UserInterface) -> bool {
        for child_handle in self.children.iter() {
            if *child_handle == node_handle {
                return true;
            }

            let result = ui
                .nodes
                .borrow(*child_handle)
                .has_descendant(node_handle, ui);
            if result {
                return true;
            }
        }
        false
    }

    /// Searches a node up on tree starting from the given root that matches a criteria defined by the given func.
    pub fn find_by_criteria_up<Func: Fn(&UiNode) -> bool>(
        &self,
        ui: &UserInterface,
        func: Func,
    ) -> Handle<UiNode> {
        let mut parent_handle = self.parent;
        while parent_handle.is_some() {
            if let Some(parent_node) = ui.nodes.try_borrow(parent_handle) {
                if func(parent_node) {
                    return parent_handle;
                }
                parent_handle = parent_node.parent;
            } else {
                break;
            }
        }
        Handle::NONE
    }

    /// Handles incoming [`WidgetMessage`]s. This method **must** be called in [`crate::control::Control::handle_routed_message`]
    /// of any derived widgets!
    pub fn handle_routed_message(&mut self, _ui: &mut UserInterface, msg: &mut UiMessage) {
        if msg.destination() == self.handle() && msg.direction() == MessageDirection::ToWidget {
            if let Some(msg) = msg.data::<WidgetMessage>() {
                match msg {
                    &WidgetMessage::Opacity(opacity) => self.opacity = opacity,
                    WidgetMessage::Background(background) => self.background = background.clone(),
                    WidgetMessage::Foreground(foreground) => self.foreground = foreground.clone(),
                    WidgetMessage::Name(name) => self.name = name.clone(),
                    &WidgetMessage::Width(width) => {
                        if self.width != width {
                            self.width = width;
                            self.invalidate_layout();
                        }
                    }
                    &WidgetMessage::Height(height) => {
                        if self.height != height {
                            self.height = height;
                            self.invalidate_layout();
                        }
                    }
                    WidgetMessage::VerticalAlignment(vertical_alignment) => {
                        if self.vertical_alignment != *vertical_alignment {
                            self.vertical_alignment = *vertical_alignment;
                            self.invalidate_layout();
                        }
                    }
                    WidgetMessage::HorizontalAlignment(horizontal_alignment) => {
                        if self.horizontal_alignment != *horizontal_alignment {
                            self.horizontal_alignment = *horizontal_alignment;
                            self.invalidate_layout();
                        }
                    }
                    WidgetMessage::MaxSize(max_size) => {
                        if self.max_size != *max_size {
                            self.max_size = *max_size;
                            self.invalidate_layout();
                        }
                    }
                    WidgetMessage::MinSize(min_size) => {
                        if self.min_size != *min_size {
                            self.min_size = *min_size;
                            self.invalidate_layout();
                        }
                    }
                    &WidgetMessage::Row(row) => {
                        if self.row != row {
                            self.row = row;
                            self.invalidate_layout();
                        }
                    }
                    &WidgetMessage::Column(column) => {
                        if self.column != column {
                            self.column = column;
                            self.invalidate_layout();
                        }
                    }
                    &WidgetMessage::Margin(margin) => {
                        if self.margin != margin {
                            self.margin = margin;
                            self.invalidate_layout();
                        }
                    }
                    WidgetMessage::HitTestVisibility(hit_test_visibility) => {
                        self.hit_test_visibility = *hit_test_visibility
                    }
                    &WidgetMessage::Visibility(visibility) => {
                        self.set_visibility(visibility);
                    }
                    &WidgetMessage::DesiredPosition(pos) => {
                        if self.desired_local_position != pos {
                            self.desired_local_position = pos;
                            self.invalidate_layout();
                        }
                    }
                    &WidgetMessage::Enabled(enabled) => {
                        self.enabled = enabled;
                    }
                    &WidgetMessage::Cursor(icon) => {
                        self.cursor = icon;
                    }
                    WidgetMessage::LayoutTransform(transform) => {
                        if &self.layout_transform != transform {
                            self.layout_transform = *transform;
                            self.invalidate_layout();
                        }
                    }
                    WidgetMessage::RenderTransform(transform) => {
                        self.render_transform = *transform;
                    }
                    _ => (),
                }
            }
        }
    }

    /// Sets new vertical alignment of the widget.
    #[inline]
    pub fn set_vertical_alignment(&mut self, vertical_alignment: VerticalAlignment) -> &mut Self {
        self.vertical_alignment = vertical_alignment;
        self
    }

    /// Returns current vertical alignment of the widget.
    #[inline]
    pub fn vertical_alignment(&self) -> VerticalAlignment {
        self.vertical_alignment
    }

    /// Sets new horizontal alignment of the widget.
    #[inline]
    pub fn set_horizontal_alignment(
        &mut self,
        horizontal_alignment: HorizontalAlignment,
    ) -> &mut Self {
        self.horizontal_alignment = horizontal_alignment;
        self
    }

    /// Returns current horizontal alignment of the widget.
    #[inline]
    pub fn horizontal_alignment(&self) -> HorizontalAlignment {
        self.horizontal_alignment
    }

    /// Sets new margin of the widget.
    #[inline]
    pub fn set_margin(&mut self, margin: Thickness) -> &mut Self {
        self.margin = margin;
        self
    }

    /// Returns current margin of the widget.
    #[inline]
    pub fn margin(&self) -> Thickness {
        self.margin
    }

    /// Performs standard measurement of children nodes. It provides available size as a constraint and returns
    /// the maximum desired size across all children. As a result, this widget will have this size as its desired
    /// size to fit all the children nodes.
    #[inline]
    pub fn measure_override(
        &self,
        ui: &UserInterface,
        available_size: Vector2<f32>,
    ) -> Vector2<f32> {
        let mut size: Vector2<f32> = Vector2::default();

        for &child in self.children.iter() {
            ui.measure_node(child, available_size);
            let desired_size = ui.node(child).desired_size();
            size.x = size.x.max(desired_size.x);
            size.y = size.y.max(desired_size.y);
        }

        size
    }

    /// Performs standard arrangement of the children nodes of the widget. It uses input final size to make a final
    /// bounding rectangle to arrange children. As a result, all the children nodes will be located at the top-left
    /// corner of this widget and stretched to fit its bounds.
    #[inline]
    pub fn arrange_override(&self, ui: &UserInterface, final_size: Vector2<f32>) -> Vector2<f32> {
        let final_rect = Rect::new(0.0, 0.0, final_size.x, final_size.y);

        for &child in self.children.iter() {
            ui.arrange_node(child, &final_rect);
        }

        final_size
    }

    #[inline]
    pub(crate) fn commit_arrange(&self, position: Vector2<f32>, size: Vector2<f32>) {
        self.actual_local_size.set(size);
        self.actual_local_position.set(position);
        self.arrange_valid.set(true);
    }

    #[inline]
    pub(crate) fn set_children(&mut self, children: Vec<Handle<UiNode>>) {
        self.invalidate_layout();
        self.request_update_visibility();
        self.children = children;
    }

    /// Returns `true` if the current results of arrangement of the widget are valid, `false` - otherwise.
    #[inline(always)]
    pub fn is_arrange_valid(&self) -> bool {
        self.arrange_valid.get()
    }

    #[inline]
    pub(crate) fn commit_measure(&self, desired_size: Vector2<f32>) {
        self.desired_size.set(desired_size);
        self.measure_valid.set(true);
    }

    /// Returns `true` if the current results of measurement of the widget are valid, `false` - otherwise.
    #[inline(always)]
    pub fn is_measure_valid(&self) -> bool {
        self.measure_valid.get()
    }

    /// Returns current actual local position of the widget. It is valid only after layout pass!
    #[inline]
    pub fn actual_local_position(&self) -> Vector2<f32> {
        self.actual_local_position.get()
    }

    /// Returns center point of the widget. It is valid only after layout pass!
    #[inline]
    pub fn center(&self) -> Vector2<f32> {
        self.actual_local_position() + self.actual_local_size().scale(0.5)
    }

    #[inline]
    pub(crate) fn set_global_visibility(&mut self, value: bool) {
        self.prev_global_visibility = self.global_visibility;
        self.global_visibility = value;
    }

    /// Returns `true` of the widget is globally visible, which means that all its parents are visible as well
    /// as this widget. It is valid only after the first update of the layout, otherwise if will be always false.
    #[inline]
    pub fn is_globally_visible(&self) -> bool {
        self.global_visibility
    }

    /// Sets new visibility of the widget.
    #[inline]
    pub fn set_visibility(&mut self, visibility: bool) -> &mut Self {
        if self.visibility != visibility {
            self.visibility = visibility;
            self.invalidate_layout();
            self.request_update_visibility();
        }
        self
    }

    /// Requests (via event queue, so the request is deferred) the update of the visibility of the widget.
    #[inline]
    pub fn request_update_visibility(&self) {
        if let Some(layout_events_sender) = self.layout_events_sender.as_ref() {
            let _ = layout_events_sender.send(LayoutEvent::VisibilityChanged(self.handle));
        }
    }

    /// Returns current visibility of the widget.
    #[inline]
    pub fn visibility(&self) -> bool {
        self.visibility
    }

    /// Enables or disables the widget. Disabled widgets does not interact with user and usually greyed out.
    #[inline]
    pub fn set_enabled(&mut self, enabled: bool) -> &mut Self {
        self.enabled = enabled;
        self
    }

    /// Returns `true` if the widget if enabled, `false` - otherwise.
    #[inline]
    pub fn enabled(&self) -> bool {
        self.enabled
    }

    /// Sets new cursor of the widget.
    #[inline]
    pub fn set_cursor(&mut self, cursor: Option<CursorIcon>) {
        self.cursor = cursor;
    }

    /// Returns current cursor of the widget.
    #[inline]
    pub fn cursor(&self) -> Option<CursorIcon> {
        self.cursor
    }

    /// Tries to fetch user-defined data of the specified type `T`.
    #[inline]
    pub fn user_data_ref<T: 'static>(&self) -> Option<&T> {
        self.user_data.as_ref().and_then(|v| v.downcast_ref::<T>())
    }

    /// Returns current clipping bounds of the widget. It is valid only after at least one layout pass.
    #[inline]
    pub fn clip_bounds(&self) -> Rect<f32> {
        self.clip_bounds.get()
    }

    /// Set new opacity of the widget. Opacity should be in `[0.0..1.0]` range.
    #[inline]
    pub fn set_opacity(&mut self, opacity: Option<f32>) -> &mut Self {
        self.opacity = opacity;
        self
    }

    /// Returns current opacity of the widget.
    #[inline]
    pub fn opacity(&self) -> Option<f32> {
        self.opacity
    }

    /// Returns current tooltip handle of the widget.
    #[inline]
    pub fn tooltip(&self) -> Option<RcUiNodeHandle> {
        self.tooltip.clone()
    }

    /// Sets new tooltip handle of the widget (if any).
    #[inline]
    pub fn set_tooltip(&mut self, tooltip: Option<RcUiNodeHandle>) -> &mut Self {
        self.tooltip = tooltip;
        self
    }

    /// Returns maximum available time to show the tooltip after the cursor was moved away from the widget.
    #[inline]
    pub fn tooltip_time(&self) -> f32 {
        self.tooltip_time
    }

    /// Set the maximum available time to show the tooltip after the cursor was moved away from the widget.
    #[inline]
    pub fn set_tooltip_time(&mut self, tooltip_time: f32) -> &mut Self {
        self.tooltip_time = tooltip_time;
        self
    }

    /// Returns current context menu of the widget.
    #[inline]
    pub fn context_menu(&self) -> Option<RcUiNodeHandle> {
        self.context_menu.clone()
    }

    /// The context menu receives `PopupMessage`s for being displayed, and so should support those.
    #[inline]
    pub fn set_context_menu(&mut self, context_menu: Option<RcUiNodeHandle>) -> &mut Self {
        self.context_menu = context_menu;
        self
    }
}

/// Implements `Deref<Target = Widget> + DerefMut` for your widget. It is used to reduce boilerplate code and
/// make it less bug-prone.
#[macro_export]
macro_rules! define_widget_deref {
    ($ty: ty) => {
        impl Deref for $ty {
            type Target = Widget;

            fn deref(&self) -> &Self::Target {
                &self.widget
            }
        }

        impl DerefMut for $ty {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.widget
            }
        }
    };
}

/// Widget builder creates [`Widget`] instances.
pub struct WidgetBuilder {
    /// Name of the widget.
    pub name: String,
    /// Width of the widget.
    pub width: f32,
    /// Height of the widget.
    pub height: f32,
    /// Desired position of the widget.
    pub desired_position: Vector2<f32>,
    /// Vertical alignment of the widget.
    pub vertical_alignment: VerticalAlignment,
    /// Horizontal alignment of the widget.
    pub horizontal_alignment: HorizontalAlignment,
    /// Max size of the widget.
    pub max_size: Option<Vector2<f32>>,
    /// Min size of the widget.
    pub min_size: Option<Vector2<f32>>,
    /// Background brush of the widget.
    pub background: Option<Brush>,
    /// Foreground brush of the widget.
    pub foreground: Option<Brush>,
    /// Row index of the widget.
    pub row: usize,
    /// Column index of the widget.
    pub column: usize,
    /// Margin of the widget.
    pub margin: Thickness,
    /// Children handles of the widget.
    pub children: Vec<Handle<UiNode>>,
    /// Whether the hit test is enabled or not.
    pub is_hit_test_visible: bool,
    /// Whether the widget is visible or not.
    pub visibility: bool,
    /// Z index of the widget.
    pub z_index: usize,
    /// Whether the dragging of the widget is allowed or not.
    pub allow_drag: bool,
    /// Whether the drop of the widget is allowed or not.
    pub allow_drop: bool,
    /// User-defined data.
    pub user_data: Option<Rc<dyn Any>>,
    /// Whether to draw the widget on top of any other or not.
    pub draw_on_top: bool,
    /// Whether the widget is enabled or not.
    pub enabled: bool,
    /// Cursor of the widget.
    pub cursor: Option<CursorIcon>,
    /// Opacity of the widget.
    pub opacity: Option<f32>,
    /// Tooltip of the widget.
    pub tooltip: Option<RcUiNodeHandle>,
    /// Visibility interval (in seconds) of the tooltip of the widget.
    pub tooltip_time: f32,
    /// Context menu of the widget.
    pub context_menu: Option<RcUiNodeHandle>,
    /// Whether the preview messages is enabled or not.
    pub preview_messages: bool,
    /// Whether the widget will handle OS events or not.
    pub handle_os_events: bool,
    /// Layout transform of the widget.
    pub layout_transform: Matrix3<f32>,
    /// Render transform of the widget.
    pub render_transform: Matrix3<f32>,
    /// Whether the widget bounds should be clipped by its parent or not.
    pub clip_to_bounds: bool,
    /// Unique id of the widget.
    pub id: Uuid,
}

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

impl WidgetBuilder {
    /// Creates new widget builder with the default values.
    pub fn new() -> Self {
        Self {
            name: Default::default(),
            width: f32::NAN,
            height: f32::NAN,
            vertical_alignment: VerticalAlignment::default(),
            horizontal_alignment: HorizontalAlignment::default(),
            max_size: None,
            min_size: None,
            background: None,
            foreground: None,
            row: 0,
            column: 0,
            margin: Thickness::zero(),
            desired_position: Vector2::default(),
            children: Vec::new(),
            is_hit_test_visible: true,
            visibility: true,
            z_index: 0,
            allow_drag: false,
            allow_drop: false,
            user_data: None,
            draw_on_top: false,
            enabled: true,
            cursor: None,
            opacity: None,
            tooltip: Default::default(),
            tooltip_time: 0.1,
            context_menu: Default::default(),
            preview_messages: false,
            handle_os_events: false,
            layout_transform: Matrix3::identity(),
            render_transform: Matrix3::identity(),
            clip_to_bounds: true,
            id: Uuid::new_v4(),
        }
    }

    /// Enables or disables message previewing of the widget. It basically defines whether the [`crate::Control::preview_message`] will
    /// be called or not.
    pub fn with_preview_messages(mut self, state: bool) -> Self {
        self.preview_messages = state;
        self
    }

    /// Enables or disables OS event handling of the widget. It basically defines whether the [`crate::Control::handle_os_event`] will
    /// be called or not.
    pub fn with_handle_os_events(mut self, state: bool) -> Self {
        self.handle_os_events = state;
        self
    }

    /// Sets the desired width of the widget.
    pub fn with_width(mut self, width: f32) -> Self {
        self.width = width;
        self
    }

    /// Sets the desired height of the widget.
    pub fn with_height(mut self, height: f32) -> Self {
        self.height = height;
        self
    }

    /// Enables or disables clipping of widget's bound to its parent's bounds.
    pub fn with_clip_to_bounds(mut self, clip_to_bounds: bool) -> Self {
        self.clip_to_bounds = clip_to_bounds;
        self
    }

    /// Enables or disables the widget.
    pub fn with_enabled(mut self, enabled: bool) -> Self {
        self.enabled = enabled;
        self
    }

    /// Sets the desired vertical alignment of the widget.
    pub fn with_vertical_alignment(mut self, valign: VerticalAlignment) -> Self {
        self.vertical_alignment = valign;
        self
    }

    /// Sets the desired horizontal alignment of the widget.
    pub fn with_horizontal_alignment(mut self, halign: HorizontalAlignment) -> Self {
        self.horizontal_alignment = halign;
        self
    }

    /// Sets the max size of the widget.
    pub fn with_max_size(mut self, max_size: Vector2<f32>) -> Self {
        self.max_size = Some(max_size);
        self
    }

    /// Sets the min size of the widget.
    pub fn with_min_size(mut self, min_size: Vector2<f32>) -> Self {
        self.min_size = Some(min_size);
        self
    }

    /// Sets the desired background brush of the widget.
    pub fn with_background(mut self, brush: Brush) -> Self {
        self.background = Some(brush);
        self
    }

    /// Sets the desired foreground brush of the widget.
    pub fn with_foreground(mut self, brush: Brush) -> Self {
        self.foreground = Some(brush);
        self
    }

    /// Sets the desired row index of the widget.
    pub fn on_row(mut self, row: usize) -> Self {
        self.row = row;
        self
    }

    /// Sets the desired column index of the widget.
    pub fn on_column(mut self, column: usize) -> Self {
        self.column = column;
        self
    }

    /// Sets the desired margin of the widget.
    pub fn with_margin(mut self, margin: Thickness) -> Self {
        self.margin = margin;
        self
    }

    /// Sets the desired position of the widget.
    pub fn with_desired_position(mut self, desired_position: Vector2<f32>) -> Self {
        self.desired_position = desired_position;
        self
    }

    /// Sets the desired layout transform of the widget.
    pub fn with_layout_transform(mut self, layout_transform: Matrix3<f32>) -> Self {
        self.layout_transform = layout_transform;
        self
    }

    /// Sets the desired render transform of the widget.
    pub fn with_render_transform(mut self, render_transform: Matrix3<f32>) -> Self {
        self.render_transform = render_transform;
        self
    }

    /// Sets the desired Z index of the widget.
    pub fn with_z_index(mut self, z_index: usize) -> Self {
        self.z_index = z_index;
        self
    }

    /// Adds a child handle to the widget. [`Handle::NONE`] values are ignored.
    pub fn with_child(mut self, handle: Handle<UiNode>) -> Self {
        if handle.is_some() {
            self.children.push(handle);
        }
        self
    }

    /// Enables or disables top-most widget drawing.
    pub fn with_draw_on_top(mut self, draw_on_top: bool) -> Self {
        self.draw_on_top = draw_on_top;
        self
    }

    /// Sets the desired set of children nodes.
    pub fn with_children<I: IntoIterator<Item = Handle<UiNode>>>(mut self, children: I) -> Self {
        for child in children.into_iter() {
            if child.is_some() {
                self.children.push(child)
            }
        }
        self
    }

    /// Sets the desired widget name.
    pub fn with_name(mut self, name: &str) -> Self {
        self.name = String::from(name);
        self
    }

    /// Enables or disables hit test of the widget.
    pub fn with_hit_test_visibility(mut self, state: bool) -> Self {
        self.is_hit_test_visible = state;
        self
    }

    /// Sets the desired widget visibility.
    pub fn with_visibility(mut self, visibility: bool) -> Self {
        self.visibility = visibility;
        self
    }

    /// Enables or disables an ability to drop other widgets on this widget.
    pub fn with_allow_drop(mut self, allow_drop: bool) -> Self {
        self.allow_drop = allow_drop;
        self
    }

    /// Enables or disables dragging of the widget.
    pub fn with_allow_drag(mut self, allow_drag: bool) -> Self {
        self.allow_drag = allow_drag;
        self
    }

    /// Sets the desired widget user data.
    pub fn with_user_data(mut self, user_data: Rc<dyn Any>) -> Self {
        self.user_data = Some(user_data);
        self
    }

    /// Sets the desired widget cursor.
    pub fn with_cursor(mut self, cursor: Option<CursorIcon>) -> Self {
        self.cursor = cursor;
        self
    }

    /// Sets the desired widget opacity.
    pub fn with_opacity(mut self, opacity: Option<f32>) -> Self {
        self.opacity = opacity;
        self
    }

    /// Sets the desired widget id.
    pub fn with_id(mut self, id: Uuid) -> Self {
        self.id = id;
        self
    }

    /// Sets the desired tooltip for the node.
    ///
    /// ## Important
    ///
    /// The widget will share the tooltip, which means that when widget will be deleted, the
    /// tooltip will be deleted only if there's no one use the tooltip anymore.
    pub fn with_tooltip(mut self, tooltip: RcUiNodeHandle) -> Self {
        self.tooltip = Some(tooltip);
        self
    }

    /// Sets the desired tooltip for the node.
    ///
    /// ## Important
    ///
    /// The widget will share the tooltip, which means that when widget will be deleted, the
    /// tooltip will be deleted only if there's no one use the tooltip anymore.
    pub fn with_opt_tooltip(mut self, tooltip: Option<RcUiNodeHandle>) -> Self {
        self.tooltip = tooltip;
        self
    }

    /// Sets the desired tooltip time.
    pub fn with_tooltip_time(mut self, tooltip_time: f32) -> Self {
        self.tooltip_time = tooltip_time;
        self
    }

    /// The context menu receives `PopupMessage`s for being displayed, and so should support those.
    pub fn with_context_menu(mut self, context_menu: RcUiNodeHandle) -> Self {
        self.context_menu = Some(context_menu);
        self
    }

    /// Finishes building of the base widget.
    pub fn build(self) -> Widget {
        Widget {
            handle: Default::default(),
            name: self.name,
            desired_local_position: self.desired_position,
            width: self.width,
            height: self.height,
            desired_size: Cell::new(Vector2::default()),
            actual_local_position: Cell::new(Vector2::default()),
            actual_local_size: Cell::new(Vector2::default()),
            min_size: self.min_size.unwrap_or_default(),
            max_size: self
                .max_size
                .unwrap_or_else(|| Vector2::new(f32::INFINITY, f32::INFINITY)),
            background: self.background.unwrap_or_else(|| BRUSH_PRIMARY.clone()),
            foreground: self.foreground.unwrap_or_else(|| BRUSH_FOREGROUND.clone()),
            row: self.row,
            column: self.column,
            vertical_alignment: self.vertical_alignment,
            horizontal_alignment: self.horizontal_alignment,
            margin: self.margin,
            visibility: self.visibility,
            global_visibility: true,
            prev_global_visibility: false,
            children: self.children,
            parent: Handle::NONE,
            command_indices: Default::default(),
            is_mouse_directly_over: false,
            measure_valid: Cell::new(false),
            arrange_valid: Cell::new(false),
            hit_test_visibility: self.is_hit_test_visible,
            prev_measure: Default::default(),
            prev_arrange: Default::default(),
            z_index: self.z_index,
            allow_drag: self.allow_drag,
            allow_drop: self.allow_drop,
            user_data: self.user_data.clone(),
            draw_on_top: self.draw_on_top,
            enabled: self.enabled,
            cursor: self.cursor,
            clip_bounds: Cell::new(Default::default()),
            opacity: self.opacity,
            tooltip: self.tooltip,
            tooltip_time: self.tooltip_time,
            context_menu: self.context_menu,
            preview_messages: self.preview_messages,
            handle_os_events: self.handle_os_events,
            layout_events_sender: None,
            layout_transform: self.layout_transform,
            render_transform: self.render_transform,
            visual_transform: Matrix3::identity(),
            clip_to_bounds: self.clip_to_bounds,
            id: self.id,
        }
    }
}