[grid] only save state once

This commit is contained in:
Emil Ernerfeldt 2021-01-14 22:29:00 +01:00
parent 795906bb24
commit 8e34cc50be
3 changed files with 31 additions and 31 deletions

View file

@ -1,6 +1,6 @@
use crate::*; use crate::*;
#[derive(Clone, Debug, Default)] #[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
pub(crate) struct State { pub(crate) struct State {
col_widths: Vec<f32>, col_widths: Vec<f32>,
@ -8,28 +8,16 @@ pub(crate) struct State {
} }
impl State { impl State {
/// Returns `true` if this made the column wider. fn set_min_col_width(&mut self, col: usize, width: f32) {
fn set_min_col_width(&mut self, col: usize, width: f32) -> bool {
self.col_widths self.col_widths
.resize(self.col_widths.len().max(col + 1), 0.0); .resize(self.col_widths.len().max(col + 1), 0.0);
if self.col_widths[col] < width { self.col_widths[col] = self.col_widths[col].max(width);
self.col_widths[col] = width;
true
} else {
false
}
} }
/// Returns `true` if this made the row higher. fn set_min_row_height(&mut self, row: usize, height: f32) {
fn set_min_row_height(&mut self, row: usize, height: f32) -> bool {
self.row_heights self.row_heights
.resize(self.row_heights.len().max(row + 1), 0.0); .resize(self.row_heights.len().max(row + 1), 0.0);
if self.row_heights[row] < height { self.row_heights[row] = self.row_heights[row].max(height);
self.row_heights[row] = height;
true
} else {
false
}
} }
fn col_width(&self, col: usize) -> Option<f32> { fn col_width(&self, col: usize) -> Option<f32> {
@ -109,20 +97,10 @@ impl GridLayout {
} }
pub(crate) fn advance(&mut self, cursor: &mut Pos2, frame_rect: Rect, widget_rect: Rect) { pub(crate) fn advance(&mut self, cursor: &mut Pos2, frame_rect: Rect, widget_rect: Rect) {
let dirty = self self.curr_state
.curr_state
.set_min_col_width(self.col, widget_rect.width()); .set_min_col_width(self.col, widget_rect.width());
let dirty = self self.curr_state
.curr_state .set_min_row_height(self.row, widget_rect.height());
.set_min_row_height(self.row, widget_rect.height())
|| dirty;
if dirty {
self.ctx
.memory()
.grid
.insert(self.id, self.curr_state.clone());
self.ctx.request_repaint();
}
self.col += 1; self.col += 1;
cursor.x += frame_rect.width() + self.spacing.x; cursor.x += frame_rect.width() + self.spacing.x;
} }
@ -146,6 +124,16 @@ impl GridLayout {
} }
} }
} }
pub(crate) fn save(&self) {
if self.curr_state != self.prev_state {
self.ctx
.memory()
.grid
.insert(self.id, self.curr_state.clone());
self.ctx.request_repaint();
}
}
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -198,7 +186,9 @@ impl Grid {
..GridLayout::new(ui, id) ..GridLayout::new(ui, id)
}; };
ui.set_grid(grid); ui.set_grid(grid);
add_contents(ui) let r = add_contents(ui);
ui.save_grid();
r
}) })
.0 .0
} }

View file

@ -21,6 +21,12 @@ impl Placer {
self.grid = Some(grid); self.grid = Some(grid);
} }
pub(crate) fn save_grid(&mut self) {
if let Some(grid) = &mut self.grid {
grid.save();
}
}
pub(crate) fn layout(&self) -> &Layout { pub(crate) fn layout(&self) -> &Layout {
&self.layout &self.layout
} }

View file

@ -1112,6 +1112,10 @@ impl Ui {
self.placer.set_grid(grid); self.placer.set_grid(grid);
} }
pub(crate) fn save_grid(&mut self) {
self.placer.save_grid();
}
/// Move to the next row in a grid layout or wrapping layout. /// Move to the next row in a grid layout or wrapping layout.
/// Otherwise does nothing. /// Otherwise does nothing.
pub fn end_row(&mut self) { pub fn end_row(&mut self) {