egui_vertical_stack 0.2.0

A vertical stack widget with resize handles for egui
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
use std::boxed::Box;
use std::collections::HashMap;
use std::hash::Hash;

use egui::scroll_area::ScrollBarVisibility;
use egui::{Color32, CornerRadius, Id, PointerButton, Rect, ScrollArea, Sense, StrokeKind, Ui, UiBuilder, Vec2};

/// A component that displays multiple panels stacked vertically, each with resize handles, all contained within
/// a scroll area.
///
/// This is very useful for layouts that need multiple 'panel' views.
///
/// The amount of panels is dynamic and can be added or removed at runtime.
///
/// Example use:
/// ```no_run
/// use egui_vertical_stack::VerticalStack;
///
/// struct MyApp {
///     vertical_stack: VerticalStack,
/// }
///
/// impl MyApp {
///     pub fn new() -> Self {
///         Self {
///             vertical_stack: VerticalStack::new()
///                 .min_panel_height(50.0)
///                 .default_panel_height(150.0),
///         }
///     }
/// }
///
/// impl eframe::App for MyApp {
///     fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
///         egui::SidePanel::left("left_panel").show(ctx, |ui| {
///             self.vertical_stack
///                 .id_salt(ui.id().with("vertical_stack"))
///                 .body(ui, |body|{
///
///                     // Panels can be conditionally added/removed at runtime.
///                     // Panels are added to the stack in the same order as the calls to `add_panel`
///                     // Each panel needs a unique ID, which is generated from the ID salt and the panel's hash
///
///                     body.add_panel("top", |ui|{
///                         ui.label("top");
///                     });
///                     body.add_panel("middle", |ui|{
///                         ui.style_mut().wrap_mode = Some(eframe::egui::TextWrapMode::Extend);
///                         ui.label("middle with some non-wrapping text");
///                     });
///                     body.add_panel("bottom", |ui|{
///                         ui.label("bottom");
///                     });
///                 });
///         });
///         egui::CentralPanel::default().show(ctx, |ui| {
///             ui.label("main content");
///         });
///     }
/// }
///
/// fn main() -> eframe::Result {
///     let native_options = eframe::NativeOptions::default();
///     eframe::run_native("egui_vertical_stack - Simple", native_options, Box::new(|_cc| Ok(Box::new(MyApp::new()))))
/// }
/// ```
///
/// For a more complete example, check out the `demos` folder in the source.
#[derive(Debug)]
pub struct VerticalStack {
    //
    // settings
    //
    id_source: Id,
    /// Maximum height for the scroll area.
    max_height: Option<f32>,
    min_panel_height: f32,
    max_panel_height: Option<f32>,
    default_panel_height: f32,
    scroll_bar_visibility: ScrollBarVisibility,
    framed: bool,
    inner_margin: f32,

    //
    // from sizing pass
    //
    panel_heights: HashMap<Id, f32>,
    /// (width, height)
    content_sizes: HashMap<Id, (f32, f32)>,

    //
    // dragging state
    //
    active_drag_handle: Option<usize>,
    drag_start_y: Option<f32>,
    drag_start_height: Option<f32>,

    //
    // other state
    //
    initialized: bool,
    last_available_height: f32,
    last_panel_count: usize,
}

impl VerticalStack {
    pub fn new() -> Self {
        Self {
            id_source: Id::new("vertical_stack"),
            max_height: None,
            min_panel_height: 50.0,
            max_panel_height: None,
            default_panel_height: 100.0,
            scroll_bar_visibility: ScrollBarVisibility::VisibleWhenNeeded,
            inner_margin: 4.0,
            framed: true,

            panel_heights: HashMap::new(),
            content_sizes: HashMap::new(),

            active_drag_handle: None,
            drag_start_y: None,
            drag_start_height: None,

            initialized: false,
            last_available_height: 0.0,
            last_panel_count: 0,
        }
    }

    /// Sets a custom ID salt for the stack.
    pub fn id_salt(&mut self, id: impl Hash) -> &mut Self {
        self.id_source = Id::new(id);
        self
    }

    pub fn inner_margin(mut self, inner_margin: f32) -> Self {
        self.inner_margin = inner_margin;
        self
    }

