egui/emigui/src/memory.rs

51 lines
1.4 KiB
Rust
Raw Normal View History

2020-04-17 13:29:48 +00:00
use std::collections::{HashMap, HashSet};
use crate::{window::WindowState, *};
#[derive(Clone, Debug, Default)]
pub struct Memory {
/// The widget being interacted with (e.g. dragged, in case of a slider).
pub(crate) active_id: Option<Id>,
/// Which foldable regions are open.
pub(crate) open_foldables: HashSet<Id>,
windows: HashMap<Id, WindowState>,
/// Top is last
window_order: Vec<Id>,
}
impl Memory {
/// default_rect: where to put it if it does NOT exist
pub fn get_or_create_window(&mut self, id: Id, default_rect: Rect) -> WindowState {
if let Some(state) = self.windows.get(&id) {
*state
} else {
let state = WindowState { rect: default_rect };
self.windows.insert(id, state);
self.window_order.push(id);
state
}
}
pub fn set_window_state(&mut self, id: Id, state: WindowState) {
self.windows.insert(id, state);
}
pub fn layer_at(&self, pos: Vec2) -> Layer {
for window_id in self.window_order.iter().rev() {
if let Some(state) = self.windows.get(window_id) {
if state.rect.contains(pos) {
return Layer::Window(*window_id);
}
}
}
Layer::Background
}
pub fn move_window_to_top(&mut self, _id: Id) {
// TODO
}
}