mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-27 04:38:49 +00:00
Cmd/Ctrl with Page Up or Page Down scrolls a focused document canvas sideways on every OS; the key role marks a document canvas, and roving groups and the Bezier overlay leave those keys
This commit is contained in:
@@ -26,6 +26,9 @@ pub struct KeyRole {
|
||||
/// The component reads arrow nudges: arrows are not moves. One that deletes (a canvas control) takes only the
|
||||
/// plain Delete and Backspace; with a modifier held they are the app's.
|
||||
pub nudge: bool,
|
||||
/// A document canvas or a control on one (`KeyUse.chrome_key`): Page Up and Page Down with Cmd (Ctrl off macOS)
|
||||
/// scroll it sideways, as Photoshop's do, instead of switching pages.
|
||||
pub document: bool,
|
||||
/// Typed letters reach the component as `Intent::Text` (type-ahead).
|
||||
pub type_ahead: bool,
|
||||
/// Typed digits reach the component as `Intent::Digit`; the other typed text stays `Intent::Text`.
|
||||
@@ -60,6 +63,7 @@ impl KeyRole {
|
||||
rename: false,
|
||||
pages: false,
|
||||
nudge: false,
|
||||
document: false,
|
||||
type_ahead: false,
|
||||
digits: false,
|
||||
value: false,
|
||||
@@ -114,6 +118,12 @@ impl KeyRole {
|
||||
self
|
||||
}
|
||||
|
||||
/// Cmd (Ctrl off macOS) turns the page moves sideways: a document canvas.
|
||||
pub const fn document(mut self, on: bool) -> Self {
|
||||
self.document = on;
|
||||
self
|
||||
}
|
||||
|
||||
/// Typed letters are delivered as text.
|
||||
pub const fn type_ahead(mut self, on: bool) -> Self {
|
||||
self.type_ahead = on;
|
||||
@@ -261,8 +271,8 @@ pub fn intent(os: Os, key: Key, mods: Mods, role: KeyRole) -> Option<Intent> {
|
||||
Key::Enter if only(Mods::CMD) => return Some(Intent::SubmitForce),
|
||||
Key::Down if only(Mods::ALT) => return Some(Intent::Open),
|
||||
Key::Up if only(Mods::ALT) => return Some(Intent::Close),
|
||||
Key::PageUp if !mac && only(Mods::CMD) => return Some(Intent::switch_page(false)),
|
||||
Key::PageDown if !mac && only(Mods::CMD) => return Some(Intent::switch_page(true)),
|
||||
Key::PageUp if !mac && !role.document && only(Mods::CMD) => return Some(Intent::switch_page(false)),
|
||||
Key::PageDown if !mac && !role.document && only(Mods::CMD) => return Some(Intent::switch_page(true)),
|
||||
Key::OpenBracket | Key::OpenCurlyBracket if mac && shifted(Mods::CMD) => return Some(Intent::switch_page(false)),
|
||||
Key::CloseBracket | Key::CloseCurlyBracket if mac && shifted(Mods::CMD) => return Some(Intent::switch_page(true)),
|
||||
Key::Tab if role.pages => {
|
||||
@@ -325,8 +335,10 @@ fn movement(mac: bool, key: Key, mods: Mods, role: KeyRole) -> Option<Intent> {
|
||||
let by = if mods.cmd { Granularity::Document } else { Granularity::Line };
|
||||
moved(if key == Key::Home { NavMove::Start } else { NavMove::End }, by)
|
||||
}
|
||||
Key::PageUp if !mods.alt && !mods.cmd && !mods.ctrl => moved(NavMove::PageUp, Granularity::Unit),
|
||||
Key::PageDown if !mods.alt && !mods.cmd && !mods.ctrl => moved(NavMove::PageDown, Granularity::Unit),
|
||||
// On a document canvas Cmd (Ctrl off macOS) is the move's `lead`: the page moves turn sideways.
|
||||
Key::PageUp | Key::PageDown if !mods.alt && !mods.ctrl && (!mods.cmd || role.document) => {
|
||||
moved(if key == Key::PageUp { NavMove::PageUp } else { NavMove::PageDown }, Granularity::Unit)
|
||||
}
|
||||
// A canvas control takes the plain keys; with a modifier they are the app's (Photoshop fills on Alt+Backspace
|
||||
// and Cmd+Backspace and opens Fill on Shift+Backspace). A canvas tool takes no row: it claims the deletes it
|
||||
// takes in its state (`CanvasTool::claims` in craft-ui).
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Roving focus groups and type-ahead matching.
|
||||
|
||||
use crate::intent::NavMove;
|
||||
use crate::intent::{MoveMods, NavMove};
|
||||
|
||||
use super::*;
|
||||
|
||||
@@ -130,10 +130,11 @@ impl Roving {
|
||||
NavState { lead, anchor: None, page, wrap: self.wrap, column: 0 }
|
||||
}
|
||||
|
||||
/// Whether the group handles `mv`: Page moves only with a page size, Start and End only with Home and End.
|
||||
pub const fn takes(&self, mv: NavMove) -> bool {
|
||||
/// Whether the group handles `mv` with `mods`: Page moves only with a page size and without `lead` (on a document
|
||||
/// canvas that scrolls sideways), Start and End only with Home and End.
|
||||
pub const fn takes(&self, mv: NavMove, mods: MoveMods) -> bool {
|
||||
match mv {
|
||||
NavMove::PageUp | NavMove::PageDown => self.page.is_some(),
|
||||
NavMove::PageUp | NavMove::PageDown => self.page.is_some() && !mods.lead,
|
||||
NavMove::Start | NavMove::End => self.home_end,
|
||||
NavMove::Back | NavMove::Forward => !matches!(self.arrows, Arrows::Vertical),
|
||||
NavMove::Up | NavMove::Down => !matches!(self.arrows, Arrows::Horizontal),
|
||||
|
||||
@@ -43,3 +43,15 @@ fn shift_left_and_right_are_no_moves_in_stacked_rows() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Off macOS Ctrl+Page Up and Page Down switch pages, except on a document canvas, where Cmd or Ctrl turns the page
|
||||
/// moves sideways on every platform.
|
||||
#[test]
|
||||
fn cmd_or_ctrl_page_keys_scroll_a_document_sideways_and_switch_pages_elsewhere() {
|
||||
let document = KeyRole::new().nudge(true).document(true);
|
||||
for os in [Os::Mac, Os::Windows, Os::Linux] {
|
||||
let lead = matches!(intent(os, Key::PageDown, Mods::CMD, document), Some(Intent::Move(craft_behavior::NavMove::PageDown, mods)) if mods.lead);
|
||||
assert!(lead, "{os:?}: a page move with lead on a document");
|
||||
}
|
||||
assert_eq!(intent(Os::Windows, Key::PageDown, Mods::CMD, KeyRole::new()), Some(Intent::switch_page(true)), "a tab strip's Ctrl+Page Down");
|
||||
}
|
||||
|
||||
@@ -784,6 +784,8 @@ impl<'a> BezierOverlay<'a> {
|
||||
while let Some((mv, mods)) = cx.intents().take_move() {
|
||||
let here = current.and_then(|c| keyboard.iter().position(|item| item.part() == c.part()));
|
||||
let picked: Option<&Item> = match mv {
|
||||
// With Cmd (Ctrl) a page key scrolls the canvas sideways; it picks no part.
|
||||
NavMove::PageDown | NavMove::PageUp if mods.lead => None,
|
||||
NavMove::PageDown | NavMove::PageUp if mods.extend => {
|
||||
let anchors: Vec<&Item> = keyboard.iter().copied().filter(|item| item.kind == Kind::Anchor).collect();
|
||||
let at = current.and_then(|c| anchors.iter().position(|a| a.path == c.path && a.knot == c.knot));
|
||||
|
||||
@@ -247,3 +247,19 @@ fn alt_held_over_a_pin_shows_the_not_allowed_cursor_its_press_deletes() {
|
||||
h.fixture.frame(Vec::new());
|
||||
assert_eq!(cursor(&h), Some(Cursor::System(CursorIcon::Move)));
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn page_down_steps_to_the_next_pin_and_with_cmd_or_ctrl_leaves_it_to_the_canvas() {
|
||||
let mut h = harness(vec![pin(150.0, 150.0), pin(250.0, 150.0)], Vec::new());
|
||||
let first = h.at(150.0, 150.0);
|
||||
h.fixture.frame(vec![pointer::moved(first)]);
|
||||
h.fixture.frame(vec![pointer::press(first)]);
|
||||
h.fixture.frame(vec![pointer::release(first)]);
|
||||
h.fixture.frame(Vec::new());
|
||||
let on_first = h.fixture.focused_stop();
|
||||
assert!(on_first.is_some_and(|(_, part)| part.is_some()), "a click focuses the pin");
|
||||
h.fixture.key(egui::Key::PageDown, Modifiers::COMMAND);
|
||||
assert_eq!(h.fixture.focused_stop(), on_first, "Cmd or Ctrl with Page Down is no step");
|
||||
h.fixture.key(egui::Key::PageDown, Modifiers::NONE);
|
||||
assert_ne!(h.fixture.focused_stop(), on_first, "Page Down steps to the next pin");
|
||||
}
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
//! Navigation rules (wheel, page keys, flick, autoscroll), the zoom motion and the momentary tool order.
|
||||
|
||||
use craft_behavior::{Wheel, WheelPhase, WheelUnit};
|
||||
use craft_keymap::Mods;
|
||||
use craft_ui::canvas::{Momentary, MomentaryCommands, MomentaryTool, NavigationOptions, TouchpadGlide};
|
||||
use craft_behavior::kurbo::{Point, Size};
|
||||
use craft_behavior::{CanvasView, Wheel, WheelPhase, WheelUnit};
|
||||
use craft_keymap::{Mods, Platform};
|
||||
use craft_ui::canvas::{Canvas, Momentary, MomentaryCommands, MomentaryTool, NavigationOptions, TouchpadGlide};
|
||||
use craft_ui::contract::ShowContainer as _;
|
||||
use craft_ui::runtime::access::Role;
|
||||
use craft_ui::runtime::commands::FirePhase;
|
||||
use craft_ui::testing::trial;
|
||||
use egui::Vec2;
|
||||
use craft_ui::testing::{Fixture, trial};
|
||||
use egui::{Key, Modifiers, Vec2};
|
||||
use std::cell::Cell;
|
||||
use std::rc::Rc;
|
||||
|
||||
#[trial]
|
||||
fn a_touchpad_glide_stops_in_the_pass_the_edge_clamp_leaves_the_view_where_it_was() {
|
||||
@@ -44,3 +49,29 @@ fn space_is_the_hand_and_cmd_or_alt_added_in_any_order_switch_to_zoom() {
|
||||
momentary.fired(MomentaryCommands::ZoomIn, FirePhase::Release);
|
||||
assert_eq!(momentary.tool(Mods::NONE, false), None);
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn cmd_or_ctrl_with_page_down_scrolls_a_focused_canvas_sideways_on_every_os() {
|
||||
for os in [Platform::Mac, Platform::Windows, Platform::Linux] {
|
||||
let view = Rc::new(Cell::new(CanvasView::new(egui::Rect::ZERO, Point::new(2000.0, 2000.0), 1.0)));
|
||||
let shown = Rc::clone(&view);
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let mut given = shown.get();
|
||||
given.rect = ui.available_rect_before_wrap();
|
||||
let canvas = Canvas::new("Document").document(Size::new(4000.0, 4000.0)).view(given).show(ui, |_, _| {});
|
||||
shown.set(canvas.output.view.unwrap_or(given));
|
||||
});
|
||||
fixture.os(os);
|
||||
fixture.settle();
|
||||
let canvas = fixture.by_role(Role::CANVAS)[0];
|
||||
fixture.click(&canvas);
|
||||
let before = view.get().center;
|
||||
// `COMMAND` is Cmd on macOS and Ctrl elsewhere.
|
||||
fixture.key(Key::PageDown, Modifiers::COMMAND);
|
||||
let sideways = view.get().center;
|
||||
assert!(sideways.x > before.x && sideways.y == before.y, "{os:?}: {before:?} -> {sideways:?}");
|
||||
fixture.key(Key::PageDown, Modifiers::NONE);
|
||||
let down = view.get().center;
|
||||
assert!(down.y > sideways.y && down.x == sideways.x, "{os:?}: plain Page Down scrolls down");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -670,7 +670,7 @@ fn stops_step(at: StopsStep<'_>, e: &mut dyn ErasedPass<'_>, facts: &TypeFacts,
|
||||
let resettable = !facts.text() && resets;
|
||||
let text = holder_widget.is_some();
|
||||
let keys = text_keys.unwrap_or_else(|| e.keys());
|
||||
let role = e.key_role().nudge(keys.nudge);
|
||||
let role = e.key_role().nudge(keys.nudge).document(keys.chrome_key);
|
||||
let role = role.type_ahead(role.type_ahead || keys.type_ahead);
|
||||
// A bound component that keeps the arrows reads them as value steps; a text value sets `steps_value` in its text options.
|
||||
let arrows = keys.keep == crate::runtime::focus::KeepKeys::BlockArrows;
|
||||
|
||||
@@ -337,7 +337,7 @@ impl State {
|
||||
(true, NavMove::Forward) => NavMove::Back,
|
||||
(_, mv) => mv,
|
||||
};
|
||||
if !group.roving.takes(mv) {
|
||||
if !group.roving.takes(mv, mods) {
|
||||
return None;
|
||||
}
|
||||
let members: Vec<StopRec> = self.last_stops.iter().filter(|rec| rec.stop.group == Some(group.id)).cloned().collect();
|
||||
|
||||
Reference in New Issue
Block a user