egui 0.1.2

Simple, portable immediate mode GUI library for Rust
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
use std::sync::Arc;

use crate::{widgets::*, *};

use super::*;

/// A wrapper around other containers for things you often want in a window
pub struct Window<'open> {
    pub title_label: Label,
    open: Option<&'open mut bool>,
    pub area: Area,
    pub frame: Option<Frame>,
    pub resize: Resize,
    pub scroll: Option<ScrollArea>,
}

impl<'open> Window<'open> {
    // TODO: Into<Label>
    pub fn new(title: impl Into<String>) -> Self {
        let title = title.into();
        let area = Area::new(&title);
        let title_label = Label::new(title)
            .text_style(TextStyle::Heading)
            .multiline(false);
        Self {
            title_label,
            open: None,
            area,
            frame: None,
            resize: Resize::default()
                .outline(false)
                .min_content_size([96.0, 32.0])
                .min_desired_size([96.0, 200.0]),
            scroll: Some(
                ScrollArea::default()
                    .always_show_scroll(false)
                    .max_height(f32::INFINITY),
            ), // As large as we can be
        }
    }

    /// If the given bool is false, the window will not be visible.
    /// If the given bool is true, the window will have a close button that sets this bool to false.
    pub fn open(mut self, open: &'open mut bool) -> Self {
        self.open = Some(open);
        self
    }

    /// Usage: `Winmdow::new(...).mutate(|w| w.resize = w.resize.auto_expand_width(true))`
    /// Not sure this is a good interface for this.
    pub fn mutate(mut self, mutate: impl Fn(&mut Self)) -> Self {
        mutate(&mut self);
        self
    }

    /// Usage: `Winmdow::new(...).resize(|r| r.auto_expand_width(true))`
    /// Not sure this is a good interface for this.
    pub fn resize(mut self, mutate: impl Fn(Resize) -> Resize) -> Self {
        self.resize = mutate(self.resize);
        self
    }

    /// Usage: `Winmdow::new(...).frame(|f| f.fill(Some(BLUE)))`
    /// Not sure this is a good interface for this.
    pub fn frame(mut self, frame: Frame) -> Self {
        self.frame = Some(frame);
        self
    }

    pub fn default_pos(mut self, default_pos: impl Into<Pos2>) -> Self {
        self.area = self.area.default_pos(default_pos);
        self
    }

    pub fn default_size(mut self, default_size: impl Into<Vec2>) -> Self {
        self.resize = self.resize.default_size(default_size);
        self
    }

    pub fn default_rect(self, rect: Rect) -> Self {
        self.default_pos(rect.min).default_size(rect.size())
    }

    pub fn fixed_size(mut self, size: impl Into<Vec2>) -> Self {
        self.resize = self.resize.fixed_size(size);
        self
    }

    /// Can you resize it with the mouse?
    /// Note that a window can still auto-resize
    pub fn resizable(mut self, resizable: bool) -> Self {
        self.resize = self.resize.resizable(resizable);
        self
    }

    /// Not resizable, just takes the size of its contents.
    pub fn auto_sized(mut self) -> Self {
        self.resize = self.resize.auto_sized();
        self.scroll = None;
        self
    }

    pub fn scroll(mut self, scroll: bool) -> Self {
        if !scroll {
            self.scroll = None;
        }
        self
    }
}

impl<'open> Window<'open> {
    pub fn show(
        self,
        ctx: &Arc<Context>,
        add_contents: impl FnOnce(&mut Ui),
    ) -> Option<InteractInfo> {
        self.show_impl(ctx, Box::new(add_contents))
    }

