egui_stack 0.31.1

Docking system for egui, continuing egui_dock
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
1703
1704
1705
1706
1707
//! Floats drawn: the docks of a [`DockSpace`] the layout's [`Float`]s show, over the main
//! window ([`FloatHost::InWindow`]) or in native windows the application hosts
//! ([`FloatHost::Native`], through a [`ViewportHost`]). See [`FloatsArea`].

use std::collections::{BTreeMap, BTreeSet, HashMap};

use egui::{
    Context, Id, LayerId, Modifiers, Rect, Ui, Vec2, ViewportBuilder, ViewportCommand, ViewportId,
    WindowDrag, vec2,
};

use crate::layout::{Float, FloatHost, Layout, Place, Screen, ScreenGone};
use crate::{
    DockArea, DockPainter, DockSpace, Docked, GroupHandles, NodePath, Style, Surface, TabViewer,
    Tree,
};

mod edges;
mod gestures;
mod work_area;

pub use gestures::{FloatMove, MoveFrom};
pub(crate) use gestures::{WindowMove, move_handle_id, mover_id};
pub(crate) use work_area::BoundZones;
pub use work_area::{Outside, WorkArea};

/// What hosts the native windows of [`FloatHost::Native`] floats: the application, which
/// creates them, places them and renders them (egui_stack never creates a window or a
/// rendering surface).
///
/// Every method has a default, on egui's immediate viewports
/// ([`Context::show_viewport_immediate`]): [`EguiViewports`] is that host. An application that
/// pairs something with a window (a second rendering surface, display settings) keeps its own
/// viewport ids stable with [`viewport_id`](Self::viewport_id), and tells the screens it knows
/// with [`screens`](Self::screens).
pub trait ViewportHost {
    /// The viewport of the float keyed `key`: stable for as long as the float is open, so the
    /// application can pair anything with it.
    fn viewport_id(&self, key: &str) -> ViewportId {
        ViewportId::from_hash_of(("egui_stack.float", key))
    }

    /// The builder of the float's native window, before egui_stack sets what its place says
    /// (position, size, maximized, full screen) where the builder leaves it unset. By default
    /// a window titled with the float's key.
    fn builder(&mut self, float: &Float) -> ViewportBuilder {
        ViewportBuilder::default().with_title(float.key.clone())
    }

    /// Shows the viewport `id` with `builder`, `body` drawing its content. By default an
    /// immediate viewport ([`Context::show_viewport_immediate`]); a backend without several
    /// viewports draws it embedded, as an `egui::Window`.
    fn show(
        &mut self,
        ctx: &Context,
        id: ViewportId,
        builder: ViewportBuilder,
        body: &mut dyn FnMut(&mut Ui),
    ) {
        ctx.show_viewport_immediate(id, builder, |ui, _| body(ui));
    }

    /// The screens (monitors) the system has, named stably, in desktop physical pixels. A
    /// change of them brings each native float back onto them
    /// ([`Layout::repair_floats`]). By default none: nothing is repaired, and a position is
    /// never invented.
    fn screens(&self) -> Vec<Screen> {
        Vec::new()
    }

    /// Whether this host opens native windows: by default, unless egui embeds its viewports
    /// ([`Context::embed_viewports`]: on the web, or a backend with a single viewport). A host
    /// that draws the floats embedded says `false`. Without native windows,
    /// [`Outside::Native`] falls back to [`Outside::Cancel`].
    fn native_windows(&self, ctx: &Context) -> bool {
        !ctx.embed_viewports()
    }

    /// The outer rect of the viewport `id` on the desktop, in physical pixels; `None` where
    /// the system does not tell window positions (Wayland). By default what egui's input says
    /// of it ([`egui::ViewportInfo::outer_rect`]).
    fn outer_rect(&self, ctx: &Context, id: ViewportId) -> Option<Rect> {
        let zoom = ctx.zoom_factor();
        ctx.input(|i| {
            let info = i.raw.viewports.get(&id)?;
            let rect = info.outer_rect?;
            let scale = info.native_pixels_per_point.unwrap_or(1.0) * zoom;
            Some(Rect::from_min_max(
                (rect.min.to_vec2() * scale).to_pos2(),
                (rect.max.to_vec2() * scale).to_pos2(),
            ))
        })
    }
}

/// The [`ViewportHost`] on egui's immediate viewports, with every default: no screens known.
#[derive(Clone, Copy, Debug, Default)]
pub struct EguiViewports;

impl ViewportHost for EguiViewports {}

/// What becomes of a float's tabs when the user closes it, from
/// [`TabViewer::close_policy`] (or, with the `bus` feature,
/// the `stack.window.close` request).
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ClosePolicy {
    /// Each tab goes back where it came from ([`TabOrigin`](crate::TabOrigin)): beside the tab
    /// it was next to, else in its dock's focused (or first) leaf; a tab with no origin, or
    /// whose dock is gone, goes to the first dock no float shows.
    #[default]
    ReturnToOrigin,
    /// The tabs go into the dock of that name, after its focused (or first) leaf's tabs; to
    /// their origin if the space has no such dock.
    Into(String),
    /// The tabs close with the window.
    CloseTabs,
    /// The window stays open: the close is cancelled.
    Keep,
}

/// A float the user closes, for [`TabViewer::close_policy`].
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct FloatClose {
    /// The float's key.
    pub key: String,
    /// The dock it shows.
    pub dock: String,
    /// Its tabs, by [`TabViewer::id`], in order.
    pub tabs: Vec<Id>,
}

/// A float's life, from [`DockSpace::take_float_events`]; layout changes are reported apart
/// ([`LayoutEvent`](crate::layout::LayoutEvent)).
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub enum FloatEvent {
    /// A float opened from a tab or a group moved out of a dock.
    Detached {
        /// The float's key.
        key: String,
        /// The dock it shows, made for it.
        dock: String,
        /// Its tabs, by [`TabViewer::id`].
        tabs: Vec<Id>,
        /// What hosts it.
        host: FloatHost,
    },
    /// A float closed.
    Closed {
        /// The float's key.
        key: String,
        /// The dock it showed.
        dock: String,
        /// The policy applied: asked when the user closed it, [`ClosePolicy::ReturnToOrigin`]
        /// when its screen went ([`ScreenGone::DockBack`]), `None` when its dock emptied or
        /// went.
        policy: Option<ClosePolicy>,
        /// Each tab moved, and the dock it went to.
        moved: Vec<(Id, String)>,
    },
    /// The user moved or resized a float; reported once it stops changing.
    Moved {
        /// The float's key.
        key: String,
        /// Where it is now.
        place: Place,
    },
    /// A float was brought back onto a screen ([`Layout::repair_floats`]).
    Repaired {
        /// The float's key.
        key: String,
        /// The screen it was on, if it named one.
        from: Option<String>,
        /// The screen it is on now; `None` when it docked back (a
        /// [`Closed`](Self::Closed) follows).
        to: Option<String>,
    },
}

