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
impl Modal
sourcepub fn new(ctx: &Context, id_source: impl Display) -> Self
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.
sourcepub fn was_outside_clicked(&self) -> bool
pub fn was_outside_clicked(&self) -> bool
Was the outer overlay clicked this frame?
sourcepub fn open(&self)
pub fn open(&self)
Open the modal; make it visible. The modal prevents user input to other parts of the application.
⚠️ WARNING ⚠️: This function requires a write lock to the egui::Context
. Using it within
closures within functions like egui::Ui::input_mut
will result in a deadlock. Tracking issue
sourcepub fn close(&self)
pub fn close(&self)
Close the modal so that it is no longer visible, allowing input to flow back into the application.
⚠️ WARNING ⚠️: This function requires a write lock to the egui::Context
. Using it within
closures within functions like egui::Ui::input_mut
will result in a deadlock. Tracking issue
sourcepub fn with_close_on_outside_click(self, do_close_on_click_ouside: bool) -> Self
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).
sourcepub fn with_style(self, style: &ModalStyle) -> Self
pub fn with_style(self, style: &ModalStyle) -> Self
Change the ModalStyle
of the modal upon creation.
sourcepub fn title(&self, ui: &mut Ui, text: impl Into<RichText>)
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");
});
sourcepub fn icon(&self, ui: &mut Ui, icon: Icon)
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);
});
});
sourcepub fn frame<R>(&self, ui: &mut Ui, add_contents: impl FnOnce(&mut Ui) -> R)
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
});
});
sourcepub fn body_and_icon(
&self,
ui: &mut Ui,
text: impl Into<WidgetText>,
icon: Icon,
)
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);
});
});
sourcepub fn body(&self, ui: &mut Ui, text: impl Into<WidgetText>)
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");
});
});
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");
});
});
Helper function for creating a normal button for the modal. Automatically closes the modal on click.
Helper function for creating a “cautioned” button for the modal. Automatically closes the modal on click.
Helper function for creating a “suggested” button for the modal. Automatically closes the modal on click.
sourcepub fn show<R>(&self, add_contents: impl FnOnce(&mut Ui) -> R)
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.
sourcepub fn open_dialog(
&self,
title: Option<impl Display>,
body: Option<impl Display>,
icon: Option<Icon>,
)
👎Deprecated since 0.3.0: use Modal::dialog
pub fn open_dialog( &self, title: Option<impl Display>, body: Option<impl Display>, icon: Option<Icon>, )
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
.
sourcepub fn dialog(&self) -> DialogBuilder
pub fn dialog(&self) -> DialogBuilder
Create a DialogBuilder
for this modal. Make sure to use DialogBuilder::open
to open the dialog.
sourcepub fn show_dialog(&mut self)
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.