Add Window::current_pos to position a winodw

This commit is contained in:
Emil Ernerfeldt 2021-01-13 23:19:36 +01:00
parent 99726decb6
commit 795906bb24
3 changed files with 21 additions and 6 deletions

View file

@ -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 a simple grid layout (`Grid`).
* Added `ui.allocate_at_least` and `ui.allocate_exact_size`. * Added `ui.allocate_at_least` and `ui.allocate_exact_size`.
* Added function `InputState::key_down`. * Added function `InputState::key_down`.
* Added `Window::current_pos` to position a window.
### Changed 🔧 ### Changed 🔧

View file

@ -47,7 +47,7 @@ pub struct Area {
interactable: bool, interactable: bool,
order: Order, order: Order,
default_pos: Option<Pos2>, default_pos: Option<Pos2>,
fixed_pos: Option<Pos2>, new_pos: Option<Pos2>,
} }
impl Area { impl Area {
@ -58,7 +58,7 @@ impl Area {
interactable: true, interactable: true,
order: Order::Middle, order: Order::Middle,
default_pos: None, default_pos: None,
fixed_pos: None, new_pos: None,
} }
} }
@ -104,11 +104,17 @@ impl Area {
/// Positions the window and prevents it from being moved /// Positions the window and prevents it from being moved
pub fn fixed_pos(mut self, fixed_pos: impl Into<Pos2>) -> Self { pub fn fixed_pos(mut self, fixed_pos: impl Into<Pos2>) -> Self {
let fixed_pos = fixed_pos.into(); let fixed_pos = fixed_pos.into();
self.default_pos = Some(fixed_pos); self.new_pos = Some(fixed_pos);
self.fixed_pos = Some(fixed_pos);
self.movable = false; self.movable = false;
self self
} }
/// Positions the window but you can still move it.
pub fn current_pos(mut self, current_pos: impl Into<Pos2>) -> Self {
let current_pos = current_pos.into();
self.new_pos = Some(current_pos);
self
}
} }
pub(crate) struct Prepared { pub(crate) struct Prepared {
@ -125,7 +131,7 @@ impl Area {
order, order,
interactable, interactable,
default_pos, default_pos,
fixed_pos, new_pos,
} = self; } = self;
let layer_id = LayerId::new(order, id); let layer_id = LayerId::new(order, id);
@ -136,7 +142,7 @@ impl Area {
size: Vec2::zero(), size: Vec2::zero(),
interactable, 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); state.pos = ctx.round_pos_to_pixels(state.pos);
Prepared { Prepared {

View file

@ -104,6 +104,13 @@ impl<'open> Window<'open> {
self 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<Pos2>) -> Self {
self.area = self.area.current_pos(current_pos);
self
}
/// Set initial position of the window. /// Set initial position of the window.
pub fn default_pos(mut self, default_pos: impl Into<Pos2>) -> Self { pub fn default_pos(mut self, default_pos: impl Into<Pos2>) -> Self {
self.area = self.area.default_pos(default_pos); self.area = self.area.default_pos(default_pos);
@ -196,6 +203,7 @@ impl<'open> Window<'open> {
} }
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<Response> { pub fn show(self, ctx: &CtxRef, add_contents: impl FnOnce(&mut Ui)) -> Option<Response> {
self.show_impl(ctx, Box::new(add_contents)) self.show_impl(ctx, Box::new(add_contents))
} }