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
use std::convert::identity;
use std::sync::Arc;

use egui::{
    CollapsingHeader, CollapsingResponse, Frame, Galley, Id, Layout, Rect, Response, Sense,
    TextStyle, Ui, Vec2, Widget,
};

use crate::{
    dock_area::{state::State, tab_removal::TabRemoval},
    utils::fade_visuals,
    DockArea, Node, Style, SurfaceIndex, TabViewer,
};

impl<'tree, Tab> DockArea<'tree, Tab> {
    pub(super) fn show_window_surface(
        &mut self,
        ui: &Ui,
        surf_index: SurfaceIndex,
        tab_viewer: &mut impl TabViewer<Tab = Tab>,
        state: &mut State,
        fade_style: Option<(&Style, f32, SurfaceIndex)>,
    ) {
        //construct egui window
        let id = format!("window {surf_index:?}").into();
        let bounds = self.window_bounds.unwrap();
        let mut open = true;
        let (window, new) = self
            .dock_state
            .get_window_state_mut(surf_index)
            .unwrap()
            .create_window(id, bounds);

        //calculate fading of the window (if any)
        let (fade_factor, fade_style) = match fade_style {
            Some((style, factor, surface_index)) => {
                if surface_index == surf_index {
                    (1.0, None)
                } else {
                    (factor, Some((style, factor)))
                }
            }
            None => (1.0, None),
        };

        // Get galley of currently selected node as a window title
        let title = {
            let node_id = self.dock_state[surf_index]
                .focused_leaf()
                .unwrap_or_else(|| {
                    for node_index in self.dock_state[surf_index].breadth_first_index_iter() {
                        if self.dock_state[surf_index][node_index].is_leaf() {
                            return node_index;
                        }
                    }
                    unreachable!("a window surface should never be empty")
                });
            let Node::Leaf { tabs, active, .. } = &mut self.dock_state[surf_index][node_id] else {
                unreachable!()
            };
            tab_viewer
                .title(&mut tabs[active.0])
                .color(ui.visuals().widgets.noninteractive.fg_stroke.color)
                .into_galley(ui, Some(false), 0.0, TextStyle::Button)
        };

        // Fade window frame (if necessary)
        let mut frame = Frame::window(ui.style());
        if fade_factor != 1.0 {
            frame.fill = frame.fill.linear_multiply(fade_factor);
            frame.stroke.color = frame.stroke.color.linear_multiply(fade_factor);
            frame.shadow.color = frame.shadow.color.linear_multiply(fade_factor);
        }

        window
            .frame(frame)
            .min_width(min_window_width(&title, ui.spacing().indent))
            .show(ui.ctx(), |ui| {
                //fade inner ui (if necessary)
                if fade_factor != 1.0 {
                    fade_visuals(ui.visuals_mut(), fade_factor);
                }

                let collapser_id = id.with("collapser");
                let collapser_state = new.then_some(true);
                let ch_res = self.show_window_body(
                    ui,
                    surf_index,
                    tab_viewer,
                    state,
                    fade_style,
                    collapser_state,
                    collapser_id,
                    title,
                );
                if self.show_window_close_buttons {
                    // Finds out if theres a reason for the close button to be disabled
                    // by iterating over the tree and finding if theres any non-closable nodes.
                    let disabled = !self.dock_state[surf_index]
                        .iter_mut()
                        .filter_map(|node| {
                            if let Node::Leaf { tabs, .. } = node {
                                Some(
                                    tabs.iter_mut()
                                        .map(|tab| tab_viewer.closeable(tab))
                                        .all(identity),
                                )
                            } else {
                                None
                            }
                        })
                        .all(identity);

                    self.show_close_button(ui, &mut open, ch_res, disabled);
                }
            });

        if !open {
            self.to_remove.push(TabRemoval::Window(surf_index));
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn show_window_body(
        &mut self,
        ui: &mut Ui,
        surf_index: SurfaceIndex,
        tab_viewer: &mut impl TabViewer<Tab = Tab>,
        state: &mut State,
        fade_style: Option<(&Style, f32)>,
        open: Option<bool>,
        id: Id,
        title: Arc<Galley>,
    ) -> Option<CollapsingResponse<()>> {
        if self.show_window_collapse_buttons {
            let ch_response = CollapsingHeader::new("")
                .id_source(id)
                .open(open)
                .show_unindented(ui, |ui| {
                    ui.set_min_size(Vec2::splat(100.0));
                    self.render_nodes(ui, tab_viewer, state, surf_index, fade_style);
                });
            ui.set_width(min_window_width(&title, ui.spacing().indent));

            if ch_response.fully_closed() {
                let rect = Rect::from_min_size(
                    ch_response.header_response.rect.left_top(),
                    Vec2::new(
                        ui.min_rect().size().x,
                        ch_response.header_response.rect.height(),
                    ),
                );
                ui.painter().galley(
                    rect.center() - (title.size() * 0.5),
                    title,
                    ui.visuals().widgets.noninteractive.fg_stroke.color,
                );
            }
            Some(ch_response)
        } else {
            // in case we don't render with a collapsing header we need to make a "blank"
            // window head in preparation for adding the close button.
            if self.show_window_close_buttons {
                ui.add_space(ui.spacing().icon_width + ui.spacing().item_spacing.y);
            }
            self.render_nodes(ui, tab_viewer, state, surf_index, fade_style);
            None
        }
    }

    fn show_close_button(
        &mut self,
        ui: &mut Ui,
        open: &mut bool,
        collapse_response: Option<CollapsingResponse<()>>,
        disabled: bool,
    ) {
        let rect = {
            let (top_right, height) = match collapse_response {
                Some(collapse) => (
                    Rect::from_two_pos(
                        collapse.header_response.rect.right_top(),
                        ui.max_rect().right_top(),
                    )
                    .right_top(),
                    collapse.header_response.rect.height(),
                ),
                None => (ui.max_rect().right_top(), ui.spacing().icon_width),
            };

            Rect::from_min_size(top_right, Vec2::new(0.0, height))
        };
        let close_button = close_button(
            disabled.then_some(
                self.dock_state
                    .translations
                    .window
                    .close_button_tooltip
                    .as_str(),
            ),
        );
        ui.allocate_ui_at_rect(rect, |ui| {
            ui.with_layout(Layout::right_to_left(egui::Align::Center), |ui| {
                ui.set_height(rect.height());
                if ui.add(close_button).clicked() {
                    *open = false;
                }
            });
        });
    }
}

fn min_window_width(title: &Galley, button_width: f32) -> f32 {
    (button_width * 2.) + title.size().x
}

fn close_button(disabled: Option<&str>) -> impl Widget + '_ {
    move |ui: &mut Ui| -> Response {
        let sense = disabled.map_or(Sense::click(), |_disabled| Sense::hover());

        //this is how CollapsableHeader decides on space,
        //don't know why it doesn't atually end up the same size.
        let size = Vec2::new(ui.spacing().indent, ui.spacing().icon_width);
        let (rect, res) = ui.allocate_exact_size(size, sense);

        let visuals = ui.style().interact(&res);
        let painter = ui.painter();

        let rect = Rect::from_center_size(
            rect.center(),
            Vec2::splat(rect.width().min(rect.height()) * 0.5),
        )
        .expand(visuals.expansion);

        let stroke = match disabled.is_some() {
            true => visuals.bg_stroke,
            false => visuals.fg_stroke,
        };
        painter.line_segment(
            [
                painter.round_pos_to_pixels(rect.left_top()),
                painter.round_pos_to_pixels(rect.right_bottom()),
            ],
            stroke,
        );
        painter.line_segment(
            [
                painter.round_pos_to_pixels(rect.right_top()),
                painter.round_pos_to_pixels(rect.left_bottom()),
            ],
            stroke,
        );
        match disabled {
            Some(reason) => res.on_hover_text(reason),
            None => res,
        }
    }
}