The slide keeps the keyboard after toolbar presses and dialogs; runtime windows carry the Nightly id

This commit is contained in:
aiongg committed 2026-11-25 14:12:47 +00:00
1 parent 44c80663b4
commit c74671d7c6
7 files changed
+156 -41

No files matched your search

+68 -32
View File
@@ -6,82 +6,118 @@ use craft_ui::runtime::overlay::{self, OpenFilter};
use super::{SlideApp, ViewMode};
use crate::framework::{document_id, focus_document, focused_widget, host_canvas};
/// How many passes the document asks for the focus: egui names the widget of a closed palette, dialog or menu as
/// focused for one pass after it was last drawn.
const ASKING_PASSES: u8 = 2;
/// What the document window's focus depends on from pass to pass.
#[derive(Default)]
pub(crate) struct DocumentFocus {
/// The presentation (its uid) whose document window the pass drew; `None` on the start screen, the File tab and in
/// a slide show.
pub(super) drawn: Option<u64>,
/// A presentation opened or became active and has yet to ask for the focus.
wanted: bool,
/// The passes left in which a presentation that opened or became active asks for the focus.
opened: u8,
/// The passes left in which the document asks for the focus after a dialog, menu or popup closed.
uncovered: u8,
}
impl SlideApp {
/// Draws the document window's widget over `rect` and declares it as the window's canvas. A press in it gives it
/// the focus. Real presses only: with the document focused, egui makes Enter and Space clicks of it.
/// the focus, unless a dialog is open. Real presses only: with the document focused, egui makes Enter and Space
/// clicks of it.
pub(crate) fn document_widget(&mut self, ui: &egui::Ui, rect: egui::Rect, sense: egui::Sense) -> egui::Response {
let resp = ui.interact(rect, document_id(), sense);
self.document_focus.drawn = self.session.active().map(|d| d.uid);
host_canvas(ui.ctx(), self.document_keeps_tab(ui.ctx()));
if resp.is_pointer_button_down_on() && ui.input(|i| i.pointer.any_pressed()) {
if resp.is_pointer_button_down_on() && ui.input(|i| i.pointer.any_pressed()) && !self.dialog_open(ui.ctx()) {
focus_document(ui.ctx());
}
resp
}
/// [`Self::document_widget`] behind widgets that take the presses (the slide sorter's thumbnails): a press on any of
/// them gives the document the focus, unless a text field has it.
pub(crate) fn document_behind(&mut self, ui: &egui::Ui, rect: egui::Rect) {
self.document_widget(ui, rect, egui::Sense::focusable_noninteractive());
let pressed = ui.input(|i| i.pointer.any_pressed()) && ui.rect_contains_pointer(rect);
if pressed && !ui.ctx().text_edit_focused() && !self.overlay_open(ui.ctx()) {
focus_document(ui.ctx());
}
}
/// Whether the document window is the slide being edited, where Tab can have work.
fn editing_slide(&self) -> bool {
self.backstage.is_none() && matches!(self.ui.view, ViewMode::Normal | ViewMode::Outline | ViewMode::Reading)
}
/// Whether the focused document keeps Tab: on the slide, while the session has something for it to do. A modal
/// dialog, a menu or a popup that is up gets it instead.
fn document_keeps_tab(&self, ctx: &egui::Context) -> bool {
matches!(self.ui.view, ViewMode::Normal | ViewMode::Outline | ViewMode::Reading) && self.session.tab_has_work() && !self.overlay_open(ctx)
self.editing_slide() && self.session.tab_has_work() && !self.overlay_open(ctx)
}
/// Whether a modal dialog, DeckCraft's or the framework's, the palette, the opening overlay, or an egui menu or
/// popup is up.
pub(super) fn overlay_open(&self, ctx: &egui::Context) -> bool {
/// Whether a dialog, DeckCraft's or a modal one of the framework's, the palette or the opening overlay is up.
fn dialog_open(&self, ctx: &egui::Context) -> bool {
self.dialog.as_ref().is_some_and(|d| d.modal())
|| self.palette.is_some()
|| self.opening.is_some()
|| ctx.any_popup_open()
|| egui::Popup::is_any_open(ctx)
|| overlay::any_open(ctx, OpenFilter::Modal)
}
/// Whether a dialog ([`Self::dialog_open`]), or an egui menu or popup, is up.
fn overlay_open(&self, ctx: &egui::Context) -> bool {
self.dialog_open(ctx) || ctx.any_popup_open() || egui::Popup::is_any_open(ctx)
}
/// Whether the document may take the focus: no text field and no widget of another layer (the framework's update
/// notice) has it.
fn focus_is_free(ctx: &egui::Context) -> bool {
let layer = |id| ctx.read_response(id).map(|widget| widget.layer_id);
!ctx.text_edit_focused() && focused_widget(ctx).is_none_or(|id| layer(id) == layer(document_id()))
}
/// Ends the document window's pass, after the views; `before` is the presentation the last pass drew. The pass's
/// input has changed what Tab has to do, so the document is declared again. One that opened or became active asks
/// for the focus. It waits for a modal dialog, menu or popup to close, and leaves the focus with a text field and
/// with a widget of another layer.
/// input has changed what Tab has to do, so the document is declared again. A presentation that opened or became
/// active asks for the focus, and so does the document when a dialog, menu or popup closes and leaves nothing
/// focused. Both wait for the last of them to close.
pub(super) fn finish_document(&mut self, ctx: &egui::Context, before: Option<u64>) {
let drawn = self.document_focus.drawn;
if drawn.is_none() {
self.document_focus.wanted = false;
self.document_focus.opened = 0;
self.document_focus.uncovered = 0;
return;
}
host_canvas(ctx, self.document_keeps_tab(ctx));
self.document_focus.wanted |= drawn != before;
if !self.document_focus.wanted || self.overlay_open(ctx) {
if drawn != before {
self.document_focus.opened = ASKING_PASSES;
}
if self.overlay_open(ctx) {
self.document_focus.uncovered = ASKING_PASSES;
return;
}
self.document_focus.wanted = false;
let layer = |id| ctx.read_response(id).map(|widget| widget.layer_id);
if !ctx.text_edit_focused() && focused_widget(ctx).is_none_or(|id| layer(id) == layer(document_id())) {
let DocumentFocus { opened, uncovered, .. } = &mut self.document_focus;
if *opened == 0 && *uncovered == 0 {
return;
}
let unfocused = ctx.memory(|m| m.focused().is_none());
if unfocused || (*opened > 0 && Self::focus_is_free(ctx)) {
(*opened, *uncovered) = (0, 0);
focus_document(ctx);
} else {
(*opened, *uncovered) = (opened.saturating_sub(1), uncovered.saturating_sub(1));
ctx.request_repaint();
}
}
/// While slide text is being edited, the text owns the keyboard (#93): a stray focus on an ordinary widget (the
/// window's drag handle, a ribbon button) would swallow every later key and end text entry, and with nothing
/// focused Tab would leave the text. The document takes the focus back; a text field or an open menu keeps its keys.
/// While slide text is being edited, the text owns the keyboard (#93). The focus on an ordinary widget (the ribbon
/// button whose Space inserted the text box) would swallow every later key and end text entry, and with nothing
/// focused (a ribbon field committed with Enter) Tab would leave the text. The document takes the focus; a text
/// field or an open menu keeps its keys.
pub(super) fn edited_text_takes_focus(&self, ctx: &egui::Context) {
let editing = self.session.active().is_some_and(|d| d.selection.text.is_some());
if !editing || ctx.text_edit_focused() || self.overlay_open(ctx) {
return;
}
if ctx.read_response(document_id()).is_some() {
if !ctx.memory(|m| m.has_focus(document_id())) {
focus_document(ctx);
}
} else if let Some(id) = ctx.memory(|m| m.focused()) {
ctx.memory_mut(|m| m.surrender_focus(id));
let editing = self.editing_slide() && self.session.active().is_some_and(|d| d.selection.text.is_some());
if editing && !ctx.memory(|m| m.has_focus(document_id())) && !self.overlay_open(ctx) && Self::focus_is_free(ctx) {
focus_document(ctx);
}
}
}
+3 -1
View File
@@ -7,7 +7,9 @@ pub(super) fn context_menu(app: &mut SlideApp, resp: &egui::Response) {
{
app.session.tool.edit_vertex = Some((path, index));
}
resp.context_menu(|ui| {
// Real clicks only: with the canvas focused, egui's own context menu would close on Enter and Space.
let open = if resp.secondary_clicked() { Some(true) } else { resp.clicked_by(egui::PointerButton::Primary).then_some(false) };
egui::Popup::context_menu(resp).open_memory(open.map(egui::SetOpenCommand::Bool)).show(|ui| {
let editing = app.session.active().and_then(|d| app.session.tool.editing_points(d).and_then(|id| d.shape(id).cloned()));
if let Some(sh) = editing {
edit_points_menu(app, ui, &sh);
@@ -4,6 +4,7 @@ use deckcraft_engine::Session;
use egui::{Event, Id, Key, Modifiers, PointerButton, Role, vec2};
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable as _;
use serde_json::json;
use crate::framework::document_id;
use crate::state::ViewMode;
@@ -84,6 +85,71 @@ fn a_press_on_the_slide_takes_the_keyboard_from_a_focused_button() {
assert_eq!(h.state().session.active().unwrap().doc.slides.len(), 2, "Enter adds a slide");
}
#[test]
fn after_a_dialog_closes_tab_selects_the_next_object() {
let mut h = harness();
h.state_mut().dialog = Some(crate::dialogs::Dialog::new("find"));
h.run_steps(3);
assert!(h.ctx.text_edit_focused(), "the dialog's field has the focus");
h.state_mut().dialog = None;
h.run_steps(4);
let first = h.state().session.active().unwrap().shapes()[0].id;
tab(&mut h);
assert_eq!((selected(&h), focused(&h)), (vec![first], Some(document_id())));
}
#[test]
fn a_press_in_the_sorter_takes_the_keyboard_from_a_focused_button() {
let mut h = harness();
h.state_mut().ui.view = ViewMode::Sorter;
h.run_steps(3);
h.get_by_role_and_label(Role::Button, "File").focus();
h.run_steps(2);
assert_ne!(focused(&h), Some(document_id()));
let pos = h.get_by_role_and_label(Role::Button, "Slide 1").rect().center();
h.hover_at(pos);
h.run_steps(1);
h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE });
h.run_steps(2);
assert_eq!(focused(&h), Some(document_id()));
}
#[test]
fn edited_text_traps_no_focus_in_the_sorter_or_the_file_tab() {
let mut h = harness();
let title = h.state().session.active().unwrap().shapes()[0].id.0;
h.state_mut().session.execute("text.edit", &json!({"id": title})).unwrap();
h.run_steps(2);
h.state_mut().ui.view = ViewMode::Sorter;
h.run_steps(2);
tab(&mut h);
let stop = focused(&h);
assert!(stop.is_some_and(|id| id != document_id()), "Tab moves on from the sorter");
tab(&mut h);
assert!(focused(&h).is_some_and(|id| Some(id) != stop && id != document_id()), "and on again");
h.state_mut().open_backstage(crate::backstage::Page::Home);
h.run_steps(3);
tab(&mut h);
h.run_steps(2);
assert!(h.state().session.active().unwrap().selection.text.is_some());
assert!(focused(&h).is_some(), "Tab stays on a control of the File tab");
}
#[test]
fn a_presentation_opened_from_the_palette_gets_the_keyboard() {
let mut h = harness();
h.state_mut().run("app.palette", json!({})).unwrap();
h.run_steps(3);
assert!(h.ctx.text_edit_focused(), "the palette's field has the focus");
let app = h.state_mut();
app.palette = None;
app.run("file.new", json!({})).unwrap();
app.run("slide.new", json!({"layout": "blank"})).unwrap();
assert!(!app.session.tab_has_work(), "an empty slide, which asks for the focus only because it opened");
h.run_steps(4);
assert_eq!(focused(&h), Some(document_id()));
}
#[test]
fn the_focused_slide_takes_arrows_escape_typing_and_tab() {
let mut h = harness();
+4 -3
View File
@@ -123,7 +123,8 @@ fn hex(c: [u8; 4]) -> String {
}
/// The slide canvas while the Eyedropper waits: the pointer is a crosshair with the colour under it
/// beside it, a click takes it. Returns true when it used the pointer (the tools get none).
/// beside it, a real click takes it (Enter and Space are egui's fake clicks of the focused canvas). Returns true when it
/// used the pointer (the tools get none).
pub fn canvas(app: &mut SlideApp, ui: &Ui, resp: &egui::Response, xf: Xf) -> bool {
let Some(state) = app.eyedropper.as_ref() else { return false };
let escape = crate::framework::escape_pressed(ui.ctx());
@@ -135,7 +136,7 @@ pub fn canvas(app: &mut SlideApp, ui: &Ui, resp: &egui::Response, xf: Xf) -> boo
let on_slide = app.slide_rect.is_some_and(|r| r.contains(pos));
if !on_slide {
ui.ctx().set_cursor_icon(CursorIcon::NotAllowed);
if resp.clicked() {
if resp.clicked_by(egui::PointerButton::Primary) {
app.eyedropper = None;
}
return true;
@@ -146,7 +147,7 @@ pub fn canvas(app: &mut SlideApp, ui: &Ui, resp: &egui::Response, xf: Xf) -> boo
if let Some(c) = under {
loupe(ui, pos, c);
}
if resp.clicked() {
if resp.clicked_by(egui::PointerButton::Primary) {
let (pick, slide) = (state.pick, state.slide);
app.eyedropper = None;
match pick {
+13
View File
@@ -59,6 +59,19 @@ fn selection(h: &Harness<'static, SlideApp>) -> Vec<u32> {
h.state().session.doc().unwrap().selection.shapes.iter().map(|s| s.0).collect()
}
#[test]
fn space_on_the_focused_slide_picks_no_colour() {
let mut h = harness();
ask(&mut h);
let on_shape = at(&h, 150.0, 150.0);
h.input_mut().events.push(Event::PointerMoved(on_shape));
h.run_steps(3);
assert!(h.ctx.memory(|m| m.has_focus(crate::framework::document_id())));
h.key_press(egui::Key::Space);
h.run_steps(3);
assert!(h.state().eyedropper.is_some() && !result_waits_for(&h.ctx, menu()));
}
#[test]
fn a_click_on_the_slide_takes_the_colour_under_it_and_nothing_else_happens() {
let mut h = harness();
+1 -4
View File
@@ -29,9 +29,6 @@ use crate::SlideApp;
#[cfg(not(target_arch = "wasm32"))]
pub use craft_ui::shell::embed::before_ui;
/// The id every OS window of the app carries, as the main window's builder sets it.
const APP_ID: &str = "ai.storyteller.deckcraft";
/// The scripts of the interface languages, for the font stack.
static SCRIPTS: I18nScripts = I18nScripts;
@@ -42,7 +39,7 @@ fn ui_fonts() -> UiFonts<'static> {
}
fn install_with(ctx: &egui::Context, shell: ShellInfo, windows: Arc<dyn WindowSystem>) -> Result<(), String> {
let spec = craft_ui::InstallSpec::new(shell, windows).app_id(APP_ID);
let spec = craft_ui::InstallSpec::new(shell, windows).app_id(crate::os_window::app_id());
embed::install(ctx, spec, &ui_fonts()).map(drop).map_err(|error| format!("the UI runtime could not be installed: {error}"))
}
+1 -1
View File
@@ -279,7 +279,7 @@ pub fn show(app: &mut SlideApp, ui: &mut Ui) {
let zoom = app.ui.sorter_zoom.clamp(0.5, 3.0);
let grouped = groups(&doc);
// Behind the thumbnails, which take the presses: the document's Tab stop and key target.
app.document_widget(ui, ui.available_rect_before_wrap(), Sense::focusable_noninteractive());
app.document_behind(ui, ui.available_rect_before_wrap());
egui::ScrollArea::vertical().auto_shrink([false, false]).show(ui, |ui| {
ui.add_space(12.0);
ui.horizontal(|ui| {