/// The floats' side of a [`DockSpace`], not saved.
#[derive(Clone, Debug, Default)]
pub(crate) struct Floating {
    /// Whether the space's floats are drawn ([`FloatsArea`]): a tab moved out of every dock
    /// then becomes a float, not a legacy window.
    pub(crate) enabled: bool,
    /// What hosts the floats made from a tab moved out.
    pub(crate) host: FloatHost,
    /// Tabs moved out, waiting for their float.
    pub(crate) pending: Vec<PendingFloat>,
    /// The layers of the floats over the main window, as last drawn.
    pub(crate) layers: Vec<LayerId>,
    /// The rects of the floats over the main window, as last drawn.
    pub(crate) rects: Vec<Rect>,
    /// The events not taken yet.
    pub(crate) events: Vec<FloatEvent>,
    /// Each native float's first builder: given again unchanged, so the window is not moved
    /// back where it opened.
    pub(crate) builders: HashMap<String, ViewportBuilder>,
    /// The floats whose place changed last frame.
    pub(crate) moving: BTreeSet<String>,
    /// Where each float's window was last seen (its place's corner): a place the application
    /// changed since then moves the window there.
    pub(crate) placed: HashMap<String, egui::Pos2>,
    /// The screens the floats were last repaired on.
    pub(crate) screens: Option<Vec<Screen>>,
    /// The last number given to a float's dock (`float-N`).
    pub(crate) next: u32,
    /// The drag in progress across the space's viewports.
    pub(crate) session: Option<crate::dock_area::session::DragSession>,
    /// The viewport of each native float drawn, by key.
    pub(crate) viewports: HashMap<String, ViewportId>,
    /// The float whose window is being moved with [`FloatMove::Dock`].
    pub(crate) window_move: Option<WindowMove>,
    /// The height a float over the main window takes around its dock (title bar, frame), by
    /// key, as last drawn.
    pub(crate) chrome: HashMap<String, f32>,
    /// The floats over the main window drawn last with all their groups collapsed.
    pub(crate) collapsed: BTreeSet<String>,
    /// The docks of the floats whose close window button was clicked this frame
    /// ([`FloatsArea::title_bar`]), closed once their window is drawn.
    pub(crate) close_asked: BTreeSet<String>,
    /// Where the floats over the main window may go ([`FloatsArea::work_area`]), as last set.
    pub(crate) work_area: WorkArea,
    /// The layout's zones last read by [`DockSpace::bind`], for the work area's regions.
    pub(crate) zones: BoundZones,
    /// Where each float just opened from a release over the main window was released: its
    /// first region is the one there, whatever its size overlaps.
    pub(crate) released_at: HashMap<String, egui::Pos2>,
    /// Whether the host opens native windows ([`ViewportHost::native_windows`]), as last told.
    pub(crate) native_windows: bool,
    /// Where the pointer held each float over the main window being moved, from its corner,
    /// while it was over the window: a float torn out ([`Outside::Native`]) opens with the
    /// pointer there.
    pub(crate) grabs: HashMap<String, egui::Vec2>,
}

impl Floating {
    /// The work area's regions this frame, in the main window's points; `None` when the floats
    /// are free. `docks` are the space's docks, for the space drawn without
    /// [`DockSpace::bind`].
    pub(crate) fn regions(&self, docks: &[crate::dock_space::Dock]) -> Option<Vec<Rect>> {
        let drawn = docks
            .iter()
            .filter(|dock| !dock.floating && work_area::main_window(dock.viewport))
            .filter_map(|dock| dock.rect);
        self.work_area.regions(&self.zones, drawn)
    }

    /// What hosts the float made from a tab or a group released over nothing at `pointer`, in
    /// the main window's points: the space's host inside the work area; past its edge, a
    /// native window with [`Outside::Native`] where native windows are available; `None` where
    /// the release is refused ([`Outside::Cancel`]).
    pub(crate) fn release_host(
        &self,
        docks: &[crate::dock_space::Dock],
        pointer: egui::Pos2,
    ) -> Option<FloatHost> {
        let Some(regions) = self.regions(docks) else {
            return Some(self.host);
        };
        if regions.iter().any(|region| region.contains(pointer)) {
            return Some(self.host);
        }
        match self.work_area.outside_rule() {
            Outside::Native if self.native_windows => Some(FloatHost::Native),
            Outside::Native | Outside::Cancel => None,
        }
    }

    /// The viewport `viewport` closed (a float's window): a drag that began there ends, and one
    /// over it is cancelled (nothing moves at its release).
    pub(crate) fn viewport_closed(&mut self, viewport: ViewportId) {
        let Some(session) = &mut self.session else {
            return;
        };
        if session.source == viewport {
            self.session = None;
        } else if let Some(report) = session.reports.remove(&viewport)
            && report.stamp + 1 >= session.pass
        {
            session.cancelled = true;
            session.reports.clear();
        }
    }
}

/// A tab or a group moved out of a dock, waiting for its float.
#[derive(Clone, Debug)]
pub(crate) struct PendingFloat {
    /// The dock made for it, which names the float too.
    pub(crate) dock: String,
    /// Where it was released, in its viewport's points.
    pub(crate) rect: Rect,
    /// The same on the desktop, in physical pixels, when the system tells where its viewport
    /// is.
    pub(crate) physical: Option<Rect>,
    /// Its tabs.
    pub(crate) tabs: Vec<Id>,
    /// What hosts it, chosen at its release: the space's host, or a native window for one
    /// torn out of the work area ([`Outside::Native`]).
    pub(crate) host: FloatHost,
}

/// Draws the floats of a [`Layout`] whose content is a dock of a [`DockSpace`]: over the main
/// window as `egui::Window`s ([`FloatHost::InWindow`]), or in native windows through a
/// [`ViewportHost`] ([`FloatHost::Native`]). Call it once a frame, in the main window's pass,
/// after the zones' docks.
///
/// Once a space's floats are drawn, a tab or a group moved out of every dock (released outside
/// them, ejected, or moved to a new window from its menu) becomes a float of the layout, in a
/// dock of its own, with its origin recorded ([`DockSpace::record_origin`]); legacy windows
/// ([`Surface::Window`]) become floats too. The float's host is the space's
/// [`float_host`](DockSpace::float_host).
///
/// Each frame it also:
///
/// - brings native floats back onto the host's screens when they change
///   ([`Layout::repair_floats`], with [`screen_gone`](Self::screen_gone));
/// - closes the floats whose dock emptied or went;
/// - keeps each float's place as the user moves and resizes it;
/// - shrinks a float over the main window whose groups are all collapsed to their tab bars,
///   without its resize grip, its place keeping the height it gets back once a group is
///   expanded. A native float keeps its window's size: the system owns the window, whose
///   frame and resizing are its own, and a collapsed group leaves its body to the other
///   groups, as in a dock;
/// - asks what becomes of the tabs of a float the user closes
///   ([`TabViewer::close_policy`]) and applies it.
///
/// What happened is reported by [`DockSpace::take_float_events`].
///
/// The application shapes the floats' docks: its [`painter`](Self::painter) paints them (tab
/// bars, outlines), [`configure`](Self::configure) sets each float's [`DockArea`] options (no
/// splits, no close or add buttons…), and [`decorate`](Self::decorate) draws over each float
/// once its dock is drawn, knowing where its tab bars are ([`FloatShown`]).
///
/// ```
/// # use egui_stack::{DockArea, DockRules, DockSpace, Docked, FloatsArea, TabViewer};
/// # use egui_stack::layout::{Content, Layout, Zone};
/// # struct Tabs;
/// # impl TabViewer for Tabs {
/// #     type Tab = String;
/// #     fn id(&mut self, tab: &mut String) -> egui::Id { egui::Id::unique(tab.as_str()) }
/// #     fn title(&mut self, tab: &mut String) -> egui::WidgetText { tab.as_str().into() }
/// #     fn ui(&mut self, ui: &mut egui::Ui, tab: &mut String) { ui.label(tab.as_str()); }
/// # }
/// let mut space = DockSpace::new();
/// space.add_dock("centre", DockRules::default(), vec!["Editor".to_owned()]);
/// let mut docked = Docked::new(Layout::new(Zone::leaf("centre", Content::Dock("centre".into()))), space);
/// # egui::__run_test_ui(|ui| {
/// DockArea::dock(&mut docked.space, "centre").show_inside(ui, &mut Tabs);
/// docked.floats().show(ui.ctx(), &mut Tabs);
/// for event in docked.space.take_float_events() {
///     println!("{event:?}");
/// }
/// # });
/// ```
pub struct FloatsArea<'a, Tab> {
    layout: &'a mut Layout,
    space: &'a mut DockSpace<Tab>,
    host: Option<&'a mut dyn ViewportHost>,
    gone: ScreenGone,
    style: Option<Style>,
    handles: GroupHandles,
    modifiers: Modifiers,
    float_move: FloatMove,
    move_from: MoveFrom,
    title_bar: bool,
    work_area: WorkArea,
    add_buttons: bool,
    eject_buttons: bool,
    painter: Option<&'a (dyn DockPainter + Sync)>,
    configure: Option<Configure<'a, Tab>>,
    decorate: Option<Decorate<'a>>,
}

/// A float's [`DockArea`] shaped by the application ([`FloatsArea::configure`]).
type Configure<'a, Tab> =
    Box<dyn for<'d> FnMut(&Float, DockArea<'d, Tab>) -> DockArea<'d, Tab> + 'a>;

/// What the application draws over a float ([`FloatsArea::decorate`]).
type Decorate<'a> = Box<dyn FnMut(&mut Ui, &FloatShown) + 'a>;

/// A float just drawn, for [`FloatsArea::decorate`].
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct FloatShown {
    /// The float's key.
    pub key: String,
    /// The dock it shows.
    pub dock: String,
    /// The viewport it is drawn in: its native window's ([`ViewportHost::viewport_id`]), or the
    /// main window's for a float over it.
    pub viewport: ViewportId,
    /// Whether it is a native float ([`FloatHost::Native`]).
    pub native: bool,
    /// The rect its dock was drawn in, in the viewport's points.
    pub rect: Rect,
    /// Its tab bars, each with its leaf, in the viewport's points: the whole bar, margins
    /// included, as [`DockArea::tab_bars`] lists them. A hidden tab bar is not listed.
    pub tab_bars: Vec<(NodePath, Rect)>,
}

impl<Tab> Docked<Tab> {
    /// Draws the pair's floats: see [`FloatsArea`].
    pub fn floats(&mut self) -> FloatsArea<'_, Tab> {
        FloatsArea::new(&mut self.layout, &mut self.space)
    }
}

impl<'a, Tab> FloatsArea<'a, Tab> {
    /// The floats of `layout`, showing docks of `space`.
    pub fn new(layout: &'a mut Layout, space: &'a mut DockSpace<Tab>) -> Self {
        Self {
            layout,
            space,
            host: None,
            gone: ScreenGone::default(),
            style: None,
            handles: GroupHandles::ALL,
            modifiers: Modifiers::SHIFT,
            float_move: FloatMove::Move,
            move_from: MoveFrom::BAR,
            title_bar: false,
            work_area: WorkArea::window(),
            add_buttons: false,
            eject_buttons: false,
            painter: None,
            configure: None,
            decorate: None,
        }
    }

    /// Sets what paints the floats' docks ([`DockArea::painter`]): their tab bars and tabs,
    /// and, while a tab is dragged out of another window where window positions are hidden,
    /// their [`window_outline`](DockPainter::window_outline). By default the area's own
    /// ([`StylePainter`](crate::StylePainter)).
    #[must_use]
    pub fn painter(mut self, painter: &'a (dyn DockPainter + Sync)) -> Self {
        self.painter = Some(painter);
        self
    }

    /// Shapes each float's [`DockArea`] before it is drawn: `configure` gets the float and the
    /// area, with egui_stack's settings applied (style, painter, handles, modifiers), and
    /// returns it, its own options set; for example
    /// `area.allowed_splits(AllowedSplits::None).show_close_buttons(false)`.
    ///
    /// Leave the area's [`id`](DockArea::id) alone: the floats' drags and move handles are
    /// keyed by it.
    #[must_use]
    pub fn configure(
        mut self,
        configure: impl for<'d> FnMut(&Float, DockArea<'d, Tab>) -> DockArea<'d, Tab> + 'a,
    ) -> Self {
        self.configure = Some(Box::new(configure));
        self
    }

    /// Draws over each float once its dock is drawn, in the same pass and the float's own
    /// [`Ui`] (its native window's, or the `egui::Window`'s): `decorate` gets that `Ui` and
    /// where the float's tab bars are ([`FloatShown::tab_bars`]), for a button of the
    /// application at the end of a bar, say. What it adds is drawn and hit above the dock.
    #[must_use]
    pub fn decorate(mut self, decorate: impl FnMut(&mut Ui, &FloatShown) + 'a) -> Self {
        self.decorate = Some(Box::new(decorate));
        self
    }

    /// Sets what hosts the native floats; by default [`EguiViewports`].
    #[must_use]
    pub fn host(mut self, host: &'a mut dyn ViewportHost) -> Self {
        self.host = Some(host);
        self
    }

    /// Sets what becomes of a native float whose screen is gone; by default
    /// [`ScreenGone::MoveOnto`].
    #[must_use]
    pub fn screen_gone(mut self, gone: ScreenGone) -> Self {
        self.gone = gone;
        self
    }

    /// Sets the docks' style in the floats; by default egui's.
    #[must_use]
    pub fn style(mut self, style: Style) -> Self {
        self.style = Some(style);
        self
    }

    /// Sets the handles of the floats' docks ([`DockArea::group_handles`]), the window's move
    /// handle ([`GroupHandles::MOVE_WINDOW`]) included; by default [`GroupHandles::ALL`].
    #[must_use]
    pub fn group_handles(mut self, handles: GroupHandles) -> Self {
        self.handles = handles;
        self
    }

    /// Sets the modifiers that make a tab's drag carry its group in the floats' docks
    /// ([`DockArea::group_drag_modifiers`]); by default [`Modifiers::SHIFT`].
    #[must_use]
    pub fn group_drag_modifiers(mut self, modifiers: Modifiers) -> Self {
        self.modifiers = modifiers;
        self
    }

    /// Sets what moving a float's window does, by its title bar or its move handle; by
    /// default [`FloatMove::Move`]: it only moves, and never docks.
    #[must_use]
    pub fn float_move(mut self, float_move: FloatMove) -> Self {
        self.float_move = float_move;
        self
    }

    /// Sets where else than its move handle and title bar a drag moves a float's window: see
    /// [`MoveFrom`]. By default [`MoveFrom::BAR`], only them. It combines with
    /// [`float_move`](Self::float_move): a float moved from elsewhere docks as one moved by
    /// its handle.
    #[must_use]
    pub fn move_from(mut self, move_from: MoveFrom) -> Self {
        self.move_from = move_from;
        self
    }

    /// Shows an Eject group button in each tab bar of the floats' docks
    /// ([`DockArea::show_leaf_eject_buttons`]), as the application's docks may: a float of
    /// several groups ejects one into a float of its own; a float of one group shows none. By
    /// default `false`. A float's [`configure`](Self::configure) can still change it.
    #[must_use]
    pub fn show_leaf_eject_buttons(mut self, show_leaf_eject_buttons: bool) -> Self {
        self.eject_buttons = show_leaf_eject_buttons;
        self
    }

    /// Sets whether a float over the main window ([`FloatHost::InWindow`]) has a title bar of
    /// its own, with its title and a close button, above its dock; by default it has none: its
    /// top tab bar is its top, the window's move handle ([`GroupHandles::MOVE_WINDOW`]) moves
    /// it and a close window button beside the handle closes it, as the title bar's close
    /// button does: it asks [`TabViewer::close_policy`] what becomes of the tabs, whether or
    /// not they can be closed ([`TabViewer::is_closeable`]). The tab bars' close all buttons
    /// close tabs, not the window. Without the move handle (turned off in
    /// [`group_handles`](Self::group_handles)), the title bar is kept, so that the window can
    /// still be moved. A native float keeps the system's decoration either way.
    #[must_use]
    pub fn title_bar(mut self, title_bar: bool) -> Self {
        self.title_bar = title_bar;
        self
    }

    /// Shows an add button in each tab bar of the floats' docks
    /// ([`DockArea::show_add_buttons`]), as the application's docks show theirs: give it the
    /// value given to the docks, so that a group shows its add button whether it is docked or
    /// floating. A click calls [`TabViewer::on_add`] with the group's path, the float's surface
    /// in it. By default `false`, as for a [`DockArea`]. A float's
    /// [`configure`](Self::configure) can still change it.
    #[must_use]
    pub fn show_add_buttons(mut self, show_add_buttons: bool) -> Self {
        self.add_buttons = show_add_buttons;
        self
    }

    /// Sets where the floats over the main window may go, and where a tab dragged out of every
    /// dock may become one: see [`WorkArea`]. By default [`WorkArea::window`], the whole window.
    /// Native floats ignore it.
    ///
    /// Give it every frame, as the other options: the space keeps it (not saved) for its docks,
    /// which decide a drop as they are drawn, before the floats. On the very first frame the
    /// docks have not read it yet, which no drag notices: a drag takes several frames.
    #[must_use]
    pub fn work_area(mut self, work_area: WorkArea) -> Self {
        self.work_area = work_area;
        self
    }

    /// Draws the floats, back to front, and keeps the model up to date (see [`FloatsArea`]).
    pub fn show(self, ctx: &Context, viewer: &mut impl TabViewer<Tab = Tab>) {
        let Self {
            layout,
            space,
            host,
            gone,
            style,
            handles,
            modifiers,
            float_move,
            move_from,
            title_bar,
            work_area,
            add_buttons,
            eject_buttons,
            painter,
            configure,
            decorate,
        } = self;
        let mut gestures = Gestures {
            handles,
            modifiers,
            float_move,
            move_from,
            title_bar,
            add_buttons,
            eject_buttons,
            painter,
            configure,
            decorate,
        };
        let mut default_host = EguiViewports;
        let host: &mut dyn ViewportHost = match host {
            Some(host) => host,
            None => &mut default_host,
        };
        space.floating.enabled = true;
        space.floating.work_area = work_area;
        space.floating.native_windows = host.native_windows(ctx);
        let screens = host.screens();

        legacy_windows(layout, space);
        open_pending(layout, space, &screens);
        sync_docks(layout, space);
        close_orphans(layout, space);
        repair(layout, space, viewer, &screens, gone);

        space.floating.layers.clear();
        space.floating.rects.clear();
        let floats: Vec<(String, String, FloatHost)> = layout
            .floats_back_to_front()
            .into_iter()
            .filter_map(|float| {
                Some((float.key.clone(), float.dock_name()?.to_owned(), float.host))
            })
            .collect();
        for (key, dock, kind) in floats {
            match kind {
                FloatHost::Native => {
                    show_native(
                        ctx,
                        layout,
                        space,
                        host,
                        viewer,
                        &screens,
                        &key,
                        &dock,
                        &style,
                        &mut gestures,
                    );
                }
                _ => show_in_window(
                    ctx,
                    layout,
                    space,
                    viewer,
                    &key,
                    &dock,
                    &style,
                    &mut gestures,
                ),
            }
        }
    }
}

/// How the floats' docks and windows answer the user's gestures, and what the application
/// sets of them.
struct Gestures<'a, Tab> {
    handles: GroupHandles,
    modifiers: Modifiers,
    float_move: FloatMove,
    /// [`FloatsArea::move_from`].
    move_from: MoveFrom,
    /// [`FloatsArea::title_bar`].
    title_bar: bool,
    /// [`FloatsArea::show_add_buttons`].
    add_buttons: bool,
    /// [`FloatsArea::show_leaf_eject_buttons`].
    eject_buttons: bool,
    painter: Option<&'a (dyn DockPainter + Sync)>,
    configure: Option<Configure<'a, Tab>>,
    decorate: Option<Decorate<'a>>,
}

impl<'a, Tab> Gestures<'a, Tab> {
    /// The area drawing the dock of `float`, with these gestures, `style`, the application's
    /// painter and options.
    fn area<'d>(
        &mut self,
        float: &Float,
        area: DockArea<'d, Tab>,
        style: &Option<Style>,
    ) -> DockArea<'d, Tab>
    where
        'a: 'd,
    {
        // Moved from its empty space, a float's tab bars no longer drag their group from theirs.
        let handles = if self.move_from.empty_space() {
            self.handles & !GroupHandles::EMPTY_SPACE
        } else {
            self.handles
        };
        let mut area = area
            .group_handles(handles)
            .group_drag_modifiers(self.modifiers)
            .show_add_buttons(self.add_buttons)
            .show_leaf_eject_buttons(self.eject_buttons);
        if let Some(style) = style {
            area = area.style(style.clone());
        }
        if let Some(painter) = self.painter {
            area = area.painter(painter);
        }
        match &mut self.configure {
            Some(configure) => configure(float, area),
            None => area,
        }
    }

    /// Hands the float `key` (dock `dock`), just drawn by the area `area_id` in `ui`, to the
    /// application's [`FloatsArea::decorate`].
    #[allow(clippy::too_many_arguments)]
    fn decorate(
        &mut self,
        ui: &mut Ui,
        space: &DockSpace<Tab>,
        area_id: Id,
        key: &str,
        dock: &str,
        native: bool,
        rect: Rect,
    ) {
        let Some(decorate) = &mut self.decorate else {
            return;
        };
        let surface = space.dock_surface(dock);
        let tab_bars = DockArea::tab_bars(ui.ctx(), area_id)
            .into_iter()
            .filter(|(path, _)| Some(path.surface) == surface)
            .collect();
        let shown = FloatShown {
            key: key.to_owned(),
            dock: dock.to_owned(),
            viewport: ui.ctx().viewport_id(),
            native,
            rect,
            tab_bars,
        };
        decorate(ui, &shown);
    }
}

/// Legacy windows of the space ([`Surface::Window`]) become floats, hosted as the space says.
fn legacy_windows<Tab>(layout: &mut Layout, space: &mut DockSpace<Tab>) {
    let any = space
        .state()
        .iter_surfaces()
        .any(|surface| matches!(surface, Surface::Window(..)));
    if !any {
        return;
    }
    let host = space.floating.host;
    for mut float in space.windows_to_floats() {
        float.host = host;
        layout.open_float(float);
    }
}

/// The floats asked for by tabs moved out of a dock.
fn open_pending<Tab>(layout: &mut Layout, space: &mut DockSpace<Tab>, screens: &[Screen]) {
    for pending in std::mem::take(&mut space.floating.pending) {
        if !space.has_dock(&pending.dock) {
            continue;
        }
        let host = pending.host;
        let place = match host {
            FloatHost::Native => pending
                .physical
                .and_then(|rect| Place::from_physical(rect, screens))
                .unwrap_or_else(|| Place::new(pending.rect)),
            _ => Place::new(pending.rect),
        };
        let screen = place.screen.clone();
        if host != FloatHost::Native {
            space
                .floating
                .released_at
                .insert(pending.dock.clone(), pending.rect.min);
        }
        layout.open_float(Float::dock(
            pending.dock.clone(),
            pending.dock.clone(),
            host,
            place,
        ));
        #[cfg(feature = "bus")]
        if let Some(bus) = &space.bus {
            let tabs = dock_keys(space, &pending.dock, bus.key);
            bus.emit::<crate::bus::WindowDetached>(crate::bus::DetachedInfo {
                key: pending.dock.clone(),
                dock: pending.dock.clone(),
                tabs,
                native: host == FloatHost::Native,
                screen: screen.clone(),
            });
            space.emit_change(float_change(crate::bus::Change::Detached, &pending.dock));
        }
        #[cfg(not(feature = "bus"))]
        let _ = screen;
        space.floating.events.push(FloatEvent::Detached {
            key: pending.dock.clone(),
            dock: pending.dock,
            tabs: pending.tabs,
            host,
        });
    }
}

/// Marks the docks the floats show.
fn sync_docks<Tab>(layout: &Layout, space: &mut DockSpace<Tab>) {
    for dock in space.docks_mut() {
        dock.floating = layout.float_of_dock(&dock.name).is_some();
    }
}

