diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index abaf120b6..ec64c211f 100644 --- a/crates/egui-winit/src/lib.rs +++ b/crates/egui-winit/src/lib.rs @@ -16,7 +16,7 @@ pub use accesskit_winit; pub use egui; #[cfg(feature = "accesskit")] use egui::accesskit; -use egui::{mutex::RwLock, Pos2, ViewportBuilder, ViewportCommand, ViewportId}; +use egui::{mutex::RwLock, ViewportBuilder, ViewportCommand, ViewportId}; pub use winit; pub mod clipboard; @@ -176,16 +176,16 @@ impl State { // This solves an issue where egui window positions would be changed when minimizing on Windows. let screen_size_in_pixels = screen_size_in_pixels(window); let screen_size_in_points = screen_size_in_pixels / pixels_per_point; - self.egui_input.screen_rect = if !window.is_minimized().unwrap_or_else(|| { + + let getting_info = !window.is_minimized().unwrap_or_else(|| { eprintln!("Cannot determine the Viewport/native window minimized state"); true - }) { - Some(egui::Rect::from_min_max( - window - .outer_position() - .map(|pos| Pos2::new(pos.x as f32, pos.y as f32)) - .unwrap_or(Pos2::ZERO), - screen_size_in_points.to_pos2(), + }); + + self.egui_input.screen_rect = if getting_info { + Some(egui::Rect::from_min_size( + egui::Pos2::ZERO, + screen_size_in_points, )) } else { None diff --git a/crates/egui/src/containers/area.rs b/crates/egui/src/containers/area.rs index ffa99219e..f493c20cd 100644 --- a/crates/egui/src/containers/area.rs +++ b/crates/egui/src/containers/area.rs @@ -376,7 +376,7 @@ impl Prepared { } pub(crate) fn content_ui(&self, ctx: &Context) -> Ui { - let screen_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max); + let screen_rect = ctx.screen_rect(); let bounds = if let Some(bounds) = self.drag_bounds { bounds.intersect(screen_rect) // protect against infinite bounds diff --git a/crates/egui/src/containers/panel.rs b/crates/egui/src/containers/panel.rs index a7e562a46..f5267e0f0 100644 --- a/crates/egui/src/containers/panel.rs +++ b/crates/egui/src/containers/panel.rs @@ -335,7 +335,7 @@ impl SidePanel { let layer_id = LayerId::background(); let side = self.side; let available_rect = ctx.available_rect(); - let clip_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max); + let clip_rect = ctx.screen_rect(); let mut panel_ui = Ui::new(ctx.clone(), layer_id, self.id, available_rect, clip_rect); let inner_response = self.show_inside_dyn(&mut panel_ui, add_contents); @@ -790,7 +790,7 @@ impl TopBottomPanel { let available_rect = ctx.available_rect(); let side = self.side; - let clip_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max); + let clip_rect = ctx.screen_rect(); let mut panel_ui = Ui::new(ctx.clone(), layer_id, self.id, available_rect, clip_rect); let inner_response = self.show_inside_dyn(&mut panel_ui, add_contents); @@ -1047,7 +1047,7 @@ impl CentralPanel { let layer_id = LayerId::background(); let id = Id::new("central_panel"); - let clip_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max); + let clip_rect = ctx.screen_rect(); let mut panel_ui = Ui::new(ctx.clone(), layer_id, id, available_rect, clip_rect); let inner_response = self.show_inside_dyn(&mut panel_ui, add_contents); diff --git a/crates/egui/src/containers/popup.rs b/crates/egui/src/containers/popup.rs index 672dcc38d..70b03966a 100644 --- a/crates/egui/src/containers/popup.rs +++ b/crates/egui/src/containers/popup.rs @@ -206,7 +206,7 @@ fn show_tooltip_at_avoid_dyn<'c, R>( } } - let position = position.at_least(Pos2::ZERO); + let position = position.at_least(ctx.screen_rect().min); let area_id = frame_state.common_id.with(frame_state.count); @@ -262,7 +262,7 @@ fn show_tooltip_area_dyn<'c, R>( .fixed_pos(window_pos) .constrain(true) .interactable(false) - .drag_bounds(Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max)) + .drag_bounds(ctx.screen_rect()) .show(ctx, |ui| { Frame::popup(&ctx.style()) .show(ui, |ui| { diff --git a/crates/egui/src/containers/resize.rs b/crates/egui/src/containers/resize.rs index 83cdeeb56..2f71a57c0 100644 --- a/crates/egui/src/containers/resize.rs +++ b/crates/egui/src/containers/resize.rs @@ -183,7 +183,7 @@ impl Resize { .at_least(self.min_size) .at_most(self.max_size) .at_most( - ui.ctx().screen_rect().max.to_vec2() - ui.spacing().window_margin.sum(), // hack for windows + ui.ctx().screen_rect().size() - ui.spacing().window_margin.sum(), // hack for windows ); State { diff --git a/crates/egui/src/containers/window.rs b/crates/egui/src/containers/window.rs index 1040e48db..c12e9a139 100644 --- a/crates/egui/src/containers/window.rs +++ b/crates/egui/src/containers/window.rs @@ -436,7 +436,7 @@ impl<'open> Window<'open> { let mut resize = resize.id(resize_id); let mut area = area.begin(ctx); - let win_size = ctx.screen_rect().max.to_vec2(); + let win_size = ctx.input(|i| i.screen_rect.size()); area.state_mut().set_left_top_pos(Pos2::ZERO); area.state_mut().size = win_size; let title_content_spacing = 2.0 * ctx.style().spacing.item_spacing.y; @@ -893,7 +893,7 @@ impl<'open> Window<'open> { let mut resize = resize.id(resize_id); let mut area = area.begin(ctx); - let win_size = ctx.screen_rect().max.to_vec2(); + let win_size = ctx.input(|i| i.screen_rect.size()); area.state_mut().set_left_top_pos(Pos2::ZERO); area.state_mut().size = win_size; let title_content_spacing = 2.0 * ctx.style().spacing.item_spacing.y; diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 3ab607e07..8bf57aa80 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -272,7 +272,7 @@ impl ContextImpl { let input = self.input.entry(viewport_id).or_default(); // This is a bit hacky, but is required to avoid jitter: let ratio = input.pixels_per_point / new_pixels_per_point; - let mut rect = Rect::from_min_max(Pos2::default(), input.screen_rect.max); + let mut rect = input.screen_rect; rect.min = (ratio * rect.min.to_vec2()).to_pos2(); rect.max = (ratio * rect.max.to_vec2()).to_pos2(); new_raw_input.screen_rect = Some(rect); @@ -305,7 +305,7 @@ impl ContextImpl { // Ensure we register the background area so panels and background ui can catch clicks: let input = self.input.get(&viewport_id).unwrap(); - let screen_rect = Rect::from_min_max(Pos2::ZERO, input.screen_rect().max); + let screen_rect = input.screen_rect(); self.memory.areas.set_state( LayerId::background(), containers::area::State { @@ -1059,7 +1059,7 @@ impl Context { /// Get a full-screen painter for a new or existing layer pub fn layer_painter(&self, layer_id: LayerId) -> Painter { - let screen_rect = Rect::from_min_max(Pos2::ZERO, self.screen_rect().max); + let screen_rect = self.screen_rect(); Painter::new(self.clone(), layer_id, screen_rect) } @@ -1384,12 +1384,12 @@ impl Context { if window.width() > area.width() { // Allow overlapping side bars. // This is important for small screens, e.g. mobiles running the web demo. - let screen_rect = Rect::from_min_max(Pos2::ZERO, self.screen_rect().max); + let screen_rect = self.screen_rect(); (area.min.x, area.max.x) = (screen_rect.min.x, screen_rect.max.x); } if window.height() > area.height() { // Allow overlapping top/bottom bars: - let screen_rect = Rect::from_min_max(Pos2::ZERO, self.screen_rect().max); + let screen_rect = self.screen_rect(); (area.min.y, area.max.y) = (screen_rect.min.y, screen_rect.max.y); } @@ -1599,8 +1599,7 @@ impl Context { // --------------------------------------------------------------------- - /// Position and size of the current viewport. - /// min is the position, max is the size + /// Position and size of the egui area. pub fn screen_rect(&self) -> Rect { self.input(|i| i.screen_rect()) } @@ -1973,7 +1972,7 @@ impl Context { size *= (max_preview_size.y / size.y).min(1.0); ui.image(texture_id, size).on_hover_ui(|ui| { // show larger on hover - let max_size = 0.5 * ui.ctx().screen_rect().max.to_vec2(); + let max_size = 0.5 * ui.ctx().screen_rect().size(); let mut size = vec2(w as f32, h as f32); size *= max_size.x / size.x.max(max_size.x); size *= max_size.y / size.y.max(max_size.y); diff --git a/crates/egui/src/frame_state.rs b/crates/egui/src/frame_state.rs index 8bb09566c..287b35c56 100644 --- a/crates/egui/src/frame_state.rs +++ b/crates/egui/src/frame_state.rs @@ -91,8 +91,8 @@ impl FrameState { } = self; used_ids.clear(); - *available_rect = Rect::from_min_max(Pos2::ZERO, input.screen_rect().max); - *unused_rect = Rect::from_min_max(Pos2::ZERO, input.screen_rect().max); + *available_rect = input.screen_rect(); + *unused_rect = input.screen_rect(); *used_by_panels = Rect::NOTHING; *tooltip_state = None; *scroll_delta = input.scroll_delta; diff --git a/crates/egui/src/input_state.rs b/crates/egui/src/input_state.rs index 7d2cbd743..ac6e6c5fa 100644 --- a/crates/egui/src/input_state.rs +++ b/crates/egui/src/input_state.rs @@ -229,8 +229,6 @@ impl InputState { } } - /// Position and size of the current viewport. - /// min is the position, max is the size #[inline(always)] pub fn screen_rect(&self) -> Rect { self.screen_rect diff --git a/crates/egui/src/menu.rs b/crates/egui/src/menu.rs index 4d09557f4..0364cf468 100644 --- a/crates/egui/src/menu.rs +++ b/crates/egui/src/menu.rs @@ -150,7 +150,7 @@ pub(crate) fn menu_ui<'c, R>( .constrain(true) .fixed_pos(pos) .interactable(true) - .drag_bounds(Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max)); + .drag_bounds(ctx.screen_rect()); area.show(ctx, |ui| { set_menu_style(ui.style_mut()); @@ -330,8 +330,7 @@ impl MenuRoot { let mut pos = response.rect.left_bottom(); if let Some(root) = root.inner.as_mut() { let menu_rect = root.menu_state.read().rect; - let screen_rect = - Rect::from_min_max(Pos2::ZERO, response.ctx.input(|i| i.screen_rect.max)); + let screen_rect = response.ctx.input(|i| i.screen_rect); if pos.y + menu_rect.height() > screen_rect.max.y { pos.y = screen_rect.max.y - menu_rect.height() - response.rect.height(); diff --git a/crates/egui_demo_app/src/wrap_app.rs b/crates/egui_demo_app/src/wrap_app.rs index 04b27e808..169f48a19 100644 --- a/crates/egui_demo_app/src/wrap_app.rs +++ b/crates/egui_demo_app/src/wrap_app.rs @@ -479,7 +479,7 @@ impl WrapApp { let painter = ctx.layer_painter(LayerId::new(Order::Foreground, Id::new("file_drop_target"))); - let screen_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max); + let screen_rect = ctx.screen_rect(); painter.rect_filled(screen_rect, 0.0, Color32::from_black_alpha(192)); painter.text( screen_rect.center(), diff --git a/crates/egui_demo_lib/src/demo/demo_app_windows.rs b/crates/egui_demo_lib/src/demo/demo_app_windows.rs index fc2c5055f..43b9f827e 100644 --- a/crates/egui_demo_lib/src/demo/demo_app_windows.rs +++ b/crates/egui_demo_lib/src/demo/demo_app_windows.rs @@ -179,7 +179,7 @@ impl DemoWindows { fn mobile_ui(&mut self, ctx: &Context) { if self.about_is_open { - let screen_size = ctx.screen_rect().max.to_vec2(); + let screen_size = ctx.input(|i| i.screen_rect.size()); let default_width = (screen_size.x - 20.0).min(400.0); let mut close = false; diff --git a/crates/egui_demo_lib/src/lib.rs b/crates/egui_demo_lib/src/lib.rs index bce336de7..13ff82bca 100644 --- a/crates/egui_demo_lib/src/lib.rs +++ b/crates/egui_demo_lib/src/lib.rs @@ -116,6 +116,6 @@ fn test_egui_zero_window_size() { /// Detect narrow screens. This is used to show a simpler UI on mobile devices, /// especially for the web demo at . pub fn is_mobile(ctx: &egui::Context) -> bool { - let screen_size = ctx.screen_rect().max; + let screen_size = ctx.screen_rect().size(); screen_size.x < 550.0 } diff --git a/examples/file_dialog/src/main.rs b/examples/file_dialog/src/main.rs index c3055ee92..ba82e3de0 100644 --- a/examples/file_dialog/src/main.rs +++ b/examples/file_dialog/src/main.rs @@ -106,7 +106,7 @@ fn preview_files_being_dropped(ctx: &egui::Context) { let painter = ctx.layer_painter(LayerId::new(Order::Foreground, Id::new("file_drop_target"))); - let screen_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max); + let screen_rect = ctx.screen_rect(); painter.rect_filled(screen_rect, 0.0, Color32::from_black_alpha(192)); painter.text( screen_rect.center(), diff --git a/examples/viewports/src/main.rs b/examples/viewports/src/main.rs index 2af12ad7f..e083d9dfc 100644 --- a/examples/viewports/src/main.rs +++ b/examples/viewports/src/main.rs @@ -51,8 +51,6 @@ impl eframe::App for App { "Current Parent Viewport Id: {}", ctx.get_viewport_id() )); - ui.label(format!("Pos: {:?}", ctx.screen_rect().min)); - ui.label(format!("Size: {:?}", ctx.screen_rect().max)); ui.label("Look at the \"Frame: \" will tell you, what viewport is rendering!"); { let mut force_embedding = ctx.force_embedding(); @@ -335,8 +333,6 @@ impl eframe::App for App { "Current Parent Viewport Id: {}", ctx.get_viewport_id() )); - ui.label(format!("Pos: {:?}", ctx.screen_rect().min)); - ui.label(format!("Size: {:?}", ctx.screen_rect().max)); ui.label(format!("Count: {state}")); if ui.button("Add").clicked() { *state += 1; @@ -350,13 +346,8 @@ impl eframe::App for App { .default_embedded(false) .show(ctx, |ui| { ui.label(format!("Frame: {}", ui.ctx().frame_nr())); + ui.label(format!("Frame: {}", ctx.frame_nr())); ui.label(format!("Current Viewport Id: {}", ctx.get_viewport_id())); - ui.label(format!( - "Current Parent Viewport Id: {}", - ctx.get_viewport_id() - )); - ui.label(format!("Pos: {:?}", ctx.screen_rect().min)); - ui.label(format!("Size: {:?}", ctx.screen_rect().max)); ui.label(format!("Count: {}", self.sync_window_state)); if ui.button("Add").clicked() { self.sync_window_state += 1;