    pub fn framed(mut self, framed: bool) -> Self {
        self.framed = framed;
        self
    }

    /// Sets the maximum height for the scroll area.
    /// If None, will use all available height.
    pub fn max_height(mut self, height: Option<f32>) -> Self {
        self.max_height = height;
        self
    }

    /// Sets the minimum height for panels.
    pub fn min_panel_height(mut self, height: f32) -> Self {
        self.min_panel_height = height;
        self
    }

    /// Sets the maximum height for individual panels.
    /// If None, panels can grow as large as needed (or up to the entire scroll area).
    pub fn max_panel_height(mut self, height: Option<f32>) -> Self {
        self.max_panel_height = height;
        self
    }

    /// Set the default height for new panels.
    pub fn default_panel_height(mut self, height: f32) -> Self {
        self.default_panel_height = height;
        self
    }

    /// Adjust the scroll-area's scrollbar visibility, the default is 'when needed', but using 'always visible' will
    /// likely yield a better UX.
    pub fn scroll_bar_visibility(mut self, visibility: ScrollBarVisibility) -> Self {
        self.scroll_bar_visibility = visibility;
        self
    }

    /// The main function to render the stack and add panels.
    pub fn body<F>(&mut self, ui: &mut Ui, mut collect_panels: F)
    where
        F: FnMut(&mut StackBodyBuilder),
    {
        let available_rect = ui.available_rect_before_wrap();
        let available_height = match self.max_height {
            Some(max_height) => max_height.min(available_rect.height()),
            None => available_rect.height(),
        };

        let mut body = StackBodyBuilder {
            panels: Vec::new(),
        };

        // Collect panel functions
        collect_panels(&mut body);

        // Get panel count
        let panel_count = body.panels.len();

        // Early return if no panels
        if panel_count == 0 {
            return;
        }

        let seen_all_panels = body
            .panels
            .iter()
            .all(|(id, _fn)| self.panel_heights.contains_key(id));

        // Check if we need a sizing pass
        if !self.initialized
            || (self.last_available_height - available_height).abs() > 1.0
            || panel_count != self.last_panel_count
            || !seen_all_panels
        {
            self.last_available_height = available_height;
            self.last_panel_count = panel_count;

            // Run a sizing pass to measure content heights
            self.do_sizing_pass(ui, &mut body);

            ui.ctx().request_discard("sizing");
            // Mark as initialized
            self.initialized = true;

            // avoid final rendering on the sizing pass
            return;
        }

        // Now do the actual rendering with known content heights
        self.do_render_pass(ui, body, available_height);
    }

    /// Perform a sizing pass to measure content heights without rendering
    fn do_sizing_pass(&mut self, ui: &mut Ui, body: &mut StackBodyBuilder) {
        //self.content_sizes.clear();
        // Create a temporary UI for measuring content heights
        ui.allocate_ui(Vec2::new(ui.available_width(), 0.0), |ui| {
            // Get the available width for panels
            let panel_width = ui.available_width();

            // Measure each panel's content
            for (hash, panel_fn) in body.panels.iter_mut() {
                // Always respect the minimum height from the beginning
                let mut panel_height = *self
                    .panel_heights
                    .get(hash)
                    .unwrap_or(&self.default_panel_height);

                // Also ensure ALL existing panel heights respect minimum height
                panel_height = panel_height.max(self.min_panel_height);

                // Also apply max_panel_height if configured
                if let Some(max_panel_height) = self.max_panel_height {
                    panel_height = panel_height.min(max_panel_height);
                }

                // Create a temporary rect with max height for measurement
                let panel_rect = Rect::from_min_size(ui.cursor().min, Vec2::new(panel_width, f32::MAX));

                // Create a new child UI with sizing pass enabled
                let mut measuring_ui = ui.new_child(
                    UiBuilder::new()
                        .max_rect(panel_rect)
                        .sizing_pass(),
                );

                // Call the panel function to measure its size
                panel_fn(&mut measuring_ui);

                // Get the measured content height
                let content_rect = measuring_ui.min_rect();
                let content_height = content_rect.height();
                self.content_sizes
                    .insert(*hash, (content_rect.width(), content_height));

                // Update the panel height
                self.panel_heights
                    .insert(*hash, panel_height);
            }
        });
    }