    fn show_impl<'c>(
        self,
        ctx: &Arc<Context>,
        add_contents: Box<dyn FnOnce(&mut Ui) + 'c>,
    ) -> Option<InteractInfo> {
        let Window {
            title_label,
            open,
            area,
            frame,
            resize,
            scroll,
        } = self;

        if matches!(open, Some(false)) {
            return None;
        }

        let window_id = Id::new(title_label.text());
        let area_layer = area.layer();
        let resize_id = window_id.with("resize");
        let collapsing_id = window_id.with("collapsing");

        let possible = PossibleInteractions {
            movable: area.is_movable(),
            resizable: resize.is_resizable()
                && collapsing_header::State::is_open(ctx, collapsing_id).unwrap_or_default(),
        };

        let area = area.movable(false); // We move it manually
        let resize = resize.resizable(false); // We move it manually

        let resize = resize.id(resize_id);

        let frame = frame.unwrap_or_else(|| Frame::window(&ctx.style()));

        let mut area = area.begin(ctx);

        // First interact (move etc) to avoid frame delay:
        let last_frame_outer_rect = area.state().rect();
        let interaction = if possible.movable || possible.resizable {
            let title_bar_height =
                title_label.font_height(ctx.fonts()) + 1.0 * ctx.style().item_spacing.y; // this could be better
            let margins = 2.0 * frame.margin + vec2(0.0, title_bar_height);
            interact(
                ctx,
                margins,
                possible,
                area_layer,
                area.state_mut(),
                window_id,
                resize_id,
                last_frame_outer_rect,
            )
        } else {
            None
        };
        let hover_interaction = resize_hover(ctx, possible, area_layer, last_frame_outer_rect);

        let mut area_content_ui = area.content_ui(ctx);

        {
            // BEGIN FRAME --------------------------------
            let mut frame = frame.begin(&mut area_content_ui);

            let default_expanded = true;
            let mut collapsing = collapsing_header::State::from_memory_with_default_open(
                &frame.content_ui,
                collapsing_id,
                default_expanded,
            );
            let show_close_button = open.is_some();
            let title_bar = show_title_bar(
                &mut frame.content_ui,
                title_label,
                show_close_button,
                collapsing_id,
                &mut collapsing,
            );

            let content_rect = collapsing
                .add_contents(&mut frame.content_ui, |ui| {
                    resize.show(ui, |ui| {
                        // Add some spacing between title and content:
                        ui.allocate_space(ui.style().item_spacing);

                        if let Some(scroll) = scroll {
                            scroll.show(ui, add_contents)
                        } else {
                            add_contents(ui)
                        }
                    })
                })
                .map(|ri| ri.1);

            let outer_rect = frame.end(&mut area_content_ui);
            // END FRAME --------------------------------

            title_bar.ui(
                &mut area_content_ui,
                outer_rect,
                content_rect,
                open,
                &mut collapsing,
            );

            area_content_ui
                .memory()
                .collapsing_headers
                .insert(collapsing_id, collapsing);

            if let Some(interaction) = interaction {
                paint_frame_interaction(
                    &mut area_content_ui,
                    outer_rect,
                    interaction,
                    ctx.style().interact.active,
                );
            } else if let Some(hover_interaction) = hover_interaction {
                paint_frame_interaction(
                    &mut area_content_ui,
                    outer_rect,
                    hover_interaction,
                    ctx.style().interact.hovered,
                );
            }
        }
        let full_interact = area.end(ctx, area_content_ui);

        Some(full_interact)
    }
}

// ----------------------------------------------------------------------------

#[derive(Clone, Copy, Debug)]
struct PossibleInteractions {
    movable: bool,
    resizable: bool,
}

#[derive(Clone, Copy, Debug)]
pub(crate) struct WindowInteraction {
    pub(crate) area_layer: Layer,
    pub(crate) start_rect: Rect,
    pub(crate) left: bool,
    pub(crate) right: bool,
    pub(crate) top: bool,
    pub(crate) bottom: bool,
}

impl WindowInteraction {
    pub fn set_cursor(&self, ctx: &Context) {
        if (self.left && self.top) || (self.right && self.bottom) {
            ctx.output().cursor_icon = CursorIcon::ResizeNwSe;
        } else if (self.right && self.top) || (self.left && self.bottom) {
            ctx.output().cursor_icon = CursorIcon::ResizeNeSw;
        } else if self.left || self.right {
            ctx.output().cursor_icon = CursorIcon::ResizeHorizontal;
        } else if self.bottom || self.top {
            ctx.output().cursor_icon = CursorIcon::ResizeVertical;
        }
    }

