[refactor] Move used_ids from Context to Memory

This commit is contained in:
Emil Ernerfeldt 2020-11-09 13:04:05 +01:00
parent 3af741e85a
commit 7cc5218630
2 changed files with 9 additions and 10 deletions

View file

@ -3,8 +3,6 @@ use std::sync::{
Arc, Arc,
}; };
use ahash::AHashMap;
use crate::{ use crate::{
animation_manager::AnimationManager, animation_manager::AnimationManager,
mutex::{Mutex, MutexGuard}, mutex::{Mutex, MutexGuard},
@ -51,8 +49,6 @@ pub struct Context {
// The output of a frame: // The output of a frame:
graphics: Mutex<GraphicLayers>, graphics: Mutex<GraphicLayers>,
output: Mutex<Output>, output: Mutex<Output>,
/// Used to debug `Id` clashes of widgets.
used_ids: Mutex<AHashMap<Id, Pos2>>,
paint_stats: Mutex<PaintStats>, paint_stats: Mutex<PaintStats>,
@ -72,7 +68,6 @@ impl Clone for Context {
used_by_panels: self.used_by_panels.clone(), used_by_panels: self.used_by_panels.clone(),
graphics: self.graphics.clone(), graphics: self.graphics.clone(),
output: self.output.clone(), output: self.output.clone(),
used_ids: self.used_ids.clone(),
paint_stats: self.paint_stats.clone(), paint_stats: self.paint_stats.clone(),
repaint_requests: self.repaint_requests.load(SeqCst).into(), repaint_requests: self.repaint_requests.load(SeqCst).into(),
} }
@ -212,8 +207,6 @@ impl Context {
fn begin_frame_mut(&mut self, new_raw_input: RawInput) { fn begin_frame_mut(&mut self, new_raw_input: RawInput) {
self.memory().begin_frame(&self.input); self.memory().begin_frame(&self.input);
self.used_ids.lock().clear();
self.input = std::mem::take(&mut self.input).begin_frame(new_raw_input); self.input = std::mem::take(&mut self.input).begin_frame(new_raw_input);
*self.available_rect.lock() = Some(self.input.screen_rect()); *self.available_rect.lock() = Some(self.input.screen_rect());
*self.used_by_panels.lock() = Some(Rect::nothing()); *self.used_by_panels.lock() = Some(Rect::nothing());
@ -342,8 +335,8 @@ impl Context {
/// If the given `Id` is not unique, an error will be printed at the given position. /// If the given `Id` is not unique, an error will be printed at the given position.
/// Call this for `Id`:s that need interaction or persistence. /// Call this for `Id`:s that need interaction or persistence.
pub(crate) fn register_unique_id(self: &Arc<Self>, id: Id, new_pos: Pos2) { pub(crate) fn register_interaction_id(self: &Arc<Self>, id: Id, new_pos: Pos2) {
if let Some(prev_pos) = self.used_ids.lock().insert(id, new_pos) { if let Some(prev_pos) = self.memory().used_ids.insert(id, new_pos) {
if prev_pos == new_pos { if prev_pos == new_pos {
// Likely same Widget being interacted with twice, which is fine. // Likely same Widget being interacted with twice, which is fine.
return; return;
@ -452,7 +445,7 @@ impl Context {
} }
let id = id.unwrap(); let id = id.unwrap();
self.register_unique_id(id, rect.min); self.register_interaction_id(id, rect.min);
let mut memory = self.memory(); let mut memory = self.memory();

View file

@ -20,6 +20,11 @@ use crate::{
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))] #[cfg_attr(feature = "serde", serde(default))]
pub struct Memory { pub struct Memory {
/// All `Id`s that were used this frame.
/// Used to debug `Id` clashes of widgets.
#[cfg_attr(feature = "serde", serde(skip))]
pub(crate) used_ids: ahash::AHashMap<Id, Pos2>,
#[cfg_attr(feature = "serde", serde(skip))] #[cfg_attr(feature = "serde", serde(skip))]
pub(crate) interaction: Interaction, pub(crate) interaction: Interaction,
@ -115,6 +120,7 @@ impl Interaction {
impl Memory { impl Memory {
pub(crate) fn begin_frame(&mut self, prev_input: &crate::input::InputState) { pub(crate) fn begin_frame(&mut self, prev_input: &crate::input::InputState) {
self.used_ids.clear();
self.interaction.begin_frame(prev_input); self.interaction.begin_frame(prev_input);
if !prev_input.mouse.down { if !prev_input.mouse.down {