mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 06:24:39 +00:00
114 lines
4.7 KiB
Rust
114 lines
4.7 KiB
Rust
//! `CanvasPanel`: a double click on the empty document area is the panel's `empty_double` (the host opens a file);
|
|
//! over a document whose canvas takes the press, it is never reported.
|
|
|
|
use craft_behavior::{HitLevel, HitPriority, PartKey, Sense};
|
|
use craft_ui::contract::{Common, Component, Cx, MeasureCx, PartsCx, Role, Show as _};
|
|
use craft_ui::shell::CanvasPanel;
|
|
use craft_ui::testing::pointer::{moved, press, release};
|
|
use craft_ui::testing::trial;
|
|
use craft_ui_test_kit::contract::PlainStyle;
|
|
use craft_ui_test_kit::windows::Windows;
|
|
use egui::{Event, Rect, Response, Vec2};
|
|
|
|
const BACKGROUND: PartKey = PartKey::new("background");
|
|
|
|
/// A document's canvas as the panel sees it: one background part over its rect that takes presses, as a `Canvas`'s
|
|
/// background part does (ui-shell cannot reach ui-canvas).
|
|
#[derive(craft_ui::contract::Component)]
|
|
struct Document {
|
|
#[common]
|
|
common: Common,
|
|
}
|
|
|
|
impl Component for Document {
|
|
const ROLE: Role = Role::GROUP;
|
|
type Style = PlainStyle;
|
|
type Output = Response;
|
|
|
|
fn style(_: &craft_ui::style::StyleCx<'_>, _: DocumentStyleKey) -> PlainStyle {
|
|
PlainStyle::default()
|
|
}
|
|
|
|
fn measure(&mut self, _: &MeasureCx<'_, Self>) -> Vec2 {
|
|
Vec2::ZERO
|
|
}
|
|
|
|
fn parts(&mut self, rect: Rect, cx: &mut PartsCx<'_, Self>) {
|
|
cx.part(BACKGROUND, rect)
|
|
.priority(HitPriority::new(HitLevel::Background, 0))
|
|
.sense(Sense::CLICK | Sense::DRAG)
|
|
.pointer_only("the document's tool takes the press");
|
|
}
|
|
|
|
fn finish(self, cx: Cx<'_, Self>) -> Response {
|
|
cx.response.clone()
|
|
}
|
|
}
|
|
|
|
/// Shows a panel with rail and tabs, and a document filling its document area when `with_document`; after two
|
|
/// settling frames, moves to the area's centre and clicks there `clicks` times, one frame per press and per release.
|
|
/// Returns the steps (0 the move) in which the panel reported `empty_double`.
|
|
fn empty_doubles(with_document: bool, clicks: usize) -> Vec<usize> {
|
|
let windows = Windows::new();
|
|
let mut document = Rect::NOTHING;
|
|
let mut reported = false;
|
|
let frame = |events: Vec<Event>, document: &mut Rect, reported: &mut bool| {
|
|
windows.frame(events, vec![], |ui| {
|
|
let shown = CanvasPanel::new().rail("Panels", |_, _| {}).tabs(|_, _| {}).show(ui, |ui, cx| {
|
|
if with_document {
|
|
Document { common: Common::new() }.show_at(ui, cx.rect);
|
|
}
|
|
});
|
|
*document = shown.document;
|
|
*reported |= shown.empty_double;
|
|
});
|
|
};
|
|
for _ in 0..2 {
|
|
frame(vec![], &mut document, &mut reported);
|
|
}
|
|
assert!(document.is_positive(), "the panel has a document area");
|
|
let at = document.center();
|
|
let mut steps = vec![vec![moved(at)]];
|
|
for _ in 0..clicks {
|
|
steps.extend([vec![press(at)], vec![release(at)]]);
|
|
}
|
|
let mut frames = Vec::new();
|
|
for (step, events) in steps.into_iter().enumerate() {
|
|
reported = false;
|
|
frame(events, &mut document, &mut reported);
|
|
if reported {
|
|
frames.push(step);
|
|
}
|
|
}
|
|
frames
|
|
}
|
|
|
|
#[trial]
|
|
fn an_empty_canvas_panel_reports_a_double_click_on_its_room() {
|
|
assert_eq!(empty_doubles(false, 1), Vec::<usize>::new(), "a single click on the empty room is no double click");
|
|
// Steps: move, press, release, press (the second press of the double click), release.
|
|
assert_eq!(empty_doubles(false, 2), vec![3], "the second press on the empty room reports once");
|
|
assert_eq!(empty_doubles(true, 2), Vec::<usize>::new(), "a document that takes the press leaves the room's double click unreported");
|
|
}
|
|
|
|
/// The panel rail is one rail column wide at the panel's end edge, and the document area runs up to it: no band of the
|
|
/// rail's chrome is left between them, whatever the room.
|
|
#[trial]
|
|
fn the_panel_rail_is_one_rail_column_at_the_end_edge() {
|
|
let windows = Windows::new();
|
|
let (mut rail, mut document, mut room, mut column) = (None, Rect::NOTHING, Rect::NOTHING, 0.0);
|
|
for _ in 0..2 {
|
|
windows.frame(vec![], vec![], |ui| {
|
|
room = ui.max_rect();
|
|
column = craft_ui::runtime::env::Env::of(ui).tokens.size.rail.width;
|
|
let shown = CanvasPanel::new().rail("Panels", |_, _| {}).show(ui, |_, _| {});
|
|
rail = shown.rail;
|
|
document = shown.document;
|
|
});
|
|
}
|
|
let rail = rail.expect("the rail shows");
|
|
assert!((rail.right() - room.right()).abs() < 0.5, "the rail ends at the panel's end edge: {rail:?} in {room:?}");
|
|
assert!((rail.width() - column).abs() < 0.5, "the rail is one rail column wide: {rail:?}, column {column}");
|
|
assert!(document.right() <= rail.left() + 0.5 && rail.left() - document.right() < column, "the document meets the rail: {document:?} {rail:?}");
|
|
}
|