mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 03:17:43 +00:00
Preserve form edits across interactive split panes
This commit is contained in:
@@ -411,7 +411,7 @@ impl Outcome for TextEditorOutput {
|
||||
/// them. Backspace and Delete by char, Alt by word, Cmd+Backspace to the line start. Cmd+A all, Cmd+C, Cmd+X and
|
||||
/// Cmd+V the clipboard. Enter a line break; Cmd+Enter commits; Esc commits or cancels; Cmd+. cancels;
|
||||
/// Cmd+T asks the host for its Character panel. In a form field ([`mode`](Self::mode)) Tab and Shift+Tab end the
|
||||
/// session toward the next or previous field, and in a one-line field Enter commits. The
|
||||
/// session toward the next or previous field, a press that moves focus away commits, and in a one-line field Enter commits. The
|
||||
/// [`TextEditorCommand`]s format. In vertical text the arrows turn with the flow, the formatting keys too: Alt+Up
|
||||
/// and Alt+Down track, Alt+Left and Alt+Right set the leading. A formatting key the host does not take moves the
|
||||
/// caret as its arrow does: a line, a word, or with Cmd to the text's start or end; Shift extends.
|
||||
@@ -674,6 +674,11 @@ impl Component for TextEditor<'_> {
|
||||
edits.session.dragging = None;
|
||||
edits.done.commit = true;
|
||||
}
|
||||
if mode != EditorMode::Canvas && cx.editor_commit_requested() {
|
||||
edits.end_composition();
|
||||
edits.session.dragging = None;
|
||||
edits.done.commit = true;
|
||||
}
|
||||
|
||||
// Commands and the claimed keys arrive with the pass's own input: a second pass of the frame takes none, so
|
||||
// a commit, a cancel, a move to another field or the panel toggle is reported by one pass only. No key ends
|
||||
|
||||
@@ -381,6 +381,18 @@ fn field(mode: EditorMode, masked: bool) -> Session {
|
||||
editor
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_form_editor_commits_when_a_press_focuses_another_editor() {
|
||||
for mode in [EditorMode::FieldLine, EditorMode::FieldLines] {
|
||||
let mut editor = session_on(Host { text: "abc".to_owned(), ..Host::default() }, None, true, mode, false);
|
||||
editor.focus();
|
||||
editor.fixture.frame(vec![Event::Text("d".to_owned())]).settle();
|
||||
editor.fixture.click_at(pos2(20.0, 50.0)).settle();
|
||||
assert_eq!(*editor.ends.borrow(), [EditorEnd::Commit], "the form session ends on a press in the other editor ({mode:?})");
|
||||
assert_eq!(editor.text(), "abcd", "the committed text is kept");
|
||||
}
|
||||
}
|
||||
|
||||
/// A one-line field ends its session on Enter (commit), Tab (next) and Shift+Tab (previous) and pastes line breaks as
|
||||
/// spaces; a multi-line field takes Enter as a line break and ends on Tab; while a composition is open no key ends a
|
||||
/// field's session; the canvas editor still types Tab as a tab character.
|
||||
|
||||
@@ -45,7 +45,7 @@ pub use raster_source::{PixelRect, RasterSource};
|
||||
mod pasteboard;
|
||||
mod split;
|
||||
pub use pasteboard::{PageBorder, PageFrame, Pasteboard, PasteboardColor, PasteboardOutput, PasteboardStyle};
|
||||
pub use split::{SplitAt, SplitLayout, SplitPane, SplitPaneAt, SplitStyle, SplitViews, SplitViewsOutput, TabDrop, TabDropTarget};
|
||||
pub use split::{PanePress, SplitAt, SplitLayout, SplitPane, SplitPaneAt, SplitStyle, SplitViews, SplitViewsOutput, TabDrop, TabDropTarget};
|
||||
pub use words::SplitWords;
|
||||
|
||||
mod app_paint;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! `SplitViews`: one canvas area shown as views of one document, or a grid of panes the caller fills (one document
|
||||
//! each). One view is active and is the full interactive view (tools, overlays, keys); the others show their document
|
||||
//! from their own view and become active on a click. The dividers between panes move with the pointer and, when
|
||||
//! from their own view and become active on a click. [`PanePress`] chooses whether that click also reaches the view.
|
||||
//! The dividers between panes move with the pointer and, when
|
||||
//! focused, with the arrow keys.
|
||||
//!
|
||||
//! The panes are the cells [`arrange_rects`] gives for the arrangement the caller names, with the dividers moved to
|
||||
@@ -37,6 +38,16 @@ const PANE: PartKey = PartKey::new("pane");
|
||||
/// panes, shows one pane over the whole area.
|
||||
pub use craft_behavior::dock::Arrangement as SplitLayout;
|
||||
|
||||
/// How a pointer press in an inactive pane activates it.
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub enum PanePress {
|
||||
/// The first press selects the pane; its contents receive later presses.
|
||||
#[default]
|
||||
Select,
|
||||
/// The press selects the pane and also reaches its contents.
|
||||
Interact,
|
||||
}
|
||||
|
||||
/// Where the dividers lie, as fractions of the area counted from its start edge and its top: one entry per column line
|
||||
/// and per row line of the layout, in order. A list that does not fit the layout (the wrong length, not ascending, or
|
||||
/// outside 0–1) lays out that axis in even cells. A saved value missing a field takes that field's default lines.
|
||||
@@ -349,9 +360,9 @@ impl<D> Outcome for SplitViewsOutput<D> {
|
||||
|
||||
/// One canvas area shown as views of one document, or a grid of panes the caller fills (one document each), in the
|
||||
/// panes of a [`SplitLayout`]. The active pane is the app's: it routes tools and keys there. An inactive pane is a
|
||||
/// Tab stop over its view: a press, Enter or Space on it reports it in [`SplitViewsOutput::activated`] and its view
|
||||
/// gets no press. With [`takes_tabs`](SplitViews::takes_tabs) a document strip tab dropped on the views is reported in
|
||||
/// [`SplitViewsOutput::tab_drop`].
|
||||
/// Tab stop: a press, Enter or Space reports it in [`SplitViewsOutput::activated`]. By default the first press selects
|
||||
/// the pane; [`PanePress::Interact`] also sends it to the view. With [`takes_tabs`](SplitViews::takes_tabs) a document
|
||||
/// strip tab dropped on the views is reported in [`SplitViewsOutput::tab_drop`].
|
||||
#[derive(crate::contract::Component, crate::contract::Props)]
|
||||
#[component(container, probe(SplitViews<'static, ()>))]
|
||||
pub struct SplitViews<'a, D: Clone + Send + Sync + 'static = ()> {
|
||||
@@ -361,9 +372,12 @@ pub struct SplitViews<'a, D: Clone + Send + Sync + 'static = ()> {
|
||||
props: ContainerProps,
|
||||
layout: SplitLayout,
|
||||
at: Bind<'a, SplitAt>,
|
||||
/// The active pane: the full interactive view (0 by default). The other panes are display-only until clicked.
|
||||
/// The active pane: the full interactive view (0 by default).
|
||||
#[prop]
|
||||
active: usize,
|
||||
/// How a press in an inactive pane selects it.
|
||||
#[prop]
|
||||
pane_press: PanePress,
|
||||
/// The documents a tile arrangement shows, one pane each (0 by default); the n-up arrangements show their own
|
||||
/// number of panes. [`show_panes`](Self::show_panes) gives each of them a body.
|
||||
#[prop]
|
||||
@@ -379,15 +393,18 @@ impl<'a> SplitViews<'a> {
|
||||
/// active until [`active`](Self::active) says otherwise.
|
||||
pub fn new(label: impl Into<Label>, layout: SplitLayout, at: Bind<'a, SplitAt>) -> Self {
|
||||
let props = ContainerProps::new().arena(true).sizing(Sizing::Fill).pad(Pad::None);
|
||||
SplitViews { common: Common::new(), props, layout, at, active: 0, count: 0, activated: None, accepts: None, tab_drop: None }.label(label)
|
||||
SplitViews {
|
||||
common: Common::new(), props, layout, at, active: 0, pane_press: PanePress::default(), count: 0, activated: None, accepts: None,
|
||||
tab_drop: None,
|
||||
}.label(label)
|
||||
}
|
||||
|
||||
/// Takes a dragged document strip tab over the views: an unsplit body offers its reading-end half, a split body
|
||||
/// each whole pane, each only when `accepts` returns true.
|
||||
#[must_use]
|
||||
pub fn takes_tabs<D: Clone + Send + Sync + 'static>(self, accepts: impl Fn(&[D], TabDropTarget) -> bool + 'a) -> SplitViews<'a, D> {
|
||||
let SplitViews { common, props, layout, at, active, count, activated, accepts: _, tab_drop: _ } = self;
|
||||
SplitViews { common, props, layout, at, active, count, activated, accepts: Some(Box::new(accepts)), tab_drop: None }
|
||||
let SplitViews { common, props, layout, at, active, pane_press, count, activated, accepts: _, tab_drop: _ } = self;
|
||||
SplitViews { common, props, layout, at, active, pane_press, count, activated, accepts: Some(Box::new(accepts)), tab_drop: None }
|
||||
}
|
||||
|
||||
/// Copies the main view's centre and zoom into the linked panes, each keeping its own rect and rotation.
|
||||
@@ -469,9 +486,9 @@ impl<D: Clone + Send + Sync + 'static> Component for SplitViews<'_, D> {
|
||||
Some(self.at.reborrow())
|
||||
}
|
||||
|
||||
/// The panes and the dividers. An inactive pane is a click and Tab stop above its view; the active one is an inert
|
||||
/// part under its view, so every pane has an accessible node. A divider is an edge part above the panes, its own
|
||||
/// Tab stop that the arrow keys move.
|
||||
/// The panes and the dividers. An inactive pane is a click and Tab stop; its press policy decides whether it sits
|
||||
/// above or below its view. The active one is inert under its view, so every pane has an accessible node. A divider
|
||||
/// is an edge part above the panes, its own Tab stop that the arrow keys move.
|
||||
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
|
||||
let at = cx.value().cloned().unwrap_or_default();
|
||||
let active = self.active_pane();
|
||||
@@ -481,11 +498,14 @@ impl<D: Clone + Send + Sync + 'static> Component for SplitViews<'_, D> {
|
||||
if pane == active {
|
||||
cx.part(PANE.at(pane as u64), pane_rect).priority(HitPriority::new(HitLevel::Background, 0));
|
||||
} else {
|
||||
cx.part(PANE.at(pane as u64), pane_rect)
|
||||
let part = cx.part(PANE.at(pane as u64), pane_rect)
|
||||
.sense(Sense::CLICK | Sense::TAKE_FOCUS)
|
||||
.priority(HitPriority::new(HitLevel::Body, 0))
|
||||
.focusable()
|
||||
.over();
|
||||
.priority(HitPriority::new(if self.pane_press == PanePress::Select { HitLevel::Body } else { HitLevel::Background }, 0));
|
||||
if self.pane_press == PanePress::Select {
|
||||
part.focusable().over();
|
||||
} else {
|
||||
part.focusable();
|
||||
}
|
||||
}
|
||||
}
|
||||
if cx.value().is_none() {
|
||||
@@ -506,10 +526,24 @@ impl<D: Clone + Send + Sync + 'static> Component for SplitViews<'_, D> {
|
||||
}
|
||||
let active = self.active_pane();
|
||||
// A press chooses the pane, as in Photoshop; Enter, Space and assistive technology activate it too.
|
||||
let pointer_pane = if self.pane_press == PanePress::Interact {
|
||||
let press = crate::runtime::pointer::frame(&cx.ctx).press.filter(|press| press.button == craft_behavior::PointerButton::Primary);
|
||||
press.and_then(|press| {
|
||||
let shapes = crate::runtime::pointer::shapes_at(&cx.ctx, press.origin);
|
||||
// A divider spans both panes; resizing it must not select either one.
|
||||
if shapes.iter().any(|(owner, part)| *owner == cx.id && part.is_some_and(|part| part.key == DIVIDER)) {
|
||||
return None;
|
||||
}
|
||||
(0..pane_count(self.layout, self.count)).find(|pane| shapes.contains(&(cx.id, Some(PANE.at(*pane as u64)))))
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let chosen = |pane: &usize| {
|
||||
let part = PANE.at(*pane as u64);
|
||||
cx.part_if_declared(part).is_some_and(|result| matches!(result.press.phase, craft_behavior::PressPhase::Down { .. }))
|
||||
|| cx.part_activated(part).is_some()
|
||||
|| pointer_pane == Some(*pane)
|
||||
};
|
||||
self.activated = (0..pane_count(self.layout, self.count)).filter(|&pane| pane != active).find(chosen);
|
||||
// The views' own target over their rect; it ranks above a strip's tear-off, which takes the window.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//! Stories of the split views.
|
||||
|
||||
use super::{DIVIDER, PANE, SplitAt, SplitLayout, SplitPaneAt, SplitViews};
|
||||
use super::{DIVIDER, PANE, PanePress, SplitAt, SplitLayout, SplitPaneAt, SplitViews};
|
||||
use crate::canvas::Canvas;
|
||||
use crate::canvas::RasterSource as _;
|
||||
use crate::canvas::testing::canvas_story::{StoryDocument, show_document};
|
||||
@@ -110,6 +110,17 @@ fn another_view_clicked(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
split(ui, SplitLayout::TwoUpVertical, 2, MAIN_OF_TWO, Bind::bound(at))
|
||||
}
|
||||
|
||||
#[story(SplitViews<'static>, covers = [Default], drive = Drive::Custom(click_inactive_pane))]
|
||||
fn interactive_panes(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let at = data.value("split", SplitAt::default());
|
||||
SplitViews::new("Views", SplitLayout::TwoUpVertical, Bind::bound(at))
|
||||
.active(MAIN_OF_TWO)
|
||||
.pane_press(PanePress::Interact)
|
||||
.show_panes(ui, |ui, _, pane| show_pane(ui, pane))
|
||||
.output
|
||||
.into()
|
||||
}
|
||||
|
||||
#[story(SplitViews<'static>, covers = [Mixed], drive = Drive::Custom(drag_divider))]
|
||||
fn splits_that_differ(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let at = data.value("split", Mixed::<SplitAt>::Mixed);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
//! bounds and spans as the pointer drags them, and the document tabs dropped on the views.
|
||||
|
||||
use craft_behavior::PartKey;
|
||||
use craft_ui::canvas::{SplitAt, SplitLayout, SplitViews, TabDrop, TabDropTarget};
|
||||
use craft_ui::canvas::{PanePress, SplitAt, SplitLayout, SplitViews, TabDrop, TabDropTarget};
|
||||
use craft_ui::contract::{Bind, Show as _, ShowContainer as _};
|
||||
use craft_ui::runtime::env::Direction;
|
||||
use craft_ui::tabs::{DocumentTab, DocumentTabs, DocumentTabsEvent};
|
||||
@@ -24,6 +24,35 @@ const TOLERANCE: f32 = 0.5;
|
||||
const PANE: PartKey = PartKey::new("pane");
|
||||
const DIVIDER: PartKey = PartKey::new("divider");
|
||||
|
||||
#[trial]
|
||||
fn clicking_a_control_in_an_inactive_pane_activates_both() {
|
||||
use craft_ui::buttons::Button;
|
||||
let seen = Rc::new(RefCell::new((Vec::new(), 0)));
|
||||
let held = seen.clone();
|
||||
let mut at = SplitAt::default();
|
||||
let mut active = 0;
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let views = SplitViews::new("Views", SplitLayout::TwoUpVertical, Bind::bound(&mut at))
|
||||
.active(active)
|
||||
.pane_press(PanePress::Interact)
|
||||
.show_panes(ui, |ui, _, pane| {
|
||||
if pane.index == 1 && Button::new("Pane action").show(ui).clicked() {
|
||||
held.borrow_mut().1 += 1;
|
||||
}
|
||||
});
|
||||
if let Some(pane) = views.output.activated {
|
||||
active = pane;
|
||||
held.borrow_mut().0.push(pane);
|
||||
}
|
||||
});
|
||||
fixture.room(ROOM).settle();
|
||||
let button = fixture.by_label("Pane action").unwrap_or_else(|| panic!("the pane action is shown"));
|
||||
fixture.click(&button).settle();
|
||||
let seen = seen.borrow();
|
||||
assert_eq!(seen.0, [1], "the inactive pane becomes active");
|
||||
assert_eq!(seen.1, 1, "the same click operates the control");
|
||||
}
|
||||
|
||||
fn near(a: f32, b: f32) -> bool {
|
||||
(a - b).abs() <= TOLERANCE
|
||||
}
|
||||
|
||||
@@ -26,6 +26,7 @@ pub(crate) struct EditorInfo {
|
||||
pub purpose: EditorPurpose,
|
||||
pub focused: bool,
|
||||
pub read_only: bool,
|
||||
pub commit_now: bool,
|
||||
}
|
||||
|
||||
impl<C: Component> PartsCx<'_, C> {
|
||||
@@ -82,11 +83,16 @@ pub(crate) fn run<C: Component>(cx: &mut Cx<'_, C>, ui: &mut Ui, step: &mut supe
|
||||
can
|
||||
});
|
||||
}
|
||||
cx.core.editor = Some(EditorInfo { widget, rect: decl.rect, purpose: decl.purpose, focused, read_only });
|
||||
cx.core.editor = Some(EditorInfo { widget, rect: decl.rect, purpose: decl.purpose, focused, read_only, commit_now: step.commit_now });
|
||||
cx.core.editor_inputs = inputs;
|
||||
}
|
||||
|
||||
impl<C: Component> Cx<'_, C> {
|
||||
/// Whether a press moved focus away from the editor and asks it to commit its session in this pass.
|
||||
pub fn editor_commit_requested(&self) -> bool {
|
||||
self.core.editor.as_ref().is_some_and(|editor| editor.commit_now)
|
||||
}
|
||||
|
||||
/// The editor part's inputs of this pass in the order they arrived; call it in `update`. The editor applies them
|
||||
/// to its own document.
|
||||
pub fn editor_input(&mut self) -> impl Iterator<Item = EditorInput> + '_ {
|
||||
|
||||
Reference in New Issue
Block a user