mirror of
https://github.com/emilk/egui.git
synced 2026-09-02 06:40:06 -04:00
Round areas/windows
This commit is contained in:
@@ -66,21 +66,25 @@ impl AreaState {
|
|||||||
pivot_pos.x - self.pivot.x().to_factor() * size.x,
|
pivot_pos.x - self.pivot.x().to_factor() * size.x,
|
||||||
pivot_pos.y - self.pivot.y().to_factor() * size.y,
|
pivot_pos.y - self.pivot.y().to_factor() * size.y,
|
||||||
)
|
)
|
||||||
|
.round()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Move the left top positions of the area.
|
/// Move the left top positions of the area.
|
||||||
pub fn set_left_top_pos(&mut self, pos: Pos2) {
|
pub fn set_left_top_pos(&mut self, pos: Pos2) {
|
||||||
let size = self.size.unwrap_or_default();
|
let size = self.size.unwrap_or_default();
|
||||||
self.pivot_pos = Some(pos2(
|
self.pivot_pos = Some(
|
||||||
pos.x + self.pivot.x().to_factor() * size.x,
|
pos2(
|
||||||
pos.y + self.pivot.y().to_factor() * size.y,
|
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.
|
/// Where the area is on screen.
|
||||||
pub fn rect(&self) -> Rect {
|
pub fn rect(&self) -> Rect {
|
||||||
let size = self.size.unwrap_or_default();
|
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 {
|
if constrain {
|
||||||
state.set_left_top_pos(
|
state.set_left_top_pos(
|
||||||
ctx.constrain_window_rect_to_area(state.rect(), constrain_rect)
|
Context::constrain_window_rect_to_area(state.rect(), constrain_rect).min,
|
||||||
.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:
|
// Update response with possibly moved/constrained rect:
|
||||||
move_response.rect = state.rect();
|
move_response.rect = state.rect();
|
||||||
|
|||||||
@@ -771,13 +771,12 @@ fn resize_response(
|
|||||||
area: &mut area::Prepared,
|
area: &mut area::Prepared,
|
||||||
resize_id: Id,
|
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;
|
return;
|
||||||
};
|
};
|
||||||
let mut new_rect = ctx.round_rect_to_pixels(new_rect);
|
|
||||||
|
|
||||||
if area.constrain() {
|
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"
|
// 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
|
let mut rect = interaction.start_rect; // prevent drift
|
||||||
|
|
||||||
if interaction.left.drag {
|
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 {
|
} 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 {
|
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 {
|
} 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(
|
fn resize_interaction(
|
||||||
|
|||||||
@@ -2065,7 +2065,7 @@ impl Context {
|
|||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
/// Constrain the position of a window/area so it fits within the provided boundary.
|
/// 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;
|
let mut pos = window.min;
|
||||||
|
|
||||||
// Constrain to screen, unless window is too large to fit:
|
// 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_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.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()).round()
|
||||||
|
|
||||||
Rect::from_min_size(pos, window.size())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user