/// Closes the floats whose dock emptied (and was removed) or went.
fn close_orphans<Tab>(layout: &mut Layout, space: &mut DockSpace<Tab>) {
    let orphans: Vec<(String, String)> = layout
        .floats
        .iter()
        .filter_map(|float| {
            let dock = float.dock_name()?;
            let emptied = space.dock(dock).is_some_and(Tree::is_empty)
                && space
                    .rules(dock)
                    .is_some_and(|rules| !rules.keep_when_empty);
            let gone = !space.has_dock(dock) || emptied;
            gone.then(|| (float.key.clone(), dock.to_owned()))
        })
        .collect();
    for (key, dock) in orphans {
        layout.close_float(&key);
        // A dock emptied but kept goes with its float.
        if space.dock(&dock).is_some_and(Tree::is_empty) {
            space.remove_dock(&dock);
        }
        forget_native(space, &key);
        closed(space, key, dock, None, Vec::new(), Vec::new());
    }
}

/// Brings the native floats back onto `screens` when they changed since the last repair.
fn repair<Tab>(
    layout: &mut Layout,
    space: &mut DockSpace<Tab>,
    viewer: &mut impl TabViewer<Tab = Tab>,
    screens: &[Screen],
    gone: ScreenGone,
) {
    if space.floating.screens.as_deref() == Some(screens) {
        return;
    }
    space.floating.screens = Some(screens.to_vec());
    let before: BTreeMap<String, Option<String>> = layout
        .floats
        .iter()
        .map(|float| (float.key.clone(), float.place.screen.clone()))
        .collect();
    let repair = layout.repair_floats(screens, gone);
    for key in repair.moved {
        let from = before.get(&key).cloned().flatten();
        let to = layout
            .float(&key)
            .and_then(|float| float.place.screen.clone());
        // A native window already open is told to move.
        space.floating.builders.remove(&key);
        repaired(space, key, from, to);
    }
    for float in repair.docked_back {
        let from = before.get(&float.key).cloned().flatten();
        repaired(space, float.key.clone(), from, None);
        forget_native(space, &float.key);
        if let Some(dock) = float.dock_name() {
            let dock = dock.to_owned();
            let (moved, keys) = put_back(space, viewer, &dock, &ClosePolicy::ReturnToOrigin);
            closed(
                space,
                float.key,
                dock,
                Some(ClosePolicy::ReturnToOrigin),
                moved,
                keys,
            );
        }
    }
}

/// A float brought back onto a screen, reported.
fn repaired<Tab>(
    space: &mut DockSpace<Tab>,
    key: String,
    from: Option<String>,
    to: Option<String>,
) {
    #[cfg(feature = "bus")]
    if let Some(bus) = &space.bus {
        bus.emit::<crate::bus::WindowRepaired>(crate::bus::RepairedInfo {
            key: key.clone(),
            from: from.clone(),
            to: to.clone(),
        });
        space.emit_change(float_change(crate::bus::Change::FloatRepaired, &key));
    }
    space
        .floating
        .events
        .push(FloatEvent::Repaired { key, from, to });
}

/// A float closed, reported. `keys` are the moved tabs' bus keys, when the space has a bus.
fn closed<Tab>(
    space: &mut DockSpace<Tab>,
    key: String,
    dock: String,
    policy: Option<ClosePolicy>,
    moved: Vec<(Id, String)>,
    keys: Vec<String>,
) {
    #[cfg(feature = "bus")]
    if let Some(bus) = &space.bus {
        bus.emit::<crate::bus::WindowClosed>(crate::bus::ClosedInfo {
            key: key.clone(),
            dock: dock.clone(),
            policy: policy.clone(),
            moved: keys
                .into_iter()
                .zip(moved.iter().map(|(_, dock)| dock.clone()))
                .collect(),
        });
        space.emit_change(float_change(crate::bus::Change::FloatClosed, &key));
    }
    #[cfg(not(feature = "bus"))]
    let _ = keys;
    space.floating.events.push(FloatEvent::Closed {
        key,
        dock,
        policy,
        moved,
    });
}

#[cfg(feature = "bus")]
fn float_change(change: crate::bus::Change, key: &str) -> crate::bus::LayoutChange {
    let mut change = crate::bus::LayoutChange::new(change);
    change.float = Some(key.to_owned());
    change
}

/// Forgets what was kept of a native float's window, closed: a drag that began in it ends, and
/// one over it is cancelled.
fn forget_native<Tab>(space: &mut DockSpace<Tab>, key: &str) {
    space.floating.released_at.remove(key);
    space.floating.chrome.remove(key);
    space.floating.collapsed.remove(key);
    space.floating.builders.remove(key);
    space.floating.moving.remove(key);
    space.floating.placed.remove(key);
    if space
        .floating
        .window_move
        .as_ref()
        .is_some_and(|moving| moving.key == key)
    {
        space.floating.window_move = None;
    }
    if let Some(viewport) = space.floating.viewports.remove(key) {
        space.floating.viewport_closed(viewport);
    }
}

/// The float `key`'s place changed to `place` (or not): kept, and reported once it stops
/// changing.
fn track_place<Tab>(layout: &mut Layout, space: &mut DockSpace<Tab>, key: &str, place: Place) {
    let Some(float) = layout.float_mut(key) else {
        return;
    };
    space.floating.placed.insert(key.to_owned(), place.rect.min);
    if float.place != place {
        float.place = place;
        space.floating.moving.insert(key.to_owned());
    } else if space.floating.moving.remove(key) {
        let place = float.place.clone();
        #[cfg(feature = "bus")]
        space.emit_change(float_change(crate::bus::Change::FloatMoved, key));
        space.floating.events.push(FloatEvent::Moved {
            key: key.to_owned(),
            place,
        });
    }
}

/// The title of the dock `dock`'s window: its focused (or first) leaf's active tab, else the
/// dock's name.
fn dock_title<Tab>(
    space: &mut DockSpace<Tab>,
    viewer: &mut impl TabViewer<Tab = Tab>,
    dock: &str,
) -> egui::WidgetText {
    space
        .dock_mut(dock)
        .and_then(|tree| tree_title(tree, viewer))
        .unwrap_or_else(|| dock.into())
}

/// The title of a float's window showing `tree`: its focused (or first) leaf's active tab;
/// `None` when the tree has no tab.
pub(crate) fn tree_title<Tab>(
    tree: &mut Tree<Tab>,
    viewer: &mut impl TabViewer<Tab = Tab>,
) -> Option<egui::WidgetText> {
    let node = tree
        .focused_leaf()
        .filter(|&node| tree.leaf(node).is_ok_and(|leaf| !leaf.tabs.is_empty()))
        .or_else(|| {
            tree.breadth_first_index_iter()
                .find(|&node| tree.leaf(node).is_ok_and(|leaf| !leaf.tabs.is_empty()))
        })?;
    let leaf = tree.leaf_mut(node).ok()?;
    let active = leaf.active.0.min(leaf.tabs.len() - 1);
    Some(viewer.title(&mut leaf.tabs[active]))
}

/// The least size of a float's content over the main window.
const MIN_SIZE: Vec2 = vec2(120.0, 80.0);

