From 1a45dd5342dbe9bfe6996593e4387a8586d95491 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 30 Sep 2024 16:46:58 +0200 Subject: [PATCH] Round areas/windows --- crates/egui/src/containers/area.rs | 19 +++++++++++-------- crates/egui/src/containers/window.rs | 15 +++++++-------- crates/egui/src/context.rs | 6 ++---- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/crates/egui/src/containers/area.rs b/crates/egui/src/containers/area.rs index fa8e5fc203b..4f6464f4150 100644 --- a/crates/egui/src/containers/area.rs +++ b/crates/egui/src/containers/area.rs @@ -66,21 +66,25 @@ impl AreaState { pivot_pos.x - self.pivot.x().to_factor() * size.x, pivot_pos.y - self.pivot.y().to_factor() * size.y, ) + .round() } /// Move the left top positions of the area. pub fn set_left_top_pos(&mut self, pos: Pos2) { let size = self.size.unwrap_or_default(); - self.pivot_pos = Some(pos2( - pos.x + self.pivot.x().to_factor() * size.x, - pos.y + self.pivot.y().to_factor() * size.y, - )); + self.pivot_pos = Some( + pos2( + pos.x + self.pivot.x().to_factor() * size.x, + pos.y + self.pivot.y().to_factor() * size.y, + ) + .round(), + ); } /// Where the area is on screen. pub fn rect(&self) -> Rect { let size = self.size.unwrap_or_default(); - Rect::from_min_size(self.left_top_pos(), size) + Rect::from_min_size(self.left_top_pos(), size).round() } } @@ -493,12 +497,11 @@ impl Area { if constrain { state.set_left_top_pos( - ctx.constrain_window_rect_to_area(state.rect(), constrain_rect) - .min, + Context::constrain_window_rect_to_area(state.rect(), constrain_rect).min, ); } - state.set_left_top_pos(ctx.round_pos_to_pixels(state.left_top_pos())); + state.set_left_top_pos(state.left_top_pos().round()); // Update response with possibly moved/constrained rect: move_response.rect = state.rect(); diff --git a/crates/egui/src/containers/window.rs b/crates/egui/src/containers/window.rs index 90b28a1d399..1f99b7c72ba 100644 --- a/crates/egui/src/containers/window.rs +++ b/crates/egui/src/containers/window.rs @@ -771,13 +771,12 @@ fn resize_response( area: &mut area::Prepared, resize_id: Id, ) { - let Some(new_rect) = move_and_resize_window(ctx, &resize_interaction) else { + let Some(mut new_rect) = move_and_resize_window(ctx, &resize_interaction) else { return; }; - let mut new_rect = ctx.round_rect_to_pixels(new_rect); if area.constrain() { - new_rect = ctx.constrain_window_rect_to_area(new_rect, area.constrain_rect()); + new_rect = Context::constrain_window_rect_to_area(new_rect, area.constrain_rect()); } // TODO(emilk): add this to a Window state instead as a command "move here next frame" @@ -802,18 +801,18 @@ fn move_and_resize_window(ctx: &Context, interaction: &ResizeInteraction) -> Opt let mut rect = interaction.start_rect; // prevent drift if interaction.left.drag { - rect.min.x = ctx.round_to_pixel(pointer_pos.x); + rect.min.x = pointer_pos.x; } else if interaction.right.drag { - rect.max.x = ctx.round_to_pixel(pointer_pos.x); + rect.max.x = pointer_pos.x; } if interaction.top.drag { - rect.min.y = ctx.round_to_pixel(pointer_pos.y); + rect.min.y = pointer_pos.y; } else if interaction.bottom.drag { - rect.max.y = ctx.round_to_pixel(pointer_pos.y); + rect.max.y = pointer_pos.y; } - Some(rect) + Some(rect.round()) } fn resize_interaction( diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index c8875dc3bf0..359e26e6222 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -2065,7 +2065,7 @@ impl Context { // --------------------------------------------------------------------- /// Constrain the position of a window/area so it fits within the provided boundary. - pub(crate) fn constrain_window_rect_to_area(&self, window: Rect, area: Rect) -> Rect { + pub(crate) fn constrain_window_rect_to_area(window: Rect, area: Rect) -> Rect { let mut pos = window.min; // Constrain to screen, unless window is too large to fit: @@ -2077,9 +2077,7 @@ impl Context { pos.y = pos.y.at_most(area.bottom() + margin_y - window.height()); // move right if needed pos.y = pos.y.at_least(area.top() - margin_y); // move down if needed - pos = self.round_pos_to_pixels(pos); - - Rect::from_min_size(pos, window.size()) + Rect::from_min_size(pos, window.size()).round() } }