diff --git a/CHANGELOG.md b/CHANGELOG.md index 0088e84d3..3ae1e5087 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * Added a simple grid layout (`Grid`). * Added `ui.allocate_at_least` and `ui.allocate_exact_size`. * Added function `InputState::key_down`. +* Added `Window::current_pos` to position a window. ### Changed 🔧 diff --git a/egui/src/containers/area.rs b/egui/src/containers/area.rs index b0ce1551d..d463254bf 100644 --- a/egui/src/containers/area.rs +++ b/egui/src/containers/area.rs @@ -47,7 +47,7 @@ pub struct Area { interactable: bool, order: Order, default_pos: Option, - fixed_pos: Option, + new_pos: Option, } impl Area { @@ -58,7 +58,7 @@ impl Area { interactable: true, order: Order::Middle, default_pos: None, - fixed_pos: None, + new_pos: None, } } @@ -104,11 +104,17 @@ impl Area { /// Positions the window and prevents it from being moved pub fn fixed_pos(mut self, fixed_pos: impl Into) -> Self { let fixed_pos = fixed_pos.into(); - self.default_pos = Some(fixed_pos); - self.fixed_pos = Some(fixed_pos); + self.new_pos = Some(fixed_pos); self.movable = false; self } + + /// Positions the window but you can still move it. + pub fn current_pos(mut self, current_pos: impl Into) -> Self { + let current_pos = current_pos.into(); + self.new_pos = Some(current_pos); + self + } } pub(crate) struct Prepared { @@ -125,7 +131,7 @@ impl Area { order, interactable, default_pos, - fixed_pos, + new_pos, } = self; let layer_id = LayerId::new(order, id); @@ -136,7 +142,7 @@ impl Area { size: Vec2::zero(), interactable, }); - state.pos = fixed_pos.unwrap_or(state.pos); + state.pos = new_pos.unwrap_or(state.pos); state.pos = ctx.round_pos_to_pixels(state.pos); Prepared { diff --git a/egui/src/containers/window.rs b/egui/src/containers/window.rs index 4d4dfdfe7..cd5c67795 100644 --- a/egui/src/containers/window.rs +++ b/egui/src/containers/window.rs @@ -104,6 +104,13 @@ impl<'open> Window<'open> { self } + /// Set current position of the window. + /// If the window is movable it is up to you to keep track of where it moved to! + pub fn current_pos(mut self, current_pos: impl Into) -> Self { + self.area = self.area.current_pos(current_pos); + self + } + /// Set initial position of the window. pub fn default_pos(mut self, default_pos: impl Into) -> Self { self.area = self.area.default_pos(default_pos); @@ -196,6 +203,7 @@ impl<'open> Window<'open> { } impl<'open> Window<'open> { + /// Returns `None` if the windows is not open (if [`Window::open`] was called with `&mut false`. pub fn show(self, ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui)) -> Option { self.show_impl(ctx, Box::new(add_contents)) }