Move new windows to the top

This commit is contained in:
Emil Ernerfeldt 2020-05-12 16:49:43 +02:00
parent e5c216447a
commit d508372334
4 changed files with 33 additions and 18 deletions

View file

@ -155,7 +155,10 @@ impl Area {
// &format!("Area size: {:?}", state.size), // &format!("Area size: {:?}", state.size),
// ); // );
if move_interact.active || mouse_pressed_on_area(ctx, layer) { if move_interact.active
|| mouse_pressed_on_area(ctx, layer)
|| !ctx.memory().areas.visible_last_frame(&layer)
{
ctx.memory().areas.move_to_top(layer); ctx.memory().areas.move_to_top(layer);
} }
ctx.memory().areas.set_state(layer, state); ctx.memory().areas.set_state(layer, state);

View file

@ -161,7 +161,6 @@ 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();
@ -180,6 +179,7 @@ impl Context {
} }
pub fn end_frame(&self) -> (Output, PaintBatches) { pub fn end_frame(&self) -> (Output, PaintBatches) {
self.memory().end_frame();
let output: Output = std::mem::take(&mut self.output()); let output: Output = std::mem::take(&mut self.output());
let paint_batches = self.paint(); let paint_batches = self.paint();
(output, paint_batches) (output, paint_batches)

View file

@ -112,9 +112,6 @@ fn show_menu_bar(ui: &mut Ui, windows: &mut OpenWindows) {
ui.add(Button::new("Don't Quit")); ui.add(Button::new("Don't Quit"));
}); });
menu::menu(ui, "Windows", |ui| { menu::menu(ui, "Windows", |ui| {
// TODO: open on top when clicking a new.
// Maybe an Window or Area can detect that: if wasn't open last frame, but is now,
// then automatically go to front?
ui.add(Checkbox::new(&mut windows.examples, "Examples")); ui.add(Checkbox::new(&mut windows.examples, "Examples"));
ui.add(Checkbox::new(&mut windows.settings, "Settings")); ui.add(Checkbox::new(&mut windows.settings, "Settings"));
ui.add(Checkbox::new(&mut windows.inspection, "Inspection")); ui.add(Checkbox::new(&mut windows.inspection, "Inspection"));

View file

@ -33,11 +33,18 @@ pub struct Areas {
order: Vec<Layer>, order: Vec<Layer>,
visible_last_frame: HashSet<Layer>, visible_last_frame: HashSet<Layer>,
visible_current_frame: HashSet<Layer>, visible_current_frame: HashSet<Layer>,
/// When an area want to be on top, it is put in here.
/// At the end of the frame, this is used to reorder the layers.
/// This means if several layers want to be on top, they will keep their relative order.
/// So if you close three windows and then reopen them all in one frame,
/// they will all be sent to the top, but keep their previous internal order.
wants_to_be_on_top: HashSet<Layer>,
} }
impl Memory { impl Memory {
pub(crate) fn begin_frame(&mut self) { pub(crate) fn end_frame(&mut self) {
self.areas.begin_frame() self.areas.end_frame()
} }
/// 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
@ -64,7 +71,6 @@ impl Areas {
let did_insert = self.areas.insert(layer.id, state).is_none(); let did_insert = self.areas.insert(layer.id, state).is_none();
if did_insert { if did_insert {
self.order.push(layer); self.order.push(layer);
self.order.sort_by_key(|layer| layer.order);
} }
} }
@ -85,25 +91,34 @@ impl Areas {
None None
} }
pub fn visible_last_frame(&self, layer: &Layer) -> bool {
self.visible_last_frame.contains(layer)
}
pub fn is_visible(&self, layer: &Layer) -> bool { pub fn is_visible(&self, layer: &Layer) -> bool {
self.visible_last_frame.contains(layer) || self.visible_current_frame.contains(layer) self.visible_last_frame.contains(layer) || self.visible_current_frame.contains(layer)
} }
pub fn move_to_top(&mut self, layer: Layer) { pub fn move_to_top(&mut self, layer: Layer) {
self.visible_current_frame.insert(layer); self.visible_current_frame.insert(layer);
self.wants_to_be_on_top.insert(layer);
if self.order.last() == Some(&layer) { if self.order.iter().find(|x| **x == layer).is_none() {
return; // common case early-out self.order.push(layer);
} }
if let Some(index) = self.order.iter().position(|x| *x == layer) {
self.order.remove(index);
}
self.order.push(layer);
self.order.sort_by_key(|layer| layer.order);
} }
pub(crate) fn begin_frame(&mut self) { pub(crate) fn end_frame(&mut self) {
self.visible_last_frame = std::mem::take(&mut self.visible_current_frame); let Self {
visible_last_frame,
visible_current_frame,
order,
wants_to_be_on_top,
..
} = self;
*visible_last_frame = std::mem::take(visible_current_frame);
order.sort_by_key(|layer| (layer.order, wants_to_be_on_top.contains(layer)));
wants_to_be_on_top.clear();
} }
} }