Skip to content

Commit

Permalink
implement style field for setting default width of modal
Browse files Browse the repository at this point in the history
  • Loading branch information
n00kii committed Jan 21, 2023
1 parent b06ec78 commit b984844
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
30 changes: 30 additions & 0 deletions examples/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,36 @@ impl eframe::App for ExampleApp {
ui.text_edit_singleline(&mut self.modal_body);
ui.end_row();

// let mut has_height = self.modal_style.default_height.is_some();
let mut has_width = self.modal_style.default_width.is_some();
// if ui.checkbox(&mut has_height, "default height").changed() {
// if has_height {
// self.modal_style.default_height = Some(100.)
// } else {
// self.modal_style.default_height = None
// }
// }
// if let Some(modal_height) = self.modal_style.default_height.as_mut() {
// let modal_height =
// DragValue::new(modal_height).clamp_range(0..=1000);
// ui.add_sized(ui.available_rect_before_wrap().size(), modal_height);
// }
// ui.end_row();

if ui.checkbox(&mut has_width, "default width").changed() {
if has_width {
self.modal_style.default_width = Some(100.)
} else {
self.modal_style.default_width = None
}
}
if let Some(modal_width) = self.modal_style.default_width.as_mut() {
let modal_width =
DragValue::new(modal_width).clamp_range(0..=1000);
ui.add_sized(ui.available_rect_before_wrap().size(), modal_width);
}
ui.end_row();

ui.label("body margin");
let body_margin =
DragValue::new(&mut self.modal_style.body_margin).clamp_range(0..=20);
Expand Down
38 changes: 30 additions & 8 deletions src/modal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ struct ModalState {
modal_type: ModalType,
}

#[derive(Clone)]
#[derive(Clone, Debug)]
/// Contains styling parameters for the modal, like body margin
/// and button colors.
pub struct ModalStyle {
Expand Down Expand Up @@ -119,6 +119,11 @@ pub struct ModalStyle {
/// The color of the error icon
pub error_icon_color: Color32,

/// The default width of the modal
pub default_width: Option<f32>,
/// The default height of the modal
// pub default_height: Option<f32>,

/// The alignment of text inside the body
pub body_alignment: Align,
}
Expand Down Expand Up @@ -164,6 +169,9 @@ impl Default for ModalStyle {
success_icon_color: SUCCESS_ICON_COLOR,
error_icon_color: ERROR_ICON_COLOR,

// default_height: None,
default_width: None,

body_alignment: Align::Min,
}
}
Expand Down Expand Up @@ -213,12 +221,13 @@ impl Modal {
/// Creates a new [`Modal`]. Can use constructor functions like [`Modal::with_style`]
/// to modify upon creation.
pub fn new(ctx: &Context, id_source: impl std::fmt::Display) -> Self {
let self_id = Id::new(id_source.to_string());
Self {
id: Id::new(id_source.to_string()),
window_id: self_id.with("window"),
id: self_id,
style: ModalStyle::default(),
ctx: ctx.clone(),
close_on_outside_click: false,
window_id: Id::new("window_".to_string() + &id_source.to_string()),
}
}

Expand All @@ -233,13 +242,13 @@ impl Modal {
modal_state.was_outside_clicked = was_clicked;
modal_state.save(&self.ctx, self.id)
}

/// Was the outer overlay clicked this frame?
pub fn was_outside_clicked(&self) -> bool {
let modal_state = ModalState::load(&self.ctx, self.id);
modal_state.was_outside_clicked
}

/// Is the modal currently open?
pub fn is_open(&self) -> bool {
let modal_state = ModalState::load(&self.ctx, self.id);
Expand Down Expand Up @@ -453,13 +462,26 @@ impl Modal {
);
});

let window = Window::new("")
.id(self.window_id)
// the below lines of code addresses a weird problem where if the default_height changes, egui doesnt respond unless
// it's a different window id
let window_id = self.style.default_width.map_or(self.window_id, |w| self.window_id.with(w.to_string()));
// window_id = self.style.default_height.map_or(window_id, |h| window_id.with(h.to_string()));

let mut window = Window::new("")
.id(window_id)
.open(&mut modal_state.is_open)
.title_bar(false)
.anchor(Align2::CENTER_CENTER, [0., 0.])
.resizable(false);


// if let Some(default_height) = self.style.default_height {
// window = window.min_height(default_height);
// }

if let Some(default_width) = self.style.default_width {
window = window.default_width(default_width);
}

let response = window.show(&ctx_clone, add_contents);
if let Some(inner_response) = response {
ctx_clone.move_to_top(inner_response.response.layer_id);
Expand Down

0 comments on commit b984844

Please sign in to comment.