mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 21:12:33 +00:00
Scoped move keys: a widget that does not act on its moves leaves a move key a scoped command binds to it; PagePreview steps from anywhere in the preview
This commit is contained in:
@@ -44,7 +44,7 @@ pub use header_footer::{HeaderFooterPage, HeaderFooterZones, HeaderFooterZonesOu
|
||||
pub use list::{PageList, PageListEvent, PageListOutput, PageListRow, PageListWords};
|
||||
pub use margins::{MarginChange, MarginEdge, MarginHandles, MarginHandlesOutput, MarginRules, PageEdges, clamp_margin, dragged_margin, margin_range};
|
||||
pub use marks::{FrameExtras, HiddenMark, PlacedMark, TextFrameMarks};
|
||||
pub use preview::{PagePreview, PagePreviewOutput, PagePreviewWords};
|
||||
pub use preview::{PagePreview, PagePreviewCommands, PagePreviewOutput, PagePreviewWords};
|
||||
pub use range::{PageRangeCodec, PageRangeField, PageRangeFieldOutput, PageRangeOptions, PageRangeOptionsOutput, PageSelection};
|
||||
pub use reading::{ReadingNav, ReadingNavOutput, ReadingState, ReadingView, ReadingViewOutput};
|
||||
pub use setup::{PageLayoutChoice, PageLayoutMenus, PageSetupDialog};
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
//! `PagePreview`: a page shown large in a modal dialog with previous and next, as PdfCraft's Combine Files preview
|
||||
//! and VectorCraft's Place PDF page choice show it: the page as large as fits, "Page 3 of 12", the arrow and Page
|
||||
//! keys to step, Escape or the close action to leave.
|
||||
//! keys to step from anywhere in the preview, Escape or the close action to leave.
|
||||
|
||||
#[cfg(feature = "screen-testing")]
|
||||
mod stories;
|
||||
|
||||
use super::view::PageSource;
|
||||
use craft_behavior::IconId;
|
||||
use craft_behavior::{Input, Metrics, NavMove, PartKey, Sense};
|
||||
use craft_behavior::{Input, Metrics, PartKey, Sense};
|
||||
use craft_ui::buttons::IconButton;
|
||||
use craft_ui::contract::{
|
||||
Common, Component, Cx, Focus as _, MeasureCx, NoValue, Outcome, PaintUi, PartsCx, Props as _, Role, Show as _, ShowContainer as _, clicked,
|
||||
CommandSet, Common, Component, Cx, MeasureCx, NoValue, Outcome, PaintUi, PartsCx, Props as _, Role, Show as _, ShowContainer as _, clicked,
|
||||
};
|
||||
use craft_ui::dialog::Dialog;
|
||||
use craft_ui::layout_components::Row;
|
||||
@@ -18,8 +18,9 @@ use craft_ui::primitives::textures::{
|
||||
MAX_TEXTURE, Priority, TextureState, TextureStoreOptions, TextureWords, ThumbPaint, ThumbRequest, ThumbnailOptions, Thumbnails,
|
||||
};
|
||||
use craft_ui::primitives::{PaintShape, TextRun};
|
||||
use craft_ui::runtime::commands::Commands as _;
|
||||
use craft_ui::runtime::env::Env;
|
||||
use craft_ui::runtime::focus::{ActivateKeys, KeepKeys, KeyUse};
|
||||
use craft_ui::runtime::focus::KeyUse;
|
||||
use craft_ui::style::{ComponentStyle, DataColor, StyleColor, StyleCx, TypeRole};
|
||||
use egui::{CornerRadius, Rect, Response, Ui, Vec2};
|
||||
use std::cell::Cell;
|
||||
@@ -34,6 +35,27 @@ craft_ui::words! {
|
||||
}
|
||||
}
|
||||
|
||||
/// The keys of the page preview: Page Up and Page Down step back and forward, and the arrows step in reading order
|
||||
/// (Left goes back in a left-to-right interface, forward in a right-to-left one). They belong to the preview's dialog,
|
||||
/// so they work wherever the focus is in it; a widget that acts on its own moves keeps them.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, CommandSet)]
|
||||
#[command(prefix = "pagePreview", scope = "pagePreview")]
|
||||
#[non_exhaustive]
|
||||
pub enum PagePreviewCommands {
|
||||
/// The previous page.
|
||||
#[command(modal = "in_dialogs", label = "Previous Page", default = "PageUp")]
|
||||
Previous,
|
||||
/// The next page.
|
||||
#[command(modal = "in_dialogs", label = "Next Page", default = "PageDown")]
|
||||
Next,
|
||||
/// The page to the left: the previous one in a left-to-right interface.
|
||||
#[command(modal = "in_dialogs", label = "Page to the Left", default = "Left")]
|
||||
Left,
|
||||
/// The page to the right: the next one in a left-to-right interface.
|
||||
#[command(modal = "in_dialogs", label = "Page to the Right", default = "Right")]
|
||||
Right,
|
||||
}
|
||||
|
||||
/// What the preview did this pass.
|
||||
#[derive(Debug)]
|
||||
#[non_exhaustive]
|
||||
@@ -61,8 +83,8 @@ impl Outcome for PagePreviewOutput {
|
||||
/// the stand-in until the new one lands), on `paper` while it loads. A page that fails to render shows [`PagePreviewWords::unavailable`].
|
||||
/// - In the footer: previous and next, disabled at the ends. The pages stepped through are `pages` (the pages a
|
||||
/// range selects), in order.
|
||||
/// - Keys (with the picture focused): Left and Page Up previous, Right and Page Down next (reversed in a
|
||||
/// right-to-left interface), Escape closes.
|
||||
/// - Keys ([`PagePreviewCommands`], wherever the focus is in the preview): Left and Page Up previous, Right and Page
|
||||
/// Down next (the arrows reversed in a right-to-left interface), Escape closes.
|
||||
#[derive(craft_ui::contract::Pattern)]
|
||||
pub struct PagePreview<'a> {
|
||||
source: &'a dyn PageSource,
|
||||
@@ -91,7 +113,9 @@ impl<'a> PagePreview<'a> {
|
||||
let title = words.page_of.fill(&[("p", source.page_label(page).as_str()), ("n", format.integer(source.page_count() as u64).as_str())]);
|
||||
let step = Cell::new(0_isize);
|
||||
let shown = *at;
|
||||
let side = if env.direction.is_rtl() { -1 } else { 1 };
|
||||
let dialog = Dialog::new(title)
|
||||
.command_scope(PagePreviewCommands::SCOPE)
|
||||
.footer(|ui, _| {
|
||||
Row::new().show(ui, |ui, _| {
|
||||
if clicked(&IconButton::new(IconId::new("chevron_left")).label(words.previous.clone()).enabled(shown > 0).show(ui)) {
|
||||
@@ -102,9 +126,17 @@ impl<'a> PagePreview<'a> {
|
||||
}
|
||||
});
|
||||
})
|
||||
.show(ui, |ui, _| {
|
||||
let picture = PagePicture::new(source, page, words.unavailable.as_str(), paper).label(source.page_label(page)).show(ui);
|
||||
step.set(step.get() + picture.step);
|
||||
.show(ui, |ui, cx| {
|
||||
while let Some((command, _)) = cx.take_command::<PagePreviewCommands>() {
|
||||
let by = match command {
|
||||
PagePreviewCommands::Previous => -1,
|
||||
PagePreviewCommands::Next => 1,
|
||||
PagePreviewCommands::Left => -side,
|
||||
PagePreviewCommands::Right => side,
|
||||
};
|
||||
step.set(step.get() + by);
|
||||
}
|
||||
PagePicture::new(source, page, words.unavailable.as_str(), paper).label(source.page_label(page)).show(ui);
|
||||
});
|
||||
let target = (*at as isize + step.get()).clamp(0, last as isize) as usize;
|
||||
let stepped = target != *at;
|
||||
@@ -125,11 +157,10 @@ pub struct PagePictureStyle {
|
||||
message: StyleColor,
|
||||
}
|
||||
|
||||
/// What the preview picture reports: the pages the keys asked to step, negative for back.
|
||||
/// What the preview picture reports.
|
||||
#[derive(Debug)]
|
||||
struct PagePictureOutput {
|
||||
response: Response,
|
||||
step: isize,
|
||||
}
|
||||
|
||||
impl Outcome for PagePictureOutput {
|
||||
@@ -140,7 +171,7 @@ impl Outcome for PagePictureOutput {
|
||||
}
|
||||
}
|
||||
|
||||
/// The page picture of the preview: one Tab stop that turns the page with the arrow and Page keys.
|
||||
/// The page picture of the preview.
|
||||
#[derive(craft_ui::contract::Component)]
|
||||
#[component(content_painter)]
|
||||
struct PagePicture<'a> {
|
||||
@@ -150,7 +181,6 @@ struct PagePicture<'a> {
|
||||
page: usize,
|
||||
unavailable: &'a str,
|
||||
paper: DataColor,
|
||||
step: isize,
|
||||
/// The page's picture as step 7 asked for it, which `paint` shows.
|
||||
state: Option<TextureState>,
|
||||
}
|
||||
@@ -160,7 +190,7 @@ const PICTURE: PartKey = PartKey::new("picture");
|
||||
|
||||
impl<'a> PagePicture<'a> {
|
||||
fn new(source: &'a dyn PageSource, page: usize, unavailable: &'a str, paper: DataColor) -> Self {
|
||||
PagePicture { common: Common::new(), source, page, unavailable, paper, step: 0, state: None }
|
||||
PagePicture { common: Common::new(), source, page, unavailable, paper, state: None }
|
||||
}
|
||||
|
||||
/// The page pictures of the component `id`.
|
||||
@@ -182,7 +212,7 @@ impl<'a> PagePicture<'a> {
|
||||
|
||||
impl Component for PagePicture<'_> {
|
||||
const ROLE: Role = Role::IMAGE;
|
||||
const KEYS: KeyUse = KeyUse::of(Input::Pointer).with_keep(KeepKeys::BlockArrows).with_activate(ActivateKeys::Neither).with_moves();
|
||||
const KEYS: KeyUse = KeyUse::of(Input::Pointer);
|
||||
type Style = PagePictureStyle;
|
||||
type Output = PagePictureOutput;
|
||||
|
||||
@@ -191,7 +221,7 @@ impl Component for PagePicture<'_> {
|
||||
}
|
||||
|
||||
fn sense(&self) -> Sense {
|
||||
Sense::FOCUS | Sense::TAKE_FOCUS
|
||||
Sense::HOVER
|
||||
}
|
||||
|
||||
fn measure(&mut self, cx: &MeasureCx<'_, Self>) -> Vec2 {
|
||||
@@ -200,19 +230,6 @@ impl Component for PagePicture<'_> {
|
||||
self.fitted(room)
|
||||
}
|
||||
|
||||
fn update(&mut self, cx: &mut Cx<'_, Self>) {
|
||||
let side = if cx.flow.is_rtl() { -1 } else { 1 };
|
||||
while let Some((mv, _)) = cx.intents().take_move() {
|
||||
match mv {
|
||||
NavMove::Back => self.step -= side,
|
||||
NavMove::Forward => self.step += side,
|
||||
NavMove::PageUp => self.step -= 1,
|
||||
NavMove::PageDown => self.step += 1,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
|
||||
let ctx = cx.ui.ctx().clone();
|
||||
let thumbs = Self::thumbnails(&ctx, cx.id);
|
||||
@@ -243,6 +260,6 @@ impl Component for PagePicture<'_> {
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> PagePictureOutput {
|
||||
PagePictureOutput { response: cx.response.clone(), step: self.step }
|
||||
PagePictureOutput { response: cx.response.clone() }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
//! The page view: the scroll anchor under a zoom change and wheel input at the end of the document.
|
||||
//! The page view and the page preview: the scroll anchor under a zoom change, wheel input at the end of the document,
|
||||
//! the view's page and arrow keys and its zoom commands, and the preview's keys.
|
||||
|
||||
use craft_behavior::kurbo;
|
||||
use craft_pages::layout::{Fit, LayoutOptions};
|
||||
use craft_pages::ui::fixtures::{DemoPages, PAPER};
|
||||
use craft_pages::ui::{PageView, PageViewState};
|
||||
use craft_pages::ui::{PagePreview, PageView, PageViewState};
|
||||
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Modifiers, pos2, vec2};
|
||||
use egui::{Key, Modifiers, pos2, vec2};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
@@ -88,3 +89,26 @@ fn wheel_gathered_past_the_bottom_is_forgotten_by_a_pass_without_wheel_input() {
|
||||
wheel(&mut fixture, -3.0);
|
||||
assert_eq!(state.borrow().current, 1, "a whole notch at once is");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn the_preview_steps_with_the_arrow_and_page_keys_from_a_footer_button() {
|
||||
let at = Rc::new(RefCell::new(1_usize));
|
||||
let kept = Rc::clone(&at);
|
||||
let pages = DemoPages::new(5);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let shown = [0, 1, 2, 3, 4];
|
||||
PagePreview::new(&pages, &shown, &mut kept.borrow_mut(), PAPER).show(ui);
|
||||
});
|
||||
fixture.room(vec2(900.0, 600.0));
|
||||
fixture.settle();
|
||||
fixture.key(Key::Tab, Modifiers::NONE);
|
||||
let button = fixture.focused_stop();
|
||||
assert!(button.is_some(), "Tab focuses a footer button");
|
||||
fixture.key(Key::ArrowRight, Modifiers::NONE);
|
||||
assert_eq!(*at.borrow(), 2, "Right steps forward");
|
||||
fixture.key(Key::PageDown, Modifiers::NONE);
|
||||
assert_eq!(*at.borrow(), 3, "Page Down steps forward");
|
||||
fixture.key(Key::ArrowLeft, Modifiers::NONE);
|
||||
assert_eq!(*at.borrow(), 2, "Left steps back");
|
||||
assert_eq!(fixture.focused_stop(), button, "the keys leave the focus where it was");
|
||||
}
|
||||
|
||||
@@ -646,10 +646,6 @@ pub(crate) fn begin(ctx: &Context) {
|
||||
continue;
|
||||
};
|
||||
let (app, instance) = with(ctx, |board| board.target_of(ctx, &hit.command));
|
||||
// A widget that only took the press as a move leaves it to the scoped commands around the focus.
|
||||
if app && shortcut.moved {
|
||||
continue;
|
||||
}
|
||||
let resolved = resolve_in(ctx, &hit.command, instance);
|
||||
if !resolved.enabled {
|
||||
continue;
|
||||
|
||||
@@ -75,9 +75,6 @@ pub(crate) struct Shortcut {
|
||||
typed: bool,
|
||||
/// The index of the press in `Batch::presses`.
|
||||
press: Option<usize>,
|
||||
/// The focused widget does not act on the arrow move the press became: a command scoped to a container around
|
||||
/// the focus takes the press, an app command does not.
|
||||
pub(crate) moved: bool,
|
||||
}
|
||||
|
||||
/// What the intent routing did with one press.
|
||||
@@ -85,8 +82,6 @@ pub(crate) struct Shortcut {
|
||||
enum Taken {
|
||||
/// Nothing: the press is a shortcut candidate.
|
||||
No,
|
||||
/// The press became an arrow move routed to a focused stop that does not act on moves.
|
||||
Moved,
|
||||
/// The press became an intent, a focus move or an activation.
|
||||
Yes,
|
||||
}
|
||||
|
||||
@@ -64,7 +64,7 @@ impl Strip<'_> {
|
||||
if input.physical.or(input.typed) == Some(Key::Enter) {
|
||||
batch.enter_down = Some(false);
|
||||
}
|
||||
batch.shortcuts.push(Shortcut { input, phase: FirePhase::Release, typed: false, press: None, moved: false });
|
||||
batch.shortcuts.push(Shortcut { input, phase: FirePhase::Release, typed: false, press: None });
|
||||
true
|
||||
}
|
||||
KeyEvent::Text(text) => {
|
||||
@@ -104,7 +104,7 @@ impl Strip<'_> {
|
||||
// A component that reads every chord gets the event as the chord's press, the way the key made it.
|
||||
self.press(batch, key, &input, false);
|
||||
} else if !sent {
|
||||
batch.shortcuts.push(Shortcut { input, phase: FirePhase::Press, typed: false, press: None, moved: false });
|
||||
batch.shortcuts.push(Shortcut { input, phase: FirePhase::Press, typed: false, press: None });
|
||||
}
|
||||
// The event stands for the chord, so the shortcut path is its only reader: egui does not copy or paste
|
||||
// on top of the command.
|
||||
@@ -159,7 +159,7 @@ impl Strip<'_> {
|
||||
// The canvas has Space (`runtime::sampler`): only the shortcut dispatcher reads it, so the dialog's
|
||||
// focused control does not activate.
|
||||
if !repeat {
|
||||
batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, typed: false, press: None, moved: false });
|
||||
batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, typed: false, press: None });
|
||||
}
|
||||
false
|
||||
} else if bare_tab && !self.keeps_tab {
|
||||
@@ -186,13 +186,7 @@ impl Strip<'_> {
|
||||
} else {
|
||||
batch.presses.push(Press { key, mods, repeat });
|
||||
if !repeat {
|
||||
batch.shortcuts.push(Shortcut {
|
||||
input: *input,
|
||||
phase: FirePhase::Press,
|
||||
typed: false,
|
||||
press: Some(batch.presses.len() - 1),
|
||||
moved: false,
|
||||
});
|
||||
batch.shortcuts.push(Shortcut { input: *input, phase: FirePhase::Press, typed: false, press: Some(batch.presses.len() - 1) });
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
//! The begin hook's routing: key presses, shortcuts and AccessKit requests to the intents of their owners.
|
||||
|
||||
use super::input::chrome_toggle_of;
|
||||
use super::{Batch, Item, Node, Press, Routed, Shortcut, Shortcuts, Taken, current, with};
|
||||
use super::{Batch, Item, Node, Press, Routed, Shortcuts, Taken, current, with};
|
||||
use crate::runtime::commands::{self, FirePhase};
|
||||
use crate::runtime::egui::input::{Action, ActionKind};
|
||||
use crate::runtime::focus::{self, StopRec};
|
||||
@@ -194,8 +194,6 @@ fn owned_shortcuts(ctx: &Context, target: Option<&StopRec>, batch: &Batch, consu
|
||||
let Some(keys) = keys else { return false };
|
||||
consumed[at] == Taken::Yes || kept(keys, at)
|
||||
};
|
||||
// A move the widget did not declare it keeps (`KeyUse::keep`) stays a candidate for the scoped commands.
|
||||
let only_moved = |keys: Option<&focus::KeyUse>, at: usize| consumed[at] == Taken::Moved && keys.is_some_and(|keys| !kept(keys, at));
|
||||
let silent = matches!(owner, KeyOwner::Class(OwnerClass::Menu | OwnerClass::Recorder));
|
||||
let dialog = owner == KeyOwner::Class(OwnerClass::Dialog);
|
||||
// The focused component keeps its own keys inside a dialog: a text field its typing, a widget its activation.
|
||||
@@ -211,7 +209,7 @@ fn owned_shortcuts(ctx: &Context, target: Option<&StopRec>, batch: &Batch, consu
|
||||
.iter()
|
||||
.filter(|shortcut| !(typing && shortcut.typed))
|
||||
.filter(|shortcut| !(widget && shortcut.press.is_some_and(|at| held_by(owner_keys, at))))
|
||||
.map(|shortcut| Shortcut { moved: widget && shortcut.press.is_some_and(|at| only_moved(owner_keys, at)), ..*shortcut })
|
||||
.copied()
|
||||
.collect()
|
||||
};
|
||||
Shortcuts { keys, typing, dialog, layout: Layout::default() }
|
||||
@@ -378,22 +376,21 @@ fn route_press(
|
||||
}
|
||||
return Taken::Yes;
|
||||
}
|
||||
let mut taken = Taken::Yes;
|
||||
match keys::intent(os, press.key, press.mods, stop.role) {
|
||||
Some(Intent::Move(mv, mods)) => {
|
||||
// Up, Down, Page, Home and End leave a row's children: the collection takes the move. A component stop keeps
|
||||
// its declared arrows from the roving group around it; a part stop belongs to the component's own group.
|
||||
let vertical = matches!(mv, NavMove::Up | NavMove::Down | NavMove::PageUp | NavMove::PageDown | NavMove::Start | NavMove::End);
|
||||
let kept = stop.part.is_none() && stop.keys.keep.keeps_arrow(mv);
|
||||
if let Some(owner) = vertical.then(|| focus::leave_row(ctx, rec.stop.key())).flatten() {
|
||||
push(routed, Some(owner), Intent::Move(mv, mods), horizontal);
|
||||
} else if (stop.part.is_none() && stop.keys.keep.keeps_arrow(mv))
|
||||
|| focus::roving_move(ctx, rec.stop.key(), mv, mods, horizontal).is_none()
|
||||
{
|
||||
} else if !kept && !stop.role.moves && commands::binds_scoped(ctx, &Chord { mods: press.mods, key: press.key }) {
|
||||
// A stop that does not act on its moves leaves a move key a scoped command around it binds to that
|
||||
// command, ahead of its roving group and the scroll owner.
|
||||
return Taken::No;
|
||||
} else if kept || focus::roving_move(ctx, rec.stop.key(), mv, mods, horizontal).is_none() {
|
||||
// Page, Home and End go to the scroll owner around the stop.
|
||||
push(routed, id, Intent::Move(mv, mods), horizontal);
|
||||
// Page, Home and End go to the scroll owner around the stop; a stop that acts on its moves keeps its arrows.
|
||||
if !stop.role.moves && matches!(press.key, Key::Left | Key::Right | Key::Up | Key::Down) {
|
||||
taken = Taken::Moved;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(Intent::Activate) => route_activate(ctx, rec, routed, activated),
|
||||
@@ -437,7 +434,7 @@ fn route_press(
|
||||
}
|
||||
None => return Taken::No,
|
||||
}
|
||||
taken
|
||||
Taken::Yes
|
||||
}
|
||||
|
||||
/// `Activate` of a group member goes to the group's root, which reads `activated_member`. A child or part of a
|
||||
|
||||
Reference in New Issue
Block a user