    pub fn is_resize(&self) -> bool {
        self.left || self.right || self.top || self.bottom
    }

    pub fn is_pure_move(&self) -> bool {
        !self.is_resize()
    }
}

fn interact(
    ctx: &Context,
    margins: Vec2,
    possible: PossibleInteractions,
    area_layer: Layer,
    area_state: &mut area::State,
    window_id: Id,
    resize_id: Id,
    rect: Rect,
) -> Option<WindowInteraction> {
    let window_interaction = window_interaction(
        ctx,
        possible,
        area_layer,
        window_id.with("frame_resize"),
        rect,
    )?;
    let new_rect = resize_window(ctx, &window_interaction)?;

    let new_rect = ctx.round_rect_to_pixels(new_rect);
    // TODO: add this to a Window state instead as a command "move here next frame"

    area_state.pos = new_rect.min;

    if window_interaction.is_resize() {
        let mut resize_state = ctx.memory().resize.get(&resize_id).cloned().unwrap();
        resize_state.requested_size = Some(new_rect.size() - margins);
        ctx.memory().resize.insert(resize_id, resize_state);
    }

    ctx.memory().areas.move_to_top(area_layer);
    Some(window_interaction)
}

fn resize_window(ctx: &Context, window_interaction: &WindowInteraction) -> Option<Rect> {
    window_interaction.set_cursor(ctx);
    let mouse_pos = ctx.input().mouse.pos?;
    let mut rect = window_interaction.start_rect; // prevent drift

    if window_interaction.is_resize() {
        if window_interaction.left {
            rect.min.x = ctx.round_to_pixel(mouse_pos.x);
        } else if window_interaction.right {
            rect.max.x = ctx.round_to_pixel(mouse_pos.x);
        }

        if window_interaction.top {
            rect.min.y = ctx.round_to_pixel(mouse_pos.y);
        } else if window_interaction.bottom {
            rect.max.y = ctx.round_to_pixel(mouse_pos.y);
        }
    } else {
        // movevement
        rect = rect.translate(mouse_pos - ctx.input().mouse.press_origin?);
    }

    Some(rect)
}

fn window_interaction(
    ctx: &Context,
    possible: PossibleInteractions,
    area_layer: Layer,
    id: Id,
    rect: Rect,
) -> Option<WindowInteraction> {
    {
        let drag_id = ctx.memory().interaction.drag_id;

        if drag_id.is_some() && drag_id != Some(id) {
            return None;
        }
    }

    let mut window_interaction = { ctx.memory().window_interaction };

    if window_interaction.is_none() {
        if let Some(hover_window_interaction) = resize_hover(ctx, possible, area_layer, rect) {
            hover_window_interaction.set_cursor(ctx);
            if ctx.input().mouse.pressed {
                ctx.memory().interaction.drag_id = Some(id);
                ctx.memory().interaction.drag_is_window = true;
                window_interaction = Some(hover_window_interaction);
                ctx.memory().window_interaction = window_interaction;
            }
        }
    }

    if let Some(window_interaction) = window_interaction {
        let is_active = ctx.memory().interaction.drag_id == Some(id);

        if is_active && window_interaction.area_layer == area_layer {
            return Some(window_interaction);
        }
    }

    None
}

