Struct egui_modal::Modal

source ·
pub struct Modal { /* private fields */ }
Expand description

A Modal is created using Modal::new(). Make sure to use a let binding when using Modal::new() to ensure you can call things like Modal::open() later on.

let modal = Modal::new(ctx, "my_modal");
modal.show(|ui| {
    ui.label("Hello world!")
});
if ui.button("modal").clicked() {
    modal.open();
}

Helper functions are also available to use that help apply margins based on the modal’s ModalStyle. They are not necessary to use, but may help reduce boilerplate.

let other_modal = Modal::new(ctx, "another_modal");
other_modal.show(|ui| {
    other_modal.frame(ui, |ui| {
        other_modal.body(ui, "Hello again, world!");
    });
    other_modal.buttons(ui, |ui| {
        other_modal.button(ui, "Close");
    });
});
if ui.button("open the other modal").clicked() {
    other_modal.open();
}

Implementations§

source§

impl Modal

source

pub fn new(ctx: &Context, id_source: impl Display) -> Self

Creates a new Modal. Can use constructor functions like Modal::with_style to modify upon creation.

source

pub fn was_outside_clicked(&self) -> bool

Was the outer overlay clicked this frame?

source

pub fn is_open(&self) -> bool

Is the modal currently open?

source

pub fn open(&self)

Open the modal; make it visible. The modal prevents user input to other parts of the application.

source

pub fn close(&self)

Close the modal so that it is no longer visible, allowing input to flow back into the application.

source

pub fn with_close_on_outside_click(self, do_close_on_click_ouside: bool) -> Self

If set to true, the modal will close itself if the user clicks outside on the modal window (onto the overlay).

source

pub fn with_style(self, style: &ModalStyle) -> Self

Change the ModalStyle of the modal upon creation.

source

pub fn title(&self, ui: &mut Ui, text: impl Into<RichText>)

Helper function for styling the title of the modal.

let modal = Modal::new(ctx, "modal");
modal.show(|ui| {
    modal.title(ui, "my title");
});
source

pub fn icon(&self, ui: &mut Ui, icon: Icon)

Helper function for styling the icon of the modal.

let modal = Modal::new(ctx, "modal");
modal.show(|ui| {
    modal.frame(ui, |ui| {
        modal.icon(ui, Icon::Info);
    });
});
source

pub fn frame<R>(&self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R)

Helper function for styling the container the of body and icon.

let modal = Modal::new(ctx, "modal");
modal.show(|ui| {
    modal.title(ui, "my title");
    modal.frame(ui, |ui| {
        // inner modal contents go here
    });
    modal.buttons(ui, |ui| {
        // button contents go here
    });
});
source

pub fn body_and_icon( &self, ui: &mut Ui, text: impl Into<WidgetText>, icon: Icon )

Helper function that should be used when using a body and icon together.

let modal = Modal::new(ctx, "modal");
modal.show(|ui| {
    modal.frame(ui, |ui| {
        modal.body_and_icon(ui, "my modal body", Icon::Warning);
    });
});
source

pub fn body(&self, ui: &mut Ui, text: impl Into<WidgetText>)

Helper function for styling the body of the modal.

let modal = Modal::new(ctx, "modal");
modal.show(|ui| {
    modal.frame(ui, |ui| {
        modal.body(ui, "my modal body");
    });
});
source

pub fn buttons<R>(&self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R)

Helper function for styling the button container of the modal.

let modal = Modal::new(ctx, "modal");
modal.show(|ui| {
    modal.buttons(ui, |ui| {
        modal.button(ui, "my modal button");
    });
});
source

pub fn button(&self, ui: &mut Ui, text: impl Into<WidgetText>) -> Response

Helper function for creating a normal button for the modal. Automatically closes the modal on click.

source

pub fn caution_button( &self, ui: &mut Ui, text: impl Into<WidgetText> ) -> Response

Helper function for creating a “cautioned” button for the modal. Automatically closes the modal on click.

source

pub fn suggested_button( &self, ui: &mut Ui, text: impl Into<WidgetText> ) -> Response

Helper function for creating a “suggested” button for the modal. Automatically closes the modal on click.

source

pub fn show<R>(&self, add_contents: impl FnOnce(&mut Ui) -> R)

The ui contained in this function will be shown within the modal window. The modal will only actually show when Modal::open is used.

source

pub fn open_dialog( &self, title: Option<impl Display>, body: Option<impl Display>, icon: Option<Icon> )

👎Deprecated since 0.3.0: use Modal::dialog

Open the modal as a dialog. This is a shorthand way of defining a Modal::show once, for example, if a function returns an Error. This should be used in conjunction with Modal::show_dialog.

source

pub fn dialog(&self) -> DialogBuilder

Create a DialogBuilder for this modal. Make sure to use DialogBuilder::open to open the dialog.

source

pub fn show_dialog(&mut self)

Needed in order to use Modal::dialog. Make sure this is called every frame, as it renders the necessary ui when using a modal as a dialog.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Modal

§

impl Send for Modal

§

impl Sync for Modal

§

impl Unpin for Modal

§

impl !UnwindSafe for Modal

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.