pub struct Frame {
pub inner_margin: Margin,
pub fill: Color32,
pub stroke: Stroke,
pub corner_radius: CornerRadius,
pub outer_margin: Margin,
pub shadow: Shadow,
}
Expand description
A frame around some content, including margin, colors, etc.
§Definitions
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).
+-----------------^-------------------------------------- -+
| | 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 innerUi
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 outerUi
, and is what is returned byResponse::rect
.
§Usage
egui::Frame::none()
.fill(egui::Color32::RED)
.show(ui, |ui| {
ui.label("Label with red background");
});
§Dynamic color
If you want to change the color of the frame based on the response of the widget, you need to break it up into multiple steps:
let mut frame = egui::Frame::default().inner_margin(4.0).begin(ui);
{
let response = frame.content_ui.label("Inside the frame");
if response.hovered() {
frame.frame.fill = egui::Color32::RED;
}
}
frame.end(ui); // Will "close" the frame.
You can also respond to the hovering of the frame itself:
let mut frame = egui::Frame::default().inner_margin(4.0).begin(ui);
{
frame.content_ui.label("Inside the frame");
frame.content_ui.label("This too");
}
let response = frame.allocate_space(ui);
if response.hovered() {
frame.frame.fill = egui::Color32::RED;
}
frame.paint(ui);
Note that you cannot change the margins after calling begin
.
Fields§
§inner_margin: Margin
Margin within the painted frame.
Known as padding
in CSS.
fill: Color32
The background fill color of the frame, within the Self::stroke
.
Known as background
in CSS.
stroke: Stroke
The width and color of the outline around the frame.
The width of the stroke is part of the total margin/padding of the frame.
corner_radius: CornerRadius
The rounding of the outer corner of the Self::stroke
(or, if there is no stroke, the outer corner of Self::fill
).
In other words, this is the corner radius of the widget rect.
outer_margin: Margin
Margin outside the painted frame.
Similar to what is called margin
in CSS.
However, egui does NOT do “Margin Collapse” like in CSS,
i.e. when placing two frames next to each other,
the distance between their borders is the SUM
of their other margins.
In CSS the distance would be the MAX of their outer margins.
Supporting margin collapse is difficult, and would
requires complicating the already complicated egui layout code.
Consider using crate::Spacing::item_spacing
for adding space between widgets.
shadow: Shadow
Optional drop-shadow behind the frame.
Implementations§
source§impl Frame
impl Frame
§Constructors
sourcepub const fn new() -> Self
pub const fn new() -> Self
No colors, no margins, no border.
Same as Frame::NONE
.
pub const fn none() -> Self
Frame::NONE
or Frame::new()
instead.sourcepub fn group(style: &Style) -> Self
pub fn group(style: &Style) -> Self
For when you want to group a few widgets together within a frame.
pub fn side_top_panel(style: &Style) -> Self
pub fn central_panel(style: &Style) -> Self
pub fn window(style: &Style) -> Self
pub fn popup(style: &Style) -> Self
sourcepub fn canvas(style: &Style) -> Self
pub fn canvas(style: &Style) -> Self
A canvas to draw on.
In bright mode this will be very bright, and in dark mode this will be very dark.
sourcepub fn dark_canvas(style: &Style) -> Self
pub fn dark_canvas(style: &Style) -> Self
A dark canvas to draw on.
source§impl Frame
impl Frame
§Builders
sourcepub fn inner_margin(self, inner_margin: impl Into<Margin>) -> Self
pub fn inner_margin(self, inner_margin: impl Into<Margin>) -> Self
Margin within the painted frame.
Known as padding
in CSS.
sourcepub fn fill(self, fill: Color32) -> Self
pub fn fill(self, fill: Color32) -> Self
The background fill color of the frame, within the Self::stroke
.
Known as background
in CSS.
sourcepub fn stroke(self, stroke: impl Into<Stroke>) -> Self
pub fn stroke(self, stroke: impl Into<Stroke>) -> Self
The width and color of the outline around the frame.
The width of the stroke is part of the total margin/padding of the frame.
sourcepub fn corner_radius(self, corner_radius: impl Into<CornerRadius>) -> Self
pub fn corner_radius(self, corner_radius: impl Into<CornerRadius>) -> Self
The rounding of the outer corner of the Self::stroke
(or, if there is no stroke, the outer corner of Self::fill
).
In other words, this is the corner radius of the widget rect.
sourcepub fn rounding(self, corner_radius: impl Into<CornerRadius>) -> Self
👎Deprecated: Renamed to corner_radius
pub fn rounding(self, corner_radius: impl Into<CornerRadius>) -> Self
corner_radius
The rounding of the outer corner of the Self::stroke
(or, if there is no stroke, the outer corner of Self::fill
).
In other words, this is the corner radius of the widget rect.
sourcepub fn outer_margin(self, outer_margin: impl Into<Margin>) -> Self
pub fn outer_margin(self, outer_margin: impl Into<Margin>) -> Self
Margin outside the painted frame.
Similar to what is called margin
in CSS.
However, egui does NOT do “Margin Collapse” like in CSS,
i.e. when placing two frames next to each other,
the distance between their borders is the SUM
of their other margins.
In CSS the distance would be the MAX of their outer margins.
Supporting margin collapse is difficult, and would
requires complicating the already complicated egui layout code.
Consider using crate::Spacing::item_spacing
for adding space between widgets.
sourcepub fn multiply_with_opacity(self, opacity: f32) -> Self
pub fn multiply_with_opacity(self, opacity: f32) -> Self
Opacity multiplier in gamma space.
For instance, multiplying with 0.5
will make the frame half transparent.
source§impl Frame
impl Frame
§Inspectors
sourcepub fn total_margin(&self) -> Marginf
pub fn total_margin(&self) -> Marginf
How much extra space the frame uses up compared to the content.
Self::inner_margin
+ [Self.stroke
].width
+ Self::outer_margin
.
sourcepub fn fill_rect(&self, content_rect: Rect) -> Rect
pub fn fill_rect(&self, content_rect: Rect) -> Rect
Calculate the fill_rect
from the content_rect
.
This is the rectangle that is filled with the fill color (inside the stroke, if any).
sourcepub fn widget_rect(&self, content_rect: Rect) -> Rect
pub fn widget_rect(&self, content_rect: Rect) -> Rect
Calculate the widget_rect
from the content_rect
.
This is the visible and interactive rectangle.
sourcepub fn outer_rect(&self, content_rect: Rect) -> Rect
pub fn outer_rect(&self, content_rect: Rect) -> Rect
Calculate the outer_rect
from the content_rect
.
This is what is allocated in the outer Ui
, and is what is returned by Response::rect
.
source§impl Frame
impl Frame
sourcepub fn begin(self, ui: &mut Ui) -> Prepared
pub fn begin(self, ui: &mut Ui) -> Prepared
Begin a dynamically colored frame.
This is a more advanced API.
Usually you want to use Self::show
instead.
See docs for Frame
for an example.
sourcepub fn show<R>(
self,
ui: &mut Ui,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R>
pub fn show<R>( self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R, ) -> InnerResponse<R>
Show the given ui surrounded by this frame.
Trait Implementations§
impl Copy for Frame
impl StructuralPartialEq for Frame
Auto Trait Implementations§
impl Freeze for Frame
impl RefUnwindSafe for Frame
impl Send for Frame
impl Sync for Frame
impl Unpin for Frame
impl UnwindSafe for Frame
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)