/// A float over the main window, as an `egui::Window`: moved by its move handle, or by its
/// title bar when it has one ([`FloatsArea::title_bar`]), and from elsewhere as
/// [`FloatsArea::move_from`] says. Its left and top edges resize it by handles of its own (`edges`).
#[allow(clippy::too_many_arguments)]
fn show_in_window<Tab>(
    ctx: &Context,
    layout: &mut Layout,
    space: &mut DockSpace<Tab>,
    viewer: &mut impl TabViewer<Tab = Tab>,
    key: &str,
    dock: &str,
    style: &Option<Style>,
    gestures: &mut Gestures<'_, Tab>,
) {
    let Some(float) = layout.float(key).cloned() else {
        return;
    };
    let place = float.place.clone();
    let app_moved = moved_by_app(space, &float);
    let title = dock_title(space, viewer, dock);
    let mut open = true;
    let id = space.id().with(("egui_stack.float", key));
    let handle = move_handle_id(space.id(), dock);
    let mover = mover_id(space.id(), dock);
    // Without a title bar, only the move handle moves the window: its body and the empty
    // part of its tab bars keep their own drags.
    let title_bar = gestures.title_bar || !gestures.handles.contains(GroupHandles::MOVE_WINDOW);
    // All its groups collapsed, the float is as tall as their tab bars and is not resized: its
    // place keeps the height it had, given back once a group is expanded.
    let mut probe = DockArea::dock(space, dock).show_add_buttons(gestures.add_buttons);
    if let Some(style) = style {
        probe = probe.style(style.clone());
    }
    let collapsed_height = probe.collapsed_dock_height(&ctx.global_style());
    let chrome = space.floating.chrome.get(key).copied().unwrap_or_else(|| {
        egui::Frame::window(&ctx.global_style())
            .total_margin()
            .sum()
            .y
            + if title_bar {
                ctx.global_style().spacing.interact_size.y
            } else {
                0.0
            }
    });
    let was_collapsed = space.floating.collapsed.contains(key);
    let window = egui::Window::new(title)
        .id(id)
        .default_pos(place.rect.min)
        .default_size(place.rect.size())
        .min_size(MIN_SIZE)
        .collapsible(false)
        .resizable(true)
        .title_bar(title_bar)
        .drag_area(if title_bar {
            WindowDrag::TitleBar
        } else {
            WindowDrag::Off
        })
        .open(&mut open);
    // The application changed the float's place: the window goes there.
    let window = if app_moved {
        window.current_pos(place.rect.min)
    } else {
        window
    };
    // The work area: the window stays in one region, moved by the pointer from one to another,
    // and is no larger than it. egui's `constrain_to` bounds the position; the size is capped
    // too (egui 0.37 also caps a window's largest size to its constrain rect). A window the
    // region moves or shrinks is kept and reported below, as one the user moved.
    let released_at = space.floating.released_at.remove(key);
    let region = space.floating.regions(space.docks()).and_then(|regions| {
        let moved_by_user = [id.with("__title_click"), handle, mover]
            .into_iter()
            .any(|id| ctx.read_response(id).is_some_and(|r| r.dragged()));
        let pointer = if moved_by_user {
            ctx.input(|i| i.pointer.latest_pos())
        } else {
            released_at
        };
        work_area::region_of(&regions, place.rect, pointer)
    });
    let window = match region {
        Some(region) => window.constrain_to(region).max_size(region.size()),
        None => window,
    };
    let window = match collapsed_height {
        Some(height) => window.fixed_size(vec2(place.rect.width(), height + chrome)),
        // Expanded again: the height it had, which egui keeps from the next frame on.
        None if was_collapsed => window
            .min_height(place.rect.height())
            .max_height(place.rect.height()),
        None => window,
    };
    // The left and top edges resize the float by handles of its own, added once it is drawn:
    // egui leaves them out of a window it does not move (no title bar), and its own left edge
    // lies under the dock's body, which takes its drags.
    let edges = collapsed_height.is_none();
    let window = match edges
        .then(|| edges::dragged(ctx, id, place.rect, MIN_SIZE, region.map(|r| r.size())))
        .flatten()
    {
        Some(rect) => {
            // Placed as the move handle places it: egui's title bar drag would undo a
            // `current_pos`. The sizes of an egui window are its outer ones.
            if let Some(mut state) = egui::AreaState::load(ctx, id) {
                state.set_left_top_pos(rect.min);
                ctx.memory_mut(|mem| {
                    mem.areas_mut()
                        .set_state(LayerId::new(egui::Order::Middle, id), state);
                });
            }
            window.min_size(rect.size()).max_size(rect.size())
        }
        None => window,
    };
    if collapsed_height.is_some() {
        space.floating.collapsed.insert(key.to_owned());
    } else {
        space.floating.collapsed.remove(key);
    }
    // The move handle dragged in the last pass: the window follows the pointer, moved as egui
    // moves it by its title bar (a `current_pos` would be undone by the title-bar-only drag).
    if [handle, mover]
        .into_iter()
        .any(|id| ctx.read_response(id).is_some_and(|r| r.dragged()))
    {
        let delta = ctx.input(|i| i.pointer.delta());
        if delta != egui::Vec2::ZERO
            && let Some(mut state) = egui::AreaState::load(ctx, id)
            && let Some(pivot) = state.pivot_pos.as_mut()
        {
            *pivot += delta;
            ctx.memory_mut(|mem| {
                mem.areas_mut()
                    .set_state(LayerId::new(egui::Order::Middle, id), state);
            });
        }
    }
    let mut content = None;
    let shown = window.show(ctx, |ui| {
        let area_id = space.id();
        let rect = ui.available_rect_before_wrap();
        content = Some(rect);
        gestures::mover(ui, mover, rect, gestures.move_from, false);
        let mut area = DockArea::dock(space, dock);
        if !title_bar {
            // Its top tab bar is its top: the close button goes there, by the move handle.
            area = area.float_close_button();
        }
        gestures.area(&float, area, style).show_inside(ui, viewer);
        gestures.decorate(ui, space, area_id, key, dock, false, rect);
        gestures::mover(ui, mover, rect, gestures.move_from, true);
    });
    if gestures.float_move == FloatMove::Dock {
        // egui's title bar of a window: its drag moves the window.
        let title_bar = ctx.read_response(id.with("__title_click"));
        let handle = ctx.read_response(handle);
        let mover = ctx.read_response(mover);
        let moving = [&title_bar, &handle, &mover]
            .iter()
            .any(|r| r.as_ref().is_some_and(|r| r.dragged()));
        let released = [&title_bar, &handle, &mover]
            .iter()
            .any(|r| r.as_ref().is_some_and(|r| r.drag_stopped()));
        let ours = space
            .floating
            .window_move
            .as_ref()
            .is_some_and(|moving| moving.key == key);
        if moving || (released && ours) {
            let pointer = ctx.input(|i| i.pointer.latest_pos());
            gestures::track(
                ctx, layout, space, viewer, style, key, dock, pointer, released,
            );
            if layout.float(key).is_none() {
                // Docked.
                return;
            }
        }
    }
    if let Some(shown) = &shown
        && tear_out(
            ctx,
            layout,
            space,
            key,
            [id.with("__title_click"), handle, mover],
            shown.response.rect,
        )
    {
        return;
    }
    if let Some(shown) = shown {
        let rect = shown.response.rect;
        if edges {
            edges::handles(ctx, id, shown.response.layer_id, rect);
        }
        space.floating.layers.push(shown.response.layer_id);
        space.floating.rects.push(rect);
        if let Some(content) = content {
            space
                .floating
                .chrome
                .insert(key.to_owned(), rect.height() - content.height());
        }
        if shown.response.contains_pointer() && ctx.input(|i| i.pointer.any_pressed()) {
            layout.raise_float(key);
        }
        let mut moved = place.clone();
        moved.rect = if collapsed_height.is_some() {
            // Collapsed, it keeps the height to expand to.
            Rect::from_min_size(rect.min, vec2(rect.width(), place.rect.height()))
        } else {
            rect
        };
        track_place(layout, space, key, moved);
    }
    // Its close window button, without a title bar, closes it as the title bar's would.
    if space.floating.close_asked.remove(dock) {
        open = false;
    }
    if !open {
        let policy = ask_close(space, viewer, key, dock);
        if policy != ClosePolicy::Keep {
            close_with(layout, space, viewer, key, policy);
        }
    }
}

