mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-26 04:27:49 +00:00
chrome-w5: net change of 2 commits
- chrome-w5: written from its sheet - chrome-w5: repair 1
This commit is contained in:
@@ -7,19 +7,19 @@ use crate::contract::{Container, Cx};
|
||||
use crate::runtime::egui::areas;
|
||||
use crate::runtime::memory::sub;
|
||||
use crate::runtime::pointer;
|
||||
use craft_behavior::{Cursor, CursorPriority, PartRef, PressState};
|
||||
use craft_behavior::{Cursor, CursorPriority, PartRef, PointerButton, PressState};
|
||||
use egui::Pos2;
|
||||
|
||||
/// Whether the second press of a double click went down on the container's move band in this pass.
|
||||
#[derive(Clone, Default)]
|
||||
struct BandDouble(bool);
|
||||
|
||||
/// Whether the primary press of this pass went down on the container's own surface (`Sense::SURFACE`), and whether it
|
||||
/// is the second of a double click.
|
||||
/// Whether the primary press of this pass went down on the container's own surface (`Sense::SURFACE`), whether it
|
||||
/// is the second of a double click, and whether this pass's release completes a click that began there.
|
||||
#[derive(Clone, Default)]
|
||||
struct SurfacePress {
|
||||
pressed: bool,
|
||||
double: bool,
|
||||
#[allow(dead_code)] // P3(chrome-w5): ch.K32
|
||||
clicked: bool,
|
||||
}
|
||||
|
||||
@@ -59,7 +59,7 @@ impl BodyCx<'_> {
|
||||
/// than `PointerFrame::slop` and was released inside. A drag, a cancel, a release elsewhere and a measuring run
|
||||
/// give false. It takes no focus and is no Tab stop.
|
||||
pub fn surface_clicked(&self) -> bool {
|
||||
todo!("ch.K32")
|
||||
!self.memory.is_peek() && self.memory.recall::<SurfacePress>(sub(self.id, "surface_press")).clicked
|
||||
}
|
||||
|
||||
/// Requests `cursor` at `priority` for this pass, in the cursor service the parts' and components' requests go
|
||||
@@ -77,24 +77,46 @@ impl BodyHooks<'_> {
|
||||
/// Records for the container's hooks whether this pass's primary press went down on its own surface
|
||||
/// (`BodyCx::surface_pressed`): no shape of last pass under the press takes it, and the press lies in the
|
||||
/// container's rect on its layer; and whether that press is the second of a double click
|
||||
/// (`BodyCx::surface_double_pressed`), read from the same press's click count.
|
||||
/// (`BodyCx::surface_double_pressed`), read from the same press's click count; and whether this pass's primary
|
||||
/// release ends a press that went down there (`BodyCx::surface_clicked`). The press origin waits in the press slot
|
||||
/// until the release; Escape and a blur clear it, and so does any pass with no button held.
|
||||
pub(super) fn surface_press<C: Container>(&self, sensed: bool, cx: &mut Cx<'_, C>) {
|
||||
let ctx = cx.core.ctx.clone();
|
||||
let pass = pointer::pass_pointer(&ctx);
|
||||
let (layer, rect) = (cx.core.response.layer_id, cx.core.rect);
|
||||
// Whether `pos` lies in the container's rect, on its layer.
|
||||
let inside = |pos: Pos2| {
|
||||
let at = areas::layer_map(&ctx, layer).map_or(pos, |map| map.to_layer(pos));
|
||||
areas::layer_at(&ctx, pos) == Some(layer) && rect.contains(at)
|
||||
};
|
||||
let surface = if sensed {
|
||||
let pass = pointer::pass_pointer(&ctx);
|
||||
pass.frame.as_ref().and_then(|frame| frame.press).filter(|press| press.button == craft_behavior::PointerButton::Primary).filter(|press| {
|
||||
let layer = cx.core.response.layer_id;
|
||||
let at = areas::layer_map(&ctx, layer).map_or(press.origin, |map| map.to_layer(press.origin));
|
||||
areas::layer_at(&ctx, press.origin) == Some(layer)
|
||||
&& cx.core.rect.contains(at)
|
||||
&& pointer::press_winner(&ctx, press.origin, pass.press_targets).is_none()
|
||||
})
|
||||
pass.frame
|
||||
.as_ref()
|
||||
.and_then(|frame| frame.press)
|
||||
.filter(|press| press.button == PointerButton::Primary)
|
||||
.filter(|press| inside(press.origin) && pointer::press_winner(&ctx, press.origin, pass.press_targets).is_none())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let shown = cx.core.memory.shown::<SurfacePress>(sub(sub(cx.core.id, "body"), "surface_press"));
|
||||
let state_id = sub(sub(cx.core.id, "body"), "surface_press");
|
||||
let mut clicked = false;
|
||||
if sensed {
|
||||
let origin = cx.core.memory.press::<Pos2>(state_id);
|
||||
if let Some(press) = surface {
|
||||
*origin = Some(press.origin);
|
||||
}
|
||||
match pass.frame.as_ref() {
|
||||
Some(frame) if frame.release == Some(PointerButton::Primary) => {
|
||||
clicked = origin.take().is_some_and(|from| frame.pos.is_some_and(|pos| inside(pos) && (pos - from).length() < frame.slop));
|
||||
}
|
||||
_ if surface.is_none() && (!pass.primary_down || pointer::cancel_of_pass(&ctx).is_some()) => *origin = None,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let shown = cx.core.memory.shown::<SurfacePress>(state_id);
|
||||
shown.pressed = surface.is_some();
|
||||
shown.double = surface.is_some_and(|press| press.clicks >= 2);
|
||||
shown.clicked = clicked;
|
||||
}
|
||||
|
||||
/// Records for the container's header hooks whether the second press of a double click went down on the move
|
||||
|
||||
@@ -812,6 +812,7 @@ impl ContainerProps {
|
||||
&& self.sizing == other.sizing
|
||||
&& self.content_in_bar == other.content_in_bar
|
||||
&& self.room_height == other.room_height
|
||||
&& self.fit_rank == other.fit_rank
|
||||
&& self.layout == other.layout
|
||||
&& self.header == other.header
|
||||
&& self.scroll == other.scroll
|
||||
|
||||
@@ -259,6 +259,11 @@ pub(crate) trait ShapeHooks<C: Component> {
|
||||
None
|
||||
}
|
||||
|
||||
/// The rank by which the component leaves a shrinking row whole (`ContainerProps::fit_rank`).
|
||||
fn fit_rank(&self, _comp: &C) -> Option<u8> {
|
||||
None
|
||||
}
|
||||
|
||||
/// The component whose memory holds this one's shown memory, when it is another: a popup body's memory belongs to
|
||||
/// the component that shows the popup, so it lasts while that component shows, open or closed.
|
||||
fn memory_owner(&self, _comp: &C) -> Option<Id> {
|
||||
@@ -508,6 +513,11 @@ pub(crate) trait Erased {
|
||||
fn read(&mut self, stage: &mut MemoryStage, id: Id, env: &Env);
|
||||
fn instance_scope(&self) -> Option<commands::CommandScope>;
|
||||
fn theme_scope(&self) -> Option<ThemeScope>;
|
||||
/// The rank by which the component leaves a shrinking row whole.
|
||||
fn fit_rank(&self) -> Option<u8> {
|
||||
// PLAN: `typed.rs` (`Erased for Typed`) and `body/hooks.rs` (`ShapeHooks::fit_rank` reading `ContainerProps::fit_rank`) are not in chrome-w5's Files table; their owner must forward the rank, or no body reports one.
|
||||
None
|
||||
}
|
||||
fn memory_owner(&self) -> Option<Id>;
|
||||
fn in_flow(&self) -> bool;
|
||||
/// Whether the shape fills the width of the room it is shown in (a body not sized from its content).
|
||||
@@ -1560,11 +1570,12 @@ fn room_step(t: &mut dyn Erased, ui: &Ui, at: ShowAt, begun: &Begun) -> Room {
|
||||
let fill_body = shape == ShapeKind::Body && in_flow && t.in_flow() && t.fills_width() && crate::layout::flow_room(ui).x.is_finite();
|
||||
let shrink = env.shrink.filter(|_| in_flow && (shape == ShapeKind::Leaf || (shape == ShapeKind::Body && t.in_flow() && !fill_body)));
|
||||
let given = match shrink {
|
||||
Some(crate::layout::Shrink::Apply(row)) if shape == ShapeKind::Leaf => crate::layout::shrink_cap(ctx, row, id),
|
||||
Some(crate::layout::Shrink::Apply(row)) => crate::layout::shrink_cap(ctx, row, id),
|
||||
_ => None,
|
||||
};
|
||||
let dropped = given.is_some_and(|given| given.dropped);
|
||||
let cap = given.map(|given| given.width);
|
||||
// A body takes its width from its own size step; the cap only tells it whether it was dropped.
|
||||
let cap = given.filter(|_| shape == ShapeKind::Leaf).map(|given| given.width);
|
||||
let room = match cap {
|
||||
Some(cap) => Some(room.map_or(cap, |room| room.min(cap))),
|
||||
None => room,
|
||||
@@ -1694,9 +1705,9 @@ fn measure_step(
|
||||
let ideal = steps::measure(style, &input, content, Vec2::ZERO, resolved).x;
|
||||
let whole = steps::whole(style, content, Vec2::ZERO, resolved);
|
||||
let least = ask(&mut *t, Proposal::Min, None, Vec2::ZERO).0.x;
|
||||
crate::layout::report_shrink(ctx, row, id, size.x, ideal, Some((whole, least)), t.cut_first(), None); // P3(chrome-w5): ch.K30
|
||||
crate::layout::report_shrink(ctx, row, id, size.x, ideal, Some((whole, least)), t.cut_first(), None);
|
||||
}
|
||||
Some(crate::layout::Shrink::Count(row)) => crate::layout::report_shrink(ctx, row, id, size.x, size.x, None, false, None), // P3(chrome-w5): ch.K30
|
||||
Some(crate::layout::Shrink::Count(row)) => crate::layout::report_shrink(ctx, row, id, size.x, size.x, None, false, t.fit_rank()),
|
||||
Some(crate::layout::Shrink::Least(row)) if shape == ShapeKind::Body => crate::layout::report_least(ctx, row, id, size.x),
|
||||
_ => {}
|
||||
}
|
||||
@@ -1704,7 +1715,7 @@ fn measure_step(
|
||||
// the units narrow to leave it, and it takes what they leave.
|
||||
if let Some(crate::layout::Shrink::Count(row)) = env.shrink.filter(|_| fill_body) {
|
||||
let content = measured.x + t.overflow();
|
||||
crate::layout::report_shrink(ctx, row, id, size.x.min(content), content, Some((content, content)), false, None); // P3(chrome-w5): ch.K30
|
||||
crate::layout::report_shrink(ctx, row, id, size.x.min(content), content, Some((content, content)), false, t.fit_rank());
|
||||
}
|
||||
// A component shown at a rect fills it: the rect is its size, and a leaf that measures nothing (a content
|
||||
// painter) still shows there.
|
||||
|
||||
@@ -449,6 +449,9 @@ fn shrink_in_turns(units: &[ShrinkUnit], whole: &[f32], first: &[bool], fixed: f
|
||||
/// to be cut (`first`) drop out from the end, each with one `gap`, until the rest fits at their least. A unit that
|
||||
/// drops gets no width and takes no place, so nothing overflows into a neighbour; controls never drop. A row that
|
||||
/// scrolls keeps every unit and scrolls instead.
|
||||
///
|
||||
/// Before those drops, the units that carry a rank (`ranks`) leave whole, scrolling row or not: the lowest rank first,
|
||||
/// the last of a rank first, until the kept units fit at their ideal widths. A unit with no rank never leaves by rank.
|
||||
fn shrink_or_drop(
|
||||
units: &[ShrinkUnit],
|
||||
whole: &[f32],
|
||||
@@ -459,12 +462,19 @@ fn shrink_or_drop(
|
||||
order: ShrinkOrder,
|
||||
drops: bool,
|
||||
) -> Vec<ShrinkCap> {
|
||||
let _ = ranks; // P3(chrome-w5): ch.K30
|
||||
let mut kept = vec![true; units.len()];
|
||||
let mut fixed = fixed;
|
||||
// Half a point: less than a pixel shows at any scale the box runs.
|
||||
let fits =
|
||||
|kept: &[bool], fixed: f32| fixed + units.iter().zip(kept).filter(|(_, kept)| **kept).map(|(unit, _)| unit.least).sum::<f32>() - room < 0.5;
|
||||
let fits_at = |kept: &[bool], fixed: f32, width: fn(&ShrinkUnit) -> f32| {
|
||||
fixed + units.iter().zip(kept).filter(|(_, kept)| **kept).map(|(unit, _)| width(unit)).sum::<f32>() - room < 0.5
|
||||
};
|
||||
let fits = |kept: &[bool], fixed: f32| fits_at(kept, fixed, |unit| unit.least);
|
||||
while !fits_at(&kept, fixed, |unit| unit.ideal) {
|
||||
let Some(rank) = (0..units.len()).filter(|at| kept[*at]).filter_map(|at| ranks[at]).min() else { break };
|
||||
let Some(at) = (0..units.len()).rev().find(|at| kept[*at] && ranks[*at] == Some(rank)) else { break };
|
||||
kept[at] = false;
|
||||
fixed -= gap;
|
||||
}
|
||||
while drops && !fits(&kept, fixed) {
|
||||
let Some(at) = (0..units.len()).rev().find(|at| kept[*at] && first[*at]) else { break };
|
||||
kept[at] = false;
|
||||
|
||||
@@ -3,3 +3,4 @@
|
||||
mod mounts;
|
||||
mod nesting;
|
||||
mod placement;
|
||||
mod surface;
|
||||
|
||||
@@ -0,0 +1,112 @@
|
||||
//! The click on a container's own surface: it reports in the pass of its primary release.
|
||||
|
||||
use craft_behavior::Sense;
|
||||
use craft_ui::contract::Show as _;
|
||||
use craft_ui::testing::input::key_press;
|
||||
use craft_ui::testing::layout::Block;
|
||||
use craft_ui::testing::{Frames, pointer, trial};
|
||||
use craft_ui_test_kit::contract::{Panel, Probe, Seen};
|
||||
use egui::{Key, Modifiers, Pos2, Rect, Ui, Vec2};
|
||||
use std::cell::{Cell, RefCell};
|
||||
|
||||
const PROBE: Vec2 = Vec2::new(40.0, 40.0);
|
||||
const BLOCK: Vec2 = Vec2::new(100.0, 60.0);
|
||||
|
||||
/// What the shown container leaves for the trial to read.
|
||||
#[derive(Default)]
|
||||
struct Shown {
|
||||
rect: Cell<Rect>,
|
||||
clicked: Cell<bool>,
|
||||
seen: RefCell<Seen>,
|
||||
}
|
||||
|
||||
impl Shown {
|
||||
/// A panel that senses its surface, with a child that takes clicks above a block that senses nothing.
|
||||
fn show(&self, ui: &mut Ui) {
|
||||
let out = Panel::new().sense(Sense::SURFACE).show(ui, |ui, cx| {
|
||||
self.clicked.set(self.clicked.get() | cx.surface_clicked());
|
||||
Probe::new(&self.seen).size(PROBE).sense(Sense::CLICK).show(ui);
|
||||
Block::new(BLOCK).show(ui);
|
||||
});
|
||||
self.rect.set(out.response.rect);
|
||||
}
|
||||
}
|
||||
|
||||
struct Rig {
|
||||
frames: Frames,
|
||||
shown: Shown,
|
||||
}
|
||||
|
||||
impl Rig {
|
||||
fn new() -> Self {
|
||||
let mut rig = Rig { frames: Frames::new(), shown: Shown::default() };
|
||||
rig.frames.idle(3, |ui| rig.shown.show(ui));
|
||||
rig
|
||||
}
|
||||
|
||||
/// Runs one frame with `events`; whether the container reported a click in it.
|
||||
fn frame(&mut self, events: Vec<egui::Event>) -> bool {
|
||||
self.shown.clicked.set(false);
|
||||
self.frames.frame(events, |ui| self.shown.show(ui));
|
||||
self.shown.clicked.get()
|
||||
}
|
||||
|
||||
/// A point on the block, which senses nothing: the container's own surface.
|
||||
fn surface(&self) -> Pos2 {
|
||||
self.shown.rect.get().min + Vec2::new(20.0, PROBE.y + 30.0)
|
||||
}
|
||||
|
||||
/// Moves to `at` and presses there; whether the press pass reported a click.
|
||||
fn press(&mut self, at: Pos2) -> bool {
|
||||
self.frame(vec![pointer::moved(at)]);
|
||||
self.frame(vec![pointer::press(at)])
|
||||
}
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_surface_click_reports_on_release_and_a_drag_a_cancel_or_a_release_elsewhere_report_none() {
|
||||
// A click reports in the release pass, not the press pass, and a click again reports again.
|
||||
let mut rig = Rig::new();
|
||||
let at = rig.surface();
|
||||
for _ in 0..2 {
|
||||
assert!(!rig.press(at), "the press pass reports no click");
|
||||
assert!(rig.frame(vec![pointer::release(at)]), "the release pass reports the click");
|
||||
assert!(!rig.frame(Vec::new()), "the next pass reports none");
|
||||
rig.frames.advance(1.0);
|
||||
}
|
||||
|
||||
// A drag past the slop reports none.
|
||||
let mut rig = Rig::new();
|
||||
let at = rig.surface();
|
||||
rig.press(at);
|
||||
let far = at + Vec2::new(40.0, 0.0);
|
||||
rig.frame(vec![pointer::moved(far)]);
|
||||
assert!(!rig.frame(vec![pointer::release(far)]), "a drag is no click");
|
||||
|
||||
// Escape cancels the press.
|
||||
let mut rig = Rig::new();
|
||||
let at = rig.surface();
|
||||
rig.press(at);
|
||||
rig.frame(vec![key_press(Key::Escape, Modifiers::NONE)]);
|
||||
assert!(!rig.frame(vec![pointer::release(at)]), "a cancelled press is no click");
|
||||
|
||||
// A blur cancels it too.
|
||||
let mut rig = Rig::new();
|
||||
let at = rig.surface();
|
||||
rig.press(at);
|
||||
rig.frame(vec![pointer::window_blurred()]);
|
||||
assert!(!rig.frame(vec![pointer::release(at)]), "a blurred press is no click");
|
||||
|
||||
// A release outside the rect, within the slop of the press, is no click.
|
||||
let mut rig = Rig::new();
|
||||
let corner = rig.shown.rect.get().max - Vec2::splat(0.5);
|
||||
rig.press(corner);
|
||||
let outside = corner + Vec2::splat(1.5);
|
||||
assert!(!rig.frame(vec![pointer::release(outside)]), "a release outside is no click");
|
||||
|
||||
// A press a child takes is no surface click.
|
||||
let mut rig = Rig::new();
|
||||
let on_child = rig.shown.rect.get().min + Vec2::splat(20.0);
|
||||
rig.press(on_child);
|
||||
assert!(!rig.frame(vec![pointer::release(on_child)]), "a press the child took is no surface click");
|
||||
}
|
||||
@@ -222,6 +222,7 @@ const CONTAINER: &[Forwarded] = &[
|
||||
("end", "$ui::contract::EndRegion", "The region at the line's end edge, outside the body scroll."),
|
||||
("position", "$ui::contract::SetPosition", "The component's place in its set, for assistive technology."),
|
||||
("collection_body", "", "The body holds one collection that owns the scroll: a layer's own body scroll is off."),
|
||||
("fit_rank", "u8", "A member of a shrinking row that leaves it whole, lowest rank first, when the row is short."),
|
||||
];
|
||||
|
||||
/// The builders a `#[container(a, b)]` field names; a bare `#[collection]` or `#[container]` forwards them all.
|
||||
|
||||
@@ -76,6 +76,8 @@ pub struct Text {
|
||||
elide: Elide,
|
||||
/// The styled spans that replace the label's text; empty for plain text.
|
||||
spans: Vec<DocRun<'static>>,
|
||||
/// The text whose width stands for the label's ideal and least width.
|
||||
sample: Option<Label>,
|
||||
/// The label's text is set in italics.
|
||||
italic: bool,
|
||||
}
|
||||
@@ -83,7 +85,7 @@ pub struct Text {
|
||||
impl Text {
|
||||
/// `text`, which is also its accessible name, in body type and the normal shade.
|
||||
pub fn new(text: impl Into<Label>) -> Self {
|
||||
Text { common: Common::new(), shade: Shade::Normal, tone: None, role: TypeRole::Body, elide: Elide::Off, spans: Vec::new(), italic: false }
|
||||
Text { common: Common::new(), shade: Shade::Normal, tone: None, role: TypeRole::Body, elide: Elide::Off, spans: Vec::new(), sample: None, italic: false }
|
||||
.label(text)
|
||||
}
|
||||
|
||||
@@ -99,9 +101,9 @@ impl Text {
|
||||
|
||||
/// Sets the text's ideal and least width to those of `sample`, so a readout keeps its width while its value changes.
|
||||
#[must_use]
|
||||
pub fn sample(self, sample: impl Into<Label>) -> Self {
|
||||
let _ = sample;
|
||||
todo!("ch.K33")
|
||||
pub fn sample(mut self, sample: impl Into<Label>) -> Self {
|
||||
self.sample = Some(sample.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Sets the text in italics: a value set apart from the words around it.
|
||||
@@ -161,6 +163,13 @@ impl Component for Text {
|
||||
};
|
||||
}
|
||||
let role = cx.style.text.role;
|
||||
let sample = self.sample.as_ref().map(|sample| cx.layout_at(&self.run(role, sample.as_str()), f32::INFINITY).size().x);
|
||||
if let Some(width) = sample
|
||||
&& matches!(cx.proposal, Proposal::Ideal | Proposal::Min)
|
||||
{
|
||||
let height = cx.layout_at(&self.run(role, cx.resolved.label), f32::INFINITY).size().y;
|
||||
return Vec2::new(width, height);
|
||||
}
|
||||
// A line breaks between words, never inside one: a wrapped text is never narrower than its widest word.
|
||||
let widest =
|
||||
|| cx.resolved.label.split_whitespace().map(|word| cx.layout_at(&self.run(role, word), f32::INFINITY).size().x).fold(0.0, f32::max);
|
||||
@@ -173,7 +182,8 @@ impl Component for Text {
|
||||
let run = self.run(role, cx.resolved.label);
|
||||
return Vec2::new(cx.least_width(&run), cx.layout_at(&run, f32::INFINITY).size().y);
|
||||
}
|
||||
let band = wrap.unwrap_or(f32::INFINITY);
|
||||
// A wrapping text is never narrower than its sample.
|
||||
let band = wrap.unwrap_or(f32::INFINITY).max(if self.elide == Elide::Off { sample.unwrap_or(0.0) } else { 0.0 });
|
||||
let mut layout = cx.layout_at(&self.run(role, cx.resolved.label), band);
|
||||
// In a row the room is what the earlier siblings left, which can be less than a word; the row's shrink step
|
||||
// then makes room for the word.
|
||||
|
||||
@@ -201,7 +201,7 @@ layout_component!(
|
||||
/// Children side by side.
|
||||
Row,
|
||||
RowStyleKey,
|
||||
[]
|
||||
[fit_rank]
|
||||
);
|
||||
layout_component!(
|
||||
/// Children row-major over a column list: a child in flow takes the next cell.
|
||||
@@ -302,14 +302,6 @@ impl Row {
|
||||
self.props.layout = std::mem::replace(&mut self.props.layout, BodyLayout::Stack).align(align);
|
||||
self
|
||||
}
|
||||
|
||||
/// The rank by which the row leaves a shrinking row whole: the row reports its members' ranks to the shrink
|
||||
/// that drops units, the lowest rank first.
|
||||
#[must_use]
|
||||
pub fn fit_rank(self, rank: u8) -> Self {
|
||||
let _ = rank;
|
||||
todo!("ch.K31")
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Row {
|
||||
|
||||
Reference in New Issue
Block a user