Unify state management

This commit is contained in:
Emil Ernerfeldt 2020-04-22 18:25:02 +02:00
parent d941c5830d
commit c604574e52
3 changed files with 14 additions and 14 deletions

View file

@ -1,12 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use crate::{collapsing_header, window::WindowState, *}; use crate::{collapsing_header, scroll_area, window, *};
#[derive(Clone, Copy, Debug, Default)]
pub struct ScrollState {
/// Positive offset means scrolling down/right
pub offset: Vec2,
}
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default)]
pub struct Memory { pub struct Memory {
@ -15,19 +9,19 @@ pub struct Memory {
// states of various types of widgets // states of various types of widgets
pub(crate) collapsing_headers: HashMap<Id, collapsing_header::State>, pub(crate) collapsing_headers: HashMap<Id, collapsing_header::State>,
pub(crate) scroll_areas: HashMap<Id, ScrollState>, pub(crate) scroll_areas: HashMap<Id, scroll_area::State>,
windows: HashMap<Id, WindowState>, windows: HashMap<Id, window::State>,
/// Top is last /// Top is last
pub window_order: Vec<Id>, pub window_order: Vec<Id>,
} }
impl Memory { impl Memory {
pub fn get_window(&mut self, id: Id) -> Option<WindowState> { pub fn get_window(&mut self, id: Id) -> Option<window::State> {
self.windows.get(&id).cloned() self.windows.get(&id).cloned()
} }
pub fn set_window_state(&mut self, id: Id, state: WindowState) { pub fn set_window_state(&mut self, id: Id, state: window::State) {
let did_insert = self.windows.insert(id, state).is_none(); let did_insert = self.windows.insert(id, state).is_none();
if did_insert { if did_insert {
self.window_order.push(id); self.window_order.push(id);

View file

@ -1,5 +1,11 @@
use crate::*; use crate::*;
#[derive(Clone, Copy, Debug, Default)]
pub struct State {
/// Positive offset means scrolling down/right
pub offset: Vec2,
}
pub struct ScrollArea { pub struct ScrollArea {
max_height: f32, max_height: f32,
} }

View file

@ -3,7 +3,7 @@ use std::sync::Arc;
use crate::{mesher::Path, widgets::*, *}; use crate::{mesher::Path, widgets::*, *};
#[derive(Clone, Copy, Debug)] #[derive(Clone, Copy, Debug)]
pub struct WindowState { pub struct State {
/// Last known pos /// Last known pos
pub outer_pos: Pos2, pub outer_pos: Pos2,
pub inner_size: Vec2, pub inner_size: Vec2,
@ -127,7 +127,7 @@ impl Window {
let (mut state, is_new_window) = match ctx.memory.lock().get_window(id) { let (mut state, is_new_window) = match ctx.memory.lock().get_window(id) {
Some(state) => (state, false), Some(state) => (state, false),
None => { None => {
let state = WindowState { let state = State {
outer_pos: default_pos, outer_pos: default_pos,
inner_size: default_inner_size, inner_size: default_inner_size,
outer_rect: Rect::from_min_size( outer_rect: Rect::from_min_size(
@ -216,7 +216,7 @@ impl Window {
state.outer_pos += ctx.input().mouse_move; state.outer_pos += ctx.input().mouse_move;
} }
state = WindowState { state = State {
outer_pos: state.outer_pos, outer_pos: state.outer_pos,
inner_size: new_inner_size, inner_size: new_inner_size,
outer_rect: outer_rect, outer_rect: outer_rect,