mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 12:32:10 +01:00
Popups: a menu a memoized view replays keeps its opener's last-shown pass
A container hung from an opener records the last pass it ran. A memoized view that holds it replays it without running it, so after a few replayed frames the next real run found a gap and closed a menu the view still showed. The stamp is now journaled: a replay re-issues it for the pass it replays in, so a replayed container is shown as the container that ran. Trial: a menu inside a MemoView, opened from the keyboard, stays open through three replayed frames and the frame the view runs again.
This commit is contained in:
1 parent
7bd9cbe80f
commit
feb2ae2505
3 files changed
+85
No files matched your search
@@ -22,6 +22,30 @@ struct OpenerOpen {
|
||||
by: Option<(Id, Trigger, Opening)>,
|
||||
}
|
||||
|
||||
/// The note a replay of the container's run re-issues: its first opener, so the slot's last-shown frame is the replay's
|
||||
/// pass ([`refresh_seen`]). An id does not move: a replay at another place keeps it.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
struct SeenNote(Id);
|
||||
|
||||
impl crate::runtime::shift::Shift for SeenNote {
|
||||
fn shifted(&self, _: &egui::RecordShift) -> Self {
|
||||
*self
|
||||
}
|
||||
|
||||
fn snapped(&self, _: &Self, _: f32) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
/// The container `note` names ran in this pass: its opener slot's last-shown frame becomes the replay's, so the next real
|
||||
/// run finds no gap (`opener_open`).
|
||||
fn refresh_seen(ctx: &egui::Context, note: &SeenNote) {
|
||||
let frame = crate::runtime::egui::pass::root_frame_nr(ctx);
|
||||
let mut own = MemoryStage::open_after(ctx, note.0, note.0);
|
||||
own.session::<OpenerOpen>(sub(note.0, "opens")).seen = frame;
|
||||
own.write_back();
|
||||
}
|
||||
|
||||
/// The open state of a container with no binding: the slot of its first opener, with the triggers of every opener
|
||||
/// applied once for the frame. A container with no opener keeps its own slot.
|
||||
pub(crate) fn opener_open(stage: &mut MemoryStage, first: &Opener) -> bool {
|
||||
@@ -46,6 +70,9 @@ pub(crate) fn opener_open(stage: &mut MemoryStage, first: &Opener) -> bool {
|
||||
}
|
||||
if real {
|
||||
slot.seen = frame;
|
||||
// A memoized view holding this container replays it without running it (nothing in it changed), so the replay
|
||||
// re-issues this note: the container does not read as gone in the pass it runs again and close a menu it shows.
|
||||
crate::runtime::replay::journal(&ctx, || SeenNote(first.id), refresh_seen);
|
||||
}
|
||||
if let Some(opener) = first.all().find(|opener| opener.fired) {
|
||||
if slot.applied != frame {
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
//! A menu inside a memoized view: while the view replays, the container does not run, so its opener slot's
|
||||
//! last-shown frame must be re-issued by the replay. Otherwise the pass the view runs again reads the container as
|
||||
//! gone and closes a menu it still shows.
|
||||
|
||||
use craft_ui::buttons::Button;
|
||||
use craft_ui::contract::{MemoProps, MemoView, Show as _, ShowContainer as _, Trigger};
|
||||
use craft_ui::menu::{Menu, MenuItem};
|
||||
use craft_ui::runtime::focus::{self, FocusTarget};
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Id, Key, Modifiers, Ui};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::rc::Rc;
|
||||
|
||||
thread_local! {
|
||||
/// The id of the "Options" opener's response, from its last run.
|
||||
static OPENER: RefCell<Option<Id>> = const { RefCell::new(None) };
|
||||
}
|
||||
|
||||
/// A bar whose opener hangs the "Options menu"; `version` is its key, so a changed version runs the view again.
|
||||
#[derive(MemoProps)]
|
||||
struct Bar {
|
||||
version: u32,
|
||||
}
|
||||
|
||||
fn bar(props: &mut Bar, ui: &mut Ui) {
|
||||
let _ = props.version;
|
||||
let picker = Button::new("Options").show(ui);
|
||||
OPENER.with(|opener| *opener.borrow_mut() = Some(picker.id));
|
||||
Menu::new("Options menu").opened_by(&picker, Trigger::Click).show(ui, |ui, _| {
|
||||
MenuItem::new("Invert").show(ui);
|
||||
});
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_menu_in_a_memoized_view_stays_open_when_the_view_runs_again_after_replays() {
|
||||
let version = Rc::new(Cell::new(0u32));
|
||||
let key = Rc::clone(&version);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let _ = MemoView::new("bar", Bar { version: key.get() }, bar).show(ui);
|
||||
});
|
||||
fixture.settle();
|
||||
let opener = OPENER.with(|opener| opener.borrow().expect("the opener ran"));
|
||||
focus::request(fixture.ctx(), FocusTarget::Id(opener));
|
||||
fixture.settle();
|
||||
fixture.key(Key::Enter, Modifiers::NONE);
|
||||
fixture.settle();
|
||||
assert!(fixture.by_label("Invert").is_some(), "Enter on the opener opens the menu");
|
||||
for _ in 0..3 {
|
||||
fixture.frame(vec![]);
|
||||
fixture.settle();
|
||||
}
|
||||
assert!(fixture.by_label("Invert").is_some(), "the menu stays open while the view replays it");
|
||||
version.set(1);
|
||||
fixture.frame(vec![]);
|
||||
fixture.settle();
|
||||
assert!(fixture.by_label("Invert").is_some(), "the menu stays open in the pass the view runs again");
|
||||
}
|
||||
@@ -5,6 +5,7 @@ pub(crate) mod bar;
|
||||
pub(crate) mod anchor_gap;
|
||||
|
||||
pub(crate) mod closed;
|
||||
pub(crate) mod memo_kept;
|
||||
|
||||
pub(crate) mod context_capture;
|
||||
pub(crate) mod context_capture_guards;
|
||||
|
||||
Reference in new issue
Block a user