mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-24 03:21:34 +01:00
Merge pull request #2182 from aiongg/contract-stack
contract: a component's run keeps only what it reads after its children on the stack
This commit is contained in:
23 files changed
+1840
-1043
No files matched your search
@@ -165,6 +165,11 @@ pub(super) struct BodyHooks<'b> {
|
||||
}
|
||||
|
||||
impl<'b> BodyHooks<'b> {
|
||||
/// The hooks of `comp`'s run, over its props as they are now.
|
||||
pub(super) fn of<C: super::Container>(comp: &C, body: &'b mut dyn FnMut(&mut Ui, &mut BodyCx<'_>)) -> Self {
|
||||
Self::new(body, Arc::new(comp.props(super::Internal::new()).clone()))
|
||||
}
|
||||
|
||||
pub(super) fn new(body: &'b mut dyn FnMut(&mut Ui, &mut BodyCx<'_>), props: Arc<ContainerProps>) -> Self {
|
||||
BodyHooks {
|
||||
body,
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -566,20 +566,26 @@ pub(super) fn shared_labels<R>(
|
||||
body: impl FnOnce(&mut Ui) -> R,
|
||||
) -> R {
|
||||
match columns {
|
||||
LabelColumns::Own => {
|
||||
let (result, scoped) = ColumnGroup::scope_measured(ui, sub(id, "labels"), seed, |ui, group| {
|
||||
group.offer(room);
|
||||
body(ui)
|
||||
});
|
||||
(found.extent, found.unsettled) = (scoped.extent, scoped.unsettled);
|
||||
result
|
||||
}
|
||||
LabelColumns::Through { sized } => {
|
||||
let (result, reached) = ColumnGroup::through(ui, sub(id, "through"), room, sized, body);
|
||||
found.reached = reached;
|
||||
result
|
||||
}
|
||||
LabelColumns::Own => own_labels(ui, id, (room, seed), found, body),
|
||||
LabelColumns::Through { sized } => through_labels(ui, id, (room, sized), found, body),
|
||||
LabelColumns::Field => ColumnGroup::field(ui, body),
|
||||
LabelColumns::Apart => ColumnGroup::barrier(ui, body),
|
||||
}
|
||||
}
|
||||
|
||||
/// The body's rows in a label group of their own, offered `room` and opened at the `seed` widths.
|
||||
fn own_labels<R>(ui: &mut Ui, id: Id, (room, seed): (f32, Option<Columns>), found: &mut LabelsFound, body: impl FnOnce(&mut Ui) -> R) -> R {
|
||||
let (result, scoped) = ColumnGroup::scope_measured(ui, sub(id, "labels"), seed, |ui, group| {
|
||||
group.offer(room);
|
||||
body(ui)
|
||||
});
|
||||
(found.extent, found.unsettled) = (scoped.extent, scoped.unsettled);
|
||||
result
|
||||
}
|
||||
|
||||
/// The body's rows in the label group around the container.
|
||||
fn through_labels<R>(ui: &mut Ui, id: Id, (room, sized): (f32, bool), found: &mut LabelsFound, body: impl FnOnce(&mut Ui) -> R) -> R {
|
||||
let (result, reached) = ColumnGroup::through(ui, sub(id, "through"), room, sized, body);
|
||||
found.reached = reached;
|
||||
result
|
||||
}
|
||||
@@ -18,7 +18,7 @@ mod words;
|
||||
|
||||
pub use access::Accessible;
|
||||
pub use body::BodyCx;
|
||||
pub(crate) use body::BodyOwner;
|
||||
pub(crate) use body::{BodyOwner, RegionShare};
|
||||
pub use context::{Activation, Cx, CxCore};
|
||||
pub(crate) use context::{RecordedText, Under, child_env};
|
||||
pub use edit::Edit;
|
||||
|
||||
@@ -33,6 +33,7 @@ pub struct BodyCx<'a> {
|
||||
}
|
||||
|
||||
impl<'a> BodyCx<'a> {
|
||||
#[allow(clippy::too_many_arguments, reason = "one argument per field the region's context starts from")]
|
||||
pub(crate) fn new(
|
||||
id: Id,
|
||||
env: EnvView<'a>,
|
||||
@@ -41,9 +42,12 @@ impl<'a> BodyCx<'a> {
|
||||
memory: &'a mut MemoryStage,
|
||||
open: &'a mut bool,
|
||||
gate: Option<&'a mut bool>,
|
||||
shared: Option<RegionShare<'a>>,
|
||||
) -> Self {
|
||||
memory.claim(id);
|
||||
BodyCx { id, env, resolved, rect, memory, open, gate, gate_mixed: false, close: None, field_message: None, owner: None }
|
||||
let (gate_mixed, field_message, owner) =
|
||||
shared.map_or((false, None, None), |shared| (shared.gate_mixed, shared.field_message, Some(shared.owner)));
|
||||
BodyCx { id, env, resolved, rect, memory, open, gate, gate_mixed, close: None, field_message, owner }
|
||||
}
|
||||
|
||||
/// Asks the platform to open `url`, in a new tab where the platform has tabs and `new_tab` is set: the opener
|
||||
@@ -61,16 +65,6 @@ impl<'a> BodyCx<'a> {
|
||||
self.env.host()?.files().take_answer(ticket)
|
||||
}
|
||||
|
||||
pub(crate) fn with_gate_mixed(mut self, mixed: bool) -> Self {
|
||||
self.gate_mixed = mixed;
|
||||
self
|
||||
}
|
||||
|
||||
pub(crate) fn with_field_message(mut self, message: Option<(Message, craft_theme::variant::Tone)>) -> Self {
|
||||
self.field_message = message;
|
||||
self
|
||||
}
|
||||
|
||||
/// The validity message of the first control in the container's label scope, recorded last frame, with the
|
||||
/// tone it shows in: a container whose footer declares `FooterRegion::status` shows it as its status text. `None`
|
||||
/// when the container has no label or no control reported one.
|
||||
@@ -147,13 +141,25 @@ impl<'a> BodyCx<'a> {
|
||||
Some(&super::Under::new(under.0, under.1)),
|
||||
);
|
||||
let show = Box::new(move |ui: &mut Ui, rect: Rect| {
|
||||
let _ =
|
||||
crate::contract::run::show_leaf(crate::contract::Props::id(content, content_id), ui, crate::contract::run::ShowAt::Rect(rect), None);
|
||||
let _ = crate::contract::run::show_leaf(
|
||||
&mut Some(crate::contract::Props::id(content, content_id)),
|
||||
ui,
|
||||
crate::contract::run::ShowAt::Rect(rect),
|
||||
None,
|
||||
);
|
||||
});
|
||||
label::flow_leaf(ui, (id, self.resolved.id), under, label, side, child, Some(label::Trailing { size, show }))
|
||||
}
|
||||
}
|
||||
|
||||
/// What a container's regions share in their `BodyCx`: whether its gate is mixed (the header's only), its field's
|
||||
/// message and the container that owns them.
|
||||
pub(crate) struct RegionShare<'a> {
|
||||
pub gate_mixed: bool,
|
||||
pub field_message: Option<(Message, craft_theme::variant::Tone)>,
|
||||
pub owner: BodyOwner<'a>,
|
||||
}
|
||||
|
||||
/// What a container's body reads of its container: the style step 2 resolved for this pass, and the body gap.
|
||||
#[derive(Clone, Copy)]
|
||||
pub(crate) struct BodyOwner<'a> {
|
||||
@@ -169,11 +175,6 @@ impl<'a> BodyOwner<'a> {
|
||||
}
|
||||
|
||||
impl<'a> BodyCx<'a> {
|
||||
pub(crate) fn with_owner(mut self, owner: BodyOwner<'a>) -> Self {
|
||||
self.owner = Some(owner);
|
||||
self
|
||||
}
|
||||
|
||||
/// The style of the container `C` whose body this is, as step 2 resolved it for this pass: the one way a body
|
||||
/// reads what its container's style holds (a dock root's docking sizes). `None` when the body's container has
|
||||
/// another style type, and for a body with no container: a popup's body or a collection's empty state.
|
||||
|
||||
@@ -334,8 +334,9 @@ impl StageAccess for CxCore<'_> {
|
||||
/// ```
|
||||
#[non_exhaustive]
|
||||
pub struct Cx<'a, C: Component> {
|
||||
/// The pass core, which the pipeline builds once per run and lends to every step.
|
||||
#[doc(hidden)]
|
||||
pub core: CxCore<'a>,
|
||||
pub core: &'a mut CxCore<'a>,
|
||||
/// The component's cached style.
|
||||
pub style: &'a C::Style,
|
||||
/// The shown copy of the value, shared with shown memory.
|
||||
@@ -354,7 +355,7 @@ impl<'a, C: Component> Deref for Cx<'a, C> {
|
||||
type Target = CxCore<'a>;
|
||||
|
||||
fn deref(&self) -> &CxCore<'a> {
|
||||
&self.core
|
||||
self.core
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -752,7 +752,7 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
|
||||
let mut empty_ui = alloc::child(ui, builder);
|
||||
let installed = Env::install_for(&empty_ui, env.derive(|scoped| scoped.extend_values(values)));
|
||||
let (resolved, view) = (cx.core.resolved, cx.core.env);
|
||||
let mut bcx = BodyCx::new(sub(id, "body"), view, resolved, rect, &mut *cx.core.memory, &mut open, None);
|
||||
let mut bcx = BodyCx::new(sub(id, "body"), view, resolved, rect, &mut *cx.core.memory, &mut open, None, None);
|
||||
comp.empty(&mut empty_ui, &mut bcx);
|
||||
Env::restore(&ctx, installed);
|
||||
}
|
||||
|
||||
@@ -303,7 +303,8 @@ pub(crate) fn flow_leaf<C: Show>(
|
||||
if cut {
|
||||
ui.set_clip_rect(Rect::NOTHING);
|
||||
}
|
||||
let output = crate::contract::run::show_leaf(crate::contract::Props::id(child, id), ui, crate::contract::run::ShowAt::Rect(control), None);
|
||||
let output =
|
||||
crate::contract::run::show_leaf(&mut Some(crate::contract::Props::id(child, id)), ui, crate::contract::run::ShowAt::Rect(control), None);
|
||||
if cut {
|
||||
ui.set_clip_rect(shown_clip);
|
||||
}
|
||||
@@ -325,7 +326,7 @@ pub(crate) fn show_leaf_in_flow<C: Show>(mut child: C, ui: &mut Ui) -> C::Output
|
||||
let named_by_scope = env.arena.is_none() && scope::in_label(ui.ctx());
|
||||
let shows = C::LABELLED_AFTER && child.shows_label() && child.common(crate::contract::Internal::new()).label != Label::Empty && !named_by_scope;
|
||||
if !shows {
|
||||
return crate::contract::run::show_leaf(child, ui, crate::contract::run::ShowAt::Flow, None);
|
||||
return crate::contract::run::show_leaf(&mut Some(child), ui, crate::contract::run::ShowAt::Flow, None);
|
||||
}
|
||||
let label = std::mem::take(&mut child.common_mut(crate::contract::Internal::new()).label);
|
||||
let facts = crate::contract::run::TypeFacts::of::<C>();
|
||||
|
||||
@@ -714,17 +714,17 @@ pub trait Show: Component + Meta<Shape = shape::Leaf> + sealed::Show {
|
||||
|
||||
/// Shows the component at `rect`, with no allocation.
|
||||
fn show_at(self, ui: &mut Ui, rect: Rect) -> Self::Output {
|
||||
run::show_leaf(self, ui, ShowAt::Rect(rect), None)
|
||||
run::show_leaf(&mut Some(self), ui, ShowAt::Rect(rect), None)
|
||||
}
|
||||
|
||||
/// Shows the component with rich tooltip content from the same call.
|
||||
fn show_with_tip(self, ui: &mut Ui, tip: impl FnOnce(&mut Ui)) -> Self::Output {
|
||||
run::show_leaf(self, ui, ShowAt::Flow, Some(Box::new(tip)))
|
||||
run::show_leaf(&mut Some(self), ui, ShowAt::Flow, Some(Box::new(tip)))
|
||||
}
|
||||
|
||||
/// Shows the component at `rect` with rich tooltip content.
|
||||
fn show_at_with_tip(self, ui: &mut Ui, rect: Rect, tip: impl FnOnce(&mut Ui)) -> Self::Output {
|
||||
run::show_leaf(self, ui, ShowAt::Rect(rect), Some(Box::new(tip)))
|
||||
run::show_leaf(&mut Some(self), ui, ShowAt::Rect(rect), Some(Box::new(tip)))
|
||||
}
|
||||
|
||||
/// Measures without allocating: steps 1 to 5 under the id `show` would use next. It registers no id, opens no
|
||||
@@ -738,18 +738,18 @@ pub trait Show: Component + Meta<Shape = shape::Leaf> + sealed::Show {
|
||||
pub trait ShowContainer: Container + sealed::ShowContainer {
|
||||
/// Shows the container in the flow of `ui`.
|
||||
fn show<R>(self, ui: &mut Ui, body: impl FnMut(&mut Ui, &mut BodyCx<'_>) -> R) -> Inner<Self::Output, R> {
|
||||
run::show_container(self, ui, ShowAt::Flow, body, None)
|
||||
run::show_container(&mut Some(self), ui, ShowAt::Flow, body, None)
|
||||
}
|
||||
|
||||
/// Shows the container at `rect`, with no allocation: a frame over a split or a layer the parent places.
|
||||
fn show_at<R>(self, ui: &mut Ui, rect: Rect, body: impl FnMut(&mut Ui, &mut BodyCx<'_>) -> R) -> Inner<Self::Output, R> {
|
||||
run::show_container(self, ui, ShowAt::Rect(rect), body, None)
|
||||
run::show_container(&mut Some(self), ui, ShowAt::Rect(rect), body, None)
|
||||
}
|
||||
|
||||
/// Shows the container at `rect` and takes that rect's room in the flow of `ui`: what is shown after it lies
|
||||
/// past it, as after [`show`](Self::show).
|
||||
fn show_at_taking_room<R>(self, ui: &mut Ui, rect: Rect, body: impl FnMut(&mut Ui, &mut BodyCx<'_>) -> R) -> Inner<Self::Output, R> {
|
||||
run::show_container_taking(self, ui, rect, body)
|
||||
run::show_container_taking(&mut Some(self), ui, rect, body)
|
||||
}
|
||||
|
||||
/// Measures the container with `body` without allocating: steps 1 to 5 under the id `show` would use next, with
|
||||
@@ -761,7 +761,7 @@ pub trait ShowContainer: Container + sealed::ShowContainer {
|
||||
|
||||
/// Shows the container with rich tooltip content.
|
||||
fn show_with_tip<R>(self, ui: &mut Ui, body: impl FnMut(&mut Ui, &mut BodyCx<'_>) -> R, tip: impl FnOnce(&mut Ui)) -> Inner<Self::Output, R> {
|
||||
run::show_container(self, ui, ShowAt::Flow, body, Some(Box::new(tip)))
|
||||
run::show_container(&mut Some(self), ui, ShowAt::Flow, body, Some(Box::new(tip)))
|
||||
}
|
||||
|
||||
/// Shows the container at `rect` with rich tooltip content.
|
||||
@@ -772,7 +772,7 @@ pub trait ShowContainer: Container + sealed::ShowContainer {
|
||||
body: impl FnMut(&mut Ui, &mut BodyCx<'_>) -> R,
|
||||
tip: impl FnOnce(&mut Ui),
|
||||
) -> Inner<Self::Output, R> {
|
||||
run::show_container(self, ui, ShowAt::Rect(rect), body, Some(Box::new(tip)))
|
||||
run::show_container(&mut Some(self), ui, ShowAt::Rect(rect), body, Some(Box::new(tip)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -780,7 +780,7 @@ pub trait ShowContainer: Container + sealed::ShowContainer {
|
||||
pub trait ShowCollection: Collection + sealed::ShowCollection {
|
||||
/// Shows the visible items of `source`, one `item` call each.
|
||||
fn show_items<'r>(self, ui: &mut Ui, source: &dyn ItemSource<Self::Key>, item: &mut ItemFn<'r, Self>) -> Self::Output {
|
||||
run::show_items(self, ui, source, item)
|
||||
run::show_items(&mut Some(self), ui, source, item)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -214,7 +214,7 @@ impl PopupHandle {
|
||||
/// Toggles or opens the popup from `trigger` at step 9, as the pass's pending open change.
|
||||
pub fn trigger<C: Component>(self, trigger: Trigger, cx: &mut Cx<'_, C>) {
|
||||
debug_assert!(C::OVERLAYS, "`{}` hosts a popup but sets `OVERLAYS = false`", std::any::type_name::<C>());
|
||||
self.registered(&cx.core);
|
||||
self.registered(cx.core);
|
||||
let core = &mut cx.core;
|
||||
let ctx = core.ctx.clone();
|
||||
let resting = core.response.hovered() || core.focus.visible;
|
||||
@@ -253,7 +253,7 @@ impl PopupHandle {
|
||||
/// Queues an open that step 15 writes. For `Trigger::Manual` this and the binding are the only ways it opens.
|
||||
pub fn open<C: Component>(self, cx: &mut Cx<'_, C>) {
|
||||
debug_assert!(C::OVERLAYS, "`{}` hosts a popup but sets `OVERLAYS = false`", std::any::type_name::<C>());
|
||||
self.open_in(&cx.core);
|
||||
self.open_in(cx.core);
|
||||
}
|
||||
|
||||
pub(super) fn open_in(self, core: &CxCore<'_>) {
|
||||
@@ -264,7 +264,7 @@ impl PopupHandle {
|
||||
/// Queues a close; step 15 writes the slot, so this pass still shows the popup open.
|
||||
pub fn close<C: Component>(self, cx: &mut Cx<'_, C>) {
|
||||
debug_assert!(C::OVERLAYS, "`{}` hosts a popup but sets `OVERLAYS = false`", std::any::type_name::<C>());
|
||||
self.close_in(&cx.core);
|
||||
self.close_in(cx.core);
|
||||
}
|
||||
|
||||
pub(super) fn close_in(self, core: &CxCore<'_>) {
|
||||
@@ -275,7 +275,7 @@ impl PopupHandle {
|
||||
/// Whether a toggle, `open` or `close` queued in this pass leaves the popup's open state different from the
|
||||
/// state the pass began with. Valid from step 9.
|
||||
pub fn changed<C: Component>(self, cx: &mut Cx<'_, C>) -> bool {
|
||||
self.registered(&cx.core);
|
||||
self.registered(cx.core);
|
||||
let began = cx.core.memory.shown::<OpenSlot>(self.id).0;
|
||||
open_now(cx.core.memory, self.id) != began
|
||||
}
|
||||
@@ -285,7 +285,7 @@ impl PopupHandle {
|
||||
/// open state that differs from the bound value is written to it. A value the bind refuses to take (a given one)
|
||||
/// never forces the popup. Returns whether the popup is open in this pass.
|
||||
pub fn bind_open<C: Component>(self, cx: &mut Cx<'_, C>, mut open: BindMut<'_, bool>) -> bool {
|
||||
self.registered(&cx.core);
|
||||
self.registered(cx.core);
|
||||
let began = cx.core.memory.shown::<OpenSlot>(self.id).0;
|
||||
let seen = cx.core.memory.session::<BoundOpen>(sub(self.id, "bound")).0.unwrap_or(began);
|
||||
let held = match open.read(cx) {
|
||||
@@ -314,7 +314,7 @@ impl PopupHandle {
|
||||
/// Whether the popup is open in this pass: valid from step 8, with the changes queued so far applied.
|
||||
pub fn is_open<C: Component>(self, cx: &mut Cx<'_, C>) -> bool {
|
||||
debug_assert!(C::OVERLAYS, "`{}` hosts a popup but sets `OVERLAYS = false`", std::any::type_name::<C>());
|
||||
self.is_open_in(&mut cx.core)
|
||||
self.is_open_in(cx.core)
|
||||
}
|
||||
|
||||
pub(crate) fn is_open_in(self, core: &mut CxCore<'_>) -> bool {
|
||||
|
||||
@@ -200,7 +200,7 @@ impl PaintUi<'_> {
|
||||
let installed = Env::install_for(kids, env);
|
||||
// The child sits in a rect its owner chose, and the owner's own measure answers for it: no body's transcript
|
||||
// holds it.
|
||||
let output = layout_memory::boundary(&ctx, true, || run::show_leaf(super::Props::id(child, id), kids, ShowAt::Rect(rect), tip));
|
||||
let output = layout_memory::boundary(&ctx, true, || run::show_leaf(&mut Some(super::Props::id(child, id)), kids, ShowAt::Rect(rect), tip));
|
||||
Env::restore(&ctx, installed);
|
||||
output
|
||||
}
|
||||
|
||||
@@ -496,7 +496,7 @@ pub(crate) trait Erased {
|
||||
fn pass_extent(&mut self, at: &Measuring<'_, '_>) -> Vec2;
|
||||
/// Builds the component's `Cx` over `core` and `style` (what `style` gave), runs `rest` with it, then `finish`.
|
||||
/// `rest` returns whether a real pass ran.
|
||||
fn with_cx<'a>(&mut self, core: CxCore<'a>, style: &'a dyn StyleView, rest: &mut dyn FnMut(&mut dyn ErasedPass<'a>) -> bool);
|
||||
fn with_cx<'a>(&mut self, core: &'a mut CxCore<'a>, style: &'a dyn StyleView, rest: &mut dyn FnMut(&mut dyn ErasedPass<'a>) -> bool);
|
||||
|
||||
fn shape(&self) -> ShapeKind;
|
||||
fn declares_parts(&self) -> bool;
|
||||
@@ -651,19 +651,6 @@ struct Pass<'p, 'r, 'a> {
|
||||
injected: Option<commands::FireSource>,
|
||||
}
|
||||
|
||||
/// Steps 6 to 16, in the `Ui` the host gives, inside the component's focus scope.
|
||||
fn pass(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, host_env: Option<&Env>) {
|
||||
let scope = if p.sizing || ui.is_sizing_pass() { None } else { p.p.focus() };
|
||||
let (ctx, id) = (p.p.core_ref().ctx.clone(), p.resolved.id);
|
||||
if let Some(policy) = scope {
|
||||
focus::push_scope(&ctx, id, policy, None);
|
||||
}
|
||||
run_in_host(p, ui, host_at, host_env);
|
||||
if scope.is_some() {
|
||||
focus::pop_scope(&ctx);
|
||||
}
|
||||
}
|
||||
|
||||
/// A component wider than its room widens the flow `Ui` it was placed in, toward the end of the line.
|
||||
fn expand_for_overflow(ui: &mut Ui, at: ShowAt, rect: Rect, overflow: f32, rtl: bool) {
|
||||
if overflow > 0.0 && matches!(at, ShowAt::Flow) {
|
||||
@@ -850,15 +837,70 @@ fn create_node(ctx: &Context, ui: &Ui, id: Id, member: bool, role: Role) -> bool
|
||||
}
|
||||
}
|
||||
|
||||
/// Steps 6 to 16 of the pass, run in the `Ui` the host gives.
|
||||
#[expect(
|
||||
clippy::cognitive_complexity,
|
||||
reason = "steps 6 to 16 in their fixed order; each step is already its own function and the rest is the order"
|
||||
)]
|
||||
/// Steps 6 to 16, in the `Ui` the host gives, inside the component's focus scope. Children run inside the overlay
|
||||
/// step, `paint`, the content and `paint_over`; each step group is its own function, so during a child's run this
|
||||
/// level keeps only `HostStep` on the stack.
|
||||
fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, host_env: Option<&Env>) {
|
||||
let scope = enter_scope(p, ui);
|
||||
let env = host_env.unwrap_or(p.env);
|
||||
if let Some(mut h) = sense_steps(p, ui, host_at, env) {
|
||||
update_steps(p, &h);
|
||||
overlay_step(p, ui, env, &h);
|
||||
state_steps(p, ui, env, &h);
|
||||
paint_step(p, ui, env, &mut h);
|
||||
content_step(p, ui, env, &h);
|
||||
paint_over_step(p, ui, env, &h);
|
||||
report_steps(p, ui, env, &h);
|
||||
}
|
||||
if let Some(ctx) = scope {
|
||||
focus::pop_scope(&ctx);
|
||||
}
|
||||
}
|
||||
|
||||
/// Opens the component's focus scope, outside a sizing pass. Returns the context to close it with.
|
||||
fn enter_scope(p: &Pass<'_, '_, '_>, ui: &Ui) -> Option<Context> {
|
||||
let policy = if p.sizing || ui.is_sizing_pass() { None } else { p.p.focus() };
|
||||
policy.map(|policy| {
|
||||
let ctx = p.p.core_ref().ctx.clone();
|
||||
focus::push_scope(&ctx, p.resolved.id, policy, None);
|
||||
ctx
|
||||
})
|
||||
}
|
||||
|
||||
/// What steps 9 to 16 of one pass read of steps 6 to 8b.
|
||||
struct HostStep {
|
||||
ctx: Context,
|
||||
id: Id,
|
||||
/// The component's own direction: right to left unless its axis keeps its order.
|
||||
rtl: bool,
|
||||
rect: Rect,
|
||||
/// The parts step 7 declared.
|
||||
decls: super::cx::Decls,
|
||||
text_keys: Option<focus::KeyUse>,
|
||||
own: Sense,
|
||||
member: Option<crate::runtime::env::ArenaId>,
|
||||
node: bool,
|
||||
/// The bar line whose rules wait for this member.
|
||||
line: Option<Id>,
|
||||
/// A bar's view cut the member: it shows nothing until the clip is restored.
|
||||
cut: bool,
|
||||
shown_clip: Rect,
|
||||
inspected: bool,
|
||||
/// The values `content_env` gave the content at step 13.
|
||||
values: AppValues,
|
||||
/// The clip around the component, and the clip inside its rect grown by its paint outset.
|
||||
outer_clip: Rect,
|
||||
inside: Rect,
|
||||
}
|
||||
|
||||
/// Steps 6 to 8b: place, parts, sense and the text part. `None` when the pass stops before 8b: a sizing pass, or an
|
||||
/// inert component.
|
||||
// Kept out of the recursive frame above it: opt-level 2 inlines it there, so its locals stay on the stack while children run.
|
||||
#[inline(never)]
|
||||
fn sense_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, env: &Env) -> Option<HostStep> {
|
||||
let ctx = p.p.core_ref().ctx.clone();
|
||||
let (id, rtl) = (p.resolved.id, p.env.direction.is_rtl());
|
||||
let (env, resolved, inert, facts, style) = (host_env.unwrap_or(p.env), p.resolved, p.inert, p.facts, p.style);
|
||||
let (resolved, inert, facts) = (p.resolved, p.inert, p.facts);
|
||||
// 6 place
|
||||
let at = host_at.unwrap_or(p.at);
|
||||
// A root that fills takes the room of the `Ui` its host runs it in: a pane's or a layer's own, not its parent's.
|
||||
@@ -877,7 +919,7 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, h
|
||||
// The output words the value in a measuring run too, so what is measured beside the component holds it.
|
||||
let text = p.p.value_text();
|
||||
*p.p.core_ref().value_text.borrow_mut() = text;
|
||||
return;
|
||||
return None;
|
||||
}
|
||||
p.real = true;
|
||||
let inspected = p.trace.recording() && inspect::open_element(ui, id, facts.name);
|
||||
@@ -1006,7 +1048,7 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, h
|
||||
if inspected {
|
||||
inspect::close_element(&ctx, id, rect, rect);
|
||||
}
|
||||
return;
|
||||
return None;
|
||||
}
|
||||
// The popups the text part and `update` register start over for this pass.
|
||||
if facts.overlays {
|
||||
@@ -1025,6 +1067,32 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, h
|
||||
super::text::run_selections(e.core(), &parts.text.selections);
|
||||
e.core().selections = parts.text.selections.iter().map(|decl| decl.part).collect();
|
||||
}
|
||||
Some(HostStep {
|
||||
ctx,
|
||||
id,
|
||||
rtl,
|
||||
rect,
|
||||
decls: parts.decls,
|
||||
text_keys,
|
||||
own,
|
||||
member,
|
||||
node,
|
||||
line,
|
||||
cut,
|
||||
shown_clip,
|
||||
inspected,
|
||||
values: AppValues::default(),
|
||||
outer_clip: Rect::NOTHING,
|
||||
inside: Rect::NOTHING,
|
||||
})
|
||||
}
|
||||
|
||||
/// Step 9, update, with the body's roving group, which registers after `update`.
|
||||
// Kept out of the recursive frame above it: opt-level 2 inlines it there, so its locals stay on the stack while children run.
|
||||
#[inline(never)]
|
||||
fn update_steps(p: &mut Pass<'_, '_, '_>, h: &HostStep) {
|
||||
let (facts, ctx, id, rtl) = (p.facts, &h.ctx, h.id, h.rtl);
|
||||
let e = &mut *p.p;
|
||||
// 9 update
|
||||
p.trace.mark(Step::Update);
|
||||
e.gesture_gate();
|
||||
@@ -1034,7 +1102,7 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, h
|
||||
e.scrub_by_label();
|
||||
e.update();
|
||||
if cfg!(debug_assertions) {
|
||||
let keys = text_keys.unwrap_or_else(|| e.keys());
|
||||
let keys = h.text_keys.unwrap_or_else(|| e.keys());
|
||||
let held = |mv: NavMove| match keys.keep {
|
||||
focus::KeepKeys::BlockArrows => true,
|
||||
focus::KeepKeys::InlineArrows => matches!(mv, NavMove::Back | NavMove::Forward),
|
||||
@@ -1057,28 +1125,39 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, h
|
||||
None => group,
|
||||
};
|
||||
focus::register_group(
|
||||
&ctx,
|
||||
ctx,
|
||||
match core.roving_arrival {
|
||||
Some(arrival) => group.selected((arrival, None)),
|
||||
None => group,
|
||||
},
|
||||
);
|
||||
}
|
||||
// 10 overlay
|
||||
}
|
||||
|
||||
/// Step 10, overlay: the component's popups show their content here.
|
||||
fn overlay_step(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, env: &Env, h: &HostStep) {
|
||||
p.trace.mark(Step::Overlay);
|
||||
if facts.overlays {
|
||||
let core = e.core();
|
||||
if p.facts.overlays {
|
||||
let (ctx, id) = (&h.ctx, h.id);
|
||||
let core = p.p.core();
|
||||
let open = overlay::open_map(&mut *core.memory, id);
|
||||
let parts = core
|
||||
.toggles
|
||||
.iter()
|
||||
.filter_map(|toggle| core.results.get(&(toggle.part.key, toggle.part.index)).map(|result| (toggle.part, result.rect)))
|
||||
.collect();
|
||||
let mut overlays = OverlayUi::new(&ctx, overlay::OverlayOwner { id, anchor: rect, parts }, &mut *ui, resolved, env.clone(), open);
|
||||
e.overlay(&mut overlays);
|
||||
overlay::apply_closes(&ctx, overlays.closed);
|
||||
let mut overlays = OverlayUi::new(ctx, overlay::OverlayOwner { id, anchor: h.rect, parts }, &mut *ui, p.resolved, env.clone(), open);
|
||||
p.p.overlay(&mut overlays);
|
||||
overlay::apply_closes(ctx, overlays.closed);
|
||||
}
|
||||
// What `update` and `overlay` left untaken goes back to the key service.
|
||||
}
|
||||
|
||||
/// Steps 11 (apply) and 12 (state), after what `update` and `overlay` left untaken goes back to the key service.
|
||||
// Kept out of the recursive frame above it: opt-level 2 inlines it there, so its locals stay on the stack while children run.
|
||||
#[inline(never)]
|
||||
fn state_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, env: &Env, h: &HostStep) {
|
||||
let (facts, resolved, style) = (p.facts, p.resolved, p.style);
|
||||
let e = &mut *p.p;
|
||||
steps::after_update(e.core());
|
||||
// 11 apply
|
||||
p.trace.mark(Step::Apply);
|
||||
@@ -1089,68 +1168,92 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, h
|
||||
e.register_reset();
|
||||
// 12 state
|
||||
p.trace.mark(Step::State);
|
||||
let open = facts.overlays && overlay::popup_shown(&ctx, id);
|
||||
let open = facts.overlays && overlay::popup_shown(&h.ctx, h.id);
|
||||
let expanded = e.expanded() == Some(true);
|
||||
steps::state(e, style, open, expanded, env.ink);
|
||||
if e.core_ref().state.flags.contains(crate::style::Flags::FOCUSED) {
|
||||
commands::note_focus(&ctx, id, &env.commands);
|
||||
commands::note_focus(&h.ctx, h.id, &env.commands);
|
||||
}
|
||||
// 13 paint
|
||||
}
|
||||
|
||||
/// Step 13, paint: a composite's `paint` shows its children here. Leaves the clip inside the component's rect for
|
||||
/// the content, and the values `content_env` gives it.
|
||||
fn paint_step(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, env: &Env, h: &mut HostStep) {
|
||||
p.trace.mark(Step::Paint);
|
||||
let e = &mut *p.p;
|
||||
let fade = steps::fade(ui, env, e.core());
|
||||
let rule_from = line.filter(|_| facts.rule && !cut).map(|line| (line, steps::next_shape(ui)));
|
||||
steps::paint_surface(ui, env, e.core_ref(), style);
|
||||
let rule_from = h.line.filter(|_| p.facts.rule && !h.cut).map(|line| (line, steps::next_shape(ui)));
|
||||
steps::paint_surface(ui, env, e.core_ref(), p.style);
|
||||
steps::paint_part_layers(ui, env, e.core_ref());
|
||||
// Paint and content stay inside the rect (grown by the type's declared outset), however small the room is.
|
||||
let outer_clip = ui.clip_rect();
|
||||
let inside = e.core_ref().rect.expand(e.paint_outset(&env.tokens)).intersect(outer_clip);
|
||||
ui.set_clip_rect(inside);
|
||||
h.outer_clip = ui.clip_rect();
|
||||
h.inside = e.core_ref().rect.expand(e.paint_outset(&env.tokens)).intersect(h.outer_clip);
|
||||
ui.set_clip_rect(h.inside);
|
||||
e.paint(ui, env);
|
||||
// The content's values may read what `paint` found this pass.
|
||||
let mut values = AppValues::default();
|
||||
e.content_env(&mut values);
|
||||
e.content_env(&mut h.values);
|
||||
steps::unfade(ui, e.core(), fade);
|
||||
ui.set_clip_rect(outer_clip);
|
||||
steps::paint_focus(ui, e.core_ref(), style);
|
||||
ui.set_clip_rect(h.outer_clip);
|
||||
steps::paint_focus(ui, e.core_ref(), p.style);
|
||||
// A rule a bar's view shows waits for the member after it.
|
||||
if let Some((line, from)) = rule_from {
|
||||
hold_rule(ui, line, from);
|
||||
}
|
||||
ui.set_clip_rect(inside);
|
||||
// 14 content, then `paint_over`
|
||||
if p.trace.mark(Step::Content) {
|
||||
if facts.role == Role::Merged {
|
||||
scope::begin(&ctx, id, id, scope::Kind::Merge);
|
||||
}
|
||||
// A real run checks a body's content against the kept transcript its records came from; a collection's rows
|
||||
// are its own measure, not the enclosing body's, and a layer's content is not part of its parent's flow.
|
||||
let scoped = (!p.sizing && !crate::runtime::measure::active(&ctx))
|
||||
.then(|| match e.shape() {
|
||||
ShapeKind::Body => Some((Some(id), !e.in_flow())),
|
||||
ShapeKind::Item => Some((None, true)),
|
||||
_ => None,
|
||||
})
|
||||
.flatten();
|
||||
if let Some((body, boundary)) = scoped {
|
||||
crate::runtime::layout_memory::open_content(&ctx, body, boundary);
|
||||
}
|
||||
e.content(ui, env, &values);
|
||||
if scoped.is_some() {
|
||||
crate::runtime::layout_memory::close_content(&ctx, &e.regions(id));
|
||||
}
|
||||
ui.set_clip_rect(h.inside);
|
||||
}
|
||||
|
||||
/// Step 14, content: a body's regions or a collection's rows.
|
||||
fn content_step(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, env: &Env, h: &HostStep) {
|
||||
if !p.trace.mark(Step::Content) {
|
||||
return;
|
||||
}
|
||||
// The `over` parts are placed before `paint_over`, so the children it shows stack above them (8.3).
|
||||
ui.set_clip_rect(outer_clip);
|
||||
sensing::place_over_widgets(ui, id, member, decls);
|
||||
ui.set_clip_rect(inside);
|
||||
let (ctx, id) = (&h.ctx, h.id);
|
||||
let e = &mut *p.p;
|
||||
if p.facts.role == Role::Merged {
|
||||
scope::begin(ctx, id, id, scope::Kind::Merge);
|
||||
}
|
||||
// A real run checks a body's content against the kept transcript its records came from; a collection's rows
|
||||
// are its own measure, not the enclosing body's, and a layer's content is not part of its parent's flow.
|
||||
let scoped = (!p.sizing && !crate::runtime::measure::active(ctx))
|
||||
.then(|| match e.shape() {
|
||||
ShapeKind::Body => Some((Some(id), !e.in_flow())),
|
||||
ShapeKind::Item => Some((None, true)),
|
||||
_ => None,
|
||||
})
|
||||
.flatten();
|
||||
if let Some((body, boundary)) = scoped {
|
||||
crate::runtime::layout_memory::open_content(ctx, body, boundary);
|
||||
}
|
||||
e.content(ui, env, &h.values);
|
||||
if scoped.is_some() {
|
||||
crate::runtime::layout_memory::close_content(ctx, &e.regions(id));
|
||||
}
|
||||
}
|
||||
|
||||
/// `paint_over`, after the `over` parts are placed, so the children it shows stack above them (8.3).
|
||||
fn paint_over_step(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, env: &Env, h: &HostStep) {
|
||||
ui.set_clip_rect(h.outer_clip);
|
||||
sensing::place_over_widgets(ui, h.id, h.member, h.decls.parts());
|
||||
ui.set_clip_rect(h.inside);
|
||||
let e = &mut *p.p;
|
||||
let fade = steps::fade(ui, env, e.core());
|
||||
e.paint_over(ui, env);
|
||||
steps::unfade(ui, e.core(), fade);
|
||||
ui.set_clip_rect(outer_clip);
|
||||
ui.set_clip_rect(h.outer_clip);
|
||||
}
|
||||
|
||||
/// Steps 15 (settle) and 16 (report, then the tooltip), and the activation step 17 fires.
|
||||
// Kept out of the recursive frame above it: opt-level 2 inlines it there, so its locals stay on the stack while children run.
|
||||
#[inline(never)]
|
||||
fn report_steps(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, env: &Env, h: &HostStep) {
|
||||
let (facts, resolved, style) = (p.facts, p.resolved, p.style);
|
||||
let (ctx, id) = (&h.ctx, h.id);
|
||||
let e = &mut *p.p;
|
||||
// 15 settle
|
||||
p.trace.mark(Step::Settle);
|
||||
e.settle();
|
||||
let edit = e.settle_values();
|
||||
let leaf = e.shape() == ShapeKind::Leaf;
|
||||
steps::rerun_after_edit(e, style, edit, &MeasureIn { ui: &*ui, env: p.env, proposal: Proposal::Ideal, room: p.room }, resolved, leaf);
|
||||
// 16 report, then the tooltip.
|
||||
p.trace.mark(Step::Report);
|
||||
@@ -1158,33 +1261,33 @@ fn run_in_host(p: &mut Pass<'_, '_, '_>, ui: &mut Ui, host_at: Option<ShowAt>, h
|
||||
e,
|
||||
facts,
|
||||
&steps::ReportIn {
|
||||
node,
|
||||
decls: &parts.decls,
|
||||
sense: own,
|
||||
node: h.node,
|
||||
decls: &h.decls,
|
||||
sense: h.own,
|
||||
live: e.live(),
|
||||
scrolls: e.scrolls(),
|
||||
expanded: e.expanded(),
|
||||
position: e.position(),
|
||||
group: steps::member_group(&ctx, env.roving, id),
|
||||
group: steps::member_group(ctx, env.roving, id),
|
||||
},
|
||||
);
|
||||
let shows_label = e.shows_label();
|
||||
tooltip::run(e.core_ref(), tooltip::TipIn { ui: &*ui, env, rich: p.tip.take(), parts: &parts.decls, shows_label, message });
|
||||
tooltip::run(e.core_ref(), tooltip::TipIn { ui: &*ui, env, rich: p.tip.take(), parts: &h.decls, shows_label, message });
|
||||
// Activations accumulate across the passes of a viewport mount; step 17 fires the bound command once, with the
|
||||
// injected source when there is one.
|
||||
let natural = e.activated() && resolved.enabled;
|
||||
let injected = e.core().injected.take().filter(|_| resolved.enabled);
|
||||
if (natural || injected.is_some()) && !e.core_ref().keep_open {
|
||||
crate::runtime::overlay::note_activated(&ctx, id);
|
||||
crate::runtime::overlay::note_activated(ctx, id);
|
||||
}
|
||||
p.activated |= natural || injected.is_some();
|
||||
p.injected = p.injected.or(injected);
|
||||
if inspected {
|
||||
if h.inspected {
|
||||
let core = e.core_ref();
|
||||
inspect::close_element(&ctx, id, core.rect, core.content_rect);
|
||||
inspect::close_element(ctx, id, core.rect, core.content_rect);
|
||||
}
|
||||
if cut {
|
||||
ui.set_clip_rect(shown_clip);
|
||||
if h.cut {
|
||||
ui.set_clip_rect(h.shown_clip);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1286,12 +1389,76 @@ pub(super) fn run(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, tip: Option<TipFn
|
||||
}
|
||||
}
|
||||
|
||||
/// Steps 1 to 17 of `run`.
|
||||
#[expect(
|
||||
clippy::cognitive_complexity,
|
||||
reason = "steps 1 to 17 in their fixed order; each step is already its own function and the rest is the order"
|
||||
)]
|
||||
fn run_steps(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option<TipFn<'_>>) {
|
||||
/// Steps 1 to 17 of `run`. A component's children run inside step 5 (a body's measuring run) and inside the passes
|
||||
/// of steps 6 to 16, so the frames on that path keep only what the run reads after them: each step runs in its own
|
||||
/// function, and `run_steps` holds what steps 1 to 5 found.
|
||||
fn run_steps(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, tip: Option<TipFn<'_>>) {
|
||||
let begun = begin(t, ui);
|
||||
let mut stage = begun.open_stage(t);
|
||||
let mixed = value_step(t, &begun, &mut stage);
|
||||
let resolved = begun.resolved(t);
|
||||
let room = room_step(t, ui, at, &begun);
|
||||
let extent = extent_step(t, ui, (at, mixed), (&begun, &resolved), &mut stage);
|
||||
let sizes = measure_step(t, ui, at, (&begun, &resolved, &room), extent, &mut stage);
|
||||
let real = run_passes(t, ui, at, tip, (&begun, &resolved, &sizes), &mut stage);
|
||||
if real {
|
||||
t.record(&mut stage, &begun.ctx, resolved.id);
|
||||
}
|
||||
if !begun.sizing {
|
||||
stage.write_back();
|
||||
}
|
||||
}
|
||||
|
||||
/// What steps 1 to 4 found, which the rest of the run reads.
|
||||
struct Begun {
|
||||
ctx: Context,
|
||||
facts: TypeFacts,
|
||||
shape: ShapeKind,
|
||||
/// The parent `Ui` is in egui's invisible sizing pass: steps 1 to 6 run and allocate the size, with no id
|
||||
/// registration, sensing or memory writes.
|
||||
sizing: bool,
|
||||
/// The inspector records this run; read once per run.
|
||||
recording: bool,
|
||||
/// The env of the parent `Ui`, which the style reads.
|
||||
env: Env,
|
||||
/// The env with the component's own command instance, when it has one.
|
||||
own_env: Option<Env>,
|
||||
/// `Common`, moved out of the component; `Resolved` borrows it.
|
||||
common: Common,
|
||||
step1: steps::EnvStep,
|
||||
style: Arc<dyn StyleView>,
|
||||
id: Id,
|
||||
}
|
||||
|
||||
impl Begun {
|
||||
/// The env steps 4 to 17 run under.
|
||||
fn env(&self) -> &Env {
|
||||
self.own_env.as_ref().unwrap_or(&self.env)
|
||||
}
|
||||
|
||||
fn trace(&self) -> Trace<'_> {
|
||||
Trace::new(&self.ctx, self.id, self.shape, self.recording)
|
||||
}
|
||||
|
||||
/// The memory stage of step 3: a copy in a sizing pass, the component's own stage otherwise.
|
||||
fn open_stage(&self, t: &dyn Erased) -> MemoryStage {
|
||||
if self.sizing {
|
||||
MemoryStage::open_peek(&self.ctx, self.id)
|
||||
} else {
|
||||
MemoryStage::open_owned(&self.ctx, self.id, t.memory_owner().unwrap_or(self.id))
|
||||
}
|
||||
}
|
||||
|
||||
/// `Common` after step 1, with the padding the shape adds.
|
||||
fn resolved(&self, t: &dyn Erased) -> Resolved<'_> {
|
||||
Resolved { pad: t.pad(&self.env().tokens), ..steps::resolved(self.id, &self.step1, &self.common) }
|
||||
}
|
||||
}
|
||||
|
||||
/// Steps 1 to 3: env, style and id.
|
||||
// Kept out of the recursive frame above it: opt-level 2 inlines it there, so its locals stay on the stack while children run.
|
||||
#[inline(never)]
|
||||
fn begin(t: &mut dyn Erased, ui: &Ui) -> Begun {
|
||||
let ctx = ui.ctx().clone();
|
||||
let facts = t.facts();
|
||||
crate::runtime::measure::count_run(&ctx, facts.name);
|
||||
@@ -1304,32 +1471,64 @@ fn run_steps(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option<TipFn<
|
||||
let sizing = ui.is_sizing_pass();
|
||||
// The inspector flag is read once per run.
|
||||
let recording = !sizing && inspect::recording(&ctx);
|
||||
// 1 env: `Common` moves out of the component; `Resolved` borrows these locals.
|
||||
// 1 env: `Common` moves out of the component; `Resolved` borrows it.
|
||||
let env = Env::of(ui);
|
||||
let common: Common = t.take_common();
|
||||
let step1 = steps::env(&ctx, &env, &common, t.variants(common.variant));
|
||||
// 2 style
|
||||
let scx = steps::style_cx(&ctx, &env, &step1);
|
||||
let style = t.style(&ctx, &scx);
|
||||
// 3 id, and the memory stage for it
|
||||
let style = t.style(&ctx, &steps::style_cx(&ctx, &env, &step1));
|
||||
// 3 id
|
||||
let id = steps::id(ui, &common.id, !sizing, true, facts.style_key, facts.name);
|
||||
let trace = Trace::new(&ctx, id, shape, recording);
|
||||
if recording {
|
||||
inspect::begin(&ctx, id);
|
||||
let trace = Trace::new(&ctx, id, shape, recording);
|
||||
for step in [Step::Env, Step::Style, Step::Id] {
|
||||
trace.mark(step);
|
||||
}
|
||||
}
|
||||
let mut stage = if sizing { MemoryStage::open_peek(&ctx, id) } else { MemoryStage::open_owned(&ctx, id, t.memory_owner().unwrap_or(id)) };
|
||||
let resolved = Resolved { pad: t.pad(&env.tokens), ..steps::resolved(id, &step1, &common) };
|
||||
// 4 value. A sizing pass reads the cancel and ends nothing: its stage is a copy, and the real run reads the
|
||||
// cancel again.
|
||||
let mixed = t.value(&mut stage, id, step1.command.as_deref(), sizing);
|
||||
t.read(&mut stage, id, &env);
|
||||
// The command instance the component is, which steps 4 to 17 run under.
|
||||
let own_env = t.instance_scope().map(|scope| env.derive_local(|scoped| scoped.inherited_mut().commands = scoped.commands.pushed(scope, id)));
|
||||
let env = own_env.as_ref().unwrap_or(&env);
|
||||
trace.mark(Step::Value);
|
||||
// 5 measure
|
||||
Begun { ctx, facts, shape, sizing, recording, env, own_env, common, step1, style, id }
|
||||
}
|
||||
|
||||
/// Step 4, value: returns whether the binding holds a mixed value. A sizing pass reads the cancel and ends nothing:
|
||||
/// its stage is a copy, and the real run reads the cancel again.
|
||||
fn value_step(t: &mut dyn Erased, begun: &Begun, stage: &mut MemoryStage) -> bool {
|
||||
let mixed = t.value(stage, begun.id, begun.step1.command.as_deref(), begun.sizing);
|
||||
t.read(stage, begun.id, &begun.env);
|
||||
begun.trace().mark(Step::Value);
|
||||
mixed
|
||||
}
|
||||
|
||||
/// What step 5 found: the room, the measured size and the size the root takes.
|
||||
struct Sizes {
|
||||
/// The width the parent offers, which step 5 and step 7's measure view both read.
|
||||
room: Option<f32>,
|
||||
/// The size step 5 measured, before `sized` made it fill.
|
||||
measured: Vec2,
|
||||
/// The size the root takes in the parent `Ui`.
|
||||
size: Vec2,
|
||||
inert: bool,
|
||||
/// The row around the leaf gave it no width: it takes no place in the flow (`layout::shrink_cap`).
|
||||
dropped: bool,
|
||||
}
|
||||
|
||||
/// What step 5 reads of the room before the measuring run: the width the parent offers and the shrink step of the
|
||||
/// body around.
|
||||
#[derive(Clone, Copy)]
|
||||
struct Room {
|
||||
room: Option<f32>,
|
||||
in_flow: bool,
|
||||
fill_body: bool,
|
||||
shrink: Option<crate::layout::Shrink>,
|
||||
cap: Option<f32>,
|
||||
dropped: bool,
|
||||
}
|
||||
|
||||
/// Step 5's room: what the parent offers, capped by the width the row around gave a leaf.
|
||||
fn room_step(t: &mut dyn Erased, ui: &Ui, at: ShowAt, begun: &Begun) -> Room {
|
||||
let (ctx, shape, id) = (&begun.ctx, begun.shape, begun.id);
|
||||
let env = begun.env();
|
||||
let room = match at {
|
||||
ShowAt::Rect(rect) => Some(rect.width()),
|
||||
// A measuring run in a content column asks for what the content needs: nothing there fills the room. Once the
|
||||
@@ -1337,7 +1536,7 @@ fn run_steps(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option<TipFn<
|
||||
ShowAt::Flow => match env.column {
|
||||
Some(ColumnSweep::Column(width)) if env.content_column && t.fills_width() => Some(width),
|
||||
_ => Some(crate::layout::flow_room(ui).x)
|
||||
.filter(|width| width.is_finite() && !(env.content_column && crate::runtime::measure::active(&ctx))),
|
||||
.filter(|width| width.is_finite() && !(env.content_column && crate::runtime::measure::active(ctx))),
|
||||
},
|
||||
};
|
||||
// A leaf or body in flow that does not fill takes part in the shrink step of the body around it: in a row that
|
||||
@@ -1348,7 +1547,7 @@ fn run_steps(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option<TipFn<
|
||||
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)) if shape == ShapeKind::Leaf => crate::layout::shrink_cap(ctx, row, id),
|
||||
_ => None,
|
||||
};
|
||||
let dropped = given.is_some_and(|given| given.dropped);
|
||||
@@ -1357,31 +1556,61 @@ fn run_steps(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option<TipFn<
|
||||
Some(cap) => Some(room.map_or(cap, |room| room.min(cap))),
|
||||
None => room,
|
||||
};
|
||||
Room { room, in_flow, fill_body, shrink, cap, dropped }
|
||||
}
|
||||
|
||||
/// Step 5's extent: a sizing pass measures the shape from its content; in a real pass a body or item shape takes
|
||||
/// what its measuring run found. A body's children run here, in its measuring run.
|
||||
fn extent_step(
|
||||
t: &mut dyn Erased,
|
||||
ui: &Ui,
|
||||
(at, mixed): (ShowAt, bool),
|
||||
(begun, resolved): (&Begun, &Resolved<'_>),
|
||||
stage: &mut MemoryStage,
|
||||
) -> Vec2 {
|
||||
let (ctx, facts, shape, sizing, id) = (&begun.ctx, &begun.facts, begun.shape, begun.sizing, begun.id);
|
||||
let env = begun.env();
|
||||
// A body root with parts steps its press machine before its measuring run, so the body reads this pass's part
|
||||
// presses in both runs (`BodyCx::part_press`). Step 8 finds the outcome. A member of another arena senses at step 8.
|
||||
if !sizing && shape == ShapeKind::Body && (facts.parts || t.declares_parts()) && (t.arena() || facts.text() || env.arena.is_none()) {
|
||||
sensing::sense_early(&ctx, id, resolved.enabled);
|
||||
sensing::sense_early(ctx, id, resolved.enabled);
|
||||
}
|
||||
// Sizes come from this pass: a sizing pass measures the shape from its content; in a real pass a body or item
|
||||
// shape takes what its measuring run found.
|
||||
let extent = if sizing || matches!(shape, ShapeKind::Body | ShapeKind::Item) {
|
||||
let measuring = Measuring { ui: &*ui, at, width: None, env, scx: &scx, resolved: &resolved, command: step1.command.clone(), mixed };
|
||||
if sizing { t.measured_extent(&measuring, &mut stage) } else { t.pass_extent(&measuring) }
|
||||
let scx = steps::style_cx(ctx, &begun.env, &begun.step1);
|
||||
if sizing || matches!(shape, ShapeKind::Body | ShapeKind::Item) {
|
||||
let measuring = Measuring { ui, at, width: None, env, scx: &scx, resolved, command: begun.step1.command.clone(), mixed };
|
||||
if sizing { t.measured_extent(&measuring, stage) } else { t.pass_extent(&measuring) }
|
||||
} else {
|
||||
t.extent(ui, at, id, &scx)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// Step 5's sizes from the `extent`, and what the rows and bodies around the component take from it.
|
||||
// Kept out of the recursive frame above it: opt-level 2 inlines it there, so its locals stay on the stack while children run.
|
||||
#[inline(never)]
|
||||
fn measure_step(
|
||||
t: &mut dyn Erased,
|
||||
ui: &Ui,
|
||||
at: ShowAt,
|
||||
(begun, resolved, room): (&Begun, &Resolved<'_>, &Room),
|
||||
extent: Vec2,
|
||||
stage: &mut MemoryStage,
|
||||
) -> Sizes {
|
||||
let (ctx, facts, shape, sizing, id) = (&begun.ctx, &begun.facts, begun.shape, begun.sizing, begun.id);
|
||||
let env = begun.env();
|
||||
let style = &*begun.style;
|
||||
let Room { room, in_flow, fill_body, shrink, cap, dropped } = *room;
|
||||
let ask = |t: &mut dyn Erased, proposal: Proposal, room: Option<f32>, extent: Vec2| {
|
||||
measured(t, &MeasureIn { ui: &*ui, env, proposal, room }, &*style, &resolved, extent)
|
||||
measured(t, &MeasureIn { ui: &*ui, env, proposal, room }, style, resolved, extent)
|
||||
};
|
||||
let (size, inputs) = ask(&mut *t, Proposal::Ideal, room, extent);
|
||||
let ideal = Call { proposal: Proposal::Ideal, room, inputs, took: size };
|
||||
// A control in a label scope reports its narrowest width, in a measuring run too: the real run's label step reads
|
||||
// it to decide whether the row fits the label beside it.
|
||||
let in_label = shape == ShapeKind::Leaf && scope::in_label(&ctx);
|
||||
let in_label = shape == ShapeKind::Leaf && scope::in_label(ctx);
|
||||
// A leaf that fills the main axis of its body tells the body its least in the body's first sweep: its share never
|
||||
// cuts it below that (`layout::fill_size`). The call joins the body's transcript, which holds it while the inputs
|
||||
// hold.
|
||||
let fills = |axis: craft_behavior::Axis| t.fills() == Some(axis) || common.sizing == Some(super::Sizing::Fill);
|
||||
let fills = |axis: craft_behavior::Axis| t.fills() == Some(axis) || begun.common.sizing == Some(super::Sizing::Fill);
|
||||
let tells_least = shape == ShapeKind::Leaf
|
||||
&& matches!(at, ShowAt::Flow)
|
||||
&& [craft_behavior::Axis::X, craft_behavior::Axis::Y].into_iter().any(|axis| fills(axis) && crate::layout::counting_fills(ui, axis));
|
||||
@@ -1394,7 +1623,7 @@ fn run_steps(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option<TipFn<
|
||||
// transcript, and a real run its kept transcript serves checks them against this run's. A leaf shown at a rect
|
||||
// takes no room in the flow: its body's layout takes the rect, and any measure its caller sized the rect by
|
||||
// records itself.
|
||||
let leaf = shape == ShapeKind::Leaf && matches!(at, ShowAt::Flow) && (!sizing || crate::runtime::measure::active(&ctx));
|
||||
let leaf = shape == ShapeKind::Leaf && matches!(at, ShowAt::Flow) && (!sizing || crate::runtime::measure::active(ctx));
|
||||
if leaf {
|
||||
let mut both;
|
||||
let mut one;
|
||||
@@ -1408,17 +1637,17 @@ fn run_steps(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option<TipFn<
|
||||
&mut one
|
||||
}
|
||||
};
|
||||
crate::runtime::layout_memory::leaf_call(&ctx, id, calls, |proposal, room| ask(&mut *t, proposal, room, extent).0);
|
||||
crate::runtime::layout_memory::leaf_call(ctx, id, calls, |proposal, room| ask(&mut *t, proposal, room, extent).0);
|
||||
}
|
||||
trace.mark(Step::Measure);
|
||||
let min = min_took.inspect(|min| scope::record_control_min(&ctx, min.x));
|
||||
begun.trace().mark(Step::Measure);
|
||||
let min = min_took.inspect(|min| scope::record_control_min(ctx, min.x));
|
||||
// The read record: the binding step 4 read, and the sizes step 5 made from it.
|
||||
if let Some(key) = t.bind_key(id) {
|
||||
let ideal = Sample { proposal: Proposal::Ideal, room, took: size };
|
||||
let measuring = sizing || crate::runtime::measure::active(&ctx);
|
||||
let measuring = sizing || crate::runtime::measure::active(ctx);
|
||||
match min {
|
||||
Some(min) => reads::note(&ctx, key, id, measuring, &[ideal, Sample { proposal: Proposal::Min, room: None, took: min }]),
|
||||
None => reads::note(&ctx, key, id, measuring, &[ideal]),
|
||||
Some(min) => reads::note(ctx, key, id, measuring, &[ideal, Sample { proposal: Proposal::Min, room: None, took: min }]),
|
||||
None => reads::note(ctx, key, id, measuring, &[ideal]),
|
||||
}
|
||||
}
|
||||
// In the least measure of a body every leaf in flow takes its least. In a stack a leaf wider than the stack's room
|
||||
@@ -1447,22 +1676,22 @@ fn run_steps(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option<TipFn<
|
||||
match shrink {
|
||||
Some(crate::layout::Shrink::Count(row)) if shape == ShapeKind::Leaf => {
|
||||
// The `Ideal` size in no room, and the least width at which its content shows whole.
|
||||
let input = MeasureIn { ui: &*ui, env, proposal: Proposal::Ideal, room: None };
|
||||
let (content, _) = t.measure_content(&input, &resolved);
|
||||
let ideal = steps::measure(&*style, &input, content, Vec2::ZERO, &resolved).x;
|
||||
let whole = steps::whole(&*style, content, Vec2::ZERO, &resolved);
|
||||
let input = MeasureIn { ui, env, proposal: Proposal::Ideal, room: None };
|
||||
let (content, _) = t.measure_content(&input, resolved);
|
||||
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());
|
||||
crate::layout::report_shrink(ctx, row, id, size.x, ideal, Some((whole, least)), t.cut_first());
|
||||
}
|
||||
Some(crate::layout::Shrink::Count(row)) => crate::layout::report_shrink(&ctx, row, id, size.x, size.x, None, false),
|
||||
Some(crate::layout::Shrink::Least(row)) if shape == ShapeKind::Body => crate::layout::report_least(&ctx, row, id, size.x),
|
||||
Some(crate::layout::Shrink::Count(row)) => crate::layout::report_shrink(ctx, row, id, size.x, size.x, None, false),
|
||||
Some(crate::layout::Shrink::Least(row)) if shape == ShapeKind::Body => crate::layout::report_least(ctx, row, id, size.x),
|
||||
_ => {}
|
||||
}
|
||||
// A body that fills the rest of a shrinking row needs at least its content: the row counts it at that width, so
|
||||
// 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);
|
||||
crate::layout::report_shrink(ctx, row, id, size.x.min(content), content, Some((content, content)), false);
|
||||
}
|
||||
// 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.
|
||||
@@ -1471,68 +1700,96 @@ fn run_steps(t: &mut dyn Erased, ui: &mut Ui, at: ShowAt, mut tip: Option<TipFn<
|
||||
ShowAt::Flow => !steps::guard(size, shape),
|
||||
};
|
||||
if inert && size != Vec2::ZERO {
|
||||
let now = crate::runtime::frame::clock(&ctx);
|
||||
let now = crate::runtime::frame::clock(ctx);
|
||||
let log = stage.shown::<SizeLog>(sub(id, "size_log"));
|
||||
if now - log.0 >= 1.0 || log.0 > now {
|
||||
log.0 = now;
|
||||
log::debug!("{} measured the size {size:?} and shows nothing", facts.name);
|
||||
}
|
||||
}
|
||||
let mut core = blank_core(env.view(), &resolved, &mut stage, ctx.clone(), env.ink, steps::inert_response(ui, id), step1.command.clone());
|
||||
core.faded = facts.faded && !resolved.enabled;
|
||||
Sizes { room, measured, size, inert, dropped }
|
||||
}
|
||||
|
||||
/// Steps 6 to 17 under the run's one `CxCore`, which lives in this frame: a sizing child that recurses inside step 5
|
||||
/// finds no core of its level on the stack. Returns whether a pass ran past the sizing check.
|
||||
// Holds the run's `CxCore`, which opt-level 2 would merge into `run_steps`, on the stack of every measuring run below.
|
||||
#[inline(never)]
|
||||
fn run_passes(
|
||||
t: &mut dyn Erased,
|
||||
ui: &mut Ui,
|
||||
at: ShowAt,
|
||||
mut tip: Option<TipFn<'_>>,
|
||||
(begun, resolved, sizes): (&Begun, &Resolved<'_>, &Sizes),
|
||||
stage: &mut MemoryStage,
|
||||
) -> bool {
|
||||
let env = begun.env();
|
||||
let id = resolved.id;
|
||||
let mut core = blank_core(env.view(), resolved, stage, begun.ctx.clone(), env.ink, steps::inert_response(ui, id), begun.step1.command.clone());
|
||||
core.faded = begun.facts.faded && !resolved.enabled;
|
||||
// 6 to 16, in the host's `Ui`, once per call of the host.
|
||||
let mut host = t.host(HostCx { ui: &*ui, env, id, label: &step1.label, size });
|
||||
let parent = ui.scope_id();
|
||||
let mut real_pass = false;
|
||||
t.with_cx(core, &*style, &mut |e| {
|
||||
let mut run = Pass {
|
||||
p: &mut *e,
|
||||
facts: &facts,
|
||||
env,
|
||||
style: &*style,
|
||||
resolved: &resolved,
|
||||
trace,
|
||||
tip: tip.take(),
|
||||
at,
|
||||
room,
|
||||
measured,
|
||||
size,
|
||||
parent,
|
||||
inert: inert || dropped,
|
||||
dropped,
|
||||
sizing,
|
||||
real: false,
|
||||
activated: false,
|
||||
injected: None,
|
||||
};
|
||||
host.run(id, ui, env, &mut |host_ui, host_at, host_env| pass(&mut run, host_ui, host_at, host_env));
|
||||
let (real, activated, injected) = (run.real, run.activated, run.injected);
|
||||
// A layer's dismissal is decided after its body so a field inside commits in the press pass; its open state is
|
||||
// written here, after the report (the one write point outside step 15, besides `request_close`).
|
||||
if real && let Some(why) = host.dismissed() {
|
||||
e.dismissed(why);
|
||||
}
|
||||
// 17 finish, once: the stage is written back after `finish` returns, so writes made in `finish` are kept.
|
||||
if real {
|
||||
trace.mark(Step::Finish);
|
||||
let changed = e.changed();
|
||||
steps::before_finish(e.core(), activated, injected, changed);
|
||||
// A control painting text through its faces (a select, a segmented control) reports that baseline too.
|
||||
let core = e.core_ref();
|
||||
let baseline = core.paint.borrow().baseline;
|
||||
if baseline.is_some() || shape == ShapeKind::Leaf {
|
||||
scope::record_line(&ctx, core.rect, baseline);
|
||||
}
|
||||
}
|
||||
real_pass = real;
|
||||
let mut host = t.host(HostCx { ui: &*ui, env, id, label: &begun.step1.label, size: sizes.size });
|
||||
let mut real = false;
|
||||
t.with_cx(&mut core, &*begun.style, &mut |e| {
|
||||
real = passes(e, &mut *host, ui, (at, tip.take()), (begun, resolved, sizes));
|
||||
real
|
||||
});
|
||||
if real_pass {
|
||||
t.record(&mut stage, &ctx, id);
|
||||
real
|
||||
}
|
||||
|
||||
/// Steps 6 to 16 in each call of the host, then the dismissal and step 17.
|
||||
fn passes<'a>(
|
||||
e: &mut dyn ErasedPass<'a>,
|
||||
host: &mut dyn Host,
|
||||
ui: &mut Ui,
|
||||
(at, tip): (ShowAt, Option<TipFn<'_>>),
|
||||
(begun, resolved, sizes): (&Begun, &Resolved<'_>, &Sizes),
|
||||
) -> bool {
|
||||
let mut run = Pass {
|
||||
p: e,
|
||||
facts: &begun.facts,
|
||||
env: begun.env(),
|
||||
style: &*begun.style,
|
||||
resolved,
|
||||
trace: begun.trace(),
|
||||
tip,
|
||||
at,
|
||||
room: sizes.room,
|
||||
measured: sizes.measured,
|
||||
size: sizes.size,
|
||||
parent: ui.scope_id(),
|
||||
inert: sizes.inert || sizes.dropped,
|
||||
dropped: sizes.dropped,
|
||||
sizing: begun.sizing,
|
||||
real: false,
|
||||
activated: false,
|
||||
injected: None,
|
||||
};
|
||||
host.run(resolved.id, ui, begun.env(), &mut |host_ui, host_at, host_env| run_in_host(&mut run, host_ui, host_at, host_env));
|
||||
finish_passes(&mut run, &*host, begun)
|
||||
}
|
||||
|
||||
/// The dismissal and step 17 after the host's passes. Returns whether a pass ran past the sizing check.
|
||||
fn finish_passes(run: &mut Pass<'_, '_, '_>, host: &dyn Host, begun: &Begun) -> bool {
|
||||
let (real, activated, injected) = (run.real, run.activated, run.injected);
|
||||
let e = &mut *run.p;
|
||||
// A layer's dismissal is decided after its body so a field inside commits in the press pass; its open state is
|
||||
// written here, after the report (the one write point outside step 15, besides `request_close`).
|
||||
if real && let Some(why) = host.dismissed() {
|
||||
e.dismissed(why);
|
||||
}
|
||||
if !sizing {
|
||||
stage.write_back();
|
||||
// 17 finish, once: the stage is written back after `finish` returns, so writes made in `finish` are kept.
|
||||
if real {
|
||||
run.trace.mark(Step::Finish);
|
||||
let changed = e.changed();
|
||||
steps::before_finish(e.core(), activated, injected, changed);
|
||||
// A control painting text through its faces (a select, a segmented control) reports that baseline too.
|
||||
let core = e.core_ref();
|
||||
let baseline = core.paint.borrow().baseline;
|
||||
if baseline.is_some() || begun.shape == ShapeKind::Leaf {
|
||||
scope::record_line(&begun.ctx, core.rect, baseline);
|
||||
}
|
||||
}
|
||||
real
|
||||
}
|
||||
|
||||
/// Measures the component in `t` as `show` will show it, without allocating: steps 1 to 5 under the id `show` would
|
||||
|
||||
@@ -25,12 +25,20 @@ use egui::{Context, Id, Rect, Ui, Vec2};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// The component a `Typed` holds: its own for a show, which `finish` consumes, or the caller's for a measure.
|
||||
/// The component a `Typed` holds: the show's slot, which `finish` empties, or the caller's for a measure. The show
|
||||
/// keeps the slot in its own frame, so the component is on the stack once while its children run.
|
||||
enum Held<'c, C> {
|
||||
Owned(Option<C>),
|
||||
Owned(&'c mut Option<C>),
|
||||
Lent(&'c mut C),
|
||||
}
|
||||
|
||||
/// The component in a show's slot. The caller of a show makes the slot from the component it shows, so the component
|
||||
/// is on the stack once while its children run; `finish` takes it out.
|
||||
#[expect(clippy::expect_used, reason = "a show's slot holds the component until `finish`")]
|
||||
fn held<C>(slot: &mut Option<C>) -> &mut C {
|
||||
slot.as_mut().expect("the show's slot holds the component")
|
||||
}
|
||||
|
||||
/// The style step 2 stored for the later steps of the run.
|
||||
#[expect(clippy::expect_used, reason = "step 2 stores the style before any step that reads it")]
|
||||
fn step_style<S>(style: &Option<Arc<S>>) -> &S {
|
||||
@@ -78,6 +86,38 @@ impl<'c, 'h, C: Component> Typed<'c, 'h, C> {
|
||||
fn new(comp: Held<'c, C>, hooks: &'h mut dyn ShapeHooks<C>) -> Self {
|
||||
Typed { comp, hooks, style: None, shown: None, mixed: false, cancelled: Vec::new(), output: None }
|
||||
}
|
||||
|
||||
/// The `Typed` of a show, whose `finish` takes the component out of `slot`.
|
||||
fn owned(slot: &'c mut Option<C>, hooks: &'h mut dyn ShapeHooks<C>) -> Self {
|
||||
Self::new(Held::Owned(slot), hooks)
|
||||
}
|
||||
|
||||
/// The output `finish` made.
|
||||
#[expect(clippy::expect_used, reason = "`super::run` runs every step, and the last one sets the output")]
|
||||
fn output(self) -> C::Output {
|
||||
self.output.expect("the pipeline finishes every run")
|
||||
}
|
||||
|
||||
/// The component's `Cx` over the run's `core`, with the value step 4 found.
|
||||
#[expect(clippy::expect_used, reason = "`Erased::style` produced the style this pipeline now hands back, so its type is `C::Style`")]
|
||||
fn cx<'a>(&mut self, core: &'a mut CxCore<'a>, style: &'a dyn StyleView) -> Cx<'a, C> {
|
||||
let style = style.as_any().downcast_ref::<C::Style>().expect("the pipeline holds the style `Erased::style` gave");
|
||||
let events = std::mem::take(&mut self.cancelled);
|
||||
// A cancel step 4 raised over a document that moved is reported, not applied: the app's value stays. One that
|
||||
// carries the value the gesture began with restores it at step 11.
|
||||
let applied = if events.iter().any(|event| matches!(event, EditEvent::Cancel { before: Some(_), .. })) { 0 } else { events.len() };
|
||||
Cx { core, style, shown: self.shown.take(), mixed: self.mixed, events, applied, validity: Validity::Ok }
|
||||
}
|
||||
|
||||
/// Step 17's `finish`: the component leaves `Typed` only now, after its children ran.
|
||||
// Takes the component out after the children ran; opt-level 2 inlines it into `with_cx`, whose frame then holds a copy during them.
|
||||
#[inline(never)]
|
||||
fn finish(&mut self, cx: Cx<'_, C>, real: bool) {
|
||||
let id = cx.core.id;
|
||||
let output = self.comp.take().finish(cx);
|
||||
debug_assert!(!real || crate::contract::Outcome::response(&output).id == id, "an output's Response is the component's own");
|
||||
self.output = Some(output);
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Component> Erased for Typed<'_, '_, C> {
|
||||
@@ -148,20 +188,10 @@ impl<C: Component> Erased for Typed<'_, '_, C> {
|
||||
self.hooks.pass_extent(self.comp.get(), at, style, self.shown.clone())
|
||||
}
|
||||
|
||||
#[expect(clippy::expect_used, reason = "`Erased::style` produced the style this pipeline now hands back, so its type is `C::Style`")]
|
||||
fn with_cx<'a>(&mut self, core: CxCore<'a>, style: &'a dyn StyleView, rest: &mut dyn FnMut(&mut dyn ErasedPass<'a>) -> bool) {
|
||||
let style = style.as_any().downcast_ref::<C::Style>().expect("the pipeline holds the style `Erased::style` gave");
|
||||
let events = std::mem::take(&mut self.cancelled);
|
||||
// A cancel step 4 raised over a document that moved is reported, not applied: the app's value stays. One that
|
||||
// carries the value the gesture began with restores it at step 11.
|
||||
let applied = if events.iter().any(|event| matches!(event, EditEvent::Cancel { before: Some(_), .. })) { 0 } else { events.len() };
|
||||
let mut cx = Cx::<C> { core, style, shown: self.shown.take(), mixed: self.mixed, events, applied, validity: Validity::Ok };
|
||||
let mut comp = self.comp.take();
|
||||
let real = rest(&mut TypedPass { comp: &mut comp, hooks: &mut *self.hooks, cx: &mut cx });
|
||||
let id = cx.core.id;
|
||||
let output = comp.finish(cx);
|
||||
debug_assert!(!real || crate::contract::Outcome::response(&output).id == id, "an output's Response is the component's own");
|
||||
self.output = Some(output);
|
||||
fn with_cx<'a>(&mut self, core: &'a mut CxCore<'a>, style: &'a dyn StyleView, rest: &mut dyn FnMut(&mut dyn ErasedPass<'a>) -> bool) {
|
||||
let mut cx = self.cx(core, style);
|
||||
let real = rest(&mut TypedPass { comp: self.comp.get(), hooks: &mut *self.hooks, cx: &mut cx });
|
||||
self.finish(cx, real);
|
||||
}
|
||||
|
||||
fn shape(&self) -> ShapeKind {
|
||||
@@ -230,11 +260,11 @@ struct TypedPass<'r, 'a, C: Component> {
|
||||
|
||||
impl<'a, C: Component> ErasedPass<'a> for TypedPass<'_, 'a, C> {
|
||||
fn core(&mut self) -> &mut CxCore<'a> {
|
||||
&mut self.cx.core
|
||||
self.cx.core
|
||||
}
|
||||
|
||||
fn core_ref(&self) -> &CxCore<'a> {
|
||||
&self.cx.core
|
||||
self.cx.core
|
||||
}
|
||||
|
||||
fn shown_state(&mut self) -> &mut steps::ShownState {
|
||||
@@ -341,11 +371,11 @@ impl<'a, C: Component> ErasedPass<'a> for TypedPass<'_, 'a, C> {
|
||||
}
|
||||
|
||||
fn paint(&mut self, ui: &mut Ui, env: &Env) {
|
||||
self.comp.paint(&mut paint_ui(ui, env, &self.cx.core, self.cx.style), self.cx);
|
||||
self.comp.paint(&mut paint_ui(ui, env, self.cx.core, self.cx.style), self.cx);
|
||||
}
|
||||
|
||||
fn paint_over(&mut self, ui: &mut Ui, env: &Env) {
|
||||
self.comp.paint_over(&mut paint_ui(ui, env, &self.cx.core, self.cx.style), self.cx);
|
||||
self.comp.paint_over(&mut paint_ui(ui, env, self.cx.core, self.cx.style), self.cx);
|
||||
}
|
||||
|
||||
fn settle_values(&mut self) -> steps::ValueEdit {
|
||||
@@ -479,13 +509,6 @@ impl<'a, C: Component> ErasedPass<'a> for TypedPass<'_, 'a, C> {
|
||||
}
|
||||
}
|
||||
|
||||
#[expect(clippy::expect_used, reason = "`super::run` runs every step, and the last one sets the output")]
|
||||
fn run<C: Component>(comp: C, ui: &mut Ui, at: ShowAt, tip: Option<TipFn<'_>>, hooks: &mut dyn ShapeHooks<C>) -> C::Output {
|
||||
let mut typed = Typed::new(Held::Owned(Some(comp)), hooks);
|
||||
super::run(&mut typed, ui, at, tip);
|
||||
typed.output.expect("the pipeline finishes every run")
|
||||
}
|
||||
|
||||
fn measure_shape<C: Component>(
|
||||
comp: &mut C,
|
||||
ui: &Ui,
|
||||
@@ -498,36 +521,38 @@ fn measure_shape<C: Component>(
|
||||
}
|
||||
|
||||
pub(crate) fn show_leaf<C: Component + Meta<Shape = crate::contract::shape::Leaf>>(
|
||||
comp: C,
|
||||
slot: &mut Option<C>,
|
||||
ui: &mut Ui,
|
||||
at: ShowAt,
|
||||
tip: Option<TipFn<'_>>,
|
||||
) -> C::Output {
|
||||
let scope = comp.common(Internal::new()).command_scope;
|
||||
run(comp, ui, at, tip, &mut LeafHooks { scope })
|
||||
let mut hooks = LeafHooks { scope: held(slot).common(Internal::new()).command_scope };
|
||||
let mut typed = Typed::owned(slot, &mut hooks);
|
||||
super::run(&mut typed, ui, at, tip);
|
||||
typed.output()
|
||||
}
|
||||
|
||||
/// Shows a container at `rect` and takes the rect's room in the flow of `ui`, through the one helper that moves a
|
||||
/// parent's flow past a rect, so a sibling shown after it lies past it.
|
||||
pub(crate) fn show_container_taking<C: Container, R>(
|
||||
comp: C,
|
||||
slot: &mut Option<C>,
|
||||
ui: &mut Ui,
|
||||
rect: Rect,
|
||||
body: impl FnMut(&mut Ui, &mut BodyCx<'_>) -> R,
|
||||
) -> Inner<C::Output, R> {
|
||||
let shown = show_container(comp, ui, ShowAt::Rect(rect), body, None);
|
||||
let shown = show_container(slot, ui, ShowAt::Rect(rect), body, None);
|
||||
crate::runtime::egui::alloc::advance_past(ui, rect);
|
||||
shown
|
||||
}
|
||||
|
||||
pub(crate) fn show_container<C: Container, R>(
|
||||
mut comp: C,
|
||||
slot: &mut Option<C>,
|
||||
ui: &mut Ui,
|
||||
at: ShowAt,
|
||||
mut body: impl FnMut(&mut Ui, &mut BodyCx<'_>) -> R,
|
||||
tip: Option<TipFn<'_>>,
|
||||
) -> Inner<C::Output, R> {
|
||||
if let Some(output) = unshown(&mut comp, ui) {
|
||||
if let Some(output) = unshown(held(slot), ui) {
|
||||
return Inner { output, inner: None, settle: None };
|
||||
}
|
||||
let mut inner: Option<R> = None;
|
||||
@@ -537,11 +562,12 @@ pub(crate) fn show_container<C: Container, R>(
|
||||
ran |= !cx.memory.is_peek();
|
||||
inner = Some(body(ui, cx));
|
||||
};
|
||||
let ctx = ui.ctx().clone();
|
||||
let props = Arc::new(comp.props(Internal::new()).clone());
|
||||
let output = run(comp, ui, at, tip, &mut crate::contract::body::BodyHooks::new(&mut call, props));
|
||||
let mut hooks = crate::contract::body::BodyHooks::of(held(slot), &mut call);
|
||||
let mut typed = Typed::owned(slot, &mut hooks);
|
||||
super::run(&mut typed, ui, at, tip);
|
||||
let output = typed.output();
|
||||
let id = crate::contract::Outcome::response(&output).id;
|
||||
let settle = ran.then(|| crate::contract::settle::SettleAt { ctx, body: sub(id, "body"), owner: id });
|
||||
let settle = ran.then(|| crate::contract::settle::SettleAt { ctx: ui.ctx().clone(), body: sub(id, "body"), owner: id });
|
||||
Inner { output, inner, settle }
|
||||
}
|
||||
|
||||
@@ -580,10 +606,18 @@ fn unshown<C: Container>(comp: &mut C, ui: &Ui) -> Option<C::Output> {
|
||||
Some(output)
|
||||
}
|
||||
|
||||
pub(crate) fn show_items<'r, C: Collection>(comp: C, ui: &mut Ui, source: &dyn ItemSource<C::Key>, item: &mut ItemFn<'r, C>) -> C::Output {
|
||||
let view = comp.filtered(source, &Env::of(ui));
|
||||
pub(crate) fn show_items<'r, C: Collection>(
|
||||
slot: &mut Option<C>,
|
||||
ui: &mut Ui,
|
||||
source: &dyn ItemSource<C::Key>,
|
||||
item: &mut ItemFn<'r, C>,
|
||||
) -> C::Output {
|
||||
let view = held(slot).filtered(source, &Env::of(ui));
|
||||
let source = view.as_deref().unwrap_or(source);
|
||||
run(comp, ui, ShowAt::Flow, None, &mut crate::contract::items::ItemsHooks::<C>::new(source, item))
|
||||
let mut hooks = crate::contract::items::ItemsHooks::<C>::new(source, item);
|
||||
let mut typed = Typed::owned(slot, &mut hooks);
|
||||
super::run(&mut typed, ui, ShowAt::Flow, None);
|
||||
typed.output()
|
||||
}
|
||||
|
||||
/// Measures `comp` as `show` will show it, without allocating: steps 1 to 5 under the id `show` would use next. It
|
||||
@@ -610,8 +644,8 @@ pub(crate) fn measure_container<C: Container, R>(
|
||||
let mut call = |ui: &mut Ui, cx: &mut BodyCx<'_>| {
|
||||
body(ui, cx);
|
||||
};
|
||||
let props = Arc::new(comp.props(Internal::new()).clone());
|
||||
measure_shape(comp, ui, None, proposal, None, &mut crate::contract::body::BodyHooks::new(&mut call, props))
|
||||
let mut hooks = crate::contract::body::BodyHooks::of(comp, &mut call);
|
||||
measure_shape(comp, ui, None, proposal, None, &mut hooks)
|
||||
}
|
||||
|
||||
/// A measuring run's extent: the shape's `measured_extent` under a `Cx` that senses nothing and writes only to the
|
||||
@@ -625,8 +659,8 @@ pub(crate) fn measured_extent<C: Component>(
|
||||
shown: Option<Arc<ValueOf<C>>>,
|
||||
) -> (Vec2, Option<Arc<ValueOf<C>>>) {
|
||||
let response = steps::inert_response(at.ui, at.resolved.id);
|
||||
let core = blank_core(at.env.view(), at.resolved, stage, at.ui.ctx().clone(), at.env.ink, response, at.command.clone());
|
||||
let mut cx = Cx::<C> { core, style, shown, mixed: at.mixed, events: Vec::new(), applied: 0, validity: Validity::Ok };
|
||||
let mut core = blank_core(at.env.view(), at.resolved, stage, at.ui.ctx().clone(), at.env.ink, response, at.command.clone());
|
||||
let mut cx = Cx::<C> { core: &mut core, style, shown, mixed: at.mixed, events: Vec::new(), applied: 0, validity: Validity::Ok };
|
||||
let ctx = at.ui.ctx();
|
||||
let extent = reads::measuring(ctx, at.resolved.id, || steps::measuring(ctx, || hooks.measured_extent(comp, at, &mut cx)));
|
||||
(extent, cx.shown.take())
|
||||
@@ -663,7 +697,7 @@ pub(crate) fn paint_face<C: Show>(ui: &mut Ui, env: &Env, source: &super::FaceSo
|
||||
core.content_rect = flow.inset(face.padding(rect, &style));
|
||||
core.flow = flow;
|
||||
core.visible = rect;
|
||||
let mut cx = Cx::<C> { core, style: &style, shown, mixed, events: Vec::new(), applied: 0, validity: Validity::Ok };
|
||||
let cx = Cx::<C> { core: &mut core, style: &style, shown, mixed, events: Vec::new(), applied: 0, validity: Validity::Ok };
|
||||
// A face shows its own inputs as `steps::state` reads them for a component: a checked check box takes the on
|
||||
// look, a selected face the selected one.
|
||||
let flags = part_state.flags | source.flags | steps::value_flags(&face, &cx);
|
||||
@@ -672,7 +706,7 @@ pub(crate) fn paint_face<C: Show>(ui: &mut Ui, env: &Env, source: &super::FaceSo
|
||||
cx.core.state = state;
|
||||
cx.core.stated = true;
|
||||
cx.core.ink = steps::surface_ink(StyleView::surface(&*style), &state, env.ink);
|
||||
steps::paint_surface(ui, env, &cx.core, &*style);
|
||||
steps::paint_surface(ui, env, cx.core, &*style);
|
||||
{
|
||||
let results = HashMap::new();
|
||||
let mut pui = PaintUi {
|
||||
@@ -840,7 +874,7 @@ pub(crate) fn member_show<C: Show>(
|
||||
core.press = sensed.press;
|
||||
core.member_of = Some(node.owner);
|
||||
core.intents = crate::runtime::keys::intents_of(&ctx, Some(id), rtl);
|
||||
let mut cx = Cx::<C> { core, style: &style, shown, mixed, events: Vec::new(), applied: 0, validity: Validity::Ok };
|
||||
let mut cx = Cx::<C> { core: &mut core, style: &style, shown, mixed, events: Vec::new(), applied: 0, validity: Validity::Ok };
|
||||
let flags = part_state.flags | steps::value_flags(&item, &cx);
|
||||
let on = f32::from(u8::from(flags.intersects(Flags::ON)));
|
||||
let state = StateSet::new(part_state.state, flags, crate::style::StateMix::new(part_state.t.hover, part_state.t.press, on));
|
||||
@@ -851,22 +885,22 @@ pub(crate) fn member_show<C: Show>(
|
||||
// Paint stays inside the row grown by the type's declared outset, as a run's does; a faded member's veil fills it.
|
||||
let outer_clip = ui.clip_rect();
|
||||
ui.set_clip_rect(row.expand(C::paint_outset(&env.tokens)).intersect(outer_clip));
|
||||
let fade = steps::fade(ui, env, &mut cx.core);
|
||||
steps::paint_surface(ui, env, &cx.core, &*style);
|
||||
steps::paint_part_layers(ui, env, &cx.core);
|
||||
item.paint(&mut super::paint_ui(ui, env, &cx.core, &*style), &cx);
|
||||
item.paint_over(&mut super::paint_ui(ui, env, &cx.core, &*style), &cx);
|
||||
steps::unfade(ui, &mut cx.core, fade);
|
||||
let fade = steps::fade(ui, env, cx.core);
|
||||
steps::paint_surface(ui, env, cx.core, &*style);
|
||||
steps::paint_part_layers(ui, env, cx.core);
|
||||
item.paint(&mut super::paint_ui(ui, env, cx.core, &*style), &cx);
|
||||
item.paint_over(&mut super::paint_ui(ui, env, cx.core, &*style), &cx);
|
||||
steps::unfade(ui, cx.core, fade);
|
||||
ui.set_clip_rect(outer_clip);
|
||||
let mut access = Access::default();
|
||||
item.access(&mut access, &cx);
|
||||
let member_node = report_member(&cx.core, &access, node, C::ROLE);
|
||||
let member_node = report_member(cx.core, &access, node, C::ROLE);
|
||||
{
|
||||
let paint = cx.core.paint.borrow();
|
||||
steps::report_parts(&cx.core, &decls, &access.nodes, &paint.faces, member_node);
|
||||
steps::report_parts(cx.core, &decls, &access.nodes, &paint.faces, member_node);
|
||||
}
|
||||
crate::contract::cx::write_cursor(&cx.core);
|
||||
let tip = member_tip(&cx.core, &decls, node.part, tooltip, row);
|
||||
crate::contract::cx::write_cursor(cx.core);
|
||||
let tip = member_tip(cx.core, &decls, node.part, tooltip, row);
|
||||
MemberShown { output: item.finish(cx), tip }
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ pub(crate) fn edit_intents<C: Component>(comp: &mut C, cx: &mut Cx<'_, C>) {
|
||||
}
|
||||
}
|
||||
let reset = cx.core.intents.take_reset();
|
||||
let double = double_click(&cx.core).filter(|_| !matches!(C::INPUT, Input::Text | Input::Editor));
|
||||
let double = double_click(cx.core).filter(|_| !matches!(C::INPUT, Input::Text | Input::Editor));
|
||||
if reset.is_some() || double.is_some() {
|
||||
let part = match reset {
|
||||
Some(whole) => cx.core.active.filter(|_| !whole),
|
||||
|
||||
@@ -420,7 +420,7 @@ pub(crate) fn run<C: Component>(
|
||||
let read_only = cx.core.resolved.read_only || !enabled;
|
||||
let ink = decl.options.ink.map_or(ink, |own| crate::style::ink_color(&crate::runtime::env::Env::of(ui).tokens, own));
|
||||
let ink = if enabled { ink } else { crate::style::rgba_color(crate::runtime::env::Env::of(ui).tokens.color.text.disabled) };
|
||||
let held = cx.popup(EDIT_MENU).is_open_in(&mut cx.core);
|
||||
let held = cx.popup(EDIT_MENU).is_open_in(cx.core);
|
||||
serve_access(cx, &mut state, widget, &decl.shown, read_only);
|
||||
if std::mem::take(&mut state.select_all) {
|
||||
let len = state.draft.as_deref().unwrap_or(&decl.shown).chars().count();
|
||||
|
||||
@@ -588,6 +588,11 @@ fn run_sharing<R>(ui: &mut Ui, env: &Env, id: Id, axis: Axis, layout: egui::Layo
|
||||
.0
|
||||
}
|
||||
|
||||
/// A child of `ui` with `layout`, built outside the generic sweeps that run a body in it.
|
||||
fn laid_out_child(ui: &mut Ui, layout: egui::Layout) -> Ui {
|
||||
ui.new_child(egui::UiBuilder::new().layout(layout))
|
||||
}
|
||||
|
||||
/// Whether a body given `axis` shares it among the children that fill it.
|
||||
fn shares_along(env: &Env, axis: Axis) -> bool {
|
||||
if axis == Axis::Y { !env.content.1 } else { !env.content.0 }
|
||||
@@ -626,7 +631,7 @@ fn share_sweep<R>(ui: &mut Ui, env: &Env, id: Id, axis: Axis, share: Option<f32>
|
||||
fn measure_sharing<R>(ui: &mut Ui, env: &Env, id: Id, axis: Axis, layout: egui::Layout, body: &mut dyn FnMut(&mut Ui) -> R) -> (R, Rect) {
|
||||
let ctx = ui.ctx().clone();
|
||||
let in_child = |ui: &mut Ui, body: &mut dyn FnMut(&mut Ui) -> R| {
|
||||
let mut child = ui.new_child(egui::UiBuilder::new().layout(layout));
|
||||
let mut child = laid_out_child(ui, layout);
|
||||
let inner = body(&mut child);
|
||||
(inner, child.min_rect())
|
||||
};
|
||||
@@ -637,7 +642,7 @@ fn measure_sharing<R>(ui: &mut Ui, env: &Env, id: Id, axis: Axis, layout: egui::
|
||||
let along = |size: Vec2| if axis == Axis::Y { size.y } else { size.x };
|
||||
let first = crate::contract::keeping_ids(&ctx, || {
|
||||
let (inner, rect, room) = {
|
||||
let mut first = ui.new_child(egui::UiBuilder::new().layout(layout));
|
||||
let mut first = laid_out_child(ui, layout);
|
||||
let room = share_room(&first, axis);
|
||||
pass_state::with::<FillCounts, _>(&ctx, |counts| counts.0.insert(id, 0));
|
||||
let inner = share_sweep(&mut first, env, id, axis, None, body);
|
||||
@@ -1153,7 +1158,7 @@ fn run_grid<R>(ui: &mut Ui, env: &Env, id: Id, columns: &[ColumnWidth], gap: Vec
|
||||
let (out, kept) = layout_memory::draft(
|
||||
&ctx,
|
||||
|| {
|
||||
let mut first = ui.new_child(egui::UiBuilder::new().layout(layout));
|
||||
let mut first = laid_out_child(ui, layout);
|
||||
let (inner, measured, short, unsettled) =
|
||||
sweep(&mut first, env, seed, columns, &|flow| cursor(columns, &none, flow).each_alone());
|
||||
(inner, measured, first.min_rect(), short, unsettled)
|
||||
@@ -1333,106 +1338,137 @@ pub(crate) fn run_body_measured<R>(
|
||||
// Each sweep of a measuring run finds the body's first line and an unwrapped row's line afresh.
|
||||
let target = env.first_line.filter(|_| sizing);
|
||||
let row = (sizing && matches!(layout, BodyLayout::Row { wrap: false, .. })).then_some(id);
|
||||
let mut body = |ui: &mut Ui| {
|
||||
let body = |ui: &mut Ui| {
|
||||
open_sweep(&ctx, target, row);
|
||||
body(ui)
|
||||
};
|
||||
// Each layout runs in its own function, so a body nested in bodies keeps only its own layout's frame on the stack.
|
||||
let line = RowIn { id, gap, shared, sizing, rtl };
|
||||
match layout {
|
||||
BodyLayout::Stack => {
|
||||
// Each leaf shows at most the stack's room, never below its least.
|
||||
let env = env.derive_local(|scoped| scoped.shrink = Some(Shrink::Fit));
|
||||
let installed = Env::install_for(ui, env.clone());
|
||||
// A held child of a column whose own room is unbounded keeps the width it measured at.
|
||||
let inner = if env.content_column && flow_room(ui).x.is_finite() {
|
||||
run_column(ui, &env, id, child::column_layout(rtl), body)
|
||||
} else {
|
||||
run_sharing(ui, &env, id, Axis::Y, child::column_layout(rtl), body)
|
||||
};
|
||||
Env::restore(&ctx, installed);
|
||||
(inner, None)
|
||||
}
|
||||
BodyLayout::Row { wrap: false, align: RowAlign::SpaceBetween } => {
|
||||
let (inner, _) = child::block(ui, None, child::row_layout(rtl, false), |ui| {
|
||||
let line = on_line(id, row_line(&ctx, sizing, id, shared), shared);
|
||||
let installed = Env::install_for(
|
||||
ui,
|
||||
env.derive_local(|scoped| {
|
||||
scoped.grid = Some(id);
|
||||
scoped.row_line = Some(line);
|
||||
scoped.first_line = None;
|
||||
}),
|
||||
);
|
||||
let inner = run_spread(ui, id, gap.x, rtl, &mut body);
|
||||
Env::restore(&ctx, installed);
|
||||
record_line(ui, sizing, id, shared);
|
||||
inner
|
||||
});
|
||||
inner
|
||||
}
|
||||
BodyLayout::Row { wrap: false, align: RowAlign::Start } => {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let line = on_line(id, row_line(&ctx, sizing, id, shared), shared);
|
||||
let inner = run_sharing(ui, env, id, Axis::X, child::row_layout(rtl, false), |ui| {
|
||||
let installed = Env::install_for(
|
||||
ui,
|
||||
Env::of(ui).derive_local(|scoped| {
|
||||
scoped.row_room = Some(room.width());
|
||||
scoped.row_line = Some(line);
|
||||
scoped.first_line = None;
|
||||
}),
|
||||
);
|
||||
let inner = run_shrinking(ui, id, &mut body);
|
||||
Env::restore(&ctx, installed);
|
||||
record_line(ui, sizing, id, shared);
|
||||
inner
|
||||
});
|
||||
(inner, None)
|
||||
}
|
||||
BodyLayout::Row { wrap, align } => {
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let rect = match *align {
|
||||
RowAlign::Start | RowAlign::SpaceBetween => None,
|
||||
RowAlign::Center | RowAlign::End => {
|
||||
let last = this_pass(&ctx, sizing, id);
|
||||
// In a body that scrolls along x the band is the width its view shows, not the scroll region.
|
||||
let band = if Env::of(ui).unbounded.0 { shrink_room(ui) } else { room.width() };
|
||||
// Without a measuring run, or in a band without an end, the block is placed as `Start`.
|
||||
last.filter(|last| band.is_finite() && last.x > 0.0 && last.x < band).map(|last| {
|
||||
let spare = band - last.x;
|
||||
let lead = if *align == RowAlign::Center { spare / 2.0 } else { spare };
|
||||
Flow::new(room, rtl).take_start(lead).1.rect()
|
||||
})
|
||||
}
|
||||
};
|
||||
// A wrapped row has a line per wrap, so its children stay on the top of each, and its first child gives its
|
||||
// first line.
|
||||
let line = (!*wrap).then(|| on_line(id, row_line(&ctx, sizing, id, shared), shared));
|
||||
// Where its least is asked, a wrapping row is as narrow as its widest child at its least: every child on a
|
||||
// line of its own.
|
||||
let block_layout = if *wrap && env.least { child::column_layout(rtl) } else { child::row_layout(rtl, *wrap) };
|
||||
let (inner, took) = child::block(ui, rect, block_layout, |ui| {
|
||||
let installed = Env::install_for(
|
||||
ui,
|
||||
env.derive_local(|scoped| {
|
||||
scoped.row_room = Some(room.width());
|
||||
scoped.row_line = line;
|
||||
if line.is_some() {
|
||||
scoped.first_line = None;
|
||||
}
|
||||
}),
|
||||
);
|
||||
let inner = if *wrap { body(ui) } else { run_shrinking(ui, id, &mut body) };
|
||||
Env::restore(&ctx, installed);
|
||||
if !*wrap {
|
||||
record_line(ui, sizing, id, shared);
|
||||
}
|
||||
inner
|
||||
});
|
||||
if *align != RowAlign::Start {
|
||||
found(&ctx, sizing, id, room.size(), took.size());
|
||||
}
|
||||
(inner, (*align != RowAlign::Start).then(|| took.width()))
|
||||
}
|
||||
BodyLayout::Stack => (run_stack(ui, env, &line, body), None),
|
||||
BodyLayout::Row { wrap: false, align: RowAlign::SpaceBetween } => run_spread_row(ui, env, &line, body),
|
||||
BodyLayout::Row { wrap: false, align: RowAlign::Start } => (run_start_row(ui, env, &line, body), None),
|
||||
BodyLayout::Row { wrap, align } => run_row(ui, env, &line, (*wrap, *align), body),
|
||||
BodyLayout::Grid { columns } => (run_grid(ui, env, id, columns, gap, body), None),
|
||||
}
|
||||
}
|
||||
|
||||
/// What the layouts of `run_body_measured` read besides the env.
|
||||
#[derive(Clone, Copy)]
|
||||
struct RowIn {
|
||||
id: Id,
|
||||
gap: Vec2,
|
||||
/// The line the body shares with its container's inline header or end region.
|
||||
shared: Option<f32>,
|
||||
sizing: bool,
|
||||
rtl: bool,
|
||||
}
|
||||
|
||||
/// A `Stack` body: each leaf shows at most the stack's room, never below its least.
|
||||
fn run_stack<R>(ui: &mut Ui, env: &Env, at: &RowIn, body: impl FnMut(&mut Ui) -> R) -> R {
|
||||
let ctx = ui.ctx().clone();
|
||||
let env = env.derive_local(|scoped| scoped.shrink = Some(Shrink::Fit));
|
||||
let installed = Env::install_for(ui, env.clone());
|
||||
// A held child of a column whose own room is unbounded keeps the width it measured at.
|
||||
let inner = if env.content_column && flow_room(ui).x.is_finite() {
|
||||
run_column(ui, &env, at.id, child::column_layout(at.rtl), body)
|
||||
} else {
|
||||
run_sharing(ui, &env, at.id, Axis::Y, child::column_layout(at.rtl), body)
|
||||
};
|
||||
Env::restore(&ctx, installed);
|
||||
inner
|
||||
}
|
||||
|
||||
/// An unwrapped `Row` that spreads its children over its room.
|
||||
fn run_spread_row<R>(ui: &mut Ui, env: &Env, at: &RowIn, mut body: impl FnMut(&mut Ui) -> R) -> (R, Option<f32>) {
|
||||
let ctx = ui.ctx().clone();
|
||||
let RowIn { id, gap, shared, sizing, rtl } = *at;
|
||||
let (inner, _) = child::block(ui, None, child::row_layout(rtl, false), |ui| {
|
||||
let line = on_line(id, row_line(&ctx, sizing, id, shared), shared);
|
||||
let installed = Env::install_for(
|
||||
ui,
|
||||
env.derive_local(|scoped| {
|
||||
scoped.grid = Some(id);
|
||||
scoped.row_line = Some(line);
|
||||
scoped.first_line = None;
|
||||
}),
|
||||
);
|
||||
let inner = run_spread(ui, id, gap.x, rtl, &mut body);
|
||||
Env::restore(&ctx, installed);
|
||||
record_line(ui, sizing, id, shared);
|
||||
inner
|
||||
});
|
||||
inner
|
||||
}
|
||||
|
||||
/// An unwrapped `Row` placed from its start, whose children share and shrink to its room.
|
||||
fn run_start_row<R>(ui: &mut Ui, env: &Env, at: &RowIn, mut body: impl FnMut(&mut Ui) -> R) -> R {
|
||||
let ctx = ui.ctx().clone();
|
||||
let RowIn { id, shared, sizing, rtl, .. } = *at;
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let line = on_line(id, row_line(&ctx, sizing, id, shared), shared);
|
||||
run_sharing(ui, env, id, Axis::X, child::row_layout(rtl, false), |ui| {
|
||||
let installed = Env::install_for(
|
||||
ui,
|
||||
Env::of(ui).derive_local(|scoped| {
|
||||
scoped.row_room = Some(room.width());
|
||||
scoped.row_line = Some(line);
|
||||
scoped.first_line = None;
|
||||
}),
|
||||
);
|
||||
let inner = run_shrinking(ui, id, &mut body);
|
||||
Env::restore(&ctx, installed);
|
||||
record_line(ui, sizing, id, shared);
|
||||
inner
|
||||
})
|
||||
}
|
||||
|
||||
/// A wrapped `Row`, or one that places its block at its center or end: the width its children take at the gap alone
|
||||
/// is what the body asks for.
|
||||
fn run_row<R>(ui: &mut Ui, env: &Env, at: &RowIn, (wrap, align): (bool, RowAlign), mut body: impl FnMut(&mut Ui) -> R) -> (R, Option<f32>) {
|
||||
let ctx = ui.ctx().clone();
|
||||
let RowIn { id, shared, sizing, rtl, .. } = *at;
|
||||
let room = ui.available_rect_before_wrap();
|
||||
let rect = match align {
|
||||
RowAlign::Start | RowAlign::SpaceBetween => None,
|
||||
RowAlign::Center | RowAlign::End => {
|
||||
let last = this_pass(&ctx, sizing, id);
|
||||
// In a body that scrolls along x the band is the width its view shows, not the scroll region.
|
||||
let band = if Env::of(ui).unbounded.0 { shrink_room(ui) } else { room.width() };
|
||||
// Without a measuring run, or in a band without an end, the block is placed as `Start`.
|
||||
last.filter(|last| band.is_finite() && last.x > 0.0 && last.x < band).map(|last| {
|
||||
let spare = band - last.x;
|
||||
let lead = if align == RowAlign::Center { spare / 2.0 } else { spare };
|
||||
Flow::new(room, rtl).take_start(lead).1.rect()
|
||||
})
|
||||
}
|
||||
};
|
||||
// A wrapped row has a line per wrap, so its children stay on the top of each, and its first child gives its
|
||||
// first line.
|
||||
let line = (!wrap).then(|| on_line(id, row_line(&ctx, sizing, id, shared), shared));
|
||||
// Where its least is asked, a wrapping row is as narrow as its widest child at its least: every child on a
|
||||
// line of its own.
|
||||
let block_layout = if wrap && env.least { child::column_layout(rtl) } else { child::row_layout(rtl, wrap) };
|
||||
let (inner, took) = child::block(ui, rect, block_layout, |ui| {
|
||||
let installed = Env::install_for(
|
||||
ui,
|
||||
env.derive_local(|scoped| {
|
||||
scoped.row_room = Some(room.width());
|
||||
scoped.row_line = line;
|
||||
if line.is_some() {
|
||||
scoped.first_line = None;
|
||||
}
|
||||
}),
|
||||
);
|
||||
let inner = if wrap { body(ui) } else { run_shrinking(ui, id, &mut body) };
|
||||
Env::restore(&ctx, installed);
|
||||
if !wrap {
|
||||
record_line(ui, sizing, id, shared);
|
||||
}
|
||||
inner
|
||||
});
|
||||
if align != RowAlign::Start {
|
||||
found(&ctx, sizing, id, room.size(), took.size());
|
||||
}
|
||||
(inner, (align != RowAlign::Start).then(|| took.width()))
|
||||
}
|
||||
@@ -241,19 +241,27 @@ impl ColumnGroup {
|
||||
seed: Option<Columns>,
|
||||
rows: impl FnOnce(&mut Ui, &mut ColumnGroup) -> R,
|
||||
) -> (R, Scoped) {
|
||||
let ctx = ui.ctx().clone();
|
||||
let id = sub(ui.scope_id(), key);
|
||||
let depth = layout_memory::depth(&ctx);
|
||||
let mut group = Self::open_scope(ui.ctx(), sub(ui.scope_id(), key), seed);
|
||||
let result = rows(ui, &mut group);
|
||||
(result, group.close_scope())
|
||||
}
|
||||
|
||||
/// Opens the group `id` at its `seed` widths. Its rows run in the caller, between this and `close_scope`.
|
||||
fn open_scope(ctx: &Context, id: Id, seed: Option<Columns>) -> ColumnGroup {
|
||||
let depth = layout_memory::depth(ctx);
|
||||
let widest = seed.unwrap_or_default();
|
||||
pass_state::with::<Open, _>(&ctx, |open| {
|
||||
pass_state::with::<Open, _>(ctx, |open| {
|
||||
open.0.push(Frame::Group { id, widest, beyond: HashMap::new(), early: HashMap::new(), room: None, depth });
|
||||
});
|
||||
let mut group = ColumnGroup { id, ctx: ctx.clone() };
|
||||
let result = rows(ui, &mut group);
|
||||
let frame = pass_state::with::<Open, _>(&ctx, |open| open.0.pop());
|
||||
ColumnGroup { id, ctx: ctx.clone() }
|
||||
}
|
||||
|
||||
/// Closes the group `open_scope` opened: in a measuring run, what its rows found.
|
||||
fn close_scope(&self) -> Scoped {
|
||||
let frame = pass_state::with::<Open, _>(&self.ctx, |open| open.0.pop());
|
||||
match frame {
|
||||
Some(frame) if crate::runtime::measure::active(&ctx) => (result, close(&ctx, frame)),
|
||||
_ => (result, Scoped::default()),
|
||||
Some(frame) if crate::runtime::measure::active(&self.ctx) => close(&self.ctx, frame),
|
||||
_ => Scoped::default(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -337,21 +345,11 @@ impl ColumnGroup {
|
||||
/// measuring run returns how far the rows reached past the columns.
|
||||
pub(crate) fn through<R>(ui: &mut Ui, id: Id, room: f32, sized: bool, rows: impl FnOnce(&mut Ui) -> R) -> (R, Option<Reached>) {
|
||||
let ctx = ui.ctx().clone();
|
||||
if pass_state::with::<Open, _>(&ctx, |open| open.innermost(&[])).is_none() {
|
||||
if !open_through(&ctx, id, room, sized) {
|
||||
return (Self::barrier(ui, rows), None);
|
||||
}
|
||||
pass_state::with::<Open, _>(&ctx, |open| open.0.push(Frame::Through { id, beyond: HashMap::new(), room, sized }));
|
||||
let result = rows(ui);
|
||||
let Some(Frame::Through { beyond, .. }) = pass_state::with::<Open, _>(&ctx, |open| open.0.pop()) else { return (result, None) };
|
||||
if !crate::runtime::measure::active(&ctx) || beyond.is_empty() {
|
||||
return (result, None);
|
||||
}
|
||||
let reached = beyond.into_iter().filter_map(|(columns, past)| {
|
||||
let (group, _) = pass_state::with::<Open, _>(&ctx, |open| open.innermost(&columns))?;
|
||||
let width = columns.iter().map(|column| current_width(&ctx, group, column)).sum();
|
||||
Some((columns, width, past))
|
||||
});
|
||||
(result, Some(Reached(reached.collect())))
|
||||
(result, close_through(&ctx))
|
||||
}
|
||||
|
||||
/// Records the width the group's container was offered. A row that sits directly in the group decides whether
|
||||
@@ -454,3 +452,26 @@ fn current_width(ctx: &Context, group: Id, column: &ColumnKey) -> f32 {
|
||||
fn column_width(ctx: &Context, group: Id, column: &ColumnKey) -> Option<f32> {
|
||||
layout_memory::measured(ctx, sub(group, column)).map(|found| found.took.x)
|
||||
}
|
||||
|
||||
/// Opens a `Frame::Through` for the rows of container `id` inside a group; `false` outside every group.
|
||||
fn open_through(ctx: &Context, id: Id, room: f32, sized: bool) -> bool {
|
||||
if pass_state::with::<Open, _>(ctx, |open| open.innermost(&[])).is_none() {
|
||||
return false;
|
||||
}
|
||||
pass_state::with::<Open, _>(ctx, |open| open.0.push(Frame::Through { id, beyond: HashMap::new(), room, sized }));
|
||||
true
|
||||
}
|
||||
|
||||
/// Closes the frame `open_through` opened: in a measuring run, how far its rows reached past the columns.
|
||||
fn close_through(ctx: &Context) -> Option<Reached> {
|
||||
let Some(Frame::Through { beyond, .. }) = pass_state::with::<Open, _>(ctx, |open| open.0.pop()) else { return None };
|
||||
if !crate::runtime::measure::active(ctx) || beyond.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let reached = beyond.into_iter().filter_map(|(columns, past)| {
|
||||
let (group, _) = pass_state::with::<Open, _>(ctx, |open| open.innermost(&columns))?;
|
||||
let width = columns.iter().map(|column| current_width(ctx, group, column)).sum();
|
||||
Some((columns, width, past))
|
||||
});
|
||||
Some(Reached(reached.collect()))
|
||||
}
|
||||
@@ -63,7 +63,7 @@ impl SurfaceSlot {
|
||||
}
|
||||
|
||||
/// Paints the surface `kind` over `rect`.
|
||||
pub(crate) fn paint(self, tokens: &UiTokens, kind: SurfaceKind, rect: Rect, direction: Direction) {
|
||||
pub(crate) fn paint(&self, tokens: &UiTokens, kind: SurfaceKind, rect: Rect, direction: Direction) {
|
||||
let shapes = Surface::shapes(&surface_style(tokens, kind), None, rect, Corners::All, direction, self.painter.pixels_per_point());
|
||||
self.painter.set(self.slot, Shape::Vec(shapes));
|
||||
}
|
||||
|
||||
@@ -24,14 +24,20 @@ pub(crate) fn pinned_layout() -> Layout {
|
||||
/// Runs `body` in a child `Ui` with an explicit layout and returns what it produced with the rect the child
|
||||
/// took in `ui`. `rect` bounds the child; `None` gives it the rest of the parent.
|
||||
pub(crate) fn block<R>(ui: &mut Ui, rect: Option<Rect>, layout: Layout, body: impl FnOnce(&mut Ui) -> R) -> (R, Rect) {
|
||||
let mut builder = UiBuilder::new().layout(layout);
|
||||
if let Some(rect) = rect {
|
||||
builder = builder.max_rect(rect);
|
||||
}
|
||||
let out = ui.scope_builder(builder, body);
|
||||
let out = ui.scope_dyn(block_builder(rect, layout), Box::new(body));
|
||||
(out.inner, out.response.rect)
|
||||
}
|
||||
|
||||
/// The builder of a `block`'s child, made outside the generic `block` so a body nested in blocks keeps one builder
|
||||
/// per level on the stack.
|
||||
fn block_builder(rect: Option<Rect>, layout: Layout) -> UiBuilder {
|
||||
let builder = UiBuilder::new().layout(layout);
|
||||
match rect {
|
||||
Some(rect) => builder.max_rect(rect),
|
||||
None => builder,
|
||||
}
|
||||
}
|
||||
|
||||
/// The physical layout of a `Stack` or `Grid` body for `right_to_left`.
|
||||
pub(crate) fn column_layout(right_to_left: bool) -> Layout {
|
||||
stack_layout(right_to_left)
|
||||
|
||||
@@ -119,7 +119,7 @@ impl Component for Block {
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> Response {
|
||||
cx.core.response
|
||||
cx.response.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
//! The body step and the mount host: regions, scrolling, move and resize, and viewport mounts.
|
||||
|
||||
mod mounts;
|
||||
mod nesting;
|
||||
mod placement;
|
||||
@@ -0,0 +1,39 @@
|
||||
//! Deep nesting: a component's run keeps only what it reads after its children on the stack.
|
||||
|
||||
use craft_ui::contract::{Props as _, Show as _, ShowContainer as _};
|
||||
use craft_ui::layout::BodyLayout;
|
||||
use craft_ui::testing::layout::Block;
|
||||
use craft_ui::testing::{Frames, trial};
|
||||
use craft_ui_test_kit::contract::Panel;
|
||||
use egui::{Ui, Vec2};
|
||||
use std::cell::Cell;
|
||||
|
||||
/// Containers sixteen deep, rows inside stacks, show on a thread with a 512 KiB stack: their measuring runs and their
|
||||
/// real runs both stay within it.
|
||||
#[trial]
|
||||
fn sixteen_nested_containers_show_on_a_512_kib_stack() {
|
||||
let reached = std::thread::Builder::new()
|
||||
.stack_size(512 * 1024)
|
||||
.spawn(|| {
|
||||
let reached = Cell::new(0);
|
||||
let mut frames = Frames::new();
|
||||
frames.idle(2, |ui| nest(ui, 16, &reached));
|
||||
reached.get()
|
||||
})
|
||||
.expect("the thread starts")
|
||||
.join()
|
||||
.expect("the frames run");
|
||||
assert!(reached > 0, "the innermost body ran");
|
||||
}
|
||||
|
||||
fn nest(ui: &mut Ui, depth: usize, reached: &Cell<usize>) {
|
||||
let layout = if depth.is_multiple_of(2) { BodyLayout::Stack } else { BodyLayout::row() };
|
||||
Panel::new().layout(layout).id_salt(depth).show(ui, |ui, _| {
|
||||
if depth == 1 {
|
||||
Block::new(Vec2::splat(10.0)).show(ui);
|
||||
reached.set(reached.get() + 1);
|
||||
} else {
|
||||
nest(ui, depth - 1, reached);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -180,7 +180,7 @@ macro_rules! layout_component {
|
||||
}
|
||||
|
||||
fn finish(self, cx: Cx<'_, Self>) -> Response {
|
||||
cx.core.response
|
||||
cx.response.clone()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user