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 crate::{collapsing_header, window::WindowState, *};
#[derive(Clone, Copy, Debug, Default)]
pub struct ScrollState {
/// Positive offset means scrolling down/right
pub offset: Vec2,
}
use crate::{collapsing_header, scroll_area, window, *};
#[derive(Clone, Debug, Default)]
pub struct Memory {
@ -15,19 +9,19 @@ pub struct Memory {
// states of various types of widgets
pub(crate) collapsing_headers: HashMap<Id, collapsing_header::State>,
pub(crate) scroll_areas: HashMap<Id, ScrollState>,
windows: HashMap<Id, WindowState>,
pub(crate) scroll_areas: HashMap<Id, scroll_area::State>,
windows: HashMap<Id, window::State>,
/// Top is last
pub window_order: Vec<Id>,
}
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()
}
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();
if did_insert {
self.window_order.push(id);

View file

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

View file

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