/// Tears the float `key` over the main window out to a native window, when its window, at
/// `rect` and moved by the drags of `movers` (its title bar, its move handle), is let go with
/// the pointer outside every region of the work area and the work area says
/// [`Outside::Native`]: the native window opens where it was let go, the pointer where it held
/// the float. Returns whether it was torn out.
fn tear_out<Tab>(
    ctx: &Context,
    layout: &mut Layout,
    space: &mut DockSpace<Tab>,
    key: &str,
    movers: [Id; 3],
    rect: Rect,
) -> bool {
    let responses = movers.map(|id| ctx.read_response(id));
    let moving = responses.iter().flatten().any(egui::Response::dragged);
    let released = responses.iter().flatten().any(egui::Response::drag_stopped);
    let pointer = ctx.input(|i| i.pointer.latest_pos());
    if !moving && !released {
        space.floating.grabs.remove(key);
        return false;
    }
    let Some(pointer) = pointer else {
        return false;
    };
    // While over the window, where the pointer holds it.
    if rect.contains(pointer) {
        space
            .floating
            .grabs
            .insert(key.to_owned(), pointer - rect.min);
    }
    if !released {
        return false;
    }
    let grab = space.floating.grabs.remove(key);
    if space.floating.work_area.outside_rule() != Outside::Native
        || space.floating.release_host(space.docks(), pointer) != Some(FloatHost::Native)
    {
        return false;
    }
    let Some(float) = layout.float_mut(key) else {
        return false;
    };
    let grab = grab.unwrap_or_else(|| (pointer - rect.min).clamp(Vec2::ZERO, rect.size()));
    let at = Rect::from_min_size(pointer - grab, rect.size());
    // On the desktop, when the system tells where the main window is.
    let ppp = ctx.pixels_per_point();
    let physical = ctx.input(|i| i.viewport().inner_rect).map(|inner| {
        Rect::from_min_size(
            ((inner.min.to_vec2() + at.min.to_vec2()) * ppp).to_pos2(),
            at.size() * ppp,
        )
    });
    let screens = space.floating.screens.clone().unwrap_or_default();
    let place = physical
        .and_then(|rect| Place::from_physical(rect, &screens))
        .unwrap_or_else(|| Place::new(at));
    float.host = FloatHost::Native;
    float.place = place.clone();
    space.floating.chrome.remove(key);
    space.floating.collapsed.remove(key);
    space.floating.moving.remove(key);
    space.floating.placed.insert(key.to_owned(), place.rect.min);
    #[cfg(feature = "bus")]
    space.emit_change(float_change(crate::bus::Change::FloatMoved, key));
    space.floating.events.push(FloatEvent::Moved {
        key: key.to_owned(),
        place,
    });
    ctx.request_repaint();
    true
}

/// A native float, through the host.
#[allow(clippy::too_many_arguments)]
fn show_native<Tab>(
    ctx: &Context,
    layout: &mut Layout,
    space: &mut DockSpace<Tab>,
    host: &mut dyn ViewportHost,
    viewer: &mut impl TabViewer<Tab = Tab>,
    screens: &[Screen],
    key: &str,
    dock: &str,
    style: &Option<Style>,
    gestures: &mut Gestures<'_, Tab>,
) {
    let Some(float) = layout.float(key).cloned() else {
        return;
    };
    let id = host.viewport_id(key);
    // Docking while moving needs to know where the window is; else the handle only moves it.
    let docks_on_move =
        gestures.float_move == FloatMove::Dock && host.outer_rect(ctx, id).is_some();
    let handle = move_handle_id(space.id(), dock);
    let mover = mover_id(space.id(), dock);
    let mut moved_by_handle = None;
    space.floating.viewports.insert(key.to_owned(), id);
    // The application changed the float's place: the window is told to go there, and the
    // builder kept is made again from the place.
    let app_moved = moved_by_app(space, &float);
    let mut move_to = None;
    if app_moved {
        space.floating.builders.remove(key);
        move_to = first_builder(ViewportBuilder::default(), &float.place, screens).position;
    }
    let builder = match space.floating.builders.get(key) {
        Some(builder) => builder.clone(),
        None => {
            let builder = first_builder(host.builder(&float), &float.place, screens);
            space
                .floating
                .builders
                .insert(key.to_owned(), builder.clone());
            builder
        }
    };
    let area_id = space.id().with(("egui_stack.float", key));
    let mut closing = None;
    let mut seen: Option<(egui::Vec2, Option<bool>, Option<bool>)> = None;
    host.show(ctx, id, builder, &mut |ui: &mut Ui| {
        let (close, maximized, fullscreen) = ui.ctx().input(|i| {
            let info = i.viewport();
            (info.close_requested(), info.maximized, info.fullscreen)
        });
        seen = Some((ui.ctx().content_rect().size(), maximized, fullscreen));
        if let Some(at) = move_to.take() {
            ui.ctx()
                .send_viewport_cmd(ViewportCommand::OuterPosition(at));
        }
        let whole = ui.max_rect();
        gestures::mover(ui, mover, whole, gestures.move_from, false);
        egui::Frame::central_panel(ui.style()).show(ui, |ui| {
            let rect = ui.available_rect_before_wrap();
            gestures
                .area(
                    &float,
                    DockArea::dock(space, dock)
                        .id(area_id)
                        .in_native_float(float.z),
                    style,
                )
                .show_inside(ui, viewer);
            gestures.decorate(ui, space, area_id, key, dock, true, rect);
        });
        gestures::mover(ui, mover, whole, gestures.move_from, true);
        for id in [handle, mover] {
            let Some(response) = ui.ctx().read_response(id) else {
                continue;
            };
            if docks_on_move {
                if moved_by_handle.is_none() {
                    moved_by_handle = handle_move(ui.ctx(), space, key, &response);
                }
            } else if response.drag_started() {
                // The system moves the window, as by its title bar.
                ui.ctx().send_viewport_cmd(ViewportCommand::StartDrag);
            }
        }
        if close {
            let policy = ask_close(space, viewer, key, dock);
            if policy == ClosePolicy::Keep {
                ui.ctx().send_viewport_cmd(ViewportCommand::CancelClose);
            } else {
                closing = Some(policy);
            }
        }
    });
    if let Some(policy) = closing {
        close_with(layout, space, viewer, key, policy);
        return;
    }
    if let Some((desktop, released)) = moved_by_handle {
        // The pointer in the main window's points, where the system tells where it is.
        let ppp = ctx.pixels_per_point();
        let pointer = desktop.and_then(|desktop| {
            let origin = ctx.input(|i| i.viewport().inner_rect)?.min;
            Some((desktop.to_vec2() / ppp - origin.to_vec2()).to_pos2())
        });
        gestures::track(
            ctx, layout, space, viewer, style, key, dock, pointer, released,
        );
        if layout.float(key).is_none() {
            // Docked.
            return;
        }
    }
    let mut place = float.place.clone();
    if let Some(physical) = host.outer_rect(ctx, id)
        && let Some(found) = Place::from_physical(physical, screens)
    {
        place.screen = found.screen;
        place.rect = found.rect;
        place.scale = found.scale;
    } else if let Some((size, _, _)) = seen {
        // Where positions are hidden, the size is all the system tells.
        place.rect = Rect::from_min_size(place.rect.min, size);
    }
    if let Some((_, maximized, fullscreen)) = seen {
        place.maximized = maximized.unwrap_or(place.maximized);
        place.fullscreen = fullscreen.unwrap_or(place.fullscreen);
    }
    if app_moved {
        // The system tells the new position from the next frame: the place stays the
        // application's until then.
        space
            .floating
            .placed
            .insert(key.to_owned(), float.place.rect.min);
    } else {
        track_place(layout, space, key, place);
    }
}