fn resize_hover(
    ctx: &Context,
    possible: PossibleInteractions,
    area_layer: Layer,
    rect: Rect,
) -> Option<WindowInteraction> {
    if let Some(mouse_pos) = ctx.input().mouse.pos {
        if let Some(top_layer) = ctx.layer_at(mouse_pos) {
            if top_layer != area_layer && top_layer.order != Order::Background {
                return None; // Another window is on top here
            }
        }

        if ctx.memory().interaction.drag_interest {
            // Another widget will become active if we drag here
            return None;
        }

        let side_interact_radius = ctx.style().resize_interact_radius_side;
        let corner_interact_radius = ctx.style().resize_interact_radius_corner;
        if rect.expand(side_interact_radius).contains(mouse_pos) {
            let (mut left, mut right, mut top, mut bottom) = Default::default();
            if possible.resizable {
                right = (rect.right() - mouse_pos.x).abs() <= side_interact_radius;
                bottom = (rect.bottom() - mouse_pos.y).abs() <= side_interact_radius;

                if rect.right_bottom().distance(mouse_pos) < corner_interact_radius {
                    right = true;
                    bottom = true;
                }

                if possible.movable {
                    left = (rect.left() - mouse_pos.x).abs() <= side_interact_radius;
                    top = (rect.top() - mouse_pos.y).abs() <= side_interact_radius;

                    if rect.right_top().distance(mouse_pos) < corner_interact_radius {
                        right = true;
                        top = true;
                    }
                    if rect.left_top().distance(mouse_pos) < corner_interact_radius {
                        left = true;
                        top = true;
                    }
                    if rect.left_bottom().distance(mouse_pos) < corner_interact_radius {
                        left = true;
                        bottom = true;
                    }
                }
            }
            let any_resize = left || right || top || bottom;

            if !any_resize && !possible.movable {
                return None;
            }

            if any_resize || possible.movable {
                Some(WindowInteraction {
                    area_layer,
                    start_rect: rect,
                    left,
                    right,
                    top,
                    bottom,
                })
            } else {
                None
            }
        } else {
            None
        }
    } else {
        None
    }
}

/// Fill in parts of the window frame when we resize by dragging that part
fn paint_frame_interaction(
    ui: &mut Ui,
    rect: Rect,
    interaction: WindowInteraction,
    style: style::WidgetStyle,
) {
    let cr = ui.style().window.corner_radius;
    let Rect { min, max } = rect;

    let mut path = Path::default();

    if interaction.right && !interaction.bottom && !interaction.top {
        path.add_line_segment([pos2(max.x, min.y + cr), pos2(max.x, max.y - cr)]);
    }
    if interaction.right && interaction.bottom {
        path.add_line_segment([pos2(max.x, min.y + cr), pos2(max.x, max.y - cr)]);
        path.add_circle_quadrant(pos2(max.x - cr, max.y - cr), cr, 0.0);
    }
    if interaction.bottom {
        path.add_line_segment([pos2(max.x - cr, max.y), pos2(min.x + cr, max.y)]);
    }
    if interaction.left && interaction.bottom {
        path.add_circle_quadrant(pos2(min.x + cr, max.y - cr), cr, 1.0);
    }
    if interaction.left {
        path.add_line_segment([pos2(min.x, max.y - cr), pos2(min.x, min.y + cr)]);
    }
    if interaction.left && interaction.top {
        path.add_circle_quadrant(pos2(min.x + cr, min.y + cr), cr, 2.0);
    }
    if interaction.top {
        path.add_line_segment([pos2(min.x + cr, min.y), pos2(max.x - cr, min.y)]);
    }
    if interaction.right && interaction.top {
        path.add_circle_quadrant(pos2(max.x - cr, min.y + cr), cr, 3.0);
        path.add_line_segment([pos2(max.x, min.y + cr), pos2(max.x, max.y - cr)]);
    }
    ui.add_paint_cmd(PaintCmd::Path {
        path,
        closed: false,
        fill: None,
        outline: style.rect_outline,
    });
}

// ----------------------------------------------------------------------------

struct TitleBar {
    title_label: Label,
    title_galley: font::Galley,
    title_rect: Rect,
    rect: Rect,
}

