Skip to main content

Widget

Trait Widget 

Source
pub trait Widget {
    // Required method
    fn ui(self, ui: &mut Ui) -> Response;

    // Provided method
    fn boxed<'a>(self) -> BoxedWidget<'a>
       where Self: Sized + 'a { ... }
}
Expand description

Anything implementing Widget can be added to a Ui with Ui::add.

Button, Label, Slider, etc all implement the Widget trait.

You only need to implement Widget if you care about being able to do ui.add(your_widget);.

Note that the widgets (Button, TextEdit etc) are builders, and not objects that hold state.

Tip: you can impl Widget for &mut YourThing { }.

|ui: &mut Ui| -> Response { … } also implements Widget.

Required Methods§

Source

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

Allocate space, interact, paint, and return a Response.

Note that this consumes self. This is because most widgets (Button, TextEdit etc) are builders

Tip: you can impl Widget for &mut YourObject { }.

Provided Methods§

Source

fn boxed<'a>(self) -> BoxedWidget<'a>
where Self: Sized + 'a,

Box this widget for dynamic dispatch.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Widget for &PaintStats

Source§

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

Source§

impl Widget for &mut TessellationOptions

Source§

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

Implementors§

Source§

impl Widget for &mut CornerRadius

Source§

impl Widget for &mut FontTweak

Source§

impl Widget for &mut Frame

Source§

impl Widget for &mut Margin

Source§

impl Widget for &mut Shadow

Source§

impl Widget for &mut Stroke

Source§

impl Widget for AtomLayout<'_>

Source§

impl Widget for Button<'_>

Source§

impl Widget for Checkbox<'_>

Source§

impl Widget for DragValue<'_>

Source§

impl Widget for Image<'_>

Source§

impl Widget for Label

Source§

impl Widget for ProgressBar

Source§

impl Widget for RadioButton<'_>

Source§

impl Widget for Separator

Source§

impl Widget for Slider<'_>

Source§

impl Widget for Spinner

Source§

impl Widget for TextEdit<'_>

Source§

impl<F> Widget for F
where F: FnOnce(&mut Ui) -> Response,

This enables functions that return impl Widget, so that you can create a widget by just returning a lambda from a function.

For instance: ui.add(slider_vec2(&mut vec2)); with:

pub fn slider_vec2(value: &mut egui::Vec2) -> impl egui::Widget + '_ {
   move |ui: &mut egui::Ui| {
       ui.horizontal(|ui| {
           ui.add(egui::Slider::new(&mut value.x, 0.0..=1.0).text("x"));
           ui.add(egui::Slider::new(&mut value.y, 0.0..=1.0).text("y"));
       })
       .response
   }
}