    /// Render the actual UI with known content heights
    fn do_render_pass(&mut self, ui: &mut Ui, body: StackBodyBuilder, available_height: f32) {
        // Handle drag state
        let pointer_is_down = ui.input(|i| {
            i.pointer
                .button_down(PointerButton::Primary)
        });

        if self.active_drag_handle.is_some() && !pointer_is_down {
            self.active_drag_handle = None;
        }

        let (max_content_width, _max_content_height) = self
            .content_sizes
            .values()
            .fold((0.0_f32, 0.0_f32), |acc, (w, h)| (acc.0.max(*w), acc.1.max(*h)));

        // Create a ScrollArea with the available height
        ScrollArea::both()
            .id_salt(self.id_source.with("scroll_area"))
            .max_height(available_height)
            .scroll_bar_visibility(self.scroll_bar_visibility)
            .auto_shrink([false, true])
            .show(ui, |ui| {
                let scroll_area_rect_before_wrap = ui.available_rect_before_wrap();
                #[cfg(feature = "layout_debugging")]
                {
                    let debug_stroke = Stroke::new(1.0, Color32::PURPLE);
                    ui.painter().rect(
                        scroll_area_rect_before_wrap,
                        CornerRadius::ZERO,
                        Color32::TRANSPARENT,
                        debug_stroke,
                        StrokeKind::Outside,
                    );
                }

                // Use vertical layout with no spacing
                ui.spacing_mut().item_spacing.y = 0.0;

                // Get the available rect for the panel content
                let panel_rect = ui.available_rect_before_wrap();

                let style = ui.style();
                let frame_stroke = style.visuals.widgets.noninteractive.bg_stroke;

                for (idx, (id, mut panel_fn)) in body.panels.into_iter().enumerate() {
                    // Get panel height (already has min_height applied above)
                    let panel_height = *self.panel_heights.get(&id).unwrap();

                    let desired_size = Vec2::new(panel_rect.width(), panel_height);
                    ui.allocate_ui(desired_size, |ui| {
                        // Set a min height for the panel content
                        ui.set_min_height(panel_height);

                        let stroke_width = if self.framed {
                            frame_stroke.width
                        } else {
                            0_f32
                        };

                        let intial_frame_width = max_content_width + ((self.inner_margin + stroke_width) * 2.0);
                        let frame_width = intial_frame_width.max(scroll_area_rect_before_wrap.width());

                        // without this, the right hand side of the frame will not be visible when the scroll area is narrower than the content
                        ui.set_min_width(frame_width);

                        // Draw the panel frame
                        let mut frame_rect = ui.max_rect();
                        frame_rect.max.x = frame_rect.min.x + frame_width;
                        frame_rect.max.y = frame_rect.min.y + panel_height;

                        if self.framed {
                            ui.painter().rect(
                                frame_rect,
                                CornerRadius::ZERO,
                                Color32::TRANSPARENT,
                                frame_stroke,
                                StrokeKind::Outside,
                            );
                        }

                        let content_rect = frame_rect.shrink(self.inner_margin);
                        let mut panel_ui = ui.new_child(UiBuilder::new().max_rect(content_rect));

                        // Intersect the clip rectangles to prevent the last panel content overflowing the bottom of
                        // the scroll area when a maximum height is set on the scroll area.
                        let panel_rect = content_rect.intersect(ui.clip_rect());
                        #[cfg(feature = "layout_debugging")]
                        {
                            let debug_stroke = Stroke::new(1.0, Color32::GREEN);
                            ui.painter().rect(
                                panel_rect,
                                CornerRadius::ZERO,
                                Color32::TRANSPARENT,
                                debug_stroke,
                                StrokeKind::Outside,
                            );
                        }
                        panel_ui.set_clip_rect(panel_rect);
                        panel_fn(&mut panel_ui);
                    });

                    self.add_resize_handle(ui, idx, &id);
                }
            });
    }

