Skip to content

Commit

Permalink
Round areas/windows
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Sep 30, 2024
1 parent 6a33dab commit 1a45dd5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
19 changes: 11 additions & 8 deletions crates/egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

Expand Down Expand Up @@ -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();
Expand Down
15 changes: 7 additions & 8 deletions crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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(
Expand Down
6 changes: 2 additions & 4 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
}
}

Expand Down

0 comments on commit 1a45dd5

Please sign in to comment.