mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 12:38:18 +00:00
Panels: focus moving between the app's windows can make floating panels disappear
Clicking between app windows (floating panels and the canvas) passes through one frame where none of them has focus. The palette levels counted the app inactive on that frame and lowered every floating panel's OS window. The panels flickered, could end up disappearing behind the main window until the next repaint, and a click on one panel's bar could hand the keyboard to another. The app now counts as inactive once none of its windows has had focus for 0.25 s, counted from the first frame that saw none. craft-ui's docking waits as long. Fixes #6986
This commit is contained in:
@@ -1355,15 +1355,35 @@ fn palette_window_state(modal: bool, popup: bool, active: bool) -> (egui::Window
|
||||
(level, true)
|
||||
}
|
||||
|
||||
/// How long none of the app's windows has focus before the app counts as inactive. Focus moving
|
||||
/// between the app's own windows passes through a frame where none has it (a press on a palette's
|
||||
/// bar takes it from the main window): lowering the palettes then let the window system give the
|
||||
/// keyboard to another palette in the middle of the press. craft-ui's docking waits as long.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
const AWAY_DELAY: f64 = 0.25;
|
||||
|
||||
/// Sends the palette windows of `layout` the level and visibility [`palette_window_state`] gives
|
||||
/// when they change. craft-ui varies a palette's level through the viewport builder, which egui
|
||||
/// applies when the window opens and not to one already on screen, so the commands go explicitly to
|
||||
/// the windows there are. A window that opens while the others are hidden (a panel floated under a
|
||||
/// dialog, a menu, a flyout or the drawer) opens shown at its palette level, so it gets the commands
|
||||
/// in its first frame.
|
||||
/// in its first frame. The app is active until none of its windows has had focus for
|
||||
/// [`AWAY_DELAY`], counted from the first frame that saw none.
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
fn palette_window_levels(ctx: &egui::Context, layout: &Layout<String>, modal: bool, popup: bool) {
|
||||
let active = ctx.input(|i| i.focused || i.raw.viewports.values().any(|v| v.focused == Some(true)));
|
||||
let (focused, now) = ctx.input(|i| (i.focused || i.raw.viewports.values().any(|v| v.focused == Some(true)), i.time));
|
||||
let away_key = area_id().with("palette-levels-away");
|
||||
let active = if focused {
|
||||
ctx.data_mut(|d| d.remove::<f64>(away_key));
|
||||
true
|
||||
} else {
|
||||
let since = ctx.data_mut(|d| *d.get_temp_mut_or_insert_with(away_key, || now));
|
||||
let left = AWAY_DELAY - (now - since);
|
||||
if left > 0.0 {
|
||||
ctx.request_repaint_after(std::time::Duration::from_secs_f64(left));
|
||||
}
|
||||
left > 0.0
|
||||
};
|
||||
let key = area_id().with("palette-levels-state");
|
||||
let state = (modal, popup, active);
|
||||
let windows: Vec<u64> = layout.floating.iter().map(|window| window.id).collect();
|
||||
|
||||
@@ -735,6 +735,40 @@ fn a_palette_window_keeps_out_of_the_way_of_the_main_windows_dialog_and_menus()
|
||||
assert_ne!(inactive.0, WindowLevel::AlwaysOnTop, "and not above other applications' windows: {:?}", inactive.0);
|
||||
}
|
||||
|
||||
/// Focus moving between the app's own windows passes through a frame where none has it: the
|
||||
/// palettes keep their level then, or the window system hands the keyboard to another palette in
|
||||
/// the middle of a press on a palette's bar. They leave the top level once the app has had no
|
||||
/// focus for a while.
|
||||
#[test]
|
||||
fn focus_moving_between_the_apps_windows_leaves_the_palettes_on_top() {
|
||||
let mut h = Harness::new();
|
||||
let viewport = h.float("navigator");
|
||||
h.host.borrow_mut().focus = ViewportId::ROOT;
|
||||
h.frames(2);
|
||||
take_palette_commands(&h, viewport);
|
||||
let nowhere = ViewportId::from_hash_of("no window of the app");
|
||||
h.host.borrow_mut().focus = nowhere;
|
||||
h.frames(1);
|
||||
h.host.borrow_mut().focus = viewport;
|
||||
h.frames(2);
|
||||
assert_eq!(take_palette_commands(&h, viewport), (vec![], vec![]), "a frame without focus leaves the palette's level");
|
||||
// The wait starts again each time focus leaves: a second frame without focus, longer after
|
||||
// the first than the wait, leaves the level too.
|
||||
h.frames(6);
|
||||
h.host.borrow_mut().focus = nowhere;
|
||||
h.frames(1);
|
||||
h.host.borrow_mut().focus = ViewportId::ROOT;
|
||||
h.frames(2);
|
||||
assert_eq!(take_palette_commands(&h, viewport), (vec![], vec![]), "the wait is counted from when focus last left");
|
||||
h.host.borrow_mut().focus = nowhere;
|
||||
h.frames(8);
|
||||
let (_, levels) = take_palette_commands(&h, viewport);
|
||||
assert_eq!(levels.last(), Some(&palette_window_state(false, false, false).0), "the app left: the palette leaves the top level");
|
||||
h.host.borrow_mut().focus = ViewportId::ROOT;
|
||||
h.frames(2);
|
||||
assert_eq!(take_palette_commands(&h, viewport).1, [egui::WindowLevel::AlwaysOnTop], "and takes it again when the app is back");
|
||||
}
|
||||
|
||||
/// With a menu of the in-window menu bar open, the palettes take the normal level (still shown):
|
||||
/// the menu is drawn in the main window, or it would open under them.
|
||||
#[test]
|
||||
|
||||
File diff suppressed because one or more lines are too long
+2
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user