diff --git a/crates/ui-egui/src/app/document_focus.rs b/crates/ui-egui/src/app/document_focus.rs deleted file mode 100644 index fee5c624b..000000000 --- a/crates/ui-egui/src/app/document_focus.rs +++ /dev/null @@ -1,125 +0,0 @@ -//! The document window (the slide canvas, the slide sorter or the notes page) as the window's one document canvas: its -//! widget, its Tab stop and when it takes the keyboard focus. - -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, - /// 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, 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()) && !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 { - self.editing_slide() && self.session.tab_has_work() && !self.overlay_open(ctx) - } - - /// 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() - || 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. A presentation that opened or became - /// active asks for the focus. So does the document with nothing focused, when a dialog, menu or popup has closed, and - /// while Tab has work on the slide (a ribbon field committed with Enter leaves nothing focused). All wait for the - /// last dialog, menu or popup to close. - pub(super) fn finish_document(&mut self, ctx: &egui::Context, before: Option) { - let drawn = self.document_focus.drawn; - if drawn.is_none() { - self.document_focus.opened = 0; - self.document_focus.uncovered = 0; - return; - } - let keeps_tab = self.document_keeps_tab(ctx); - host_canvas(ctx, keeps_tab); - if drawn != before { - self.document_focus.opened = ASKING_PASSES; - } - if self.overlay_open(ctx) { - self.document_focus.uncovered = ASKING_PASSES; - return; - } - let unfocused = ctx.memory(|m| m.focused().is_none()); - let DocumentFocus { opened, uncovered, .. } = &mut self.document_focus; - if *opened == 0 && *uncovered == 0 && !(unfocused && keeps_tab) { - return; - } - 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). 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.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); - } - } -} diff --git a/crates/ui-egui/src/document_focus_tests.rs b/crates/ui-egui/src/document_focus_tests.rs deleted file mode 100644 index 6eabc22d9..000000000 --- a/crates/ui-egui/src/document_focus_tests.rs +++ /dev/null @@ -1,232 +0,0 @@ -//! The document window as the window's one canvas stop: Tab, real presses and its keys. - -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; -use crate::{Services, SlideApp}; - -fn harness() -> Harness<'static, SlideApp> { - let mut session = Session::with_new(); - session.prefs.autocorrect = false; - let mut app = SlideApp::new(session, Services::default()); - app.ui.notes = false; - let mut h = Harness::builder().with_context_setup(crate::framework::install_headless).with_size(vec2(1200.0, 800.0)).build_ui_state( - |ui, app: &mut SlideApp| { - app.logic(ui.ctx()); - app.ui(ui); - }, - app, - ); - h.run_steps(4); - h -} - -fn focused(h: &Harness<'_, SlideApp>) -> Option { - h.ctx.memory(|m| m.focused()) -} - -fn press(h: &mut Harness<'_, SlideApp>, modifiers: Modifiers, key: Key) { - h.key_press_modifiers(modifiers, key); - h.run_steps(2); -} - -fn tab(h: &mut Harness<'_, SlideApp>) { - press(h, Modifiers::NONE, Key::Tab); -} - -fn selected(h: &Harness<'_, SlideApp>) -> Vec { - h.state().session.active().unwrap().selection.shapes.clone() -} - -#[test] -fn tab_selects_the_slides_objects_then_moves_on_and_shift_tab_walks_back() { - let mut h = harness(); - assert_eq!(focused(&h), Some(document_id()), "an opened presentation has the keyboard"); - let objects: Vec<_> = h.state().session.active().unwrap().shapes().iter().map(|s| s.id).collect(); - assert!(objects.len() > 1); - for object in &objects { - tab(&mut h); - assert_eq!((selected(&h), focused(&h)), (vec![*object], Some(document_id()))); - } - press(&mut h, Modifiers::SHIFT, Key::Tab); - let before = focused(&h); - assert!(before.is_some() && before != Some(document_id()), "with the last object selected, Shift+Tab leaves the slide"); - tab(&mut h); - assert_eq!(focused(&h), Some(document_id()), "Tab from the stop before it stops at the slide"); - tab(&mut h); - let after = focused(&h); - assert!(after.is_some() && after != Some(document_id()) && after != before, "past the last object, Tab moves on"); - press(&mut h, Modifiers::SHIFT, Key::Tab); - assert_eq!(focused(&h), Some(document_id()), "Shift+Tab walks back to the slide"); - assert_eq!(selected(&h), objects[objects.len() - 1..]); -} - -#[test] -fn a_press_on_the_slide_takes_the_keyboard_from_a_focused_button() { - let mut h = harness(); - h.get_by_role_and_label(Role::Button, "File").focus(); - h.run_steps(2); - assert!(h.get_by_role_and_label(Role::Button, "File").is_focused()); - let pos = h.state().canvas_rect.unwrap().right_bottom() - vec2(10.0, 10.0); - h.hover_at(pos); - h.run_steps(1); - for pressed in [true, false] { - h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }); - h.run_steps(1); - } - assert_eq!(focused(&h), Some(document_id())); - press(&mut h, Modifiers::NONE, Key::Enter); - assert!(!h.state().backstage_open(), "the File button isn't activated"); - assert_eq!(h.state().session.active().unwrap().doc.slides.len(), 2, "Enter adds a slide"); -} - -#[test] -fn after_a_press_on_a_pane_tab_selects_the_next_object() { - let mut h = harness(); - let pos = egui::pos2(h.state().ui.thumbs_width / 2.0, 300.0); - h.hover_at(pos); - h.run_steps(1); - for pressed in [true, false] { - h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }); - h.run_steps(2); - } - assert!(h.state().slides_focused, "the press is in the thumbnail pane"); - 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 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 after_enter_in_a_ribbon_field_tab_selects_the_next_object() { - let mut h = harness(); - h.state_mut().ui.tab = "Transitions".into(); - tab(&mut h); - let objects: Vec<_> = h.state().session.active().unwrap().shapes().iter().map(|s| s.id).collect(); - assert_eq!(selected(&h), objects[..1]); - h.get_by_role_and_label(Role::TextInput, "Duration value").focus(); - h.run_steps(2); - assert!(h.ctx.text_edit_focused()); - press(&mut h, Modifiers::NONE, Key::Enter); - assert!(!h.ctx.text_edit_focused(), "Enter commits the field"); - assert!(h.state().session.active().unwrap().selection.text.is_none(), "and reaches nothing on the slide"); - tab(&mut h); - assert_eq!((selected(&h), focused(&h)), (vec![objects[1]], 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(); - let left = |h: &Harness<'_, SlideApp>| { - let st = h.state().session.active().unwrap(); - deckcraft_engine::cmd::xfrm_of(&st.doc, &st.selection, &st.shapes()[0]).x - }; - tab(&mut h); - let x = left(&h); - press(&mut h, Modifiers::NONE, Key::ArrowRight); - assert!(left(&h) > x, "the arrow moves the object, not the focus"); - press(&mut h, Modifiers::NONE, Key::Escape); - assert!(selected(&h).is_empty()); - assert_eq!(focused(&h), Some(document_id()), "Escape leaves the focus on the slide"); - tab(&mut h); - h.event(Event::Text("ab".into())); - h.run_steps(2); - tab(&mut h); - h.event(Event::Text("c".into())); - h.run_steps(2); - let st = h.state().session.active().unwrap(); - assert_eq!(st.shapes()[0].text.as_ref().unwrap().text(), "ab\tc"); - assert!(st.selection.text.is_some()); - press(&mut h, Modifiers::NONE, Key::Escape); - assert!(h.state().session.active().unwrap().selection.text.is_none(), "Escape ends text editing"); - assert_eq!(focused(&h), Some(document_id())); -} - -#[test] -fn every_view_draws_the_document_and_the_file_tab_takes_its_focus() { - let mut h = harness(); - for view in [ViewMode::Outline, ViewMode::Reading, ViewMode::NotesPage, ViewMode::Sorter] { - h.state_mut().ui.view = view; - h.run_steps(3); - assert_eq!(focused(&h), Some(document_id()), "the focus stays in the document window"); - } - tab(&mut h); - assert!(focused(&h).is_some_and(|id| id != document_id()), "Tab has no work in the slide sorter"); - h.state_mut().ui.view = ViewMode::Normal; - h.state_mut().open_backstage(crate::backstage::Page::Home); - h.run_steps(3); - assert!(h.state().backstage_open()); - h.key_press(Key::Escape); - h.run_steps(3); - assert!(!h.state().backstage_open(), "Escape leaves the File tab"); - assert_eq!(focused(&h), Some(document_id()), "and the slide has the keyboard again"); -} diff --git a/crates/ui-egui/src/framework.rs b/crates/ui-egui/src/framework.rs deleted file mode 100644 index 6099d4a23..000000000 --- a/crates/ui-egui/src/framework.rs +++ /dev/null @@ -1,182 +0,0 @@ -//! The craft-ui runtime, hosted from DeckCraft's own eframe `App` (craft-libs docs/craft-framework.md, section 2.2). -//! The desktop app calls [`install`] once, [`SlideApp::before_input`] in `raw_input_hook`, [`SlideApp::close_path`] -//! last in `logic`, and [`before_ui`] first in `ui`; the browser build and tests install with [`install_headless`]. -//! `SlideApp::ui` runs the framework dialogs ([`SlideApp::framework_dialogs`]) after DeckCraft's own UI. -//! -//! The runtime takes Escape, Tab and the clipboard events for the whole window unless one of DeckCraft's own egui -//! widgets has the keyboard. The window's Escape is read with [`escape_pressed`] or [`take_window_cancel`]; the -//! document window is declared to the runtime with [`host_canvas`] each pass that draws it, which keeps the edit chords -//! and the clipboard events, and Tab while it has the focus and Tab has work, in egui's input for DeckCraft's own key -//! routing (`SlideApp::keyboard`). That routing has the keyboard unless [`widget_has_keyboard`]. - -use std::sync::Arc; - -use craft_host::target::ShellInfo; -use craft_host::{AppInfo, Host}; -use craft_keymap::{Chord, Key, Mods}; -use craft_ui::fonts::{I18nScripts, SystemFonts, UiFonts}; -use craft_ui::runtime::env::{RootEnv, Scale}; -use craft_ui::runtime::focus::{self, EditSet, FocusTarget, KeepKeys, KeyUse}; -use craft_ui::runtime::frame::{FrameInput, FrameOutput}; -use craft_ui::runtime::keys::{Intent, with_window_intents}; -use craft_ui::shell::embed::{self, CloseDecision}; -use craft_window::system::WindowSystem; - -use crate::SlideApp; - -/// Each pass of the root window, first in `App::ui`: hands the runtime the native window it answers the hit test of -/// (Windows) and fits the window to its monitor once. -#[cfg(not(target_arch = "wasm32"))] -pub use craft_ui::shell::embed::before_ui; - -/// The scripts of the interface languages, for the font stack. -static SCRIPTS: I18nScripts = I18nScripts; - -/// The runtime's first font stack: egui's bundled faces and no installed system fonts. DeckCraft's own definitions -/// (`theme::install_fonts_with`, in the first UI pass) replace it before anything is drawn. -fn ui_fonts() -> UiFonts<'static> { - UiFonts::new(&SCRIPTS).system_fonts(SystemFonts::Off) -} - -fn install_with(ctx: &egui::Context, shell: ShellInfo, windows: Arc) -> Result<(), String> { - 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}")) -} - -/// Installs the runtime on the desktop window's context, once, before the first pass. `forced_x11` when the app -/// opens its window through XWayland. DeckCraft draws its own title bar row (`ribbon::title_bar`) and gives the -/// runtime no frame decision, so the runtime takes no part of the window's move area or hit test. -#[cfg(not(target_arch = "wasm32"))] -pub fn install(ctx: &egui::Context, forced_x11: bool) -> Result<(), String> { - use craft_host::target::Backend; - let backend = if forced_x11 { Backend::X11 } else { Backend::from_env() }; - install_with(ctx, ShellInfo::default().backend(backend), Arc::new(craft_window::system::SystemWindows)) -} - -/// Installs the runtime on a context with no native window of its own: the browser build's canvas and test windows. -pub fn install_headless(ctx: &egui::Context) { - if let Err(error) = install_with(ctx, ShellInfo::default(), Arc::new(craft_window::system::NoWindows)) { - log::warn!("{error}"); - } -} - -/// Who DeckCraft is to the framework's dialogs (the update dialogs show its name and icon). -pub(crate) fn host() -> Arc { - Arc::new( - Host::builder(AppInfo { id: "deckcraft", name: "DeckCraft", version: env!("CARGO_PKG_VERSION") }) - .icon_png(include_bytes!("../../../assets/app-icon/hicolor/128x128/apps/ai.storyteller.deckcraft.png")) - .build(), - ) -} - -impl SlideApp { - /// The framework's part of `App::raw_input_hook`, once per frame before the first pass: takes what the runtime - /// gathered since the last frame and gives the coming passes the root values (theme mode, interface scale). - pub fn before_input(&mut self, ctx: &egui::Context) { - embed::before_input(ctx, |output| self.frame_input(output)); - } - - /// The root values of this frame. DeckCraft registers no commands with the runtime and lets it toggle no chrome, - /// so the output carries nothing it acts on. The interface scale is DeckCraft's own (`UiState::ui_scale`, which - /// `ui_setup` applies), so the runtime's zoom and DeckCraft's agree. - fn frame_input(&mut self, _output: FrameOutput) -> FrameInput { - let mode = if self.ui.brightness == crate::theme::Brightness::Dark { craft_theme::Mode::Dark } else { craft_theme::Mode::Light }; - // The slide show and the canvas read Ctrl/Cmd+wheel themselves, so a smooth Ctrl+wheel stays a wheel event. - let root = RootEnv::new().mode(mode).scale(Scale::new(self.ui.ui_scale.clamp(0.5, 3.0))).ctrl_wheel_pinch(false); - FrameInput::new(root).host(Arc::clone(&self.framework_host)) - } - - /// The framework's part of `App::logic`, after DeckCraft's own: the root window's close path. With unsaved - /// presentations the window stays open while the quit dialog asks; its answer closes the window again with - /// `quit_confirmed` set. - pub fn close_path(&mut self, ctx: &egui::Context) { - embed::logic(ctx, || { - if self.quit_confirmed || !self.session.documents().iter().any(|d| d.is_dirty()) { - return CloseDecision::Close; - } - self.show = None; - if self.dialog.as_ref().is_none_or(|d| d.id != "quit") { - self.dialog = Some(crate::dialogs::Dialog::new("quit")); - } - CloseDecision::Hold - }); - } -} - -/// Alt+1 to Alt+9, the Quick Access Toolbar's positions on Windows and Linux (`menus::ui_shortcut`). Claimed, so -/// the runtime's Alt+numpad code entry leaves them to DeckCraft's shortcuts. -const QUICK_ACCESS_KEYS: [Chord; 9] = [ - Chord::new(Mods::ALT, Key::Num1), - Chord::new(Mods::ALT, Key::Num2), - Chord::new(Mods::ALT, Key::Num3), - Chord::new(Mods::ALT, Key::Num4), - Chord::new(Mods::ALT, Key::Num5), - Chord::new(Mods::ALT, Key::Num6), - Chord::new(Mods::ALT, Key::Num7), - Chord::new(Mods::ALT, Key::Num8), - Chord::new(Mods::ALT, Key::Num9), -]; - -/// The keys DeckCraft's document window keeps while it has the focus or is the window's key target: every clipboard -/// event and edit chord, and the Quick Access Toolbar's Alt+digits, which `SlideApp::keyboard` routes itself. -const DOCUMENT_KEYS: KeyUse = KeyUse::CANVAS.with_edits(EditSet::ALL).with_claims(&QUICK_ACCESS_KEYS); - -/// The document window's widget: the slide canvas, the slide sorter or the notes page, whichever the view draws. -pub(crate) fn document_id() -> egui::Id { - egui::Id::unique("deckcraft_document") -} - -/// Declares DeckCraft's document window, drawn in this pass, as the window's canvas. With nothing focused it is the -/// window's key target: the edit chords, the clipboard events and the claimed Alt+digits stay in egui's input for -/// DeckCraft. Tab and Shift+Tab stop at it; with `keeps_tab` the focused document keeps them in egui's input, else they -/// move on from it. -pub(crate) fn host_canvas(ctx: &egui::Context, keeps_tab: bool) { - let keys = if keeps_tab { DOCUMENT_KEYS.with_keep(KeepKeys::Tab) } else { DOCUMENT_KEYS }; - focus::host_canvas(ctx, document_id(), keys); -} - -/// Gives the document window the keyboard focus. It is the runtime's focus, which an Escape does not take away. -pub(crate) fn focus_document(ctx: &egui::Context) { - focus::request(ctx, FocusTarget::Id(document_id())); -} - -/// The widget that has the keyboard, the document window left out: a focused document leaves the keyboard to -/// DeckCraft's own key routing, as with nothing focused. -pub(crate) fn focused_widget(ctx: &egui::Context) -> Option { - ctx.memory(|m| m.focused()).filter(|id| *id != document_id()) -} - -/// Whether a widget other than the document window has the keyboard. -pub(crate) fn widget_has_keyboard(ctx: &egui::Context) -> bool { - focused_widget(ctx).is_some() -} - -/// Whether Escape was pressed in this pass: from egui while one of DeckCraft's own widgets or popups has the keyboard, -/// otherwise the window's cancel, which the runtime routes to no component. Leaves it for later readers. -pub(crate) fn escape_pressed(ctx: &egui::Context) -> bool { - ctx.input(|i| i.key_pressed(egui::Key::Escape)) || window_escape(ctx) -} - -/// Whether the window's cancel (an Escape while nothing has framework focus, which the runtime takes out of egui's -/// events) is there and untaken. Key loops that read egui's events add it as an Escape press. -pub(crate) fn window_escape(ctx: &egui::Context) -> bool { - window_cancel(ctx, false) -} - -/// The Escape press [`window_escape`] stands for, for key loops over egui's events. -pub(crate) const ESCAPE_PRESS: egui::Event = - egui::Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: egui::Modifiers::NONE }; - -/// Takes the window's cancel (an Escape while nothing has framework focus): whether there was one. -pub(crate) fn take_window_cancel(ctx: &egui::Context) -> bool { - window_cancel(ctx, true) -} - -fn window_cancel(ctx: &egui::Context, take: bool) -> bool { - let mut pending = false; - with_window_intents(ctx, false, |intents| { - pending = intents.untaken(&Intent::Cancel); - take && pending && intents.take(Intent::Cancel) - }); - pending -} diff --git a/crates/ui-egui/src/theme.rs b/crates/ui-egui/src/theme.rs index 743234ef1..271e1672c 100644 --- a/crates/ui-egui/src/theme.rs +++ b/crates/ui-egui/src/theme.rs @@ -179,7 +179,7 @@ pub fn install_fonts(ctx: &egui::Context) { /// installed or embedded faces that draw text the interface faces lack (Hangul, Simplified /// Chinese, Devanagari…, #101). See [`crate::uifonts`]. pub fn install_fonts_with(ctx: &egui::Context, extra: &[std::sync::Arc]) { - craft_ui::fonts::load(ctx, font_definitions(extra)); + ctx.set_fonts(font_definitions(extra)); } /// The interface font definitions; see [`install_fonts_with`]. @@ -213,10 +213,6 @@ pub fn font_definitions(extra: &[std::sync::Arc]) -> // A bold family for headings and selected tabs. let mut bold: Vec = names.iter().filter(|(fam, st, _)| *fam == "Inter" && *st == "SemiBold").map(|(_, _, n)| n.clone()).collect(); bold.extend(defs.families.get(&FontFamily::Proportional).cloned().unwrap_or_default()); - // The framework's dialogs draw their titles in the semibold family and signatures in the - // signature family; DeckCraft's SemiBold chain and its proportional chain are those. - defs.families.insert(FontFamily::Name(craft_ui::fonts::SEMIBOLD.into()), bold.clone()); - defs.families.insert(craft_ui::fonts::FontRef::signature().family(), defs.families.get(&FontFamily::Proportional).cloned().unwrap_or_default()); defs.families.insert(FontFamily::Name("bold".into()), bold); // Hack is already bundled for Monospace and covers the interface's arrows and triangles. // Keep it after every proportional face so ordinary text metrics stay unchanged.