fn show_title_bar(
    ui: &mut Ui,
    title_label: Label,
    show_close_button: bool,
    collapsing_id: Id,
    collapsing: &mut collapsing_header::State,
) -> TitleBar {
    let title_bar_and_rect = ui.inner_layout(Layout::horizontal(Align::Center), |ui| {
        ui.set_desired_height(title_label.font_height(ui.fonts()));

        let item_spacing = ui.style().item_spacing;
        let button_size = ui.style().start_icon_width;

        {
            // TODO: make clickable radius larger
            ui.allocate_space(vec2(0.0, 0.0)); // HACK: will add left spacing

            let rect = ui.allocate_space(Vec2::splat(button_size));
            let collapse_button_interact = ui.interact(rect, collapsing_id, Sense::click());
            if collapse_button_interact.clicked {
                collapsing.toggle(ui);
            }
            collapsing.paint_icon(ui, &collapse_button_interact);
        }

        let title_galley = title_label.layout(ui);
        let title_rect = ui.allocate_space(title_galley.size);

        if show_close_button {
            // Reserve space for close button which will be added later:
            let close_max_x = title_rect.right() + item_spacing.x + button_size + item_spacing.x;
            let close_max_x = close_max_x.max(ui.rect_finite().right());
            let close_rect = Rect::from_min_size(
                pos2(
                    close_max_x - button_size,
                    title_rect.center().y - 0.5 * button_size,
                ),
                Vec2::splat(button_size),
            );
            ui.expand_to_include_child(close_rect);
        }

        TitleBar {
            title_label,
            title_galley,
            title_rect,
            rect: Default::default(), // Will be filled in later
        }
    });

    TitleBar {
        rect: title_bar_and_rect.1,
        ..title_bar_and_rect.0
    }
}

impl TitleBar {
    fn ui(
        mut self,
        ui: &mut Ui,
        outer_rect: Rect,
        content_rect: Option<Rect>,
        open: Option<&mut bool>,
        collapsing: &mut collapsing_header::State,
    ) {
        if let Some(content_rect) = content_rect {
            // Now we know how large we got to be:
            self.rect.max.x = self.rect.max.x.max(content_rect.max.x);
        }

        if let Some(open) = open {
            // Add close button now that we know our full width:
            if self.close_button_ui(ui).clicked {
                *open = false;
            }
        }

        // TODO: pick style for title based on move interaction
        self.title_label
            .paint_galley(ui, self.title_rect.min, self.title_galley);

        if let Some(content_rect) = content_rect {
            // paint separator between title and content:
            let left = outer_rect.left();
            let right = outer_rect.right();
            let y = content_rect.top() + ui.style().item_spacing.y * 0.5;
            ui.add_paint_cmd(PaintCmd::LineSegment {
                points: [pos2(left, y), pos2(right, y)],
                style: ui.style().interact.inactive.rect_outline.unwrap(),
            });
        }

        let title_bar_id = ui.make_child_id("title_bar");
        if ui
            .interact(self.rect, title_bar_id, Sense::click())
            .double_clicked
        {
            collapsing.toggle(ui);
        }
    }

    fn close_button_ui(&self, ui: &mut Ui) -> InteractInfo {
        let button_size = ui.style().start_icon_width;
        let button_rect = Rect::from_min_size(
            pos2(
                self.rect.right() - ui.style().item_spacing.x - button_size,
                self.rect.center().y - 0.5 * button_size,
            ),
            Vec2::splat(button_size),
        );

        close_button(ui, button_rect)
    }
}

fn close_button(ui: &mut Ui, rect: Rect) -> InteractInfo {
    let close_id = ui.make_child_id("window_close_button");
    let interact = ui.interact(rect, close_id, Sense::click());
    ui.expand_to_include_child(interact.rect);

    let stroke_color = ui.style().interact(&interact).stroke_color;
    let stroke_width = ui.style().interact(&interact).stroke_width;
    ui.add_paint_cmd(PaintCmd::line_segment(
        [rect.left_top(), rect.right_bottom()],
        stroke_color,
        stroke_width,
    ));
    ui.add_paint_cmd(PaintCmd::line_segment(
        [rect.right_top(), rect.left_bottom()],
        stroke_color,
        stroke_width,
    ));
    interact
}