    fn add_resize_handle(&mut self, ui: &mut Ui, panel_idx: usize, id: &Id) {
        let handle_height = 7.0;

        // For the last panel handle, we don't need to check the next panel's index
        let is_last_panel = panel_idx == self.panel_heights.len() - 1;

        // Skip if not valid (except for last panel)
        if !is_last_panel && (panel_idx >= self.panel_heights.len() || panel_idx + 1 >= self.panel_heights.len()) {
            return;
        }

        // Calculate handle rect
        let available_rect = ui.available_rect_before_wrap();
        let handle_rect = Rect::from_min_size(available_rect.min, Vec2::new(available_rect.width(), handle_height));

        // Allocate exact size with drag sense
        let (rect, response) = ui.allocate_exact_size(handle_rect.size(), Sense::drag());

        // Set the resize cursor when hovering or dragging
        if response.hovered() || response.dragged() {
            ui.ctx()
                .set_cursor_icon(egui::CursorIcon::ResizeVertical);
        }

        // Draw a small horizontal rule to indicate the drag handle
        // Calculate exact position for the 1px line with 3px padding above and below
        let painter = ui.painter();
        let handle_stroke = if response.hovered() || response.dragged() {
            ui.style()
                .visuals
                .widgets
                .active
                .fg_stroke
        } else {
            ui.style()
                .visuals
                .widgets
                .noninteractive
                .bg_stroke
        };

        // Position line exactly in the middle of the handle area
        let line_y = rect.min.y + (rect.height() / 2.0);

        // Draw a thin line that's always visible
        painter.line_segment(
            [
                egui::Pos2::new(rect.left(), line_y),
                egui::Pos2::new(rect.right(), line_y),
            ],
            handle_stroke,
        );

        #[cfg(feature = "layout_debugging")]
        {
            // Debug visualization of handle rect to see its exact bounds (can be removed in production)
            let debug_stroke = Stroke::new(1.0, Color32::YELLOW);
            painter.rect_stroke(rect, 0.0, debug_stroke, StrokeKind::Outside);
        }

        // Handle drag start
        if response.drag_started() {
            self.active_drag_handle = Some(panel_idx);

            // Store initial values
            if let Some(pointer_pos) = ui.ctx().pointer_latest_pos() {
                self.drag_start_y = Some(pointer_pos.y);

                let panel_height = *self.panel_heights.get(id).unwrap();
                self.drag_start_height = Some(panel_height);
            }
            return;
        }

        // Check if this is the active drag handle
        let is_active_handle = self.active_drag_handle == Some(panel_idx);

        if !is_active_handle {
            // Nothing else to do
            return;
        }

        // Handle ongoing drag
        if response.dragged() {
            if let (Some(start_y), Some(start_height)) = (self.drag_start_y, self.drag_start_height) {
                if let Some(current_pos) = ui.ctx().pointer_latest_pos() {
                    let panel_height = self.panel_heights.get_mut(id).unwrap();

                    // Calculate delta from initial position
                    let total_delta = current_pos.y - start_y;

                    // Apply delta to the initial height
                    let mut new_height = (start_height + total_delta).max(self.min_panel_height);

                    // Apply maximum constraint if set
                    if let Some(max_panel_height) = self.max_panel_height {
                        new_height = new_height.min(max_panel_height);
                    }

                    // Update the panel height
                    *panel_height = new_height;
                }
            }
        }

        // Handle drag end
        if response.drag_stopped() {
            self.active_drag_handle = None;
            self.drag_start_y = None;
            self.drag_start_height = None;
        }
    }
}

/// Collects panel closures and ids for later use.
pub struct StackBodyBuilder {
    panels: Vec<(Id, Box<dyn FnMut(&mut Ui)>)>,
}

impl StackBodyBuilder {
    /// Add a panel to the stack with the given content.
    ///
    /// Each panel should have its own id, usually just the panel index, but if you re-arrange
    /// panels then use something that uniquely identifies the panel, e.g a name.
    ///
    /// Panels can be conditionally added/removed at runtime by calling/not-calling this method.
    /// Panels are added to the stack in the same order as the calls to `add_panel`
    pub fn add_panel<F>(&mut self, id_salt: impl Hash, add_contents: F)
    where
        F: FnMut(&mut Ui) + 'static,
    {
        let id = Id::new(id_salt);
        // Box the function and store it for later execution
        self.panels
            .push((id, Box::new(add_contents)));
    }
}

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