From 4b4414955a4f61ae9725db9801770428ab3ff646 Mon Sep 17 00:00:00 2001 From: Konkitoman Date: Sun, 30 Jul 2023 21:07:07 +0300 Subject: [PATCH] Now Memory::interaction is stored per viewport in Memory::interactions This will allow in the future drag and drop betwen windows Fixes if a an other window is rendered will forgot what was draging, this was very annoying --- crates/egui/src/context.rs | 18 ++++++++++-------- crates/egui/src/memory.rs | 27 +++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index c7669d862..165e08311 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -240,6 +240,7 @@ impl ContextImpl { self.memory.begin_frame( self.input.get(&viewport_id).unwrap_or(&Default::default()), &new_raw_input, + viewport_id, ); let input = self @@ -1299,6 +1300,14 @@ impl Context { /// Call at the end of each frame. #[must_use] pub fn end_frame(&self) -> FullOutput { + let mut viewports: Vec = self.read(|ctx| { + ctx.viewports + .iter() + .map(|(_, (_, id, _, _, _))| *id) + .collect() + }); + viewports.push(0); + if self.input(|i| i.wants_repaint()) { self.request_repaint(); } @@ -1306,6 +1315,7 @@ impl Context { let textures_delta = self.write(|ctx| { ctx.memory.end_frame( ctx.input.get(&ctx.current_rendering_viewport).unwrap(), + &viewports, &ctx.frame_state.used_ids, ); @@ -1352,14 +1362,6 @@ impl Context { } } - let mut viewports: Vec = self.read(|ctx| { - ctx.viewports - .iter() - .map(|(_, (_, id, _, _, _))| *id) - .collect() - }); - viewports.push(0); - self.write(|ctx| ctx.input.retain(|id, _| viewports.contains(&id))); let repaint_after = self.write(|ctx| { diff --git a/crates/egui/src/memory.rs b/crates/egui/src/memory.rs index db76f7e7c..04dda036f 100644 --- a/crates/egui/src/memory.rs +++ b/crates/egui/src/memory.rs @@ -1,3 +1,5 @@ +use ahash::HashMap; + use crate::{area, window, Id, IdMap, InputState, LayerId, Pos2, Rect, Style}; // ---------------------------------------------------------------------------- @@ -69,9 +71,16 @@ pub struct Memory { #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) new_font_definitions: Option, + #[cfg_attr(feature = "persistence", serde(skip))] + pub(crate) interactions: HashMap, + #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) interaction: Interaction, + // Current viewport + #[cfg_attr(feature = "persistence", serde(skip))] + pub(crate) viewport_id: u64, + #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) window_interaction: Option, @@ -354,19 +363,33 @@ impl Memory { &mut self, prev_input: &crate::input_state::InputState, new_input: &crate::data::input::RawInput, + viewport_id: u64, ) { - self.interaction.begin_frame(prev_input, new_input); + self.viewport_id = viewport_id; + self.interactions + .entry(viewport_id) + .or_default() + .begin_frame(prev_input, new_input); + self.interaction = self.interactions.remove(&viewport_id).unwrap(); if !prev_input.pointer.any_down() { self.window_interaction = None; } } - pub(crate) fn end_frame(&mut self, input: &InputState, used_ids: &IdMap) { + pub(crate) fn end_frame( + &mut self, + input: &InputState, + viewports: &[u64], + used_ids: &IdMap, + ) { self.caches.update(); self.areas.end_frame(); self.interaction.focus.end_frame(used_ids); + self.interactions + .insert(self.viewport_id, std::mem::take(&mut self.interaction)); self.drag_value.end_frame(input); + self.interactions.retain(|id, _| viewports.contains(id)) } /// Top-most layer at the given position.