ui: fix modal width detection

This commit is contained in:
ardocrat 2023-06-02 10:04:54 +03:00
parent 7c1699a658
commit 532a79fc47
3 changed files with 9 additions and 17 deletions

View file

@ -55,7 +55,7 @@ impl App {
frame: &mut eframe::Frame, frame: &mut eframe::Frame,
cb: &dyn PlatformCallbacks) { cb: &dyn PlatformCallbacks) {
let location = ModalLocation::Global; let location = ModalLocation::Global;
Navigator::modal_ui(ui, frame, location, |ui, frame, modal| { Navigator::modal_ui(ui, location, |ui, modal| {
match modal.id { match modal.id {
ModalId::Exit => { ModalId::Exit => {
if self.show_exit_progress { if self.show_exit_progress {

View file

@ -179,10 +179,8 @@ impl Navigator {
/// Show [Modal] with provided location at app UI. /// Show [Modal] with provided location at app UI.
pub fn modal_ui(ui: &mut egui::Ui, pub fn modal_ui(ui: &mut egui::Ui,
frame: &mut eframe::Frame,
location: ModalLocation, location: ModalLocation,
add_content: impl FnOnce(&mut egui::Ui, &mut eframe::Frame, &Modal)) { add_content: impl FnOnce(&mut egui::Ui, &Modal)) {
let r_nav = NAVIGATOR_STATE.read().unwrap(); let r_nav = NAVIGATOR_STATE.read().unwrap();
let modal = match location { let modal = match location {
ModalLocation::Global => { &r_nav.global_modal } ModalLocation::Global => { &r_nav.global_modal }
@ -190,7 +188,7 @@ impl Navigator {
ModalLocation::Screen => { &r_nav.screen_modal } ModalLocation::Screen => { &r_nav.screen_modal }
}; };
if modal.is_some() { if modal.is_some() {
modal.as_ref().unwrap().ui(ui, frame, add_content); modal.as_ref().unwrap().ui(ui, add_content);
} }
} }

View file

@ -118,11 +118,8 @@ impl Modal {
} }
/// Show [`Modal`] with provided content. /// Show [`Modal`] with provided content.
pub fn ui(&self, pub fn ui(&self, ui: &mut egui::Ui, add_content: impl FnOnce(&mut egui::Ui, &Modal)) {
ui: &mut egui::Ui, let width = min(ui.available_width() as i64 - 20, Self::DEFAULT_WIDTH) as f32;
frame: &mut eframe::Frame,
add_content: impl FnOnce(&mut egui::Ui, &mut eframe::Frame, &Modal)) {
let cw = min(frame.info().window_info.size.x as i64 - 20, Self::DEFAULT_WIDTH) as f32;
// Show background Window at full available size // Show background Window at full available size
egui::Window::new(self.window_id(true)) egui::Window::new(self.window_id(true))
@ -144,7 +141,7 @@ impl Modal {
.title_bar(false) .title_bar(false)
.resizable(false) .resizable(false)
.collapsible(false) .collapsible(false)
.default_width(cw) .default_width(width)
.anchor(self.modal_position(), Vec2::default()) .anchor(self.modal_position(), Vec2::default())
.frame(egui::Frame { .frame(egui::Frame {
rounding: Rounding::same(8.0), rounding: Rounding::same(8.0),
@ -155,7 +152,7 @@ impl Modal {
if self.title.is_some() { if self.title.is_some() {
self.draw_title(ui); self.draw_title(ui);
} }
self.draw_content(ui, frame, add_content); self.draw_content(ui, add_content);
}).unwrap().response.layer_id; }).unwrap().response.layer_id;
// Always show main content Window above background Window // Always show main content Window above background Window
@ -187,10 +184,7 @@ impl Modal {
} }
/// Draw provided content. /// Draw provided content.
fn draw_content(&self, fn draw_content(&self, ui: &mut egui::Ui, add_content: impl FnOnce(&mut egui::Ui, &Modal)) {
ui: &mut egui::Ui,
frame: &mut eframe::Frame,
add_content: impl FnOnce(&mut egui::Ui, &mut eframe::Frame, &Modal)) {
let mut rect = ui.available_rect_before_wrap(); let mut rect = ui.available_rect_before_wrap();
rect.min += egui::emath::vec2(6.0, 0.0); rect.min += egui::emath::vec2(6.0, 0.0);
rect.max -= egui::emath::vec2(6.0, 0.0); rect.max -= egui::emath::vec2(6.0, 0.0);
@ -216,7 +210,7 @@ impl Modal {
// Draw main content. // Draw main content.
let mut content_rect = ui.allocate_ui_at_rect(rect, |ui| { let mut content_rect = ui.allocate_ui_at_rect(rect, |ui| {
(add_content)(ui, frame, self); (add_content)(ui, self);
}).response.rect; }).response.rect;
// Setup background shape to be painted behind main content. // Setup background shape to be painted behind main content.