mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
ui-test-kit: a test can focus or close one of several child windows (C5-8) (#3722)
Windows::focus, ::close and ::child_facts target one child viewport; the other children keep their facts and get no close request. Claude-Session: https://claude.ai/code/session_01FypgCWNarDG3QfXK2YMzdx Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8bea6009f9
commit
134155e1fb
2 files changed
+87
-3
No files matched your search
@@ -38,6 +38,10 @@ thread_local! {
|
||||
/// The window facts of each child opened from another child (a popup window): it opens where its builder puts it
|
||||
/// and follows its own commands.
|
||||
static OWN_INFO: RefCell<Vec<(ViewportId, ViewportInfo)>> = const { RefCell::new(Vec::new()) };
|
||||
/// The child [`Windows::focus`] focused, whose window alone of the children reports focus; `None` until it is called.
|
||||
static FOCUSED: Cell<Option<ViewportId>> = const { Cell::new(None) };
|
||||
/// The children whose next pass reports a close request, as the window system sends one once.
|
||||
static CLOSES: RefCell<Vec<ViewportId>> = const { RefCell::new(Vec::new()) };
|
||||
/// The window facts the root reports.
|
||||
static ROOT_INFO: RefCell<ViewportInfo> = RefCell::new(ViewportInfo::default());
|
||||
/// The children whose windows are open: those that ran a pass and are still shown. Like an integration, every pass's
|
||||
@@ -85,6 +89,15 @@ const FRAME_SECONDS: f64 = 1.0 / 60.0;
|
||||
|
||||
/// Runs `f` on the facts `id` reports: its own for a child opened from another child, else the ones every child
|
||||
/// opened from the root shares.
|
||||
/// The window facts the child `id` reports: its own or the shared ones, with the focus [`Windows::focus`] gave.
|
||||
fn facts_of(id: ViewportId) -> ViewportInfo {
|
||||
let mut info = with_facts(id, |info| info.clone());
|
||||
if let Some(focused) = FOCUSED.with(Cell::get) {
|
||||
info.focused = Some(focused == id);
|
||||
}
|
||||
info
|
||||
}
|
||||
|
||||
fn with_facts<R>(id: ViewportId, f: impl FnOnce(&mut ViewportInfo) -> R) -> R {
|
||||
let own = OWN_INFO.with(|own| {
|
||||
let mut own = own.borrow_mut();
|
||||
@@ -221,8 +234,12 @@ fn render_child(ctx: &Context, viewport: egui::ImmediateViewport<'_>) {
|
||||
events.insert(0, egui::Event::ModifiersChanged(CHILD_MODIFIERS.with(Cell::get)));
|
||||
let (hovered_files, dropped) = CHILD_FILES.with(|files| std::mem::take(&mut *files.borrow_mut()));
|
||||
let dropped_files = dropped.into_iter().map(|name| Arc::new(DroppedPath(name.into())) as egui::DroppedFileHandle).collect();
|
||||
let mut info = with_facts(viewport.ids.this, |info| info.clone());
|
||||
let mut info = facts_of(this);
|
||||
info.parent = Some(viewport.ids.parent);
|
||||
if CLOSES.with(|closes| closes.borrow().contains(&this)) {
|
||||
CLOSES.with(|closes| closes.borrow_mut().retain(|held| *held != this));
|
||||
info.events.push(egui::ViewportEvent::Close);
|
||||
}
|
||||
let mut input = RawInput {
|
||||
viewport_id: viewport.ids.this,
|
||||
events,
|
||||
@@ -353,6 +370,8 @@ impl Windows {
|
||||
CHILD_INFO.with(|info| *info.borrow_mut() = ViewportInfo::default());
|
||||
OWN_INFO.with(|own| own.borrow_mut().clear());
|
||||
CHILDREN.with(|children| children.borrow_mut().clear());
|
||||
FOCUSED.with(|focused| focused.set(None));
|
||||
CLOSES.with(|closes| closes.borrow_mut().clear());
|
||||
COMMANDS.with(|kept| kept.borrow_mut().clear());
|
||||
BUILDERS.with(|kept| kept.borrow_mut().clear());
|
||||
TEXTURES.with(|textures| textures.borrow_mut().clear());
|
||||
@@ -373,9 +392,33 @@ impl Windows {
|
||||
}
|
||||
|
||||
/// The window facts `viewport` reports: its own for a child opened from another child, else those
|
||||
/// [`child_info`](Self::child_info) gave.
|
||||
/// [`child_info`](Self::child_info) gave, with the focus [`focus`](Self::focus) gave.
|
||||
pub fn facts(&self, viewport: ViewportId) -> ViewportInfo {
|
||||
with_facts(viewport, |info| info.clone())
|
||||
facts_of(viewport)
|
||||
}
|
||||
|
||||
/// Focuses the child `viewport`'s window and unfocuses every other child's, overriding the focus in
|
||||
/// [`child_info`](Self::child_info): from the next frame on their facts say so, and each open window whose focus
|
||||
/// changed gets `Event::WindowFocused`.
|
||||
pub fn focus(&self, viewport: ViewportId) {
|
||||
let mut open = CHILDREN.with(|children| children.borrow().clone());
|
||||
if !open.contains(&viewport) {
|
||||
open.push(viewport);
|
||||
}
|
||||
let before: Vec<_> = open.iter().map(|&id| facts_of(id).focused).collect();
|
||||
FOCUSED.with(|focused| focused.set(Some(viewport)));
|
||||
for (id, was) in open.into_iter().zip(before) {
|
||||
let now = id == viewport;
|
||||
if was != Some(now) {
|
||||
self.send(id, vec![Event::WindowFocused(now)]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Has the window system ask the child `viewport` to close: its next pass reports one close request, and no other
|
||||
/// child's does.
|
||||
pub fn close(&self, viewport: ViewportId) {
|
||||
CLOSES.with(|closes| closes.borrow_mut().push(viewport));
|
||||
}
|
||||
|
||||
/// The modifiers held in the child's window, which its passes report from now on.
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
//! The kit's window driver gives focus and close requests to one child of several.
|
||||
|
||||
use craft_ui_test_kit::windows::Windows;
|
||||
use egui::{ViewportBuilder, ViewportId, ViewportInfo};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
/// What each of three children saw in a frame: (close requested, focused).
|
||||
fn run(kit: &Windows) -> BTreeMap<&'static str, (bool, bool)> {
|
||||
let seen = RefCell::new(BTreeMap::new());
|
||||
kit.frame(Vec::new(), Vec::new(), |ui| {
|
||||
for name in ["a", "b", "c"] {
|
||||
ui.ctx().show_viewport_immediate(ViewportId::from_hash_of(name), ViewportBuilder::default(), |ui, _| {
|
||||
let (close, focused) = ui.ctx().input(|i| (i.viewport().close_requested(), i.viewport().focused == Some(true)));
|
||||
seen.borrow_mut().insert(name, (close, focused));
|
||||
});
|
||||
}
|
||||
});
|
||||
seen.into_inner()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn one_of_three_children_gets_the_close_and_the_focus() {
|
||||
let kit = Windows::new();
|
||||
let (a, b, c) = (ViewportId::from_hash_of("a"), ViewportId::from_hash_of("b"), ViewportId::from_hash_of("c"));
|
||||
run(&kit);
|
||||
kit.focus(b);
|
||||
kit.close(b);
|
||||
let seen = run(&kit);
|
||||
assert!(seen["b"].0 && !seen["a"].0 && !seen["c"].0, "only b is asked to close");
|
||||
assert!(seen["b"].1 && !seen["a"].1 && !seen["c"].1, "only b is focused");
|
||||
assert!(!run(&kit)["b"].0, "a close request comes once");
|
||||
kit.focus(c);
|
||||
let seen = run(&kit);
|
||||
assert!(seen["c"].1 && !seen["b"].1 && !seen["a"].1);
|
||||
assert_eq!(kit.facts(a).focused, Some(false));
|
||||
let scale = kit.facts(a).native_pixels_per_point;
|
||||
kit.child_info(ViewportInfo { native_pixels_per_point: Some(3.0), ..ViewportInfo::default() });
|
||||
assert_ne!(kit.facts(a).native_pixels_per_point, scale, "the shared facts still reach a child after a focus");
|
||||
assert_eq!(kit.facts(c).focused, Some(true), "and the focus outlasts them");
|
||||
}
|
||||
Reference in new issue
Block a user