mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
Merge pull request #2896 from aiongg/rework-d3
Focus: a removal's new focus is not revealed; the canvas text editor returns the focus to the canvas
This commit is contained in:
@@ -40,7 +40,7 @@ use craft_ui::contract::{
|
||||
use craft_ui::primitives::{EditorLine, EditorPurpose, EditorRun, EditorState, PaintShape, StrokeWidth};
|
||||
use craft_ui::runtime::commands::ClipItem;
|
||||
use craft_ui::runtime::env::Label;
|
||||
use craft_ui::runtime::focus::KeyUse;
|
||||
use craft_ui::runtime::focus::{FocusPolicy, KeyUse};
|
||||
use craft_ui::runtime::pending::Pending;
|
||||
use craft_ui::style::{ComponentStyle, StyleColor, StyleCx};
|
||||
use egui::{Pos2, Rect, Response, Vec2};
|
||||
@@ -461,6 +461,8 @@ impl Component for TextEditor<'_> {
|
||||
const INPUT: craft_ui::contract::Input = craft_ui::contract::Input::Editor;
|
||||
/// Escape always ends the session, so the editor keeps it from its row and from the shortcuts while focused.
|
||||
const KEYS: KeyUse = KeyUse::CODE.with_claims(&[ESCAPE]);
|
||||
/// When the session ends, focus goes back to the stop it came from (the canvas), not to the stop before the editor.
|
||||
const FOCUS_SCOPE: FocusPolicy = FocusPolicy::TAB_LAYER;
|
||||
type Style = TextEditorStyle;
|
||||
type Output = TextEditorOutput;
|
||||
|
||||
|
||||
@@ -346,3 +346,51 @@ fn formatting_keys_follow_the_flow_and_fall_back_to_caret_moves() {
|
||||
assert!(!plain.host.borrow().styles.is_empty(), "the host was asked for the leading step");
|
||||
assert_eq!(plain.selection.borrow().caret, before.saturating_sub(LINE), "the step the host did not take moved the caret a line up");
|
||||
}
|
||||
|
||||
/// #15555: a session the app ends (Cmd+Enter, then the editor is not shown) on a canvas with rulers hands the focus back
|
||||
/// to the canvas it came from, not to the stop before the editor (the rulers' corner, whose arrows move the zero point),
|
||||
/// and the view stays where it was. Without rulers the canvas is that stop anyway.
|
||||
#[trial]
|
||||
fn an_ended_session_returns_the_focus_to_the_canvas_and_the_view_stays() {
|
||||
const EDITOR: craft_behavior::DocKey = craft_behavior::DocKey::new("trial.type-editor");
|
||||
use craft_ui::canvas::Rulers;
|
||||
use craft_ui::contract::Props as _;
|
||||
use craft_ui::runtime::focus::{FocusTarget, request};
|
||||
use craft_ui::testing::access::{focused_key, name};
|
||||
|
||||
for rulers in [true, false] {
|
||||
let editing = Rc::new(Cell::new(false));
|
||||
let view = Rc::new(Cell::new(None::<CanvasView>));
|
||||
let (shown_editing, shown_view) = (Rc::clone(&editing), Rc::clone(&view));
|
||||
let mut host = Host { text: "abc".to_owned(), ..Host::default() };
|
||||
let mut selection = TextSelection::caret_at(3);
|
||||
let mut zero = Point::ZERO;
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let given = shown_view.get().unwrap_or_else(|| CanvasView::new(room, Point::new(100.0, 100.0), 1.0));
|
||||
let strips = rulers.then(|| Rulers::new("Rulers", Bind::bound(&mut zero)));
|
||||
let canvas = Canvas::new("Document").view(given).with_rulers(strips).show(ui, |ui, body| {
|
||||
if shown_editing.get() {
|
||||
let bounds = Rect::from_min_size(body.rect.min + vec2(40.0, 40.0), vec2(200.0, 20.0));
|
||||
drop(TextEditor::new("Text", &mut host, Bind::bound(&mut selection)).doc_key(EDITOR).show_at(ui, bounds));
|
||||
}
|
||||
});
|
||||
shown_view.set(Some(canvas.output.view.unwrap_or(given)));
|
||||
});
|
||||
fixture.room(vec2(600.0, 400.0)).settle();
|
||||
let focused = |fixture: &Fixture| name(fixture.tree(), focused_key(fixture.tree())).map(str::to_owned);
|
||||
// A press on the canvas focuses it, and the session that press starts asks for the editor's focus.
|
||||
fixture.click_at(pos2(400.0, 300.0)).settle();
|
||||
assert_eq!(focused(&fixture).as_deref(), Some("Document"), "rulers {rulers}: the press focused the canvas");
|
||||
editing.set(true);
|
||||
fixture.settle();
|
||||
request(fixture.ctx(), FocusTarget::Doc(EDITOR));
|
||||
fixture.settle();
|
||||
assert_eq!(focused(&fixture).as_deref(), Some("Text"), "rulers {rulers}: the editor took the focus");
|
||||
let before = view.get();
|
||||
editing.set(false);
|
||||
fixture.settle();
|
||||
assert_eq!(focused(&fixture).as_deref(), Some("Document"), "rulers {rulers}: the focus went back to the canvas");
|
||||
assert_eq!(view.get(), before, "rulers {rulers}: the view stays");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -356,6 +356,12 @@ pub trait Component: Meta + Sized {
|
||||
/// Whether the component or its parts can take key focus: `false` skips the stop registration of step 7, except
|
||||
/// for the roving group of a body the shape declares.
|
||||
const STOPS: bool = true;
|
||||
/// The focus scope a leaf runs in, as `ContainerProps::focus` gives a container's: the leaf's own stop and its
|
||||
/// parts lie inside it, and `FocusPolicy::NONE` (the default) opens none. A scope that restores
|
||||
/// (`FocusPolicy::TAB_LAYER`) makes the stop focus came from its opener, so focus goes back there when the leaf
|
||||
/// goes away with focus inside (a canvas text editor hands it back to the canvas). Containers and collections take
|
||||
/// their scope from their props, not from this.
|
||||
const FOCUS_SCOPE: crate::runtime::focus::FocusPolicy = crate::runtime::focus::FocusPolicy::NONE;
|
||||
/// Keys the focused component keeps from egui and the shortcut dispatcher.
|
||||
const KEYS: crate::runtime::focus::KeyUse = crate::runtime::focus::KeyUse::of(Self::INPUT);
|
||||
/// The keys this instance keeps in its current state. The default is `KEYS`; a component whose key use depends
|
||||
|
||||
@@ -317,6 +317,10 @@ impl<C: Component> ShapeHooks<C> for LeafHooks {
|
||||
self.scope
|
||||
}
|
||||
|
||||
fn focus(&self, _: &C) -> Option<crate::runtime::focus::FocusPolicy> {
|
||||
(C::FOCUS_SCOPE != crate::runtime::focus::FocusPolicy::NONE).then_some(C::FOCUS_SCOPE)
|
||||
}
|
||||
|
||||
fn sized(&self, comp: &C, ui: &Ui, id: Id, size: Vec2) -> Vec2 {
|
||||
use craft_behavior::Axis;
|
||||
let room = crate::layout::leaf_fill_room(ui);
|
||||
|
||||
@@ -785,7 +785,8 @@ pub(crate) enum FocusOrigin {
|
||||
/// it counts as an arrival by key, and a text part selects its whole text so typing replaces it.
|
||||
Open,
|
||||
/// The focused stop went away and handed focus to its neighbour: neither the keyboard nor a request chose it, so
|
||||
/// it is no arrival by key (a deleted guide selects no other guide).
|
||||
/// it is no arrival by key (a deleted guide selects no other guide) and no scroll owner reveals it (a closed canvas
|
||||
/// text editor does not pan the canvas to the stop before it).
|
||||
Removal,
|
||||
}
|
||||
|
||||
|
||||
@@ -168,11 +168,14 @@ impl State {
|
||||
self.focus_stop_from(rec, FocusOrigin::Request)
|
||||
}
|
||||
|
||||
/// [`focus_stop`](Self::focus_stop) with what moved focus there.
|
||||
/// [`focus_stop`](Self::focus_stop) with what moved focus there. A stop that inherits the focus from a removed one
|
||||
/// is not revealed: nothing chose to show it.
|
||||
fn focus_stop_from(&mut self, rec: StopRec, origin: FocusOrigin) -> Vec<Act> {
|
||||
let key = rec.stop.key();
|
||||
let acts = self.enter_stop(rec, origin);
|
||||
self.revealed = Some(key);
|
||||
if origin != FocusOrigin::Removal {
|
||||
self.revealed = Some(key);
|
||||
}
|
||||
acts
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
//! Focus after the focused stop goes away.
|
||||
|
||||
use craft_ui::contract::{ContainerProps, Props as _, Scroll, Show as _, ShowContainer as _};
|
||||
use craft_ui::runtime::focus::{FocusTarget, request};
|
||||
use craft_ui::testing::layout::{Block, in_region};
|
||||
use craft_ui::testing::{Frames, trial};
|
||||
use craft_ui_test_kit::body::Slab;
|
||||
use craft_ui_test_kit::contract::{Probe, Seen};
|
||||
use egui::{Pos2, Rect, Ui, Vec2};
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
const ORIGIN: Pos2 = Pos2::new(10.0, 10.0);
|
||||
const VIEW: Vec2 = Vec2::new(200.0, 100.0);
|
||||
|
||||
/// The focused stop goes away and hands the focus to the next stop in its scope, far below a scrolling body's view:
|
||||
/// the stop takes the focus and the body stays where it is. A neighbour that inherits the focus was not shown by
|
||||
/// anyone, so nothing reveals it (#15555: the rulers' corner panned the canvas when a closed text editor handed it the
|
||||
/// focus).
|
||||
#[trial]
|
||||
fn a_stop_that_inherits_the_focus_from_a_removed_one_is_not_revealed() {
|
||||
let (first, next) = (RefCell::new(Seen::default()), RefCell::new(Seen::default()));
|
||||
let shown = Cell::new(true);
|
||||
let top = Cell::new(0.0f32);
|
||||
let mut show = |ui: &mut Ui| {
|
||||
in_region(ui, Rect::from_min_size(ORIGIN, VIEW), |ui| {
|
||||
Slab::new(ContainerProps::new().scroll(Scroll::vertical())).id_salt("panel").show(ui, |ui, _| {
|
||||
top.set(ui.next_widget_position().y);
|
||||
if shown.get() {
|
||||
Probe::new(&first).label("First").id_salt("first").show(ui);
|
||||
}
|
||||
Block::new(Vec2::new(VIEW.x, VIEW.y * 4.0)).show(ui);
|
||||
Probe::new(&next).label("Next").id_salt("next").show(ui);
|
||||
});
|
||||
});
|
||||
};
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(3, &mut show);
|
||||
let first_id = first.borrow().id.expect("the first stop ran");
|
||||
request(&frames.ctx, FocusTarget::Id(first_id));
|
||||
frames.idle(3, &mut show);
|
||||
assert_eq!(craft_ui::testing::focus::egui_focused(&frames.ctx), Some(first_id), "the first stop took the focus");
|
||||
let start = top.get();
|
||||
shown.set(false);
|
||||
frames.idle(6, &mut show);
|
||||
let next_id = next.borrow().id.expect("the next stop ran");
|
||||
assert_eq!(craft_ui::testing::focus::egui_focused(&frames.ctx), Some(next_id), "the next stop inherits the focus");
|
||||
assert!((top.get() - start).abs() < 0.5, "the body keeps its offset: content top {} from {start}", top.get());
|
||||
}
|
||||
@@ -7,6 +7,7 @@ pub(crate) mod cursor;
|
||||
pub(crate) mod derived;
|
||||
pub(crate) mod elapsed;
|
||||
pub(crate) mod env;
|
||||
pub(crate) mod focus;
|
||||
pub(crate) mod frame;
|
||||
pub(crate) mod latch;
|
||||
pub(crate) mod memory;
|
||||
|
||||
@@ -1837,6 +1837,7 @@ The component traits, context types, `PaintUi`, `Looks`, the pipeline and its st
|
||||
- `const OVERLAYS: bool`. Whether the component hosts popups: `false` skips the overlay bookkeeping of steps 9, 10, 12, 15 and 16.
|
||||
- `const PARTS: bool`. Whether the component declares parts, a text part or an editor part: `false` skips the parts step (the parts
|
||||
- `const STOPS: bool`. Whether the component or its parts can take key focus: `false` skips the stop registration of step 7, except
|
||||
- `const FOCUS_SCOPE: crate::runtime::focus::FocusPolicy`. The focus scope a leaf runs in, as `ContainerProps::focus` gives a container's: the leaf's own stop and its
|
||||
- `const KEYS: crate::runtime::focus::KeyUse`. Keys the focused component keeps from egui and the shortcut dispatcher.
|
||||
- `fn keys(&self) -> crate::runtime::focus::KeyUse`. The keys this instance keeps in its current state. The default is `KEYS`; a component whose key use depends
|
||||
- `const SHOWS_LABEL: bool`. Whether the component paints its label as visible text. `false` for a face that shows only an icon or a
|
||||
|
||||
Reference in New Issue
Block a user