egui_alignments 0.3.8

Simple alignment tools 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
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
use egui::{
    Align, Align2, Id, InnerResponse, Layout, Margin, Pos2, Rect, Sense, Ui, UiBuilder, Vec2,
};

use crate::resize_layout_rect;

/// Represents an alignment strategy.
/// You can directly use `egui::Align2` or closure `FnOnce(egui::Vec2, egui::Rect) -> egui::Rect`
/// to align the contents.
/// Or you can implement your own alignment.
pub trait Alignment {
    fn align(self, item_size: Vec2, bounds: Rect) -> Rect;
}

impl Alignment for egui::Align2 {
    fn align(self, item_size: Vec2, bounds: Rect) -> Rect {
        self.align_size_within_rect(item_size, bounds)
    }
}

impl<T> Alignment for T
where
    T: FnOnce(Vec2, Rect) -> Rect,
{
    fn align(self, item_size: Vec2, bounds: Rect) -> Rect {
        self(item_size, bounds)
    }
}

/// Determines how [`Aligner`] allocate space for the aligned contents.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum AllocateType {
    /// Allocate no space.
    None,

    /// Allocate only the space allocated by the contents
    Content,

    /// Allocate only the height allocated by the contents
    /// and the whole width specified to align the contents.
    ContentRow,

    /// Allocate only the width allocated by the contents
    /// and the whole height specified to align the contents.
    ContentColumn,

    /// Allocate the whole bounds specified to align the contents.
    Bounds,
}

/// The bounds in which its contents will be aligned.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Bounds {
    /// Align in Ui's next widget position with the given size.
    AvailableRect(Vec2),

    /// Align in the whole Ui, ignoring the specified margin.
    MaxRect(Margin),
}

impl Bounds {
    #[inline]
    /// Align in all the available space.
    pub fn available_rect() -> Self {
        Bounds::AvailableRect(Vec2::INFINITY)
    }

    #[inline]
    /// Align in the whole Ui.
    pub fn max_rect() -> Self {
        Bounds::MaxRect(0.0.into())
    }
}

/// A container which aligns its contents
/// within the given alignment and bounds.
///
/// # Example
/// ```
/// use egui_alignments::Aligner;
///
/// # egui::__run_test_ui(|ui| {
/// Aligner::center()
///     .show(ui, |ui| {
///         ui.label("This label will be shown at the center");
///     });
/// # });
/// ```
pub struct Aligner<T: Alignment> {
    /// Used to memorize content size.
    /// If not set, the id will be generated automatically.
    pub id: Option<Id>,

    /// The alignment.
    /// Could be a `egui::Align2`, a closure or a custom [`Alignment`].
    pub align: T,

    /// The bounds in which its contents will be aligned.
    /// See [`Bounds`]
    pub bounds: Bounds,

    /// See [`AllocateType`]
    pub allocate_type: AllocateType,

    /// The layout of the contents.
    /// If None, use the layout of the current ui.
    pub layout: Option<Layout>,
}

impl Default for Aligner<Align2> {
    fn default() -> Self {
        Self {
            id: None,
            align: egui::Align2::LEFT_TOP,
            bounds: Bounds::available_rect(),
            allocate_type: AllocateType::Content,
            layout: None,
        }
    }
}

impl Aligner<Align2> {
    #[inline]
    /// Create an `Alignable`
    /// which aligns its contents to the center of all the available space.
    pub fn center() -> Self {
        Self::from_align(Align2::CENTER_CENTER)
    }

    #[inline]
    /// Create an `Alignable`
    /// which aligns its contents to the center-bottom of all the available space.
    pub fn center_top() -> Self {
        Self::from_align(Align2::CENTER_TOP)
    }

    #[inline]
    /// Create an `Alignable`
    /// which aligns its contents to the center-bottom of all the available space.
    pub fn center_bottom() -> Self {
        Self::from_align(Align2::CENTER_BOTTOM)
    }

    #[inline]
    /// Create an `Alignable`
    /// which aligns its contents to the left of the available space.
    pub fn left() -> Self {
        Self::from_align(Align2::LEFT_CENTER)
    }

    #[inline]
    /// Create an `Alignable`
    /// which aligns its contents to the left-top of all the available space.
    pub fn left_top() -> Self {
        Self::from_align(Align2::LEFT_TOP)
    }

    #[inline]
    /// Create an `Alignable`
    /// which aligns its contents to the left-bottom of all the available space.
    pub fn left_bottom() -> Self {
        Self::from_align(Align2::LEFT_BOTTOM)
    }

    #[inline]
    /// Create an `Alignable`
    /// which aligns its contents to the right of the available space.
    pub fn right() -> Self {
        Self::from_align(Align2::RIGHT_CENTER)
    }

    #[inline]
    /// Create an `Alignable`
    /// which aligns its contents to the right-top of all the available space.
    pub fn right_top() -> Self {
        Self::from_align(Align2::RIGHT_TOP)
    }

    #[inline]
    /// Create an `Alignable`
    /// which aligns its contents to the right-bottom of all the available space.
    pub fn right_bottom() -> Self {
        Self::from_align(Align2::RIGHT_BOTTOM)
    }
}

impl<T: Alignment> Aligner<T> {
    /// Create an `Alignable`
    /// which aligns its contents using the given alignment.
    pub fn from_align(align: T) -> Self {
        Self {
            id: None,
            align,
            bounds: Bounds::AvailableRect(Vec2::INFINITY),
            allocate_type: AllocateType::Content,
            layout: None,
        }
    }
}

impl<T: Alignment> Aligner<T> {
    #[inline]
    /// Set the id of the aligned widget.
    /// The id is used to memorize the content size.
    /// If not set, the id will be generated automatically.
    pub fn id(mut self, id: Id) -> Self {
        self.id = Some(id);
        self
    }

    #[inline]
    /// Set the alignment.
    /// The alignment is used to align the contents.
    /// Could be a `egui::Align2`, a closure or a custom [`Alignment`].
    pub fn align(mut self, align: T) -> Self {
        self.align = align;
        self
    }

    #[inline]
    /// Set the space in which its contents will be aligned.
    pub fn bounds(mut self, bounds: Bounds) -> Self {
        self.bounds = bounds;
        self
    }

    #[inline]
    /// See [`AllocateType`]
    pub fn allocate_type(mut self, allocate_type: AllocateType) -> Self {
        self.allocate_type = allocate_type;
        self
    }

    #[inline]
    /// Set the layout of the contents.
    /// If not set, use the layout of the current ui.
    pub fn layout(mut self, layout: Layout) -> Self {
        self.layout = Some(layout);
        self
    }
}