/// Whether the application changed `float`'s place since its window was last seen.
fn moved_by_app<Tab>(space: &DockSpace<Tab>, float: &Float) -> bool {
    space
        .floating
        .placed
        .get(&float.key)
        .is_some_and(|at| *at != float.place.rect.min)
}

/// The move handle of the native float `key` moving its window itself ([`FloatMove::Dock`],
/// window positions known): the window follows the pointer. Returns, while the handle is
/// dragged or on its release, the pointer on the desktop (physical pixels) and whether it was
/// released.
fn handle_move<Tab>(
    ctx: &Context,
    space: &mut DockSpace<Tab>,
    key: &str,
    response: &egui::Response,
) -> Option<(Option<egui::Pos2>, bool)> {
    let released = response.drag_stopped();
    if !response.dragged() && !released {
        return None;
    }
    let ppp = ctx.pixels_per_point();
    let (inner, outer, pointer) = ctx.input(|i| {
        let info = i.viewport();
        (info.inner_rect, info.outer_rect, i.pointer.latest_pos())
    });
    let desktop = inner
        .zip(pointer)
        .map(|(inner, pointer)| ((inner.min.to_vec2() + pointer.to_vec2()) * ppp).to_pos2());
    let moving = space
        .floating
        .window_move
        .get_or_insert_with(|| WindowMove::new(key));
    if moving.key != key {
        *moving = WindowMove::new(key);
    }
    if moving.grab.is_none()
        && let (Some(desktop), Some(outer)) = (desktop, outer)
    {
        moving.grab = Some(desktop - (outer.min.to_vec2() * ppp).to_pos2());
    }
    // Only when the pointer moved: the system tells the pointer again, in the window's new
    // points, once the window has moved under it.
    let pointer_moved = ctx.input(|i| i.pointer.delta() != egui::Vec2::ZERO);
    if let (Some(desktop), Some(grab)) = (desktop, moving.grab)
        && !released
        && pointer_moved
    {
        let corner = ((desktop - grab).to_vec2() / ppp).to_pos2();
        ctx.send_viewport_cmd(ViewportCommand::OuterPosition(corner));
    }
    moving.desktop = desktop.or(moving.desktop);
    Some((moving.desktop, released))
}

/// The builder a native float's window opens with: the host's, with the place's position (on
/// a known screen), size and states where the host left them unset.
fn first_builder(
    mut builder: ViewportBuilder,
    place: &Place,
    screens: &[Screen],
) -> ViewportBuilder {
    if builder.position.is_none()
        && let Some(screen) = place
            .screen
            .as_deref()
            .and_then(|name| screens.iter().find(|screen| screen.name == name))
    {
        let physical = place.physical(screen);
        builder = builder.with_position((physical.min.to_vec2() / screen.scale).to_pos2());
    }
    if builder.inner_size.is_none() {
        builder = builder.with_inner_size(place.rect.size());
    }
    if builder.maximized.is_none() && place.maximized {
        builder = builder.with_maximized(true);
    }
    if builder.fullscreen.is_none() && place.fullscreen {
        builder = builder.with_fullscreen(true);
    }
    builder
}

/// The tabs of the dock `dock`, by the viewer's ids, in order.
fn dock_ids<Tab>(
    space: &mut DockSpace<Tab>,
    viewer: &mut impl TabViewer<Tab = Tab>,
    dock: &str,
) -> Vec<Id> {
    let Some(tree) = space.dock_mut(dock) else {
        return Vec::new();
    };
    let mut ids = Vec::new();
    for node in tree.iter_mut() {
        if let Some(leaf) = node.get_leaf_mut() {
            ids.extend(leaf.tabs.iter_mut().map(|tab| viewer.id(tab)));
        }
    }
    ids
}

/// The tabs of the dock `dock`, by the bus's key function, in order.
#[cfg(feature = "bus")]
fn dock_keys<Tab>(space: &DockSpace<Tab>, dock: &str, key: fn(&Tab) -> String) -> Vec<String> {
    space
        .dock(dock)
        .map(|tree| {
            tree.iter()
                .filter_map(|node| node.tabs())
                .flatten()
                .map(key)
                .collect()
        })
        .unwrap_or_default()
}

/// What becomes of the tabs of the float `key` the user closes: the viewer's answer, then the
/// bus's.
fn ask_close<Tab>(
    space: &mut DockSpace<Tab>,
    viewer: &mut impl TabViewer<Tab = Tab>,
    key: &str,
    dock: &str,
) -> ClosePolicy {
    let tabs = dock_ids(space, viewer, dock);
    let close = FloatClose {
        key: key.to_owned(),
        dock: dock.to_owned(),
        tabs,
    };
    let policy = viewer.close_policy(&close);
    #[cfg(feature = "bus")]
    if let Some(bus) = &space.bus {
        let origins = close
            .tabs
            .iter()
            .map(|&tab| space.origin(tab).map(|origin| origin.dock.clone()))
            .collect();
        let ask = crate::bus::CloseAsk {
            key: key.to_owned(),
            dock: dock.to_owned(),
            tabs: dock_keys(space, dock, bus.key),
            origins,
        };
        return bus.close_policy(ask, policy);
    }
    policy
}

/// Closes the float `key` with `policy` (not [`ClosePolicy::Keep`]), and reports it.
fn close_with<Tab>(
    layout: &mut Layout,
    space: &mut DockSpace<Tab>,
    viewer: &mut impl TabViewer<Tab = Tab>,
    key: &str,
    policy: ClosePolicy,
) {
    let Some(float) = layout.close_float(key) else {
        return;
    };
    forget_native(space, key);
    let Some(dock) = float.dock_name().map(str::to_owned) else {
        return;
    };
    let (moved, keys) = put_back(space, viewer, &dock, &policy);
    closed(space, key.to_owned(), dock, Some(policy), moved, keys);
    sync_docks(layout, space);
}

/// Empties the dock `dock` (and removes it) as `policy` says: its tabs go back to their
/// origins, into a dock, or close. Returns each tab moved with the dock it went to, and the
/// moved tabs' bus keys.
fn put_back<Tab>(
    space: &mut DockSpace<Tab>,
    viewer: &mut impl TabViewer<Tab = Tab>,
    dock: &str,
    policy: &ClosePolicy,
) -> (Vec<(Id, String)>, Vec<String>) {
    let Some(mut tree) = space.remove_dock(dock) else {
        return (Vec::new(), Vec::new());
    };
    let mut tabs: Vec<Tab> = Vec::new();
    for node in tree.iter_mut() {
        if let Some(leaf) = node.get_leaf_mut() {
            tabs.append(&mut leaf.tabs);
        }
    }
    let mut moved = Vec::new();
    #[cfg_attr(not(feature = "bus"), allow(unused_mut))]
    let mut keys = Vec::new();
    for mut tab in tabs {
        let id = viewer.id(&mut tab);
        let origin = space.take_origin(id);
        #[cfg(feature = "bus")]
        let key = space.bus.as_ref().map(|bus| (bus.key)(&tab));
        let into = match policy {
            ClosePolicy::CloseTabs | ClosePolicy::Keep => continue,
            ClosePolicy::Into(name) if space.has_dock(name) => Some((name.clone(), None)),
            _ => origin
                .filter(|origin| space.has_dock(&origin.dock))
                .map(|origin| (origin.dock.clone(), Some((origin.before, origin.after)))),
        };
        let fallback = || {
            space
                .docks()
                .iter()
                .find(|dock| !dock.floating)
                .map(|dock| (dock.name.clone(), None))
        };
        let Some((target, near)) = into.or_else(fallback) else {
            continue;
        };
        let near = near.map(|(before, after)| crate::dock_space::Near {
            before,
            after,
            index: None,
        });
        if crate::dock_space::put_tab(space, &mut |tab| viewer.id(tab), &target, tab, near).is_err()
        {
            continue;
        }
        moved.push((id, target));
        #[cfg(feature = "bus")]
        keys.extend(key);
    }
    (moved, keys)
}