diff --git a/crates/egui-winit/src/lib.rs b/crates/egui-winit/src/lib.rs index 3d98a783c..6f3cb9ede 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, ViewportBuilder, ViewportCommand, ViewportId}; +use egui::{mutex::RwLock, Pos2, ViewportBuilder, ViewportCommand, ViewportId}; pub use winit; pub mod clipboard; @@ -178,9 +178,12 @@ impl State { let screen_size_in_points = screen_size_in_pixels / pixels_per_point; self.egui_input.screen_rect = if screen_size_in_points.x > 0.0 && screen_size_in_points.y > 0.0 { - Some(egui::Rect::from_min_size( - egui::Pos2::ZERO, - screen_size_in_points, + 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(), )) } else { None diff --git a/crates/egui/src/containers/area.rs b/crates/egui/src/containers/area.rs index 380c175b3..aec087f69 100644 --- a/crates/egui/src/containers/area.rs +++ b/crates/egui/src/containers/area.rs @@ -374,7 +374,7 @@ impl Prepared { } pub(crate) fn content_ui(&self, ctx: &Context) -> Ui { - let screen_rect = ctx.screen_rect(); + let screen_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max); 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 d829ac219..6578d37c5 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 = ctx.screen_rect(); + let clip_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max); 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); @@ -787,7 +787,7 @@ impl TopBottomPanel { let available_rect = ctx.available_rect(); let side = self.side; - let clip_rect = ctx.screen_rect(); + let clip_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max); 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); @@ -1044,7 +1044,7 @@ impl CentralPanel { let layer_id = LayerId::background(); let id = Id::new("central_panel"); - let clip_rect = ctx.screen_rect(); + let clip_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max); 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 70b03966a..672dcc38d 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(ctx.screen_rect().min); + let position = position.at_least(Pos2::ZERO); 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(ctx.screen_rect()) + .drag_bounds(Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max)) .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 befb51a10..f99706164 100644 --- a/crates/egui/src/containers/resize.rs +++ b/crates/egui/src/containers/resize.rs @@ -180,7 +180,7 @@ impl Resize { .at_least(self.min_size) .at_most(self.max_size) .at_most( - ui.ctx().screen_rect().size() - ui.spacing().window_margin.sum(), // hack for windows + ui.ctx().screen_rect().max.to_vec2() - 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 19f696f25..a6e488f80 100644 --- a/crates/egui/src/containers/window.rs +++ b/crates/egui/src/containers/window.rs @@ -421,7 +421,7 @@ impl<'open> Window<'open> { let mut resize = resize.id(resize_id); let mut area = area.begin(ctx); - let win_size = ctx.input(|i| i.screen_rect.size()); + let win_size = ctx.screen_rect().max.to_vec2(); 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; @@ -878,7 +878,7 @@ impl<'open> Window<'open> { let mut resize = resize.id(resize_id); let mut area = area.begin(ctx); - let win_size = ctx.input(|i| i.screen_rect.size()); + let win_size = ctx.screen_rect().max.to_vec2(); 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 37b15d422..85cba1acb 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -271,7 +271,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 = input.screen_rect; + let mut rect = Rect::from_min_max(Pos2::default(), input.screen_rect.max); 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); @@ -304,7 +304,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 = input.screen_rect(); + let screen_rect = Rect::from_min_max(Pos2::ZERO, input.screen_rect().max); self.memory.areas.set_state( LayerId::background(), containers::area::State { @@ -1036,7 +1036,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 = self.screen_rect(); + let screen_rect = Rect::from_min_max(Pos2::ZERO, self.screen_rect().max); Painter::new(self.clone(), layer_id, screen_rect) } @@ -1361,12 +1361,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 = self.screen_rect(); + let screen_rect = Rect::from_min_max(Pos2::ZERO, self.screen_rect().max); (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 = self.screen_rect(); + let screen_rect = Rect::from_min_max(Pos2::ZERO, self.screen_rect().max); (area.min.y, area.max.y) = (screen_rect.min.y, screen_rect.max.y); } @@ -1577,6 +1577,7 @@ impl Context { // --------------------------------------------------------------------- /// Position and size of the egui area. + /// min is the position, max is the size pub fn screen_rect(&self) -> Rect { self.input(|i| i.screen_rect()) } @@ -1941,7 +1942,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().size(); + let max_size = 0.5 * ui.ctx().screen_rect().max.to_vec2(); 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 0d7106843..1b5d2ba8f 100644 --- a/crates/egui/src/frame_state.rs +++ b/crates/egui/src/frame_state.rs @@ -93,8 +93,8 @@ impl FrameState { } = self; used_ids.clear(); - *available_rect = input.screen_rect(); - *unused_rect = input.screen_rect(); + *available_rect = Rect::from_min_max(Pos2::ZERO, input.screen_rect().max); + *unused_rect = Rect::from_min_max(Pos2::ZERO, input.screen_rect().max); *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 1b49757bc..7383e1470 100644 --- a/crates/egui/src/input_state.rs +++ b/crates/egui/src/input_state.rs @@ -229,6 +229,8 @@ 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 5e27e9536..8b3cc83ab 100644 --- a/crates/egui/src/menu.rs +++ b/crates/egui/src/menu.rs @@ -146,7 +146,7 @@ pub(crate) fn menu_ui<'c, R>( .constrain(true) .fixed_pos(pos) .interactable(true) - .drag_bounds(ctx.screen_rect()); + .drag_bounds(Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max)); area.show(ctx, |ui| { set_menu_style(ui.style_mut()); @@ -324,7 +324,8 @@ 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 = response.ctx.input(|i| i.screen_rect); + let screen_rect = + Rect::from_min_max(Pos2::ZERO, response.ctx.input(|i| i.screen_rect.max)); 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 6aea50014..8af75d4db 100644 --- a/crates/egui_demo_app/src/wrap_app.rs +++ b/crates/egui_demo_app/src/wrap_app.rs @@ -449,7 +449,7 @@ impl WrapApp { let painter = ctx.layer_painter(LayerId::new(Order::Foreground, Id::new("file_drop_target"))); - let screen_rect = ctx.screen_rect(); + let screen_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max); 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 d65830677..75f13012c 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.input(|i| i.screen_rect.size()); + let screen_size = ctx.screen_rect().max.to_vec2(); 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 13ff82bca..bce336de7 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().size(); + let screen_size = ctx.screen_rect().max; screen_size.x < 550.0 } diff --git a/examples/file_dialog/src/main.rs b/examples/file_dialog/src/main.rs index ba82e3de0..c3055ee92 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 = ctx.screen_rect(); + let screen_rect = Rect::from_min_max(Pos2::ZERO, ctx.screen_rect().max); 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 593862e57..65c74fa70 100644 --- a/examples/viewports/src/main.rs +++ b/examples/viewports/src/main.rs @@ -44,6 +44,8 @@ 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 desktop = ctx.is_desktop(); @@ -70,6 +72,8 @@ 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; @@ -91,6 +95,8 @@ 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: {}", self.sync_viewport_state)); if ui.button("Add").clicked() { self.sync_viewport_state += 1; @@ -114,6 +120,8 @@ 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; @@ -127,8 +135,13 @@ 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;