use egui::{Id, Pos2, Rect, Vec2};
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct WindowState {
screen_rect: Option<Rect>,
dragged: bool,
next_position: Option<Pos2>,
next_size: Option<Vec2>,
new: bool,
}
impl Default for WindowState {
fn default() -> Self {
Self {
screen_rect: None,
dragged: false,
next_position: None,
next_size: None,
new: true,
}
}
}
impl WindowState {
pub(crate) fn new() -> Self {
Self::default()
}
pub fn set_position(&mut self, position: Pos2) -> &mut Self {
self.next_position = Some(position);
self
}
pub fn set_size(&mut self, size: Vec2) -> &mut Self {
self.next_size = Some(size);
self
}
pub fn rect(&self) -> Rect {
self.screen_rect.unwrap_or(Rect::NOTHING)
}
pub fn dragged(&self) -> bool {
self.dragged
}
#[inline(always)]
pub(crate) fn next_position(&mut self) -> Option<Pos2> {
self.next_position.take()
}
#[inline(always)]
pub(crate) fn next_size(&mut self) -> Option<Vec2> {
self.next_size.take()
}
pub(crate) fn create_window(&mut self, id: Id, bounds: Rect) -> (egui::Window<'static>, bool) {
let new = self.new;
let mut window_constructor = egui::Window::new("")
.id(id)
.constrain_to(bounds)
.title_bar(false);
if let Some(position) = self.next_position() {
window_constructor = window_constructor.current_pos(position);
}
if let Some(size) = self.next_size() {
window_constructor = window_constructor.fixed_size(size);
}
self.new = false;
(window_constructor, new)
}
}