impl<T: Alignment> Aligner<T> {
    /// Show the aligned contents.
    pub fn show<R>(
        self,
        ui: &mut Ui,
        add_contents: impl FnOnce(&mut egui::Ui) -> R,
    ) -> InnerResponse<R> {
        let id = self.id.unwrap_or_else(|| {
            let id = ui.next_auto_id();
            // hold the id
            ui.skip_ahead_auto_ids(1);
            id
        });

        let layout = self.layout.unwrap_or(*ui.layout());

        // calculate the bounds
        let bounds = match self.bounds {
            Bounds::AvailableRect(size) => {
                ui.new_child(UiBuilder::new())
                    .allocate_space(size.min(ui.available_size()))
                    .1
            }
            Bounds::MaxRect(margin) => ui.max_rect() - margin,
        };

        // try to read content size from context memory
        // if not found, use the whole available rect to draw the contents
        let mut memorized = true;
        let content_size = ui.ctx().data(|r| r.get_temp(id)).unwrap_or_else(|| {
            memorized = false;
            bounds.size()
        });

        // get the expected rect
        let expected_rect = self.align.align(content_size, bounds);
        // expand to allow content grow
        let content_rect = resize_layout_rect(expected_rect, bounds.size(), &layout);

        // create child ui
        let mut child_ui = ui.new_child({
            let builder = UiBuilder::new().max_rect(content_rect).layout(layout);

            if memorized {
                builder
            } else {
                // no size memorized, set the pass to sizing pass
                ui.ctx().request_discard("new Aligner");
                builder.sizing_pass().invisible()
            }
        });

        // paint the contents
        let inner = add_contents(&mut child_ui);

        // allocate space and get response
        // when already arranged, even if the content has grown, we allocate the expected size
        // if we allocate the whole new rect, it's actually in the wrong place and could disrupt the layout
        let content_rect = if memorized {
            expected_rect
        } else {
            content_rect
        };
        let response = ui.allocate_rect(
            match self.allocate_type {
                AllocateType::None => Rect::from_min_size(ui.next_widget_position(), Vec2::ZERO),
                AllocateType::Content => content_rect,
                AllocateType::ContentRow => {
                    let min = Pos2::new(bounds.left(), content_rect.top());
                    let max = Pos2::new(bounds.right(), content_rect.bottom());
                    Rect::from_min_max(min, max)
                }
                AllocateType::ContentColumn => {
                    let min = Pos2::new(content_rect.left(), bounds.top());
                    let max = Pos2::new(content_rect.right(), bounds.bottom());
                    Rect::from_min_max(min, max)
                }
                AllocateType::Bounds => bounds,
            },
            Sense::hover(),
        );

        // if the content changed size or not memorized, update the memorized size
        let new_size = child_ui.min_size();
        if new_size != content_size || !memorized {
            ui.ctx().data_mut(|w| w.insert_temp(id, new_size));
        }

        InnerResponse { inner, response }
    }

    #[inline]
    /// Show the contents horizontally.
    pub fn show_horizontal<R>(
        self,
        ui: &mut Ui,
        add_contents: impl FnOnce(&mut Ui) -> R,
    ) -> InnerResponse<R> {
        let layout = if ui.layout().prefer_right_to_left() {
            Layout::right_to_left(Align::Center)
        } else {
            Layout::left_to_right(Align::Center)
        }
        .with_main_wrap(false);

        self.layout(layout).show(ui, add_contents)
    }

    #[inline]
    /// Show the contents horizontally and wrap them when necessary.
    pub fn show_horizontal_wrapped<R>(
        self,
        ui: &mut Ui,
        add_contents: impl FnOnce(&mut Ui) -> R,
    ) -> InnerResponse<R> {
        let layout = if ui.layout().prefer_right_to_left() {
            Layout::right_to_left(Align::Center)
        } else {
            Layout::left_to_right(Align::Center)
        }
        .with_main_wrap(true);

        self.layout(layout).show(ui, add_contents)
    }

    #[inline]
    /// Show the contents vertically.
    pub fn show_vertical<R>(
        self,
        ui: &mut Ui,
        add_contents: impl FnOnce(&mut Ui) -> R,
    ) -> InnerResponse<R> {
        let layout = Layout::top_down(Align::Center);

        self.layout(layout).show(ui, add_contents)
    }
}

#[inline]
/// Center the contents horizontally.
pub fn center_horizontal<R>(
    ui: &mut Ui,
    add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
    let layout = if ui.layout().prefer_right_to_left() {
        Layout::right_to_left(Align::Center)
    } else {
        Layout::left_to_right(Align::Center)
    };

    Aligner::center().layout(layout).show(ui, add_contents)
}

#[inline]
/// Center the contents horizontally and wrap them when necessary.
pub fn center_horizontal_wrapped<R>(
    ui: &mut Ui,
    add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
    let layout = if ui.layout().prefer_right_to_left() {
        Layout::right_to_left(Align::Center)
    } else {
        Layout::left_to_right(Align::Center)
    }
    .with_main_wrap(true);

    Aligner::center().layout(layout).show(ui, add_contents)
}

#[inline]
/// Center the contents vertically.
pub fn center_vertical<R>(
    ui: &mut Ui,
    add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
    Aligner::center()
        .layout(Layout::top_down(Align::Center))
        .show(ui, add_contents)
}

