Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change modal position to a fixed vertical distance from the top of the window #4700

Merged
merged 3 commits into from
Jan 5, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions crates/re_ui/src/modal.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use egui::NumExt;

/// Helper object to handle a [`Modal`] window.
///
/// A [`Modal`] is typically held only so long as it is displayed, so it's typically stored in an
Expand Down Expand Up @@ -74,6 +76,25 @@ pub struct ModalResponse<R> {
/// [`Modal`] fakes as a modal window, since egui [doesn't have them yet](https://github.com/emilk/egui/issues/686).
/// This done by dimming the background and capturing clicks outside the window.
///
/// The positioning of the modal is as follows:
///
/// ```text
/// ┌─rerun window─────▲─────────────────────┐
/// │ │ 75px / 10% │
/// │ ╔═modal═▼══════════╗ ▲ │
/// │ ║ ▲ ║ │ │
/// │ ║ actual height │ ║ │ │
/// │ ║ based on │ ║ │ max │
/// │ ║ content │ ║ │ height│
/// │ ║ │ ║ │ │
/// │ ║ ▼ ║ │ │
/// │ ╚══════════════════╝ │ │
/// │ │ │ │ │
/// │ └───────▲──────────┘ ▼ │
/// │ │ 75px / 10% │
/// └──────────────────▼─────────────────────┘
/// ```
///
/// Note that [`Modal`] are typically used via the [`ModalHandler`] helper object to reduce boilerplate.
pub struct Modal {
title: String,
Expand Down Expand Up @@ -109,10 +130,16 @@ impl Modal {

let mut open = ui.input(|i| !i.key_pressed(egui::Key::Escape));

let screen_height = ui.ctx().screen_rect().height();
let modal_vertical_margins = (75.0).at_most(screen_height * 0.1);

let mut window = egui::Window::new(&self.title)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut window = egui::Window::new(&self.title)
let mut window = egui::Window::new(&self.title)
.id(egui::Id::new("modal", &self.title)) // Workaround for https://github.com/emilk/egui/pull/3721 until we make a new egui release

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works (well—to the extent that it invalidates any existing cache).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the bug was only when changing pivot, right ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's correct. So we should be good.

.pivot(egui::Align2::CENTER_CENTER)
.fixed_pos(ui.ctx().screen_rect().center())
.pivot(egui::Align2::CENTER_TOP)
.fixed_pos(
ui.ctx().screen_rect().center_top() + egui::vec2(0.0, modal_vertical_margins),
)
.constrain_to(ui.ctx().screen_rect())
.max_height(screen_height - 2.0 * modal_vertical_margins)
.collapsible(false)
.resizable(true)
.frame(egui::Frame {
Expand Down
Loading