mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 00:34:25 +00:00
fwb2-fix: ToolHost::controls_pressing
(cherry picked from commit 93d1dbd7d798831fda938917098eebdf72c1181f)
This commit is contained in:
@@ -703,6 +703,14 @@ impl<'m> ToolHost<'m> {
|
||||
with_session(ctx, state_id(canvas), |state: &mut HostState| state.gesture.is_some())
|
||||
}
|
||||
|
||||
/// Whether a control the body of the canvas `canvas` shows (a canvas control, a box, a handle set) has a gesture
|
||||
/// in progress: a drag of one of them, not the tool's (that is [`in_gesture`](Self::in_gesture)). It reads the
|
||||
/// controls' own gesture records in the viewport the canvas last ran in; false before the canvas's first pass.
|
||||
pub fn controls_pressing(ctx: &Context, canvas: Id) -> bool {
|
||||
let viewport = with_session(ctx, state_id(canvas), |state: &mut HostState| state.viewport);
|
||||
crate::runtime::edit::member_gesture_running(ctx, viewport, canvas)
|
||||
}
|
||||
|
||||
/// The canvas space the host of `canvas` used in its last pass, `None` before its first.
|
||||
pub fn shown_space(ctx: &Context, canvas: Id) -> Option<CanvasSpace> {
|
||||
with_session(ctx, state_id(canvas), |state: &mut HostState| state.space)
|
||||
|
||||
@@ -124,6 +124,32 @@ fn a_press_on_a_canvas_control_never_reaches_the_tool_and_one_away_from_it_does(
|
||||
assert_eq!(fixture.tool().count(Call::Press), 1, "a press away from the corners reaches the tool: {:?}", fixture.tool().calls);
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn controls_pressing_reads_a_canvas_control_drag_not_the_tool() {
|
||||
let crop = Rc::new(RefCell::new(craft_behavior::geometry::CropBox::upright(craft_behavior::kurbo::Rect::new(20.0, 20.0, 100.0, 80.0))));
|
||||
let shown = Rc::clone(&crop);
|
||||
let mut fixture = ToolFixture::with_body("Document", Recorder::default(), move |ui, body| {
|
||||
let room = Rect::from_min_size(body.rect.min, Vec2::new(160.0, 120.0));
|
||||
let _ = CropOverlay::new("Crop", Bind::bound(&mut *shown.borrow_mut())).show_at(ui, room);
|
||||
});
|
||||
let canvas = fixture.canvas_id();
|
||||
let pressing = |fixture: &mut ToolFixture<Recorder>| ToolHost::controls_pressing(fixture.fixture().ctx(), canvas);
|
||||
let tool_held = |fixture: &mut ToolFixture<Recorder>| ToolHost::in_gesture(fixture.fixture().ctx(), canvas);
|
||||
|
||||
let corner = fixture.fixture().by_label("Crop corner, top left").expect("the crop shows its corners").rect.center();
|
||||
fixture.press_at(corner).move_to(corner + Vec2::new(15.0, 10.0)).move_to(corner + Vec2::new(30.0, 20.0));
|
||||
assert!(pressing(&mut fixture), "a corner drag is a control's gesture");
|
||||
assert!(!tool_held(&mut fixture), "the tool holds no gesture under a control's drag");
|
||||
fixture.release_at(corner + Vec2::new(30.0, 20.0));
|
||||
assert!(!pressing(&mut fixture), "the release ends the control's gesture");
|
||||
|
||||
let away = fixture.canvas().rect.right_bottom() - Vec2::splat(20.0);
|
||||
fixture.press_at(away).move_to(away - Vec2::new(30.0, 20.0));
|
||||
assert!(tool_held(&mut fixture), "a press away from the overlay is the tool's");
|
||||
assert!(!pressing(&mut fixture), "the tool's gesture is no control's");
|
||||
fixture.release_at(away - Vec2::new(30.0, 20.0));
|
||||
}
|
||||
|
||||
#[trial]
|
||||
fn a_gesture_cancelled_by_the_app_starts_no_new_one_while_the_button_stays_down() {
|
||||
let mut fixture = ToolFixture::new("Document", Recorder::default());
|
||||
|
||||
@@ -355,6 +355,20 @@ fn member_widget(member: Id) -> Id {
|
||||
sub(member, "member")
|
||||
}
|
||||
|
||||
/// The members of `arena` in `viewport` as of its latest run: while the root's run of this pass is under way, the
|
||||
/// members of last pass and those registered so far; empty for an arena that has not run. A read creates no arena.
|
||||
pub(crate) fn members(ctx: &Context, viewport: ViewportId, arena: ArenaId) -> SmallVec<[Id; 8]> {
|
||||
let pass_nr = pass::pass_nr(ctx, viewport);
|
||||
PerViewport::<Arenas>::with(ctx, viewport, |arenas| {
|
||||
let Some(state) = arenas.by_root.get(&arena.0) else { return SmallVec::new() };
|
||||
let mut ids: SmallVec<[Id; 8]> = state.members.iter().map(|member| member.id).collect();
|
||||
if state.pass == pass_nr {
|
||||
ids.extend(state.previous.iter().map(|member| member.id).filter(|id| !state.members.iter().any(|member| member.id == *id)));
|
||||
}
|
||||
ids
|
||||
})
|
||||
}
|
||||
|
||||
/// Notes a root shown in the body of `arena` that is not one of its members and takes a press, over `rect`: the
|
||||
/// members shown after it over that rect stack above it.
|
||||
pub(crate) fn note_nested(ctx: &Context, arena: ArenaId, rect: Rect) {
|
||||
|
||||
@@ -4,7 +4,7 @@ use super::egui::repaint;
|
||||
use crate::runtime::memory::MemoryStage;
|
||||
use crate::runtime::pass_state;
|
||||
use craft_behavior::{CancelReason, Gesture, GestureId, Value};
|
||||
use egui::{Context, Id};
|
||||
use egui::{Context, Id, ViewportId};
|
||||
|
||||
/// The latest `cancel_all`: a serial that grows with each call, and why.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
@@ -41,6 +41,15 @@ pub fn gesture_note<V: Value, N: Clone + 'static>(ctx: &Context, id: Id) -> Opti
|
||||
})
|
||||
}
|
||||
|
||||
/// Whether a member of the arena root `arena` (a canvas control in a canvas, say) has a running gesture, in
|
||||
/// `viewport`: begun and not yet committed, cancelled or removed. It reads each member's own gesture record, so a
|
||||
/// reader before or after the arena's pass reads the same fact.
|
||||
pub fn member_gesture_running(ctx: &Context, viewport: ViewportId, arena: Id) -> bool {
|
||||
crate::primitives::sensing::members(ctx, viewport, crate::runtime::env::ArenaId::new(arena))
|
||||
.into_iter()
|
||||
.any(|member| MemoryStage::peek_with::<RemovalWatch, _>(ctx, crate::contract::gesture_key(member), RemovalWatch::armed))
|
||||
}
|
||||
|
||||
/// The newest gesture id handed out in this context, and how many times a component has reported its gesture running
|
||||
/// at an apply step.
|
||||
#[derive(Clone, Copy, Default)]
|
||||
@@ -104,6 +113,11 @@ impl RemovalWatch {
|
||||
pub fn disarm(&mut self) {
|
||||
self.armed = None;
|
||||
}
|
||||
|
||||
/// The gesture it watches runs.
|
||||
pub(crate) fn armed(&self) -> bool {
|
||||
self.armed.is_some()
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for RemovalWatch {
|
||||
|
||||
Reference in New Issue
Block a user