egui::containers::frame

Struct Frame

source
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 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.

§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

§Constructors
source

pub const NONE: Self = _

No colors, no margins, no border.

This is also the default.

source

pub const fn new() -> Self

No colors, no margins, no border.

Same as Frame::NONE.

source

pub const fn none() -> Self

👎Deprecated: Use Frame::NONE or Frame::new() instead.
source

pub fn group(style: &Style) -> Self

For when you want to group a few widgets together within a frame.

source

pub fn side_top_panel(style: &Style) -> Self

source

pub fn central_panel(style: &Style) -> Self

source

pub fn window(style: &Style) -> Self

source

pub fn menu(style: &Style) -> Self

source

pub fn popup(style: &Style) -> Self

source

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.

source

pub fn dark_canvas(style: &Style) -> Self

A dark canvas to draw on.

source§

impl Frame

§Builders
source

pub fn inner_margin(self, inner_margin: impl Into<Margin>) -> Self

Margin within the painted frame.

Known as padding in CSS.

source

pub fn fill(self, fill: Color32) -> Self

The background fill color of the frame, within the Self::stroke.

Known as background in CSS.

source

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.

source

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.

source

pub fn rounding(self, corner_radius: impl Into<CornerRadius>) -> Self

👎Deprecated: Renamed to 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.

source

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.

source

pub fn shadow(self, shadow: Shadow) -> Self

Optional drop-shadow behind the frame.

source

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

§Inspectors
source

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.

source

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).

source

pub fn widget_rect(&self, content_rect: Rect) -> Rect

Calculate the widget_rect from the content_rect.

This is the visible and interactive rectangle.

source

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

source

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.

source

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.

source

pub fn show_dyn<'c, R>( self, ui: &mut Ui, add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>, ) -> InnerResponse<R>

Show using dynamic dispatch.

source

pub fn paint(&self, content_rect: Rect) -> Shape

Paint this frame as a shape.

Trait Implementations§

source§

impl Clone for Frame

source§

fn clone(&self) -> Frame

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Frame

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Frame

source§

fn default() -> Frame

Returns the “default value” for a type. Read more
source§

impl PartialEq for Frame

source§

fn eq(&self, other: &Frame) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Widget for &mut Frame

source§

fn ui(self, ui: &mut Ui) -> Response

Allocate space, interact, paint, and return a Response. Read more
source§

impl Copy for Frame

source§

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> SerializableAny for T
where T: 'static + Any + Clone + for<'a> Send + Sync,