1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-26 22:53:14 -04:00
Files
egui/crates/egui_demo_lib/src/demo/frame_demo.rs
Emil Ernerfeldt 6607cd95f9 ⚠️ Frame now includes stroke width as part of padding (#5575)
* Part of https://github.com/emilk/egui/issues/4019

`Frame` now includes the width of the stroke as part of its size. From
the new docs:

### `Frame` docs
The total (outer) size of a frame is `content_size + inner_margin +
2*stroke.width + outer_margin`.

Everything within the stroke is filled with the fill color (if any).

```text
+-----------------^-------------------------------------- -+
|                 | outer_margin                           |
|    +------------v----^------------------------------+    |
|    |                 | stroke width                 |    |
|    |    +------------v---^---------------------+    |    |
|    |    |                | inner_margin        |    |    |
|    |    |    +-----------v----------------+    |    |    |
|    |    |    |             ^              |    |    |    |
|    |    |    |             |              |    |    |    |
|    |    |    |<------ content_size ------>|    |    |    |
|    |    |    |             |              |    |    |    |
|    |    |    |             v              |    |    |    |
|    |    |    +------- content_rect -------+    |    |    |
|    |    |                                      |    |    |
|    |    +-------------fill_rect ---------------+    |    |
|    |                                                |    |
|    +----------------- widget_rect ------------------+    |
|                                                          |
+---------------------- outer_rect ------------------------+
```

The four rectangles, from inside to outside, are:
* `content_rect`: the rectangle that is made available to the inner
[`Ui`] or widget.
* `fill_rect`: the rectangle that is filled with the fill color (inside
the stroke, if any).
* `widget_rect`: is the interactive part of the widget (what sense
clicks etc).
* `outer_rect`: what is allocated in the outer [`Ui`], and is what is
returned by [`Response::rect`].

### Notes
This required rewriting a lot of the layout code for `egui::Window`,
which was a massive pain. But now the window margin and stroke width is
properly accounted for everywhere.
2025-01-04 10:29:22 +01:00

74 lines
2.3 KiB
Rust

/// Shows off a table with dynamic layout
#[derive(PartialEq)]
pub struct FrameDemo {
frame: egui::Frame,
}
impl Default for FrameDemo {
fn default() -> Self {
Self {
frame: egui::Frame::new()
.inner_margin(12)
.outer_margin(24)
.rounding(14)
.shadow(egui::Shadow {
offset: [8, 12],
blur: 16,
spread: 0,
color: egui::Color32::from_black_alpha(180),
})
.fill(egui::Color32::from_rgba_unmultiplied(97, 0, 255, 128))
.stroke(egui::Stroke::new(1.0, egui::Color32::GRAY)),
}
}
}
impl crate::Demo for FrameDemo {
fn name(&self) -> &'static str {
"▣ Frame"
}
fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.resizable(false)
.show(ctx, |ui| {
use crate::View as _;
self.ui(ui);
});
}
}
impl crate::View for FrameDemo {
fn ui(&mut self, ui: &mut egui::Ui) {
ui.horizontal(|ui| {
ui.vertical(|ui| {
ui.add(&mut self.frame);
ui.add_space(8.0);
ui.set_max_width(ui.min_size().x);
ui.vertical_centered(|ui| egui::reset_button(ui, self, "Reset"));
});
ui.separator();
ui.vertical(|ui| {
// We want to paint a background around the outer margin of the demonstration frame, so we use another frame around it:
egui::Frame::default()
.stroke(ui.visuals().widgets.noninteractive.bg_stroke)
.rounding(ui.visuals().widgets.noninteractive.rounding)
.show(ui, |ui| {
self.frame.show(ui, |ui| {
ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend);
ui.label(egui::RichText::new("Content").color(egui::Color32::WHITE));
});
});
});
});
ui.set_max_width(ui.min_size().x);
ui.separator();
ui.vertical_centered(|ui| ui.add(crate::egui_github_link_file!()));
}
}