mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
ui-contract: one tracked hover-position read; cursor requests and pointer facts journaled
Reading where the pointer hovers marks what reads it as following the pointer: Cx::hover_pos marks the part's hit area, and pointer::hover_point_in marks the rect it reads in. PartResult::hover_pos is crate-private, member responses carry the pointer only for a press, and the collections' row hover, edge band, the menus, canvas, colour, timeline and font readers go through the accessors. design-lint's contract/pointer rule bans egui's hover-position reads. Cursor requests are journaled, so a replay requests its cursor again. A record notes the arena roots, follows rects and wheel takers its run registered.
This commit is contained in:
1 parent
de45e61fad
commit
27e8a77145
33 files changed
+159
-42
No files matched your search
@@ -845,7 +845,7 @@ impl<P: Clone + Send + Sync + 'static> Component for GradientBar<'_, P> {
|
||||
let edge_of = |m: StopRef| {
|
||||
if self.selected_marker == Some(m) {
|
||||
style.selected
|
||||
} else if !dragging && cx.hover_pos(part_of(m)).is_some() {
|
||||
} else if !dragging && cx.part_if_declared(part_of(m)).is_some_and(|result| result.hovered) {
|
||||
style.hovered
|
||||
} else {
|
||||
cx.resolve(&style.edge)
|
||||
|
||||
@@ -229,7 +229,7 @@ impl Component for CaptureView<'_> {
|
||||
.get_or_put(ctx, self.key, Revision::default(), || ColorImage::from_rgba_unmultiplied(image.size.map(|n| n as usize), &image.rgba));
|
||||
let whole = Rect::from_min_max(Pos2::ZERO, pos2(1.0, 1.0));
|
||||
ui.draw().texture(cx.rect, texture.id, whole);
|
||||
let Some(pointer) = cx.response.hover_pos().filter(|at| cx.rect.contains(*at)) else { return };
|
||||
let Some(pointer) = craft_ui::runtime::pointer::hover_point_in(ctx, cx.rect) else { return };
|
||||
let Some((x, y)) = self.pixel(cx.rect, pointer) else { return };
|
||||
self.loupe(ui, cx, pointer, (x, y));
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//! `contract/pointer`: drag and click queries and press-origin reads outside `runtime::egui`.
|
||||
//! `contract/pointer`: drag and click queries, press-origin reads and hover-position reads outside `runtime::egui`.
|
||||
|
||||
use crate::report::Finding;
|
||||
|
||||
@@ -7,6 +7,13 @@ use super::Cx;
|
||||
pub const ID: &str = "contract/pointer";
|
||||
|
||||
pub(super) fn check(cx: &Cx<'_>, out: &mut Vec<Finding>) {
|
||||
cx.flag_methods(
|
||||
out,
|
||||
ID,
|
||||
ID,
|
||||
"reads egui's pointer position; `Cx::hover_pos` and `pointer::hover_point_in` read it and mark what follows the pointer",
|
||||
|name, args| matches!(name, "pointer_hover_pos" | "pointer_latest_pos" | "pointer_interact_pos") || (name == "hover_pos" && args.count == 0),
|
||||
);
|
||||
cx.flag_methods(out, ID, ID, "reads egui's pointer state; the pointer frame carries presses and drags", |name, _| {
|
||||
matches!(
|
||||
name,
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
fn f(r: &Response) {
|
||||
fn f(r: &Response, ctx: &Context) {
|
||||
if r.drag_started() || r.clicked() {}
|
||||
let _ = r.drag_delta();
|
||||
let _ = r.hover_pos();
|
||||
let _ = ctx.pointer_latest_pos();
|
||||
}
|
||||
@@ -853,7 +853,7 @@ impl<'a> BezierOverlay<'a> {
|
||||
continue;
|
||||
}
|
||||
let Some(result) = cx.part_if_declared(item.part()) else { continue };
|
||||
let (press, hover) = (result.press, result.hover_pos);
|
||||
let (press, hover) = (result.press, cx.hover_pos(item.part()));
|
||||
pressed |= press.phase.is_press();
|
||||
if let Some(at) = hover {
|
||||
let rank = match item.kind {
|
||||
@@ -916,7 +916,7 @@ impl<'a> BezierOverlay<'a> {
|
||||
if matches!(self.tool, BezierTool::AddAnchor | BezierTool::DeleteAnchor)
|
||||
&& let Some(result) = cx.part_if_declared(EMPTY)
|
||||
{
|
||||
let (press, hover) = (result.press, result.hover_pos);
|
||||
let (press, hover) = (result.press, cx.hover_pos(EMPTY));
|
||||
// The part covers the canvas background, so it owns the cursor the host tool shows off the paths too.
|
||||
if hover.is_some() {
|
||||
let tool = if self.tool == BezierTool::AddAnchor { PathTool::AddAnchor } else { PathTool::DeleteAnchor };
|
||||
|
||||
@@ -827,7 +827,7 @@ impl<'a> TransformBox<'a> {
|
||||
let suspend = suspend_key(snap.as_ref());
|
||||
for (reference, part) in self.pointer_parts() {
|
||||
let Some(result) = cx.part_if_declared(reference) else { continue };
|
||||
let (press, hover) = (result.press, result.hover_pos.filter(|_| result.hovered));
|
||||
let (press, hover) = (result.press, cx.hover_pos(reference));
|
||||
let mods = self.mods.unwrap_or(press.mods);
|
||||
let acting = drag_mods(mods, suspend);
|
||||
if let Some(pos) = hover
|
||||
|
||||
@@ -698,7 +698,7 @@ pub(super) fn popup<'a>(picker: &mut FontPicker<'a>, ui: &mut OverlayUi<'_>, cx:
|
||||
}
|
||||
});
|
||||
over_rows = rows.response.contains_pointer();
|
||||
let pointer = ui.ctx().pointer_hover_pos();
|
||||
let pointer = craft_ui::runtime::pointer::hover_point_in(ui.ctx(), rows.response.rect);
|
||||
under_pointer = pointer.and_then(|at| rows.shown.iter().find(|row| row.rect.contains(at)).map(|row| row.key.clone()));
|
||||
events = rows.events;
|
||||
});
|
||||
|
||||
@@ -405,7 +405,7 @@ impl Component for CylinderWarp<'_> {
|
||||
results.push((handle, result.press));
|
||||
}
|
||||
}
|
||||
if let Some((hover_pos, press)) = cx.part_if_declared(BODY).map(|result| (result.hover_pos, result.press)) {
|
||||
if let Some((hover_pos, press)) = cx.part_if_declared(BODY).map(|result| (cx.hover_pos(BODY), result.press)) {
|
||||
let on_surface = hover_pos.is_some_and(|at| self.mesh.surface_at(space.view.to_doc(at)).is_some());
|
||||
if on_surface {
|
||||
cx.cursor(CursorIcon::Move, craft_behavior::CursorPriority::Part);
|
||||
|
||||
@@ -288,7 +288,11 @@ impl<'a> WarpGrid<'a> {
|
||||
None => cx.part_if_declared(SURFACE),
|
||||
};
|
||||
let Some(result) = found else { continue };
|
||||
let (hover_at, press) = (result.hover_pos, result.press);
|
||||
let hover_at = match point {
|
||||
Some(index) => cx.hover_pos(POINT.at(index as u64)),
|
||||
None => cx.hover_pos(SURFACE),
|
||||
};
|
||||
let press = result.press;
|
||||
if hover.is_none()
|
||||
&& let Some(at) = hover_at
|
||||
{
|
||||
|
||||
@@ -353,7 +353,7 @@ impl PinWarp<'_> {
|
||||
fn run_pointer(&mut self, cx: &mut Cx<'_, Self>, space: &CanvasSpace, pins: &[Pin]) {
|
||||
for i in 0..pins.len() {
|
||||
let Some(result) = cx.part_if_declared(PIN.at(i as u64)) else { continue };
|
||||
let (press, hovered, hover_pos) = (result.press, result.hovered, result.hover_pos);
|
||||
let (press, hovered, hover_pos) = (result.press, result.hovered, cx.hover_pos(PIN.at(i as u64)));
|
||||
if hovered {
|
||||
self.report.hovered = Some(i);
|
||||
self.report.pointer = hover_pos.map(|at| space.view.to_doc(at));
|
||||
@@ -370,7 +370,7 @@ impl PinWarp<'_> {
|
||||
}
|
||||
}
|
||||
let Some(result) = cx.part_if_declared(MESH) else { return };
|
||||
let (press, hovered, hover_pos) = (result.press, result.hovered, result.hover_pos);
|
||||
let (press, hovered, hover_pos) = (result.press, result.hovered, cx.hover_pos(MESH));
|
||||
if hovered && self.report.pointer.is_none() {
|
||||
self.report.pointer = hover_pos.map(|at| space.view.to_doc(at));
|
||||
}
|
||||
|
||||
@@ -186,7 +186,7 @@ impl Component for TableSizePicker {
|
||||
}
|
||||
|
||||
fn update(&mut self, cx: &mut Cx<'_, Self>) {
|
||||
let pointer = (0..ROWS * COLUMNS).find(|&index| cx.hover_pos(Self::part(index)).is_some());
|
||||
let pointer = (0..ROWS * COLUMNS).find(|&index| cx.part_if_declared(Self::part(index)).is_some_and(|result| result.hovered));
|
||||
let focused = cx.active_part().and_then(|part| (0..ROWS * COLUMNS).find(|&index| Self::part(index) == part)).filter(|_| cx.focus_visible());
|
||||
self.lead = pointer.or(focused).or_else(|| self.preset_index());
|
||||
self.picked = (0..ROWS * COLUMNS).find(|&index| cx.part_activated(Self::part(index)).is_some());
|
||||
|
||||
@@ -358,7 +358,7 @@ impl<'a> PathTextMarkers<'a> {
|
||||
let mut held: Option<Grip> = *cx.press_slot::<Grip>();
|
||||
for (reference, part) in self.pointer_parts() {
|
||||
let Some(result) = cx.part_if_declared(reference) else { continue };
|
||||
let (press, hover) = (result.press, result.hover_pos.filter(|_| result.hovered));
|
||||
let (press, hover) = (result.press, cx.hover_pos(reference));
|
||||
if let Some(pos) = hover
|
||||
&& held.is_none()
|
||||
{
|
||||
|
||||
@@ -488,11 +488,10 @@ impl Component for TimelineView<'_> {
|
||||
return;
|
||||
}
|
||||
let mut next = target;
|
||||
let pointer = cx.response.hover_pos().unwrap_or_else(|| cx.rect.center());
|
||||
let left = cx.flow.span(self.inset(), width).left();
|
||||
if let Some(wheel) = cx.take_wheel() {
|
||||
// The wheel's own point: over a child (the ruler) the view itself has no hover.
|
||||
let pointer = wheel.at.unwrap_or(pointer);
|
||||
let pointer = wheel.at.unwrap_or_else(|| cx.rect.center());
|
||||
let at = next.time_at(pointer.x.max(left), left);
|
||||
self.wheel(wheel, &mut next, at, width);
|
||||
}
|
||||
|
||||
@@ -744,7 +744,7 @@ impl Component for CropOverlay<'_> {
|
||||
None => craft_behavior::PartRef { key, index: None },
|
||||
};
|
||||
let Some(result) = cx.part_if_declared(part) else { continue };
|
||||
let (press, hover) = (result.press, result.hover_pos);
|
||||
let (press, hover) = (result.press, cx.hover_pos(part));
|
||||
let mods = press.mods;
|
||||
match press.phase {
|
||||
PressPhase::Down { start, .. } if start.button == PointerButton::Primary => {
|
||||
@@ -796,8 +796,10 @@ impl Component for CropOverlay<'_> {
|
||||
}
|
||||
self.report.hovered = (0..=8)
|
||||
.filter_map(frame_part)
|
||||
.find(|part| cx.hover_pos(craft_behavior::PartRef { key: FRAME, index: Some(part.index()) }).is_some())
|
||||
.or_else(|| [TURN, OUTSIDE].into_iter().find_map(|key| cx.hover_pos(key)).map(|_| BoxPart::Outside));
|
||||
.find(|part| cx.part_if_declared(craft_behavior::PartRef { key: FRAME, index: Some(part.index()) }).is_some_and(|result| result.hovered))
|
||||
.or_else(|| {
|
||||
[TURN, OUTSIDE].into_iter().any(|key| cx.part_if_declared(key).is_some_and(|result| result.hovered)).then_some(BoxPart::Outside)
|
||||
});
|
||||
self.run_keys(cx, &space, &frame, live.drag.is_some());
|
||||
if live.drag.as_ref().is_some_and(|drag| matches!(drag.kind, Kind::Move)) {
|
||||
cx.cursor(CursorIcon::Grabbing, CursorPriority::Drag);
|
||||
|
||||
@@ -273,7 +273,12 @@ impl<'a> FullWindowCanvas<'a> {
|
||||
let event = EditEvent::set(Mixed::Same(given), next, name.clone(), None);
|
||||
let _ = bind.write(cx, &event);
|
||||
}
|
||||
let hover = output.response.hover_pos().filter(|_| output.response.hovered()).map(|at| next.view().to_doc(at));
|
||||
let hover = output
|
||||
.response
|
||||
.hovered()
|
||||
.then(|| crate::runtime::pointer::hover_point_in(ui.ctx(), output.response.rect))
|
||||
.flatten()
|
||||
.map(|at| next.view().to_doc(at));
|
||||
FullWindowCanvasOutput { response: output.response, hover, tool: hosted }
|
||||
});
|
||||
root.inner.unwrap_or_else(|| FullWindowCanvasOutput { response: root.output, hover: None, tool: ToolHostResponse::default() })
|
||||
|
||||
@@ -960,7 +960,7 @@ impl ToolPass {
|
||||
/// in each pass the pointer should stay hidden, and stops asking when it moves. A measuring run publishes no pass,
|
||||
/// so only the real run asks.
|
||||
pub fn hide_pointer(&self, ui: &egui::Ui) {
|
||||
if ui.ctx().pointer_hover_pos().is_some_and(|at| self.rect.contains(at)) {
|
||||
if crate::runtime::pointer::hover_point_in(ui.ctx(), self.rect).is_some() {
|
||||
let hidden = craft_behavior::Cursor::Painted(craft_behavior::PaintedCursor::Hidden);
|
||||
crate::runtime::cursor::request(ui.ctx(), hidden, CursorPriority::Hidden);
|
||||
}
|
||||
|
||||
@@ -21,9 +21,10 @@ pub trait Pointer {
|
||||
/// condition.
|
||||
fn part_if_declared(&self, part: impl Into<PartRef>) -> Option<&PartResult>;
|
||||
/// Where the pointer is over `part` this pass, in the component's layer space; `None` when `part` is not hovered.
|
||||
fn hover_pos(&self, part: impl Into<PartRef>) -> Option<Pos2> {
|
||||
self.part_if_declared(part).and_then(|result| result.hover_pos)
|
||||
}
|
||||
/// The read marks the part as following the pointer (`pointer::follows_pointer`): a memoized view around the
|
||||
/// component runs on each move over the part, and the pointer-move gate runs a pass for it. A component that only
|
||||
/// asks whether the part is hovered reads `PartResult::hovered`.
|
||||
fn hover_pos(&self, part: impl Into<PartRef>) -> Option<Pos2>;
|
||||
/// The component's own press.
|
||||
fn press(&self) -> &PressState;
|
||||
/// The pointer is over the component's own shapes.
|
||||
@@ -110,6 +111,13 @@ impl Pointer for CxCore<'_> {
|
||||
self.results.get(&(part.key, part.index))
|
||||
}
|
||||
|
||||
fn hover_pos(&self, part: impl Into<PartRef>) -> Option<Pos2> {
|
||||
let result = self.part_if_declared(part)?;
|
||||
let at = result.hover_pos?;
|
||||
pointer::follows_part(&self.ctx, self.response.layer_id, result.rect);
|
||||
Some(at)
|
||||
}
|
||||
|
||||
fn press(&self) -> &PressState {
|
||||
&self.press
|
||||
}
|
||||
@@ -239,6 +247,10 @@ impl<C: Component> Pointer for Cx<'_, C> {
|
||||
self.core.part_if_declared(part)
|
||||
}
|
||||
|
||||
fn hover_pos(&self, part: impl Into<PartRef>) -> Option<Pos2> {
|
||||
self.core.hover_pos(part)
|
||||
}
|
||||
|
||||
fn press(&self) -> &PressState {
|
||||
self.core.press()
|
||||
}
|
||||
|
||||
@@ -16,10 +16,15 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
pub(super) fn edge_parts(&mut self, comp: &C, pcx: &mut PartsCx<'_, C>) {
|
||||
self.edge = None;
|
||||
let Some(rule) = comp.edge_press() else { return };
|
||||
let frame = crate::runtime::pointer::frame_raw(pcx.measure.ui.ctx());
|
||||
let (Some(pos), true) = (frame.pos, frame.mods == rule.mods()) else { return };
|
||||
let ctx = pcx.measure.ui.ctx().clone();
|
||||
if crate::runtime::pointer::frame_raw(&ctx).mods != rule.mods() {
|
||||
return;
|
||||
}
|
||||
let Some(layout) = self.layout.clone() else { return };
|
||||
let view = self.below_header(pcx.content_rect);
|
||||
// The band lies at the line nearest the pointer, which the rows then follow.
|
||||
let screen = crate::runtime::egui::areas::layer_to_screen(&ctx, pcx.measure.ui.layer_id()) * view;
|
||||
let Some(pos) = crate::runtime::pointer::hover_point_in(&ctx, screen) else { return };
|
||||
let rtl = pcx.flow.is_rtl();
|
||||
let at = self.xf.map_or(pos, |map| map.to_layer(pos));
|
||||
if !view.contains(at) {
|
||||
|
||||
@@ -621,7 +621,7 @@ impl Header {
|
||||
}
|
||||
}
|
||||
if cx.part(HEADER.at(hash)).hovered && crate::runtime::pointer::frame_raw(&ctx).release == Some(PointerButton::Secondary) {
|
||||
self.ask_menu(&ctx, Some(key.clone()), ctx.pointer_latest_pos().unwrap_or(rect.left_bottom()));
|
||||
self.ask_menu(&ctx, Some(key.clone()), crate::runtime::pointer::frame_raw(&ctx).pos.unwrap_or(rect.left_bottom()));
|
||||
}
|
||||
if movable && primary && matches!(phase, PressPhase::DragStart { .. }) {
|
||||
cx.start_drag(ColumnDrag { table: id, key: key.clone() }, DragPreview::None);
|
||||
@@ -668,7 +668,7 @@ impl Header {
|
||||
&& cx.part(ROOM).hovered
|
||||
&& crate::runtime::pointer::frame_raw(&ctx).release == Some(PointerButton::Secondary)
|
||||
{
|
||||
self.ask_menu(&ctx, None, ctx.pointer_latest_pos().unwrap_or(room.left_bottom()));
|
||||
self.ask_menu(&ctx, None, crate::runtime::pointer::frame_raw(&ctx).pos.unwrap_or(room.left_bottom()));
|
||||
}
|
||||
self.column_drop(cx, id, rtl);
|
||||
self.keyboard_resize(cx);
|
||||
|
||||
@@ -434,8 +434,11 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
// The pointer counts as over a row only while the sensing pass puts it on this collection, so a popup or another
|
||||
// widget above it takes the hover as it does for the rows' parts.
|
||||
let outcome = crate::primitives::sensing::read_outcome(&ctx, ArenaId::new(cx.core.id));
|
||||
let hover_at = ctx.pointer_latest_pos().filter(|_| outcome.hover.any());
|
||||
let over_at = ctx.pointer_latest_pos().filter(|_| outcome.contains);
|
||||
// The row under the pointer follows the pointer over the rows.
|
||||
let screen = crate::runtime::egui::areas::layer_to_screen(&ctx, ui.layer_id()) * rect;
|
||||
let pointer = (outcome.hover.any() || outcome.contains).then(|| crate::runtime::pointer::hover_point_in(&ctx, screen)).flatten();
|
||||
let hover_at = pointer.filter(|_| outcome.hover.any());
|
||||
let over_at = pointer.filter(|_| outcome.contains);
|
||||
self.hovered = None;
|
||||
let menu_open = self.drags.menu.is_some() && crate::contract::overlay::container_open::opened_from(&ctx, cx.core.id);
|
||||
// A double click on a column's edge fits the column to its widest cell among the rows built here, in view.
|
||||
|
||||
@@ -193,9 +193,11 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
|
||||
}
|
||||
}
|
||||
|
||||
/// The pointer's position, in screen space, while it is over the collection.
|
||||
/// The pointer's position, in screen space, while it is over the collection, which then follows the pointer.
|
||||
fn hovered_pos(cx: &Cx<'_, C>) -> Option<Pos2> {
|
||||
crate::runtime::pointer::frame_raw(&cx.core.ctx).pos.filter(|_| cx.core.response.hovered())
|
||||
let response = &cx.core.response;
|
||||
let screen = crate::runtime::egui::areas::layer_to_screen(&cx.core.ctx, response.layer_id) * response.rect;
|
||||
response.hovered().then(|| crate::runtime::pointer::hover_point_in(&cx.core.ctx, screen)).flatten()
|
||||
}
|
||||
|
||||
/// The row under `pos` (screen space) in the rows' placement `at`: the pointer-to-row mapping of `hover_lead`,
|
||||
|
||||
@@ -421,7 +421,7 @@ fn item_openers(ui: &Ui, ctx: &Context, id: Id, enabled: bool, openers: Vec<Item
|
||||
clicked: on_part(outcome.activated),
|
||||
..response::PressView::default()
|
||||
},
|
||||
pointer: ctx.pointer_latest_pos(),
|
||||
pointer: crate::runtime::pointer::frame_raw(&ctx).pos,
|
||||
};
|
||||
let response = response::member(ui, opener_id(opener.item, opener.part), rect, enabled, view);
|
||||
Some((opener, response))
|
||||
|
||||
@@ -36,8 +36,9 @@ pub struct PartResult {
|
||||
pub press: PressState,
|
||||
/// The pointer is over it and nothing above takes the hover.
|
||||
pub hovered: bool,
|
||||
/// The pointer's position while `hovered`, in the owner's layer space.
|
||||
pub hover_pos: Option<Pos2>,
|
||||
/// The pointer's position while `hovered`, in the owner's layer space: read through `Cx::hover_pos`, which marks the
|
||||
/// part as following the pointer.
|
||||
pub(crate) hover_pos: Option<Pos2>,
|
||||
/// The value flags the component set on the part.
|
||||
pub flags: Flags,
|
||||
/// What the part senses, as declared.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
//! The cursor service (8.5): requests of a pass collect here, the end hook picks the winner and records it, and a
|
||||
//! painted winner reaches the window as its cursor image where the window shows one.
|
||||
|
||||
use crate::runtime::Shift;
|
||||
use crate::runtime::egui::areas::LayerKey;
|
||||
use crate::runtime::egui::{output, pass, viewports};
|
||||
use crate::runtime::frame::PerViewport;
|
||||
@@ -50,6 +51,19 @@ pub struct CursorRegion {
|
||||
pub clip: Rect,
|
||||
}
|
||||
|
||||
impl Shift for CursorRegion {
|
||||
fn shifted(&self, by: &egui::RecordShift) -> Self {
|
||||
CursorRegion { layer: self.layer, clip: crate::runtime::Clip::new(self.clip).shifted(by).rect() }
|
||||
}
|
||||
|
||||
fn snapped(&self, to: &Self, within: f32) -> Self {
|
||||
CursorRegion { layer: self.layer, clip: self.clip.snapped(&to.clip, within) }
|
||||
}
|
||||
}
|
||||
|
||||
// A cursor is drawn at the pointer, wherever the record that asked for it replays.
|
||||
crate::stays!(Cursor, CursorPriority);
|
||||
|
||||
impl CursorRegion {
|
||||
/// The region of `clip` on `layer`.
|
||||
pub fn new(layer: LayerKey, clip: Rect) -> Self {
|
||||
@@ -83,7 +97,13 @@ pub fn request_within(ctx: &Context, cursor: Cursor, priority: CursorPriority, r
|
||||
push(ctx, cursor, priority, Some(region));
|
||||
}
|
||||
|
||||
/// A request is a registration of the pass: a settled view's replay requests its cursor again.
|
||||
fn push(ctx: &Context, cursor: Cursor, priority: CursorPriority, region: Option<CursorRegion>) {
|
||||
crate::runtime::replay::journal(
|
||||
ctx,
|
||||
|| (cursor.clone(), priority, region),
|
||||
|ctx, (cursor, priority, region)| push(ctx, cursor.clone(), *priority, *region),
|
||||
);
|
||||
PerViewport::<State>::with(ctx, pass::current_viewport(ctx), |state| {
|
||||
state.requests.push((cursor, priority));
|
||||
state.regions.push(region);
|
||||
|
||||
@@ -102,7 +102,8 @@ pub(crate) fn member(ui: &Ui, id: Id, rect: Rect, enabled: bool, view: MemberVie
|
||||
fn member_on(ctx: &egui::Context, layer_id: LayerId, clip: Rect, id: Id, rect: Rect, enabled: bool, view: MemberView) -> Response {
|
||||
let mut flags = Flags::empty();
|
||||
let hovered = enabled && view.hovered;
|
||||
// egui gives the interaction position through the pass the press ends in.
|
||||
// egui gives the interaction position through the pass the press ends in. A hover position goes through
|
||||
// `Cx::hover_pos`, which marks what reads it as following the pointer.
|
||||
let mine = enabled && (view.press.pressed || view.press.drag_stopped || view.press.clicked);
|
||||
if enabled {
|
||||
flags |= Flags::ENABLED;
|
||||
@@ -117,7 +118,7 @@ fn member_on(ctx: &egui::Context, layer_id: LayerId, clip: Rect, id: Id, rect: R
|
||||
rect,
|
||||
interact_rect: rect.intersect(clip),
|
||||
sense: egui::Sense::hover(),
|
||||
interact_pointer_pos_or_nan: if hovered || mine { view.pointer.unwrap_or(Pos2::NAN) } else { Pos2::NAN },
|
||||
interact_pointer_pos_or_nan: if mine { view.pointer.unwrap_or(Pos2::NAN) } else { Pos2::NAN },
|
||||
intrinsic_size_or_nan: Vec2::NAN,
|
||||
flags,
|
||||
};
|
||||
|
||||
@@ -633,6 +633,7 @@ pub(crate) fn record_shapes_in(ctx: &Context, viewport: ViewportId, layer: Layer
|
||||
/// `record_shapes_in` with no note in the run records, for a caller whose own note makes the shapes again: shapes at
|
||||
/// stack positions a replay takes from the arena it joins (`sensing`), not from the recorded run.
|
||||
pub(crate) fn add_shapes_in(ctx: &Context, viewport: ViewportId, layer: Layer, root: Id, clip: Rect, shapes: Vec<Shape<Id>>) {
|
||||
crate::runtime::replay::note_root(ctx, root);
|
||||
PerViewport::<Shapes>::with(ctx, viewport, |record| {
|
||||
let order = record.next;
|
||||
record.next += 1;
|
||||
@@ -667,11 +668,32 @@ pub(crate) fn record_layer_widget(ctx: &Context, widget: Id) {
|
||||
/// Records that what the running pass shows in `rect` (screen points) follows the pointer: a cursor drawn at the
|
||||
/// pointer, a readout of the point under it. A pointer move into, out of or within `rect` runs a pass even when
|
||||
/// nothing under the pointer changes ([`gate_pointer_moves`]).
|
||||
///
|
||||
/// A memoized view whose run marked a rect runs on each pointer move into, out of or within it (`runtime::lazy`).
|
||||
pub fn follows_pointer(ctx: &Context, rect: Rect) {
|
||||
crate::runtime::replay::journal(ctx, || rect, |ctx, &rect| follows_pointer(ctx, rect));
|
||||
crate::runtime::replay::note_follows(ctx, rect);
|
||||
PerViewport::<Shapes>::with(ctx, pass::current_viewport(ctx), |record| record.this_follows.push(rect));
|
||||
}
|
||||
|
||||
/// The running pass's hover point (screen points) when it lies in `rect` (screen points), for code that shows what
|
||||
/// it reads there (a readout, a cursor drawn at the point, the row under it): `rect` then follows the pointer
|
||||
/// ([`follows_pointer`]). The one way component code reads where the pointer hovers.
|
||||
pub fn hover_point_in(ctx: &Context, rect: Rect) -> Option<Pos2> {
|
||||
let at = with_frame(ctx, |_, hover| hover).filter(|at| rect.contains(*at))?;
|
||||
follows_pointer(ctx, rect);
|
||||
Some(at)
|
||||
}
|
||||
|
||||
/// Marks the hit area of a part with the bounds `rect` on `layer` (layer space) as following the pointer: its owner
|
||||
/// read the hover position there (`Cx::hover_pos`). The area is the bounds grown to the least hit size across, as
|
||||
/// sensing grows them.
|
||||
pub(crate) fn follows_part(ctx: &Context, layer: Layer, rect: Rect) {
|
||||
let target = with_frame(ctx, |frame, _| frame.map_or(0.0, |frame| frame.target));
|
||||
let grow = (Vec2::splat(target) - rect.size()).max(Vec2::ZERO) / 2.0;
|
||||
follows_pointer(ctx, areas::layer_to_screen(ctx, layer) * rect.expand2(grow));
|
||||
}
|
||||
|
||||
/// Lets a pointer move run no pass when nothing it would change differs: egui's gate
|
||||
/// ([`egui::Context::set_pointer_move_gate`]) answers [`move_needs_pass`]. An app opts in once; without it every move
|
||||
/// runs a pass.
|
||||
@@ -743,6 +765,7 @@ pub(crate) fn record_widgets_in(ctx: &Context, viewport: ViewportId, layer: Laye
|
||||
|| (layer, root, widgets.to_vec(), click),
|
||||
|ctx, (layer, root, widgets, click)| record_widgets(ctx, *layer, *root, widgets, *click),
|
||||
);
|
||||
crate::runtime::replay::note_root(ctx, root);
|
||||
PerViewport::<Shapes>::with(ctx, viewport, |record| {
|
||||
record.this_made.insert(root);
|
||||
record.this_made.extend(widgets.iter().copied());
|
||||
|
||||
@@ -184,6 +184,13 @@ pub(crate) struct Facts {
|
||||
pub(crate) overlays: Vec<ShownLayer>,
|
||||
/// The layer stack entries the run recorded (`note_layer`): a close queued for one is taken only by a run.
|
||||
pub(crate) layers: Vec<egui::Id>,
|
||||
/// The arena roots the run registered shapes or egui widgets for (`note_root`): what pointer sensing gives them is
|
||||
/// what the run saw of the pointer (`pointer::Sensed`).
|
||||
pub(crate) roots: IdSet,
|
||||
/// The screen rects the run's content follows the pointer in (`pointer::follows_pointer`).
|
||||
pub(crate) follows: Vec<Rect>,
|
||||
/// The wheel takers the run registered (`wheel::register`): a share of the wheel for one runs it.
|
||||
pub(crate) takers: Vec<egui::Id>,
|
||||
}
|
||||
|
||||
/// An overlay layer a run showed: its egui layer and its rect in screen space.
|
||||
@@ -306,6 +313,9 @@ pub(crate) fn close(ctx: &Context, layer: Layer) -> Record {
|
||||
outer.facts.instances.extend(open.facts.instances.iter().copied());
|
||||
outer.facts.overlays.extend(open.facts.overlays.iter().copied());
|
||||
outer.facts.layers.extend(open.facts.layers.iter().copied());
|
||||
outer.facts.roots.extend(open.facts.roots.iter().copied());
|
||||
outer.facts.follows.extend(open.facts.follows.iter().copied());
|
||||
outer.facts.takers.extend(open.facts.takers.iter().copied());
|
||||
} else {
|
||||
recorder.journal.clear();
|
||||
}
|
||||
@@ -555,6 +565,23 @@ pub(crate) fn note_overlay(ctx: &Context, layer: Layer, rect: Rect) {
|
||||
with_open(ctx, |facts| facts.overlays.push(ShownLayer { layer, rect }));
|
||||
}
|
||||
|
||||
/// Notes that the running code registered shapes or egui widgets for the arena root `root`, by a run or a replay.
|
||||
pub(crate) fn note_root(ctx: &Context, root: egui::Id) {
|
||||
with_open(ctx, |facts| {
|
||||
facts.roots.insert(root);
|
||||
});
|
||||
}
|
||||
|
||||
/// Notes that what the running code shows in the screen rect `rect` follows the pointer, by a run or a replay.
|
||||
pub(crate) fn note_follows(ctx: &Context, rect: Rect) {
|
||||
with_open(ctx, |facts| facts.follows.push(rect));
|
||||
}
|
||||
|
||||
/// Notes that the running code registered the wheel taker `owner`, by a run or a replay.
|
||||
pub(crate) fn note_taker(ctx: &Context, owner: egui::Id) {
|
||||
with_open(ctx, |facts| facts.takers.push(owner));
|
||||
}
|
||||
|
||||
/// Notes that the running code recorded the layer stack entry `id`, by a run or a replay.
|
||||
pub(crate) fn note_layer(ctx: &Context, id: egui::Id) {
|
||||
with_open(ctx, |facts| facts.layers.push(id));
|
||||
|
||||
@@ -155,6 +155,7 @@ pub(crate) fn register(ctx: &Context, taker: WheelTaker<Id>) {
|
||||
return;
|
||||
}
|
||||
crate::runtime::replay::journal(ctx, || taker, |ctx, &taker| register(ctx, taker));
|
||||
crate::runtime::replay::note_taker(ctx, taker.owner);
|
||||
let layer = crate::runtime::overlay::drawing_parent(ctx);
|
||||
PerViewport::<State>::with(ctx, pass::current_viewport(ctx), |state| {
|
||||
state.this_layers.insert(taker.owner, layer);
|
||||
|
||||
@@ -748,7 +748,8 @@ impl Component for MenuBar<'_> {
|
||||
};
|
||||
let open = cx.core.toggles.iter().any(|toggle| toggle.part == part && toggle.open);
|
||||
// Under the pointer a title is the active row, as it is while its menu is open or the keyboard is on it.
|
||||
let lit = open || cx.hover_pos(part).is_some() || cx.part_state(part).flags.contains(Flags::FOCUS_VISIBLE);
|
||||
let lit =
|
||||
open || cx.part_if_declared(part).is_some_and(|result| result.hovered) || cx.part_state(part).flags.contains(Flags::FOCUS_VISIBLE);
|
||||
let mnemonic = match slot {
|
||||
Open::Title(at) if underlined => texts[at].1.and_then(|(_, index)| index),
|
||||
_ => None,
|
||||
|
||||
@@ -75,7 +75,8 @@ impl RowInput {
|
||||
/// Only the focus the menu gave as it opened is withheld from: a row or part that a request focused (an AccessKit
|
||||
/// `Focus`, the app) was aimed at, and the key runs it.
|
||||
pub(crate) fn of<C: Component>(cx: &mut Cx<'_, C>, level: &MenuLevel) -> RowInput {
|
||||
let moving = cx.core.ctx.input(|input| input.pointer.delta() != Vec2::ZERO);
|
||||
// The rest restarts at each move: a tracked read.
|
||||
let moving = crate::runtime::pointer::frame(&cx.core.ctx).delta != Vec2::ZERO;
|
||||
let hovered = cx.response.hovered();
|
||||
let focused = cx.is_focused();
|
||||
let keyboard = level.keyboard();
|
||||
|
||||
@@ -391,7 +391,8 @@ impl MenuLevel {
|
||||
/// The level a menu container puts into its body's env: the rows of the body register with it.
|
||||
pub(crate) fn level_env<C: Component>(cx: &Cx<'_, C>, values: &mut AppValues, submenu: bool, valued: bool, pad: f32) {
|
||||
let level = MenuLevel::of(&cx.recall::<LevelMemory>());
|
||||
let pointer = cx.core.ctx.input(|input| input.pointer.latest_pos());
|
||||
// The aim follows every move of the pointer while the menu shows: a tracked read.
|
||||
let pointer = crate::runtime::pointer::frame(&cx.core.ctx).pos;
|
||||
let keyboard = crate::runtime::focus::keyboard_in_menu(&cx.core.ctx);
|
||||
level.begin(submenu, valued, keyboard, (!cx.env.shell().os.is_mac()).then(|| cx.env.folder()), pointer, pad);
|
||||
values.set(level);
|
||||
|
||||
@@ -646,7 +646,7 @@ impl Component for Tab<'_> {
|
||||
}
|
||||
if let (Some(at), Some((side, rest, hover))) = (placed.close, style.close_glyph) {
|
||||
let mut glyph = IconSpec::from(IconId::new("close"));
|
||||
glyph.color = Some(if cx.hover_pos(CLOSE).is_some() { hover } else { rest });
|
||||
glyph.color = Some(if cx.part_if_declared(CLOSE).is_some_and(|result| result.hovered) { hover } else { rest });
|
||||
ui.draw().icon(Rect::from_center_size(at.center(), Vec2::splat(side)), &glyph);
|
||||
} else if placed.close.is_some() {
|
||||
let words = cx.words::<TabWords>();
|
||||
|
||||
@@ -890,7 +890,7 @@ impl<K: Clone + Eq + Hash + Send + Sync + 'static> Component for TileGrid<'_, K>
|
||||
}
|
||||
// The gap under the pointer shows where an insertion lands; the part's state layer shows hover and focus.
|
||||
for &(at, rect) in &self.placed_gaps {
|
||||
let hovered = cx.hover_pos(GAP.at(at as u64)).is_some();
|
||||
let hovered = cx.part_if_declared(GAP.at(at as u64)).is_some_and(|result| result.hovered);
|
||||
if hovered {
|
||||
let line = PaintShape::Path(vec![rect.center_top(), rect.center_bottom()]);
|
||||
ui.draw().stroke(&line, StrokeWidth::Thick, style.insert);
|
||||
|
||||
Reference in new issue
Block a user