mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 02:09:00 +00:00
pdf-chrome-w7: net change of 1 commits
- pdf-chrome-w7: written from its sheet
This commit is contained in:
@@ -6,14 +6,17 @@
|
||||
//! The panes are the cells [`arrange_rects`] gives for the arrangement the caller names, with the dividers moved to
|
||||
//! the [`SplitAt`] the caller binds ([`SplitAt::placed`]). A linked pane follows the active view's zoom and position
|
||||
//! ([`SplitViews::follow`]).
|
||||
//!
|
||||
//! With [`SplitViews::takes_tabs`] the views take a dragged document strip tab: an unsplit body offers its reading-end
|
||||
//! half, a split body each whole pane, and the drop comes back in [`SplitViewsOutput::tab_drop`].
|
||||
|
||||
#[cfg(feature = "testing")]
|
||||
mod stories;
|
||||
|
||||
use super::words::SplitWords;
|
||||
use crate::contract::{
|
||||
Access, Bind, BindMut, Common, Component, Container, ContainerProps, Cx, Edit as _, EditResponse, Name, Outcome, Pad, PaintUi, PartsCx,
|
||||
Pointer as _, Props as _, Role, Sizing, WordSetCx as _,
|
||||
Access, Bind, BindMut, Common, Component, Container, ContainerProps, Cx, DropMark, DropOperation, DropTarget, Edit as _, EditResponse, ListDrag,
|
||||
Name, Outcome, Pad, PaintUi, PartsCx, Pointer as _, Props as _, Role, Sizing, WordSetCx as _,
|
||||
};
|
||||
use crate::docking::divider::{self, Divider, Drag};
|
||||
use crate::docking::{DockSizes, Splitter};
|
||||
@@ -273,6 +276,41 @@ pub struct TabDrop<D> {
|
||||
pub target: TabDropTarget,
|
||||
}
|
||||
|
||||
/// Where a dragged tab may land on `laid` in `rect`, in physical space: each pane of a split body, numbered as `laid`
|
||||
/// numbers them, or the reading-end half of an unsplit body (the left half under right-to-left).
|
||||
fn tab_areas(laid: &Laid, rect: Rect, rtl: bool) -> Vec<(TabDropTarget, Rect)> {
|
||||
let flow = Flow::new(rect, rtl);
|
||||
if laid.panes.len() > 1 {
|
||||
laid.panes.iter().enumerate().map(|(pane, area)| (TabDropTarget::Pane(pane), flow.to_physical(*area))).collect()
|
||||
} else {
|
||||
let end = Rect::from_min_max(Pos2::new(rect.center().x, rect.top()), rect.max);
|
||||
vec![(TabDropTarget::SplitEnd, flow.to_physical(end))]
|
||||
}
|
||||
}
|
||||
|
||||
/// The views' target for a dragged document strip tab: the area under the pointer, when the caller's rule takes the
|
||||
/// dragged documents there. The drop carries the area, which the ghost layer marks.
|
||||
struct TabTarget<'t, D> {
|
||||
areas: Vec<(TabDropTarget, Rect)>,
|
||||
accepts: &'t dyn Fn(&[D], TabDropTarget) -> bool,
|
||||
}
|
||||
|
||||
impl<D: Clone> DropTarget<ListDrag<D>> for TabTarget<'_, D> {
|
||||
type Drop = (TabDrop<D>, Rect);
|
||||
|
||||
fn accept(&self, payload: &ListDrag<D>, at: Pos2, _: DropOperation) -> Option<Self::Drop> {
|
||||
let &(target, area) = self.areas.iter().find(|(_, area)| area.contains(at))?;
|
||||
(self.accepts)(&payload.keys, target).then(|| (TabDrop { documents: payload.keys.clone(), target }, area))
|
||||
}
|
||||
|
||||
fn mark(&self, (drop, area): &Self::Drop) -> DropMark {
|
||||
match drop.target {
|
||||
TabDropTarget::SplitEnd => DropMark::Split(*area),
|
||||
TabDropTarget::Pane(_) => DropMark::Into(*area),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<D> Outcome for SplitViewsOutput<D> {
|
||||
type Value = SplitAt;
|
||||
|
||||
@@ -289,7 +327,8 @@ 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.
|
||||
/// gets no press. 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 = ()> {
|
||||
@@ -307,8 +346,9 @@ pub struct SplitViews<'a, D: Clone + Send + Sync + 'static = ()> {
|
||||
#[prop]
|
||||
count: usize,
|
||||
activated: Option<usize>,
|
||||
#[allow(dead_code)] // P3(pdf-chrome-w7): CH-C21
|
||||
accepts: Option<Box<dyn Fn(&[D], TabDropTarget) -> bool + 'a>>,
|
||||
/// The tab drop released over the views in this pass.
|
||||
tab_drop: Option<TabDrop<D>>,
|
||||
}
|
||||
|
||||
impl<'a> SplitViews<'a> {
|
||||
@@ -316,15 +356,15 @@ 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 }.label(label)
|
||||
SplitViews { common: Common::new(), props, layout, at, active: 0, 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 _ = accepts;
|
||||
todo!("CH-C21")
|
||||
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 }
|
||||
}
|
||||
|
||||
/// Copies the main view's centre and zoom into the linked panes, each keeping its own rect and rotation.
|
||||
@@ -437,6 +477,13 @@ impl<D: Clone + Send + Sync + 'static> Component for SplitViews<'_, D> {
|
||||
|| cx.part_activated(part).is_some()
|
||||
};
|
||||
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.
|
||||
if let Some(accepts) = self.accepts.as_deref() {
|
||||
let at = cx.value().cloned().unwrap_or_default();
|
||||
let areas = tab_areas(&self.laid(&at, cx.rect), cx.rect, cx.flow.is_rtl());
|
||||
let target = TabTarget { areas, accepts };
|
||||
self.tab_drop = cx.drop_target::<ListDrag<D>, _>(&target).map(|(drop, _)| drop);
|
||||
}
|
||||
}
|
||||
|
||||
fn paint_over(&mut self, ui: &mut PaintUi<'_>, cx: &Cx<'_, Self>) {
|
||||
@@ -471,7 +518,7 @@ impl<D: Clone + Send + Sync + 'static> Component for SplitViews<'_, D> {
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> SplitViewsOutput<D> {
|
||||
SplitViewsOutput { edit: cx.edit_response(), activated: self.activated, tab_drop: None } // P3(pdf-chrome-w7): CH-C21
|
||||
SplitViewsOutput { edit: cx.edit_response(), activated: self.activated, tab_drop: self.tab_drop }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,11 +6,12 @@ use crate::canvas::RasterSource as _;
|
||||
use crate::canvas::testing::canvas_story::{StoryDocument, show_document};
|
||||
use crate::contract::{Bind, ShowContainer as _, Validity};
|
||||
use crate::runtime::env::Message;
|
||||
use crate::tabs::{DocumentTab, DocumentTabs};
|
||||
use crate::testing::{Drive, Fixture, StoryData, StoryOutput, story};
|
||||
use craft_behavior::canvas::zoom;
|
||||
use craft_behavior::kurbo::Size;
|
||||
use craft_behavior::{CanvasView, Mixed};
|
||||
use egui::{Ui, Vec2};
|
||||
use egui::{Pos2, Rect, Ui, Vec2};
|
||||
|
||||
/// The widest the left views of the invalid story may be, as a fraction of the area.
|
||||
const MOST_LEFT: f32 = 0.8;
|
||||
@@ -21,19 +22,25 @@ const MAIN_OF_TWO: usize = 1;
|
||||
/// The pane of four views that shows the main view: the bottom right one.
|
||||
const MAIN_OF_FOUR: usize = 3;
|
||||
|
||||
/// Shows a view of the document, fitted to the pane, in each pane of `layout` with `count` documents. Pane `main` is
|
||||
/// the active one and its canvas is named "Main view".
|
||||
fn split(ui: &mut Ui, layout: SplitLayout, count: usize, main: usize, at: Bind<'_, SplitAt>, shown: SplitAt) -> StoryOutput {
|
||||
/// Shows a view of the document, fitted to its pane, in each of `panes`. Pane `main` is the active one and its canvas
|
||||
/// is named "Main view".
|
||||
fn show_panes(ui: &mut Ui, panes: Vec<Rect>, main: usize) {
|
||||
let document = &StoryDocument::STANDARD;
|
||||
let size = document.size();
|
||||
let ppp = ui.pixels_per_point();
|
||||
for (pane, rect) in panes.into_iter().enumerate() {
|
||||
let mut view = CanvasView::new(rect, document.centre(), 1.0);
|
||||
zoom::fit(&mut view, Size::new(f64::from(size.width), f64::from(size.height)), ppp, 0.0);
|
||||
let name = if pane == main { "Main view" } else { "View" };
|
||||
Canvas::new(name).view(view).show_at(ui, rect, |ui, body| show_document(ui, body.rect, document));
|
||||
}
|
||||
}
|
||||
|
||||
/// Shows a view of the document, fitted to the pane, in each pane of `layout` with `count` documents. Pane `main` is
|
||||
/// the active one and its canvas is named "Main view".
|
||||
fn split(ui: &mut Ui, layout: SplitLayout, count: usize, main: usize, at: Bind<'_, SplitAt>, shown: SplitAt) -> StoryOutput {
|
||||
let views = SplitViews::new("Views", layout, at).count(count).active(main).show(ui, |ui, body| {
|
||||
for (pane, rect) in shown.placed(layout, count, body.rect, body.env.direction().is_rtl()).into_iter().enumerate() {
|
||||
let mut view = CanvasView::new(rect, document.centre(), 1.0);
|
||||
zoom::fit(&mut view, Size::new(f64::from(size.width), f64::from(size.height)), ppp, 0.0);
|
||||
let name = if pane == main { "Main view" } else { "View" };
|
||||
Canvas::new(name).view(view).show_at(ui, rect, |ui, body| show_document(ui, body.rect, document));
|
||||
}
|
||||
show_panes(ui, shown.placed(layout, count, body.rect, body.env.direction().is_rtl()), main);
|
||||
});
|
||||
views.output.into()
|
||||
}
|
||||
@@ -62,6 +69,16 @@ fn drag_row_divider(fixture: &mut Fixture) {
|
||||
}
|
||||
}
|
||||
|
||||
/// Drags the first document tab onto the right half of the unsplit views: the reading-end half, where a drop splits
|
||||
/// them, in left-to-right.
|
||||
fn drag_tab_onto_views(fixture: &mut Fixture) {
|
||||
let Some(id) = fixture.root_id() else { return };
|
||||
let Some(views) = fixture.part(id, PANE.at(0)) else { return };
|
||||
let Some(tab) = fixture.by_label("Untitled-1") else { return };
|
||||
let end = Pos2::new(views.rect.left() + views.rect.width() * 0.75, views.rect.center().y);
|
||||
fixture.drag_at(tab.center(), end - tab.center());
|
||||
}
|
||||
|
||||
/// Clicks the first pane, which is not active: the main view of two views is pane 1.
|
||||
fn click_inactive_pane(fixture: &mut Fixture) {
|
||||
let Some(id) = fixture.root_id() else { return };
|
||||
@@ -110,6 +127,19 @@ fn splits_that_differ(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
split(ui, SplitLayout::TwoUpVertical, 2, MAIN_OF_TWO, Bind::bound_mixed(at), SplitAt::default())
|
||||
}
|
||||
|
||||
#[story(SplitViews<'static, ()>, covers = [Default], drive = Drive::Custom(drag_tab_onto_views))]
|
||||
fn drop_a_tab_to_split(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let tabs = [DocumentTab::new(0_usize, "Untitled-1"), DocumentTab::new(1, "Photo.psd")];
|
||||
let _tabs = DocumentTabs::new("Documents", &tabs).selected(0).reorderable(true).tear_off(true).show(ui);
|
||||
let at = data.value("split", SplitAt::default());
|
||||
let shown = at.clone();
|
||||
let layout = SplitLayout::Tile;
|
||||
let views = SplitViews::new("Views", layout, Bind::bound(at)).count(1).takes_tabs(|_: &[usize], _| true).show(ui, |ui, body| {
|
||||
show_panes(ui, shown.placed(layout, 1, body.rect, body.env.direction().is_rtl()), 0);
|
||||
});
|
||||
views.output.into()
|
||||
}
|
||||
|
||||
#[story(SplitViews<'static>, covers = [Invalid], drive = Drive::Custom(drag_divider))]
|
||||
fn views_too_far_left(ui: &mut Ui, data: &mut StoryData) -> StoryOutput {
|
||||
let at = data.value("split", SplitAt::new(MOST_LEFT + 0.1, 0.5));
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
//! `SplitViews`: the panes of the arrangements in reading order, the divider lists that do not fit, and the dividers'
|
||||
//! bounds and spans as the pointer drags them.
|
||||
//! `SplitViews`: the panes of the arrangements in reading order, the divider lists that do not fit, the dividers'
|
||||
//! 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};
|
||||
use craft_ui::canvas::{SplitAt, SplitLayout, SplitViews, TabDrop, TabDropTarget};
|
||||
use craft_ui::contract::{Bind, ShowContainer as _};
|
||||
use craft_ui::runtime::env::Direction;
|
||||
use craft_ui::tabs::{DocumentTab, DocumentTabs, DocumentTabsEvent};
|
||||
use craft_ui::testing::{Fixture, Story, StoryData, StoryOutput, trial};
|
||||
use egui::{Pos2, Rect, Vec2};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
/// The area the geometry rows lay out in, in points.
|
||||
const FULL: Rect = Rect::from_min_max(Pos2::new(0.0, 0.0), Pos2::new(600.0, 400.0));
|
||||
@@ -64,6 +68,85 @@ fn drag_divider(fixture: &mut Fixture, index: u64, by: Vec2) {
|
||||
fixture.drag(&grip, by);
|
||||
}
|
||||
|
||||
/// What the tab-drop host reported since the last drag.
|
||||
#[derive(Default)]
|
||||
struct Seen {
|
||||
drops: Vec<TabDrop<usize>>,
|
||||
torn: usize,
|
||||
views: Rect,
|
||||
}
|
||||
|
||||
/// A document strip above split views in `layout` with `count` documents that take every drop but at `refused`.
|
||||
fn tab_host(layout: SplitLayout, count: usize, refused: Option<TabDropTarget>, direction: Direction) -> (Fixture, Rc<RefCell<Seen>>) {
|
||||
let seen = Rc::new(RefCell::new(Seen::default()));
|
||||
let held = seen.clone();
|
||||
let mut at = SplitAt::default();
|
||||
let mut fixture = Fixture::new(move |ui| {
|
||||
let tabs = [DocumentTab::new(0_usize, "One"), DocumentTab::new(1, "Two")];
|
||||
let strip = DocumentTabs::new("Documents", &tabs).selected(0).reorderable(true).tear_off(true).show(ui);
|
||||
let views = SplitViews::new("Views", layout, Bind::bound(&mut at))
|
||||
.count(count)
|
||||
.takes_tabs(move |_: &[usize], target| Some(target) != refused)
|
||||
.show(ui, |_, _| ());
|
||||
let mut seen = held.borrow_mut();
|
||||
seen.torn += strip.events.iter().filter(|event| matches!(event, DocumentTabsEvent::TornOff { .. })).count();
|
||||
seen.drops.extend(views.output.tab_drop);
|
||||
seen.views = views.output.edit.response.rect;
|
||||
});
|
||||
fixture.direction(direction).room(ROOM).settle();
|
||||
(fixture, seen)
|
||||
}
|
||||
|
||||
/// Drags the tab "One" to the point `across` of the views' width at their middle height and lets it go there: the
|
||||
/// drops the views reported, as documents and target, and how many tear-offs the strip reported.
|
||||
fn drop_tab(fixture: &mut Fixture, seen: &RefCell<Seen>, across: f32) -> (Vec<(Vec<usize>, TabDropTarget)>, usize) {
|
||||
let tab = fixture.by_label("One").unwrap_or_else(|| panic!("the tab is shown"));
|
||||
let views = seen.borrow().views;
|
||||
let to = Pos2::new(views.left() + views.width() * across, views.center().y);
|
||||
{
|
||||
let mut seen = seen.borrow_mut();
|
||||
seen.drops.clear();
|
||||
seen.torn = 0;
|
||||
}
|
||||
fixture.drag_at(tab.center(), to - tab.center()).settle();
|
||||
let seen = seen.borrow();
|
||||
(seen.drops.iter().map(|drop| (drop.documents.clone(), drop.target)).collect(), seen.torn)
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_tab_dropped_on_the_views_splits_or_moves_to_the_other_pane() {
|
||||
// An unsplit view takes the tab on its reading-end half, and the strip does not tear it off there.
|
||||
let (mut fixture, seen) = tab_host(SplitLayout::Tile, 1, None, Direction::Ltr);
|
||||
let (drops, torn) = drop_tab(&mut fixture, &seen, 0.75);
|
||||
assert_eq!(drops, vec![(vec![0], TabDropTarget::SplitEnd)], "a drop on the right half splits the view");
|
||||
assert_eq!(torn, 0, "over the views the drop is theirs, not a tear-off");
|
||||
let (drops, _) = drop_tab(&mut fixture, &seen, 0.25);
|
||||
assert!(drops.is_empty(), "the reading-start half takes nothing: {drops:?}");
|
||||
|
||||
// Under right-to-left the reading end is the left half.
|
||||
let (mut fixture, seen) = tab_host(SplitLayout::Tile, 1, None, Direction::Rtl);
|
||||
let (drops, torn) = drop_tab(&mut fixture, &seen, 0.25);
|
||||
assert_eq!(drops, vec![(vec![0], TabDropTarget::SplitEnd)], "under right-to-left the left half splits the view");
|
||||
assert_eq!(torn, 0, "over the views the drop is theirs, not a tear-off");
|
||||
let (drops, _) = drop_tab(&mut fixture, &seen, 0.75);
|
||||
assert!(drops.is_empty(), "under right-to-left the right half takes nothing: {drops:?}");
|
||||
|
||||
// A split view reports the pane under the pointer, in reading order.
|
||||
let (mut fixture, seen) = tab_host(SplitLayout::TwoUpVertical, 2, None, Direction::Ltr);
|
||||
let (drops, torn) = drop_tab(&mut fixture, &seen, 0.75);
|
||||
assert_eq!(drops, vec![(vec![0], TabDropTarget::Pane(1))], "a drop on the right pane moves the tab there");
|
||||
assert_eq!(torn, 0, "over the views the drop is theirs, not a tear-off");
|
||||
let (drops, _) = drop_tab(&mut fixture, &seen, 0.25);
|
||||
assert_eq!(drops, vec![(vec![0], TabDropTarget::Pane(0))], "a drop on the left pane moves the tab there");
|
||||
|
||||
// A pane the caller's rule refuses takes nothing; the others still do.
|
||||
let (mut fixture, seen) = tab_host(SplitLayout::TwoUpVertical, 2, Some(TabDropTarget::Pane(0)), Direction::Ltr);
|
||||
let (drops, _) = drop_tab(&mut fixture, &seen, 0.25);
|
||||
assert!(drops.is_empty(), "a refused pane takes nothing: {drops:?}");
|
||||
let (drops, _) = drop_tab(&mut fixture, &seen, 0.75);
|
||||
assert_eq!(drops, vec![(vec![0], TabDropTarget::Pane(1))], "the pane the rule takes still reports the drop");
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn grid_and_stacked_splits_lay_out_and_drag_their_dividers() {
|
||||
// Tile with five documents: three columns, two rows, the last row two cells.
|
||||
|
||||
Reference in New Issue
Block a user