#[inline]
/// Align the contents to the top horizontally.
pub fn top_horizontal<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
    let layout = if ui.layout().prefer_right_to_left() {
        Layout::right_to_left(Align::TOP)
    } else {
        Layout::left_to_right(Align::TOP)
    };

    Aligner::from_align(egui::Align2::CENTER_TOP)
        .layout(layout)
        .show(ui, add_contents)
}

#[inline]
/// Align the contents to the top horizontally and wrap them when necessary.
pub fn top_horizontal_wrapped<R>(
    ui: &mut Ui,
    add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
    let layout = if ui.layout().prefer_right_to_left() {
        Layout::right_to_left(Align::TOP)
    } else {
        Layout::left_to_right(Align::TOP)
    }
    .with_main_wrap(true);

    Aligner::from_align(Align2::CENTER_TOP)
        .layout(layout)
        .show(ui, add_contents)
}

#[inline]
/// Align the contents to the top vertically.
pub fn top_vertical<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
    ui.vertical_centered(add_contents)
}

#[inline]
/// Align the contents to the bottom horizontally.
pub fn bottom_horizontal<R>(
    ui: &mut Ui,
    add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
    let layout = if ui.layout().prefer_right_to_left() {
        Layout::right_to_left(Align::BOTTOM)
    } else {
        Layout::left_to_right(Align::BOTTOM)
    };

    Aligner::from_align(egui::Align2::CENTER_BOTTOM)
        .layout(layout)
        .show(ui, add_contents)
}

#[inline]
/// Align the contents to the bottom horizontally and wrap them when necessary.
pub fn bottom_horizontal_wrapped<R>(
    ui: &mut Ui,
    add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
    let layout = if ui.layout().prefer_right_to_left() {
        Layout::right_to_left(Align::BOTTOM)
    } else {
        Layout::left_to_right(Align::BOTTOM)
    }
    .with_main_wrap(true);

    Aligner::from_align(egui::Align2::CENTER_BOTTOM)
        .layout(layout)
        .show(ui, add_contents)
}

#[inline]
/// Align the contents to the bottom vertically.
pub fn bottom_vertical<R>(
    ui: &mut Ui,
    add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
    ui.with_layout(Layout::bottom_up(Align::Center), add_contents)
}

#[inline]
/// Align the contents to the left horizontally.
pub fn left_horizontal<R>(
    ui: &mut Ui,
    add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
    ui.horizontal_centered(add_contents)
}

#[inline]
/// Align the contents to the left horizontally and wrap them when necessary.
pub fn left_horizontal_wrapped<R>(
    ui: &mut Ui,
    add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
    let layout = Layout::left_to_right(Align::Center).with_main_wrap(true);

    Aligner::from_align(egui::Align2::LEFT_CENTER)
        .layout(layout)
        .show(ui, add_contents)
}

#[inline]
/// Align the contents to the left vertically.
pub fn left_vertical<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
    Aligner::from_align(egui::Align2::LEFT_CENTER)
        .layout(Layout::top_down(Align::Min))
        .show(ui, add_contents)
}

#[inline]
/// Align the contents to the right horizontally.
pub fn right_horizontal<R>(
    ui: &mut Ui,
    add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
    ui.with_layout(Layout::right_to_left(Align::Center), add_contents)
}

#[inline]
/// Align the contents to the right horizontally and wrap them when necessary.
pub fn right_horizontal_wrapped<R>(
    ui: &mut Ui,
    add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
    let layout = Layout::right_to_left(Align::Center).with_main_wrap(true);
    Aligner::from_align(Align2::RIGHT_CENTER)
        .layout(layout)
        .show(ui, add_contents)
}

#[inline]
/// Align the contents to the right vertically.
pub fn right_vertical<R>(ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
    Aligner::from_align(Align2::RIGHT_CENTER)
        .layout(Layout::top_down(Align::Max))
        .show(ui, add_contents)
}