A window's records survive the frame that opens it: closed viewports' records go as a frame begins

Every pass's end hook dropped the per-viewport records of viewports its input did not list. That list dates from the
frame's start, so the root's end hook dropped the records of a window the frame had opened. A palette that opened
hidden under an open menu lost the note that it was hidden, and stayed hidden once the menu was gone: Float in a docked
tab's menu left no window to see when the opening frame ran a second pass. The root's begin hook now drops the records
of closed viewports in a frame's first pass, where the list names every window opened before.
This commit is contained in:
aiongg
2026-11-25 18:16:05 +00:00
parent c79d8d399d
commit 6801f1f9b5
7 changed files with 229 additions and 15 deletions
@@ -83,8 +83,8 @@ pub(crate) fn note_pass_end(ctx: &Context) {
/// ending viewport's, or forgets that viewport's update while AccessKit is off.
pub(crate) fn settle_output(ctx: &Context, output: &mut egui::FullOutput) {
settle_focus(output);
// A closed window never runs again to replace or drop its entry, so the frame's end hook prunes the store to the
// viewports that are open.
// A closed window never runs again to replace or drop its entry, so the begin hook prunes the store to the viewports
// that are open as a frame begins.
crate::runtime::frame::register_prune(ctx, std::any::TypeId::of::<Settled>(), prune_settled);
let update = output.platform_output.accesskit_update.clone();
memory::with::<Settled, _>(ctx, |settled| {
+7 -1
View File
@@ -39,11 +39,17 @@ pub(crate) fn content_rect(ctx: &Context) -> egui::Rect {
ctx.content_rect()
}
/// The viewports egui reports for the running pass.
/// The viewports egui reports for the running pass: the windows open when its frame began, so one that opens in the
/// frame joins the list in the next, and one that closes in it leaves it there.
pub(crate) fn live_viewports(ctx: &Context) -> Vec<ViewportId> {
ctx.input(|input| input.raw.viewports.keys().copied().collect())
}
/// Whether the running pass is the first of its frame: not one that repeats a discarded pass.
pub(crate) fn first_of_frame(ctx: &Context) -> bool {
ctx.current_pass_index() == 0
}
/// The pass's time in seconds.
pub(crate) fn now(ctx: &Context) -> f64 {
ctx.input(|input| input.time)
@@ -69,7 +69,7 @@ pub(crate) fn request_discard(ctx: &Context, rerun: Rerun) {
/// Why the running pass runs again: `None` in a frame's first pass, else the reasons the discarded pass asked for
/// (empty when egui discarded it on its own).
pub(crate) fn rerun_reasons(ctx: &Context) -> Option<Vec<Rerun>> {
if ctx.current_pass_index() == 0 {
if super::pass::first_of_frame(ctx) {
return None;
}
let (id, pass) = (asked_id(ctx), running_pass(ctx));
+20 -11
View File
@@ -190,6 +190,7 @@ pub(crate) struct FrameState {
passes_open: u32,
/// How many root-viewport passes the begin hook has run for: egui counts a pass when it ends.
root_passes_hooked: u64,
/// What drops the records of closed viewports as a frame begins.
prunes: HashMap<TypeId, fn(&Context, &[ViewportId])>,
sweeps: HashMap<TypeId, fn(&Context)>,
/// What a layer above the runtime reads at the begin hook, after the pointer snapshot and before any component runs.
@@ -219,8 +220,8 @@ struct PendingToggle {
/// How many passes a shell has to take its toggle before the toggle is dropped.
const TOGGLE_PASSES: u64 = 2;
/// Records of one runtime service, one per viewport. A viewport egui stops reporting has its record dropped
/// at once, by the frame loop's end hook.
/// Records of one runtime service, one per viewport, kept from the viewport's first pass until it closes: the root's
/// begin hook drops the records of the viewports egui no longer reports as a frame begins.
#[doc(hidden)]
pub struct PerViewport<T> {
/// One or two entries: a linear search beats hashing.
@@ -374,13 +375,25 @@ impl<T: Stored> PerViewport<Stamped<T>> {
}
}
/// Registers the end hook's prune of a service's per-viewport records, once per service type.
/// Registers the prune of a service's per-viewport records to the open viewports ([`prune_closed`]), once per service
/// type.
pub(crate) fn register_prune(ctx: &Context, key: TypeId, prune: fn(&Context, &[ViewportId])) {
pass_state::with::<FrameState, _>(ctx, |state| {
state.prunes.entry(key).or_insert(prune);
});
}
/// Drops the records of the viewports that have closed, at the start of a frame. A pass's list of viewports dates from
/// the start of its frame, so only there does it name every viewport with a record that is still open: later in the
/// frame it lacks the ones the frame opened.
fn prune_closed(ctx: &Context) {
let live = pass::live_viewports(ctx);
let prunes: Vec<fn(&Context, &[ViewportId])> = pass_state::with::<FrameState, _>(ctx, |state| state.prunes.values().copied().collect());
for prune in prunes {
prune(ctx, &live);
}
}
/// Registers a sweep the end hook runs for the running viewport's pass, once per service type.
pub(crate) fn register_end_sweep(ctx: &Context, key: TypeId, sweep: fn(&Context)) {
pass_state::with::<FrameState, _>(ctx, |state| {
@@ -822,6 +835,9 @@ pub(crate) fn begin_pass(ui: &mut Ui) {
running.push(RunningPass { ctx: ctx.clone(), facts: PassFacts { viewport, pass_nr, frame_nr, now, elapsed } });
});
pass_state::hold(&ctx);
if root_pass.is_some() && pass::first_of_frame(&ctx) {
prune_closed(&ctx);
}
if cancelled {
raw_input::stop_dragging(&ctx);
}
@@ -885,14 +901,7 @@ pub(crate) fn end_pass(ui: &mut Ui) {
motion::end_pass(&ctx);
windows::end(&ctx);
ime::end_pass(&ctx);
let live = pass::live_viewports(&ctx);
let prunes: Vec<fn(&Context, &[ViewportId])> = pass_state::with::<FrameState, _>(&ctx, |state| {
state.passes_open = state.passes_open.saturating_sub(1);
state.prunes.values().copied().collect()
});
for prune in prunes {
prune(&ctx, &live);
}
pass_state::with::<FrameState, _>(&ctx, |state| state.passes_open = state.passes_open.saturating_sub(1));
#[cfg(feature = "testing")]
crate::runtime::egui::access::check_parents_acyclic(&ctx);
RUNNING.with(|running| {
@@ -1300,6 +1300,40 @@ fn a_palette_hides_while_the_root_shows_a_modal_or_popup_and_returns_when_it_clo
assert_eq!(back, Some(true), "the palette returns once the modal closed");
}
/// A palette that opens while the palettes still yield to a modal or popup of the root window that has just closed
/// opens hidden and appears in the frames after.
#[trial]
fn a_palette_opened_as_the_roots_modal_or_popup_closes_appears() {
let windows = Windows::new();
let frame = |modal: bool, palette: bool| {
windows.frame(vec![], vec![], |ui| {
if modal {
let props = ContainerProps::new().mount(Mount::Layer(Overlay::modal()));
Modal { common: Common::new(), props }.id_salt("modal").show(ui, |_, _| {});
}
if palette {
Palette::new("tool", Some(WindowSpec::tool())).show(ui, |_, _| {});
}
})
};
for _ in 0..3 {
frame(true, false);
}
frame(false, false);
let out = frame(false, true);
let (tool, builder) = builder_titled(&out, "tool");
let mut shown = builder.visible != Some(false);
assert!(!shown, "it opens hidden while the palettes yield");
for out in [out].into_iter().chain((0..3).map(|_| frame(false, true))) {
for (id, command) in out.commands {
if let (true, ViewportCommand::Visible(visible)) = (id == tool, command) {
shown = visible;
}
}
}
assert!(shown, "it appears once the modal is gone");
}
/// A key automation holds is in `keys::held` while it is held and not after its release.
#[trial]
fn a_key_held_by_automation_reads_as_held() {
@@ -36,6 +36,7 @@ pub(crate) mod workspace_dialogs;
pub(crate) mod native;
pub(crate) mod native_drag;
pub(crate) mod native_float;
pub(crate) mod panes;
/// The docking sizes of the default theme with the given divider gap, least pane and strip height.
@@ -0,0 +1,164 @@
//! Float in a docked tab's menu, in a dock that opens its floats in OS windows: the panel's window opens hidden, as the
//! palettes yield to the open menu, and shows once the menu is gone.
use super::Numbered;
use craft_behavior::dock::{Layout, Node, Permissions, Workspace};
use craft_host::target::{Caps, Os, ShellInfo};
use craft_ui::contract::{Bind, BodyCx, Role, Tracked};
use craft_ui::docking::{DockConfig, DockPanels};
use craft_ui::patterns::dock::DockArea;
use craft_ui::runtime::InstallSpec;
use craft_ui::testing::access::{AccessTree, focused_key, name};
use craft_ui::testing::input::key_press;
use craft_ui::testing::pointer::{moved, press, release, secondary};
use craft_ui::testing::trial;
use craft_ui::testing::windows::FakeSystem;
use craft_ui_test_kit::windows::Windows;
use egui::{Event, Key, Modifiers, Pos2, Rect, Ui, ViewportCommand, ViewportId, ViewportInfo};
use std::sync::Arc;
const FLOATED: usize = 1;
const LEFT: usize = 2;
/// The main window on the desktop.
const MAIN_AT: Rect = Rect { min: Pos2::new(100.0, 100.0), max: Pos2::new(900.0, 700.0) };
/// Where the OS puts the float's window.
const FLOAT_AT: Rect = Rect { min: Pos2::new(200.0, 180.0), max: Pos2::new(440.0, 480.0) };
/// The frames a menu's fade or a focus move may take.
const SETTLED: usize = 60;
/// The panel left behind asks for a second pass when it first shows, as egui's own containers do (`Grid`): in the frame
/// that opens the other panel's window.
struct Panels<'a> {
left_shown: &'a mut bool,
}
impl DockPanels<usize> for Panels<'_> {
fn body(&mut self, panel: &usize, ui: &mut Ui, _: &mut BodyCx<'_>) {
if *panel == LEFT && !std::mem::replace(self.left_shown, true) {
ui.ctx().request_discard("the panel left behind shows first");
}
}
}
/// A dock of one group of two panels, in a host that keeps the workspace and applies the dock's actions after its pass.
struct Host {
windows: Windows,
workspace: Arc<Workspace<usize>>,
specs: Tracked<Numbered>,
left_shown: bool,
/// What the main window's last pass produced.
tree: Option<AccessTree>,
/// The float's OS window and whether it is visible: as its first builder opened it, then as the commands left it.
window: Option<(ViewportId, bool)>,
}
impl Host {
fn new() -> Self {
let shell = ShellInfo::default().os(Os::Windows).caps(Caps::native(Os::Windows));
let windows = Windows::with(InstallSpec::new(shell, Arc::new(FakeSystem::new())));
windows.root_info(info(MAIN_AT));
windows.child_info(info(FLOAT_AT));
let workspace = Arc::new(Workspace::new(Layout::new(Some(Node::tabs(vec![FLOATED, LEFT], 0)), Vec::new())));
let mut host = Host { windows, workspace, specs: Tracked::new(Numbered), left_shown: false, tree: None, window: None };
for _ in 0..3 {
host.frame(vec![]);
}
host
}
fn frame(&mut self, events: Vec<Event>) {
let shown = Arc::clone(&self.workspace);
let mut actions = Vec::new();
let (specs, left_shown) = (&self.specs, &mut self.left_shown);
let out = self.windows.frame(events, vec![], |ui| {
let mut panels = Panels { left_shown: &mut *left_shown };
actions = DockArea::new("Dock", Bind::given(Arc::clone(&shown)), specs, &mut panels, DockConfig::default()).show(ui).actions;
});
for action in actions {
Arc::make_mut(&mut self.workspace).apply(action, false, |_| Permissions::ALL).expect("the dock's action applies");
}
if self.window.is_none() {
let opened = out.builders.iter().find(|(viewport, _)| *viewport != ViewportId::ROOT);
self.window = opened.map(|(viewport, builder)| (*viewport, builder.visible != Some(false)));
}
for (viewport, command) in &out.commands {
if let (ViewportCommand::Visible(visible), Some((window, shown))) = (command, &mut self.window)
&& viewport == window
{
*shown = *visible;
}
}
self.tree = out.trees.into_iter().find(|(viewport, _)| *viewport == ViewportId::ROOT).map(|(_, tree)| tree);
}
fn tree(&self) -> &AccessTree {
self.tree.as_ref().expect("the main window ran a pass")
}
/// A frame with `event`, then the frames the app asks for.
fn settle(&mut self, event: Event) {
self.frame(vec![event]);
for _ in 0..SETTLED {
if !self.windows.ctx.has_requested_repaint() {
return;
}
self.frame(vec![]);
}
}
/// Where the main window shows the `role` named `name`.
fn at(&self, name: &str, role: Role) -> Option<Pos2> {
let tree = self.tree();
let key = tree.with_role(role).into_iter().find(|key| tree.name_of(*key) == Some(name))?;
tree.bounds_of(key).map(|rect| rect.center())
}
fn assert_floats_in_a_shown_window(&self) {
let layout = self.workspace.layout();
assert!(layout.floating.iter().any(|window| window.contains(&FLOATED)), "the panel floats: {layout:?}");
assert!(self.window.is_some_and(|(_, shown)| shown), "its OS window shows: {:?}", self.window);
}
}
fn info(rect: Rect) -> ViewportInfo {
ViewportInfo {
focused: Some(true),
inner_rect: Some(rect),
outer_rect: Some(rect),
native_pixels_per_point: Some(1.0),
..ViewportInfo::default()
}
}
/// Float clicked in the menu a secondary click on the tab opens.
#[trial]
fn float_in_a_docked_tabs_menu_shows_the_panels_os_window() {
let mut host = Host::new();
let tab = host.at("Panel 1", Role::TAB).expect("the docked tab shows");
for event in [moved(tab), secondary(tab, true), secondary(tab, false)] {
host.settle(event);
}
let row = host.at("Float", Role::MENU_ITEM).expect("the tab menu's Float row");
for event in [moved(row), press(row), release(row)] {
host.settle(event);
}
host.assert_floats_in_a_shown_window();
}
/// Float picked with Enter in the menu Shift+F10 opens on the focused tab.
#[trial]
fn float_in_a_tab_menu_opened_by_the_keyboard_shows_the_panels_os_window() {
let mut host = Host::new();
host.settle(key_press(Key::Tab, Modifiers::NONE));
host.settle(key_press(Key::F10, Modifiers::SHIFT));
let rows = host.tree().with_role(Role::MENU_ITEM).len();
assert!(rows > 0, "the tab menu opened");
for _ in 0..rows {
if name(host.tree(), focused_key(host.tree())) == Some("Float") {
break;
}
host.settle(key_press(Key::ArrowDown, Modifiers::NONE));
}
host.settle(key_press(Key::Enter, Modifiers::NONE));
host.assert_floats_in_a_shown_window();
}