Add Window::current_pos to position a winodw
This commit is contained in:
parent
99726decb6
commit
795906bb24
3 changed files with 21 additions and 6 deletions
|
@ -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 🔧
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ pub struct Area {
|
|||
interactable: bool,
|
||||
order: Order,
|
||||
default_pos: Option<Pos2>,
|
||||
fixed_pos: Option<Pos2>,
|
||||
new_pos: Option<Pos2>,
|
||||
}
|
||||
|
||||
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<Pos2>) -> 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<Pos2>) -> 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 {
|
||||
|
|
|
@ -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<Pos2>) -> 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<Pos2>) -> 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<Response> {
|
||||
self.show_impl(ctx, Box::new(add_contents))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue