bug fix: only catch clicks on currently open windows

This commit is contained in:
Emil Ernerfeldt 2020-05-10 10:32:28 +02:00
parent 3a1d677840
commit dba494e306
4 changed files with 27 additions and 11 deletions

View File

@ -13,7 +13,6 @@ This is the core library crate Emigui. It is fully platform independent without
* [x] Tooltip * [x] Tooltip
* [x] Movable/resizable windows * [x] Movable/resizable windows
* [x] Kinetic windows * [x] Kinetic windows
* [ ] BUG FIX: Don't catch clicks on closed windows
* [ ] Windows should open from Regions and be boxed by parent region. * [ ] Windows should open from Regions and be boxed by parent region.
* Then we could open the example app inside a window in the example app, recursively. * Then we could open the example app inside a window in the example app, recursively.
* [ ] Scroll areas * [ ] Scroll areas

View File

@ -156,6 +156,7 @@ impl Context {
if !self.last_raw_input.mouse_down || self.last_raw_input.mouse_pos.is_none() { if !self.last_raw_input.mouse_down || self.last_raw_input.mouse_pos.is_none() {
self.memory().active_id = None; self.memory().active_id = None;
} }
self.memory().begin_frame();
self.used_ids.lock().clear(); self.used_ids.lock().clear();
@ -181,7 +182,7 @@ impl Context {
fn drain_paint_lists(&self) -> Vec<(Rect, PaintCmd)> { fn drain_paint_lists(&self) -> Vec<(Rect, PaintCmd)> {
let memory = self.memory(); let memory = self.memory();
self.graphics().drain(&memory.floating_order).collect() self.graphics().drain(memory.floating_order()).collect()
} }
fn paint(&self) -> PaintBatches { fn paint(&self) -> PaintBatches {

View File

@ -37,11 +37,11 @@ impl GraphicLayers {
pub fn drain( pub fn drain(
&mut self, &mut self,
window_oreder: &[Id], window_order: &[Id],
) -> impl ExactSizeIterator<Item = (Rect, PaintCmd)> { ) -> impl ExactSizeIterator<Item = (Rect, PaintCmd)> {
let mut all_commands: Vec<_> = self.bg.drain(..).collect(); let mut all_commands: Vec<_> = self.bg.drain(..).collect();
for id in window_oreder { for id in window_order {
if let Some(window) = self.windows.get_mut(id) { if let Some(window) = self.windows.get_mut(id) {
all_commands.extend(window.drain(..)); all_commands.extend(window.drain(..));
} }

View File

@ -1,4 +1,4 @@
use std::collections::HashMap; use std::collections::{HashMap, HashSet};
use crate::{ use crate::{
containers::{collapsing_header, floating, resize, scroll_area}, containers::{collapsing_header, floating, resize, scroll_area},
@ -20,10 +20,12 @@ pub struct Memory {
pub(crate) collapsing_headers: HashMap<Id, collapsing_header::State>, pub(crate) collapsing_headers: HashMap<Id, collapsing_header::State>,
pub(crate) scroll_areas: HashMap<Id, scroll_area::State>, pub(crate) scroll_areas: HashMap<Id, scroll_area::State>,
pub(crate) resize: HashMap<Id, resize::State>, pub(crate) resize: HashMap<Id, resize::State>,
floating: HashMap<Id, floating::State>,
floating: HashMap<Id, floating::State>,
/// Top is last /// Top is last
pub floating_order: Vec<Id>, floating_order: Vec<Id>,
floating_visible_last_frame: HashSet<Id>,
floating_visible_current_frame: HashSet<Id>,
} }
impl Memory { impl Memory {
@ -31,7 +33,12 @@ impl Memory {
self.floating.get(&id).cloned() self.floating.get(&id).cloned()
} }
pub(crate) fn floating_order(&self) -> &[Id] {
&self.floating_order
}
pub(crate) fn set_floating_state(&mut self, id: Id, state: floating::State) { pub(crate) fn set_floating_state(&mut self, id: Id, state: floating::State) {
self.floating_visible_current_frame.insert(id);
let did_insert = self.floating.insert(id, state).is_none(); let did_insert = self.floating.insert(id, state).is_none();
if did_insert { if did_insert {
self.floating_order.push(id); self.floating_order.push(id);
@ -41,10 +48,14 @@ impl Memory {
/// TODO: call once at the start of the frame for the current mouse pos /// TODO: call once at the start of the frame for the current mouse pos
pub fn layer_at(&self, pos: Pos2) -> Layer { pub fn layer_at(&self, pos: Pos2) -> Layer {
for floating_id in self.floating_order.iter().rev() { for floating_id in self.floating_order.iter().rev() {
if let Some(state) = self.floating.get(floating_id) { if self.floating_visible_last_frame.contains(floating_id)
let rect = Rect::from_min_size(state.pos, state.size); || self.floating_visible_current_frame.contains(floating_id)
if rect.contains(pos) { {
return Layer::Window(*floating_id); if let Some(state) = self.floating.get(floating_id) {
let rect = Rect::from_min_size(state.pos, state.size);
if rect.contains(pos) {
return Layer::Window(*floating_id);
}
} }
} }
} }
@ -59,5 +70,10 @@ impl Memory {
self.floating_order.remove(index); self.floating_order.remove(index);
} }
self.floating_order.push(id); self.floating_order.push(id);
self.floating_visible_current_frame.insert(id);
}
pub(crate) fn begin_frame(&mut self) {
self.floating_visible_last_frame = std::mem::take(&mut self.floating_visible_current_frame);
} }
} }