mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-22 10:31:37 +01:00
Layout: a control finds an egui grid's cell through the UiStack, with_env is the one scope wrapper, an egui cell is decided at what its row needs
- Env::of sets Scoped::egui_cell when its walk from a Ui to the entry it finds passes an egui::Grid's Ui; scopes that pass the env on keep it, a body or a component's children end it. It replaces Ui::is_grid in the label step and the container hooks. - In an egui cell the row is decided at what the label and the control at its ideal width need, from the cell's left edge, narrowed as well as widened. - flow_scope folds into with_env (Overrides::id_salt); the dock group header and the Liquify footer are craft Rows. - text_edit and the selectable text read the caller's env before they make their child Ui. - Grid docs: rows after an egui container are placed over it.
This commit is contained in:
1 parent
017ecd683f
commit
6ca8b40158
16 files changed
+125
-84
No files matched your search
@@ -77,11 +77,12 @@ use craft_ui::dialog::{
|
||||
FieldRefusal, FullWindowCanvas, FullWindowDialog, FullWindowSide, FullWindowTitle, FullWindowView, FullWindowWorkspace, RasterOverlay,
|
||||
WorkspaceCx, WorkspacePass, serde_fields, set_serde_field,
|
||||
};
|
||||
use craft_ui::layout::RowAlign;
|
||||
use craft_ui::layout_components::Row;
|
||||
use craft_ui::primitives::Checker;
|
||||
use craft_ui::rails::{LastUsed, ToolRail, ToolSlot};
|
||||
use craft_ui::runtime::commands::{CommandSet as _, Commands as _, FirePhase};
|
||||
use craft_ui::runtime::env::{Env, flow_scope};
|
||||
use craft_ui::runtime::env::Env;
|
||||
use craft_ui::runtime::memory::{SaveKey, saved_value, set_saved_value};
|
||||
use craft_ui::runtime::words::CommonWords;
|
||||
use craft_ui::sections::{DialogRole, FooterButton};
|
||||
@@ -462,7 +463,7 @@ impl LiquifyWorkspace {
|
||||
.opened(open)
|
||||
.buttons([FooterButton::new(common.ok.clone(), DialogRole::Accept), FooterButton::new(common.cancel.clone(), DialogRole::Cancel)])
|
||||
.footer(|ui, _| {
|
||||
let _ = flow_scope(ui, egui::UiBuilder::new().layout(egui::Layout::top_down(egui::Align::Center)), |ui| {
|
||||
let _ = Row::new().fill_width().align(RowAlign::Center).show(ui, |ui, _| {
|
||||
let mut guard = this.borrow_mut();
|
||||
let work: &mut LiquifyWorkspace = &mut guard;
|
||||
let events = Checkbox::new(Bind::given(work.preview)).label(preview_label.clone()).show(ui).events;
|
||||
|
||||
@@ -297,7 +297,7 @@ impl<C: Container> ShapeHooks<C> for BodyHooks<'_> {
|
||||
self.held = self.held(props, &env);
|
||||
self.across = self.held && matches!(env.column, Some(ColumnSweep::Column(..)));
|
||||
self.in_column = label::in_column(ui);
|
||||
self.egui_cell = ui.is_grid();
|
||||
self.egui_cell = env.egui_cell;
|
||||
self.given.1 |= self.given_room(props, &env, at, ui.ctx(), id);
|
||||
let measured = self.measured_body.unwrap_or(Vec2::ZERO);
|
||||
let body = if self.presence.visible { Vec2::new(measured.x, measured.y * self.presence.t) } else { Vec2::ZERO };
|
||||
@@ -1841,7 +1841,7 @@ impl BodyHooks<'_> {
|
||||
self.room.x = width;
|
||||
}
|
||||
self.in_column = label::in_column(at.ui);
|
||||
self.egui_cell = at.ui.is_grid();
|
||||
self.egui_cell = at.env.egui_cell;
|
||||
// A body that fills the main axis of the body it sits in is measured and shown at its share of it. The first
|
||||
// sweep of that body's measuring run only counts it: its contents run in the second sweep, at the share.
|
||||
let (x, y) = (craft_behavior::Axis::X, craft_behavior::Axis::Y);
|
||||
|
||||
@@ -29,9 +29,9 @@ pub(super) struct MeasureKey {
|
||||
/// Whether the body shows, and how much of it an animated collapse shows.
|
||||
shows: (bool, f32),
|
||||
env: EnvFacts,
|
||||
/// Where its parent flow showed the container: as a row of a vertical flow (`label::in_column`), with a rule just
|
||||
/// before it (a rule above its header is not drawn), and in a cell of an `egui::Grid`.
|
||||
flow: (bool, bool, bool),
|
||||
/// Where its parent flow showed the container: as a row of a vertical flow (`label::in_column`), and with a rule
|
||||
/// just before it (a rule above its header is not drawn).
|
||||
flow: (bool, bool),
|
||||
}
|
||||
|
||||
/// The props a body was measured under, compared on the fields its measure reads.
|
||||
@@ -61,6 +61,7 @@ pub(crate) struct EnvFacts {
|
||||
offered: Option<f32>,
|
||||
measure: Option<f32>,
|
||||
grid: Option<Id>,
|
||||
egui_cell: bool,
|
||||
}
|
||||
|
||||
impl EnvFacts {
|
||||
@@ -80,6 +81,7 @@ impl EnvFacts {
|
||||
offered: env.offered,
|
||||
measure: env.measure,
|
||||
grid: env.grid,
|
||||
egui_cell: env.egui_cell,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,7 +97,7 @@ impl<'b> BodyHooks<'b> {
|
||||
width,
|
||||
shows: (self.open, self.presence.t),
|
||||
env: EnvFacts::of(env),
|
||||
flow: (self.in_column, self.after_rule, self.egui_cell),
|
||||
flow: (self.in_column, self.after_rule),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -419,12 +419,13 @@ impl Under {
|
||||
}
|
||||
|
||||
/// An env derived for children `PaintUi` builds: the composite's resolved `enabled` and `size`, and only the inside
|
||||
/// label `under` gives.
|
||||
/// label `under` gives. The composite places its children itself, so they are in no egui grid's cell.
|
||||
pub(crate) fn child_env(env: &Env, under: &Under) -> Env {
|
||||
env.derive_local(|scoped| {
|
||||
scoped.enabled &= under.enabled;
|
||||
scoped.size = under.size;
|
||||
scoped.inside_label = under.inside.clone().map(|inside| inside as crate::runtime::env::Held);
|
||||
scoped.cell_fills = false;
|
||||
scoped.egui_cell = false;
|
||||
})
|
||||
}
|
||||
@@ -95,8 +95,9 @@ pub(super) fn decide(ctx: &egui::Context, env: &Env, input: &LabelIn<'_>, kids:
|
||||
}
|
||||
}
|
||||
let baseline = leaf.baseline.get();
|
||||
// An egui grid's cell grows at its right edge in any direction (`LabelIn::egui_cell`); a row grows at its end edge.
|
||||
let grows = if input.egui_cell { Grows::Right } else { Grows::End };
|
||||
// An egui grid's cell is decided at what it needs from its left edge in any direction (`LabelIn::egui_cell`); a
|
||||
// row grows at its end edge.
|
||||
let grows = if input.egui_cell { Grows::Cell } else { Grows::End };
|
||||
if input.side.layout == FieldLayout::Line {
|
||||
let number = env.control(input.size).number;
|
||||
let flow = if input.egui_cell { widened(flow, natural.x + gap + number, grows) } else { flow };
|
||||
@@ -165,24 +166,25 @@ pub(super) fn decide(ctx: &egui::Context, env: &Env, input: &LabelIn<'_>, kids:
|
||||
}
|
||||
}
|
||||
|
||||
/// The edge a flow that is too narrow grows at (`widened`).
|
||||
/// How a flow takes the width its label and control need (`widened`).
|
||||
#[derive(Clone, Copy)]
|
||||
enum Grows {
|
||||
/// Its end edge: the left one under RTL.
|
||||
/// A flow that is too narrow grows at its end edge: the left one under RTL.
|
||||
End,
|
||||
/// Its right edge, in either direction.
|
||||
Right,
|
||||
/// An egui grid's cell: exactly as wide as they need from its left edge, in either direction, narrowed as well as
|
||||
/// widened. egui offers a grid's last column the rest of the room, where the control would sit at its far edge
|
||||
/// under RTL.
|
||||
Cell,
|
||||
}
|
||||
|
||||
/// `flow` widened to `need` at the edge `grows` names, when it is narrower.
|
||||
/// `flow` widened to `need` as `grows` says.
|
||||
fn widened(flow: Flow, need: f32, grows: Grows) -> Flow {
|
||||
let mut wide = flow.rect();
|
||||
if wide.width() >= need {
|
||||
return flow;
|
||||
}
|
||||
match grows {
|
||||
Grows::Cell => wide.max.x = wide.min.x + need,
|
||||
Grows::End if wide.width() >= need => return flow,
|
||||
Grows::End if flow.is_rtl() => wide.min.x = wide.max.x - need,
|
||||
Grows::End | Grows::Right => wide.max.x = wide.min.x + need,
|
||||
Grows::End => wide.max.x = wide.min.x + need,
|
||||
}
|
||||
Flow::new(wide, flow.is_rtl())
|
||||
}
|
||||
|
||||
@@ -33,7 +33,8 @@ pub(crate) struct LabelIn<'a> {
|
||||
/// since the column is as wide as the label, the gap and the control need.
|
||||
pub content_row: bool,
|
||||
/// The row is a cell of an `egui::Grid` (`LeafLabel::egui_cell`): the row is as wide as its label and control need,
|
||||
/// so the label neither wraps nor stacks for want of room, and it grows at its right edge in any direction.
|
||||
/// so the label neither wraps nor stacks for want of room, and the row is that wide from its left edge in any
|
||||
/// direction.
|
||||
pub egui_cell: bool,
|
||||
pub text: &'a Label,
|
||||
pub side: &'a SideLabel,
|
||||
@@ -42,6 +43,7 @@ pub(crate) struct LabelIn<'a> {
|
||||
pub enabled: bool,
|
||||
pub size: craft_theme::ControlSize,
|
||||
/// The narrowest the control can be; a row too narrow for the label column, the gap and this stacks the label above.
|
||||
/// In an egui grid's cell (`egui_cell`), the width the control takes there: a leaf's ideal width.
|
||||
pub control_min: f32,
|
||||
/// The control is a leaf child shown in the flow with its label after it, not a field's body.
|
||||
pub leaf: bool,
|
||||
@@ -85,8 +87,9 @@ pub(crate) struct LeafLabel<'a> {
|
||||
pub parent: Option<Id>,
|
||||
/// The row the label and the child share.
|
||||
pub row: Rect,
|
||||
/// The row is a cell of an `egui::Grid`: egui places it from the cell's left edge in any direction, and gives the
|
||||
/// column the width it takes from the next frame. `row` then gives only the cell's left edge, top and height.
|
||||
/// The row is a cell of an `egui::Grid` (`Scoped::egui_cell`): egui places it from the cell's left edge in any
|
||||
/// direction, and gives the column the width it takes from the next frame. `row` then gives only the cell's left
|
||||
/// edge, top and height.
|
||||
pub egui_cell: bool,
|
||||
pub text: &'a Label,
|
||||
pub side: &'a SideLabel,
|
||||
@@ -123,7 +126,9 @@ impl LeafPlaced {
|
||||
pub(crate) fn decide_leaf<C: Show>(ui: &mut Ui, env: &Env, at: &LeafLabel<'_>, child: &mut C, trailing: Vec2) -> Decided {
|
||||
// Read before the child measures: nothing it does there may count as a child placed in flow.
|
||||
let in_column = in_column(ui);
|
||||
let control_min = crate::contract::run::measure_under(child, &*ui, Some(at.id), craft_behavior::Proposal::Min, Some(at.under)).x;
|
||||
// An egui grid's cell is as wide as the row takes, and the control shows at its ideal width there.
|
||||
let proposal = if at.egui_cell { craft_behavior::Proposal::Ideal } else { craft_behavior::Proposal::Min };
|
||||
let control_min = crate::contract::run::measure_under(child, &*ui, Some(at.id), proposal, Some(at.under)).x;
|
||||
let facts = LeafFacts { control_min, in_column, claims: C::LABELLED_AFTER };
|
||||
decide_step(ui, env, &leaf_input(env, at, at.row, facts, trailing))
|
||||
}
|
||||
@@ -401,14 +406,15 @@ fn flow_leaf_row<C: Show>(
|
||||
let row = Rect::from_min_max(avail.min, egui::pos2(avail.max.x, avail.min.y + ideal.y));
|
||||
// An `egui::Grid` offers a column with no width from the last frame its least cell width, where the label would
|
||||
// wrap and keep the column that narrow: in its cell the label is decided at its own width.
|
||||
let egui_cell = ui.is_grid();
|
||||
let egui_cell = env.egui_cell;
|
||||
let at = LeafLabel { id, owner, parent: None, row, egui_cell, text: &label, side: &side, under: &under };
|
||||
let decided = decide_leaf(ui, &env, &at, &mut child, trailing.as_ref().map_or(Vec2::ZERO, |trailing| trailing.size));
|
||||
// In a column that sizes from its content the row is the label plus the control, never the room around them.
|
||||
// A row of a horizontal flow is as wide as its label and control: the room it has there is unbounded.
|
||||
// So is a grid's cell: the room before it is the whole grid's, and a row spanning it measures its column as wide
|
||||
// as the grid, so the grid could never set two such cells beside each other (two columns of check boxes).
|
||||
let content = env.content.0 || ui.layout().is_horizontal() || in_cell;
|
||||
// as the grid, so the grid could never set two such cells beside each other (two columns of check boxes). An egui
|
||||
// grid's cell is too, also inside a scope of its own there.
|
||||
let content = env.content.0 || ui.layout().is_horizontal() || in_cell || egui_cell;
|
||||
let control_width = if content { decided.control().width().min(ideal.x) } else { decided.control().width() };
|
||||
let rtl = env.direction.is_rtl();
|
||||
let span = |cell: Rect| Flow::new(Rect::from_min_size(cell.min, Vec2::new(cell.width(), ideal.y)), rtl).span(0.0, control_width);
|
||||
|
||||
@@ -94,7 +94,8 @@ impl BodyLayout {
|
||||
|
||||
/// Children row-major over `columns`: a `'static` slice, or columns built in the pass. The cells are the body's
|
||||
/// craft children: an egui container shown in the body is no cell, and egui places it below the cells placed so
|
||||
/// far. Content that should take a cell goes in a craft body (`Row`).
|
||||
/// far. The rows after it are placed over it: show it as the last child, or put it in a craft body (`Row`), which
|
||||
/// takes a cell.
|
||||
pub fn grid(columns: impl Into<Cow<'static, [ColumnWidth]>>) -> Self {
|
||||
BodyLayout::Grid { columns: columns.into(), fit: GridFit::Stack, cells: CellAlign::Center }
|
||||
}
|
||||
|
||||
@@ -518,13 +518,13 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
let env = crate::runtime::env::Env::of(input.ui);
|
||||
let multiline = input.options.multiline();
|
||||
let scroll = if multiline { scroll_before(ctx, widget) } else { 0.0 };
|
||||
let filter = egui_focus::event_filter(input.keys.keep, false);
|
||||
// A single line sits on the baseline of its control's line, whatever its face: a number field's mono digits on
|
||||
// the body text's.
|
||||
let baseline = (!input.options.multiline()).then(|| {
|
||||
let env = crate::runtime::env::Env::of(input.ui);
|
||||
let text_env = super::text::TextEnv { tokens: &env.tokens, direction: env.direction, ink: input.ink, fonts: crate::fonts::stack_facts(ctx) };
|
||||
super::text::line_baseline(ctx, &text_env, input.options.role, input.rect.center().y)
|
||||
});
|
||||
@@ -557,7 +557,7 @@ pub(crate) fn run(state: &mut TextState, mut input: AdapterIn<'_>) -> AdapterOut
|
||||
// egui's widget moves the caret to any press, so the selection from before the press is put back.
|
||||
let secondary = crate::runtime::pointer::frame_raw(ctx).press.is_some_and(|press| press.button == craft_behavior::PointerButton::Secondary);
|
||||
let kept = selection(ctx, widget).filter(|(caret, anchor)| secondary && caret != anchor);
|
||||
let shown = text_edit::show(input.ui, edit);
|
||||
let shown = text_edit::show(input.ui, &env, edit);
|
||||
crate::runtime::pointer::record_text_widget(ctx, widget, input.id);
|
||||
if let Some((caret, anchor)) = kept
|
||||
&& selection(ctx, widget).is_some_and(|(at, _)| (caret.min(anchor)..=caret.max(anchor)).contains(&at))
|
||||
@@ -644,6 +644,7 @@ pub(crate) fn show_selectable(ui: &mut Ui, text: Selectable<'_>) {
|
||||
let multiline = run.wrap == crate::primitives::Wrap::Wrap;
|
||||
let keep = if multiline { KeepKeys::BlockArrows } else { KeepKeys::InlineArrows };
|
||||
let mut shown = layout.full().to_owned();
|
||||
let env = crate::runtime::env::Env::of(ui);
|
||||
// A line longer than its room is cut at the room's edge, as a field's is.
|
||||
let mut cut = (layout.size().x > rect.width()).then(|| {
|
||||
let mut cut = crate::runtime::egui::alloc::child(ui, egui::UiBuilder::new().max_rect(rect));
|
||||
@@ -652,6 +653,7 @@ pub(crate) fn show_selectable(ui: &mut Ui, text: Selectable<'_>) {
|
||||
});
|
||||
text_edit::show(
|
||||
cut.as_mut().unwrap_or(&mut *ui),
|
||||
&env,
|
||||
text_edit::Edit {
|
||||
id: widget,
|
||||
rect: layout.galley.rect.translate(at.to_vec2()),
|
||||
|
||||
@@ -43,10 +43,14 @@ pub(crate) fn column_layout(right_to_left: bool) -> Layout {
|
||||
stack_layout(right_to_left)
|
||||
}
|
||||
|
||||
/// Runs `body` in a child `Ui`. `disabled` disables the child and everything below it. `direction` gives the
|
||||
/// child a cloned style carrying that direction and an explicit layout, never the parent's.
|
||||
pub(crate) fn scope<R>(ui: &mut Ui, disabled: bool, direction: Option<bool>, body: impl FnOnce(&mut Ui) -> R) -> R {
|
||||
/// Runs `body` in a child `Ui`, with the id salt `id_salt` when given. `disabled` disables the child and everything
|
||||
/// below it. `direction` gives the child a cloned style carrying that direction and an explicit layout, never the
|
||||
/// parent's.
|
||||
pub(crate) fn scope<R>(ui: &mut Ui, id_salt: Option<egui::Id>, disabled: bool, direction: Option<bool>, body: impl FnOnce(&mut Ui) -> R) -> R {
|
||||
let mut builder = UiBuilder::new();
|
||||
if let Some(salt) = id_salt {
|
||||
builder = builder.id_salt(salt);
|
||||
}
|
||||
if let Some(right_to_left) = direction {
|
||||
let mut style = (**ui.style()).clone();
|
||||
style.right_to_left = right_to_left;
|
||||
|
||||
@@ -1,13 +1,29 @@
|
||||
//! `UiStack` walks for `Env::of`.
|
||||
|
||||
use egui::{Id, Ui};
|
||||
use egui::{Id, Ui, UiKind};
|
||||
|
||||
/// The unique id of `ui`'s stack node: the key env entries are installed under.
|
||||
pub(crate) fn node_id(ui: &Ui) -> Id {
|
||||
ui.stack().unique_id
|
||||
}
|
||||
|
||||
/// What `nearest` found walking from a `Ui` outwards.
|
||||
pub(crate) struct Found<T> {
|
||||
/// The first value found, and whether it was found for the `Ui`'s own node.
|
||||
pub value: Option<(T, bool)>,
|
||||
/// The walk passed the `Ui` of an `egui::Grid` (`UiKind::Grid`) before the node the value was found for, or
|
||||
/// anywhere on the stack when none was found.
|
||||
pub grid: bool,
|
||||
}
|
||||
|
||||
/// The first value `lookup` finds walking from `ui` outwards, by the stack nodes' unique ids.
|
||||
pub(crate) fn nearest<T>(ui: &Ui, lookup: impl Fn(Id) -> Option<T>) -> Option<T> {
|
||||
ui.stack().iter().find_map(|node| lookup(node.unique_id))
|
||||
pub(crate) fn nearest<T>(ui: &Ui, lookup: impl Fn(Id) -> Option<T>) -> Found<T> {
|
||||
let mut grid = false;
|
||||
for (depth, node) in ui.stack().iter().enumerate() {
|
||||
if let Some(value) = lookup(node.unique_id) {
|
||||
return Found { value: Some((value, depth == 0)), grid };
|
||||
}
|
||||
grid |= node.kind() == Some(UiKind::Grid);
|
||||
}
|
||||
Found { value: None, grid }
|
||||
}
|
||||
@@ -65,8 +65,8 @@ pub(crate) struct Shown {
|
||||
pub content: f32,
|
||||
}
|
||||
|
||||
/// Runs the widget at `edit.rect`.
|
||||
pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown {
|
||||
/// Runs the widget at `edit.rect`, under `env`, the env of the `Ui` the caller shows the field in.
|
||||
pub(crate) fn show(ui: &mut Ui, env: &crate::runtime::env::Env, edit: Edit<'_>) -> Shown {
|
||||
let Edit {
|
||||
id,
|
||||
rect,
|
||||
@@ -93,7 +93,6 @@ pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown {
|
||||
scroll,
|
||||
} = edit;
|
||||
// The same font and optical size as a label of the same role: one text path.
|
||||
let env = crate::runtime::env::Env::of(ui);
|
||||
let tokens = &env.tokens;
|
||||
// Behind selected text and the selected text itself: the field's roles.
|
||||
let selection = (tokens.color.accent.soft, tokens.color.text.normal);
|
||||
@@ -177,7 +176,7 @@ pub(crate) fn show(ui: &mut Ui, edit: Edit<'_>) -> Shown {
|
||||
super::style::disable_without_fade(ui);
|
||||
}
|
||||
// The placeholder is the faint text colour, the disabled one when the field is off.
|
||||
let text_colors = &crate::runtime::env::Env::of(ui).tokens.color.text;
|
||||
let text_colors = &tokens.color.text;
|
||||
super::style::set_hint_color(ui, crate::style::rgba_color(if enabled { text_colors.faint } else { text_colors.disabled }));
|
||||
super::style::set_text_cursor(ui, caret_visible);
|
||||
super::style::set_text_selection(ui, crate::style::rgba_color(selection.0), crate::style::rgba_color(selection.1));
|
||||
|
||||
@@ -44,7 +44,7 @@ mod view;
|
||||
pub use app_values::{AppValues, EnvValue, StyleValue};
|
||||
pub use craft_platform::appearance::SystemReader;
|
||||
use host_words::{host_dates, host_format, host_words};
|
||||
pub use overrides::{Overrides, ThemeScope, WheelScope, WordsOverride, flow_scope, with_env};
|
||||
pub use overrides::{Overrides, ThemeScope, WheelScope, WordsOverride, with_env};
|
||||
pub use root::{ResolvedRoot, RootEnv};
|
||||
pub use system::{SystemState, system_appearance};
|
||||
pub(crate) use tokens::TokenInputs;
|
||||
@@ -90,6 +90,7 @@ pub(crate) struct Flow {
|
||||
unbounded: (bool, bool),
|
||||
cell_fills: bool,
|
||||
fits: bool,
|
||||
egui_cell: bool,
|
||||
}
|
||||
|
||||
/// The values of an env.
|
||||
@@ -216,6 +217,12 @@ pub struct Scoped {
|
||||
/// The scope is the body of a beside label in an inspector (`LabelScheme::Inspector`): the control shown there
|
||||
/// fills the width the label leaves. Only that control sees it, as with `inside_label`.
|
||||
pub(crate) cell_fills: bool,
|
||||
/// The scope lays out in a cell of an `egui::Grid` that egui code made below the body around it: the grid places
|
||||
/// each child from its cell's left edge in any direction, and gives the column the width the child takes from the
|
||||
/// next frame. `Env::of` sets it where the stack between a `Ui` and its entry holds a grid's `Ui`; a scope that
|
||||
/// passes `Env::of` on (`with_env`, `Lazy`, a memoized view) keeps it, and a body (`start_room`) or a component's
|
||||
/// children (`child_env`) end it.
|
||||
pub(crate) egui_cell: bool,
|
||||
}
|
||||
|
||||
/// The values of an env that a scope rarely changes: the resolved tokens, the platform, the language and the app's
|
||||
@@ -433,9 +440,10 @@ impl Scoped {
|
||||
self.shrink = None;
|
||||
self.column = None;
|
||||
self.offered = None;
|
||||
self.egui_cell = false;
|
||||
}
|
||||
|
||||
/// Whether any of the facts `start_room` ends is set.
|
||||
/// Whether any of the facts `start_room` ends is set, besides `egui_cell`, which `Env::of` sets.
|
||||
fn has_room(&self) -> bool {
|
||||
self.grid.is_some()
|
||||
|| self.grid_top
|
||||
@@ -454,7 +462,8 @@ impl Scoped {
|
||||
}
|
||||
|
||||
/// Starts the layout room of a memoized view's content (`start_room`), keeping whether the enclosing body is a
|
||||
/// content-sized or a given column, as `start_room` keeps the content axes. The view's content then measures and
|
||||
/// content-sized or a given column and whether the view lies in an egui grid's cell, as `start_room` keeps the
|
||||
/// content axes. The view's content then measures and
|
||||
/// fills as it would shown without the view: a child that fills takes its measured size where the body sizes from
|
||||
/// content, never the open room. Where a leaf shown here would fit its room (`view_fits`), the leaves of the view's
|
||||
/// content fit the room the view is given (`Shrink::Fit`). A view that is a unit of a shrinking `Row` sizes from its
|
||||
@@ -463,9 +472,9 @@ impl Scoped {
|
||||
pub(crate) fn start_view_room(&mut self) {
|
||||
let fits = self.view_fits();
|
||||
let row_unit = matches!(self.shrink, Some(Shrink::Count(_) | Shrink::Least(_) | Shrink::Apply(_)));
|
||||
let (content_column, given_column) = (self.content_column, self.given_column);
|
||||
let (content_column, given_column, egui_cell) = (self.content_column, self.given_column, self.egui_cell);
|
||||
self.start_room();
|
||||
(self.content_column, self.given_column) = (content_column, given_column);
|
||||
(self.content_column, self.given_column, self.egui_cell) = (content_column, given_column, egui_cell);
|
||||
if fits {
|
||||
self.shrink = Some(Shrink::Fit);
|
||||
}
|
||||
@@ -524,6 +533,7 @@ impl Scoped {
|
||||
unbounded: self.unbounded,
|
||||
cell_fills: self.cell_fills,
|
||||
fits: self.view_fits(),
|
||||
egui_cell: self.egui_cell,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -675,6 +685,7 @@ impl Env {
|
||||
item_facts: None,
|
||||
inside_label: None,
|
||||
cell_fills: false,
|
||||
egui_cell: false,
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -687,21 +698,27 @@ impl Env {
|
||||
/// it shows in the `Ui` it installed its env for. A `Ui` egui code makes below that one (an `egui::Grid`,
|
||||
/// `ui.horizontal`, `ui.push_id` in a caller's closure) lays out its own children: an entry found for an ancestor
|
||||
/// of `ui` comes without those facts, so a component there takes no cell, line or shrink step of the body around it.
|
||||
/// Where that stack holds an `egui::Grid`'s `Ui`, the env says the component lays out in the grid's cell
|
||||
/// (`Scoped::egui_cell`).
|
||||
///
|
||||
/// While apps still use `add_enabled_ui`, it also ANDs `ui.is_enabled()`, outside sizing passes.
|
||||
pub fn of(ui: &Ui) -> Env {
|
||||
let ctx = ui.ctx();
|
||||
let viewport = pass::current_viewport(ctx);
|
||||
let own = stack::node_id(ui);
|
||||
let found = PerViewport::<ViewportEnvs>::with(ctx, viewport, |envs| {
|
||||
stack::nearest(ui, |node| envs.entries.get(&node).map(|env| (env.clone(), node == own)))
|
||||
.or_else(|| envs.root.clone().map(|env| (env, true)))
|
||||
// The root env stands for an entry above the whole stack.
|
||||
let (found, grid) = PerViewport::<ViewportEnvs>::with(ctx, viewport, |envs| {
|
||||
let stack::Found { value, grid } = stack::nearest(ui, |node| envs.entries.get(&node).cloned());
|
||||
(value.or_else(|| envs.root.clone().map(|env| (env, false))), grid)
|
||||
});
|
||||
let env = match found {
|
||||
Some((env, true)) => env,
|
||||
Some((env, false)) if env.scoped.has_room() => env.derive_local(Scoped::start_room),
|
||||
Some((env, false)) => env,
|
||||
None => Env::from_root(ctx, &RootEnv::fallback(ctx), None, scale::pixels_per_point(ctx)),
|
||||
let (env, own) = found.unwrap_or_else(|| (Env::from_root(ctx, &RootEnv::fallback(ctx), None, scale::pixels_per_point(ctx)), false));
|
||||
let egui_cell = env.egui_cell || grid;
|
||||
let env = if own || !(env.scoped.has_room() || egui_cell != env.egui_cell) {
|
||||
env
|
||||
} else {
|
||||
env.derive_local(|scoped| {
|
||||
scoped.start_room();
|
||||
scoped.egui_cell = egui_cell;
|
||||
})
|
||||
};
|
||||
// egui's `invisible()` also disables the `Ui`, and a sizing `Ui` is invisible, not disabled: in a sizing pass
|
||||
// the env keeps the enabled state the real run has, so a memoized view keys alike in both.
|
||||
|
||||
+10
-10
@@ -128,6 +128,8 @@ pub struct Overrides {
|
||||
pub words: Option<WordsOverride>,
|
||||
/// App values set for the scope.
|
||||
pub values: AppValues,
|
||||
/// The child `Ui`'s id salt, as `ui.push_id` gives it: the ids of what it shows derive from it.
|
||||
pub id_salt: Option<egui::Id>,
|
||||
}
|
||||
|
||||
/// How a scope changes the built-in text: the only override path for words.
|
||||
@@ -176,27 +178,25 @@ impl Overrides {
|
||||
}
|
||||
}
|
||||
|
||||
/// Runs `body` in a child `Ui` built from `builder`, as `ui.scope_builder` does, under the env of `ui`: the components
|
||||
/// it shows are children of the body around `ui` as those shown in `ui` itself are, on its line and in its cells. The
|
||||
/// craft form of `ui.scope`, `ui.push_id` (`UiBuilder::id_salt`) and `ui.with_layout` around craft content: what a
|
||||
/// body sets up for its children in flow does not reach a `Ui` egui code makes below the body's own (`Env::of`).
|
||||
pub fn flow_scope<R>(ui: &mut Ui, builder: egui::UiBuilder, body: impl FnOnce(&mut Ui) -> R) -> egui::InnerResponse<R> {
|
||||
let env = Env::of(ui);
|
||||
ui.scope_builder(builder, |child| Env::run_in(child, env, body))
|
||||
}
|
||||
|
||||
/// Runs `body` in a child `Ui` with `set`'s changes applied to the inherited values. A scope that disables
|
||||
/// disables its child `Ui`; one that changes direction builds the child with an explicit layout and never
|
||||
/// inherits the parent's.
|
||||
///
|
||||
/// The components the child `Ui` shows are children of the body around `ui` as those shown in `ui` itself are, on its
|
||||
/// line and in its cells. It is the craft form of `ui.scope` and
|
||||
/// `ui.push_id` (`Overrides::id_salt`) around craft content: what a body sets up for its children in flow does not
|
||||
/// reach a `Ui` egui code makes below the body's own (`Env::of`). Content laid out along the other axis goes in a
|
||||
/// craft `Row` or `Stack`, a body of its own.
|
||||
pub fn with_env<R>(ui: &mut Ui, set: impl FnOnce(&mut Overrides), body: impl FnOnce(&mut Ui) -> R) -> R {
|
||||
let mut overrides = Overrides::default();
|
||||
set(&mut overrides);
|
||||
let id_salt = overrides.id_salt.take();
|
||||
let outer = Env::of(ui);
|
||||
let ctx = ui.ctx().clone();
|
||||
let inner = outer.derive(|scoped| overrides.apply(&ctx, scoped));
|
||||
let direction = (inner.direction != outer.direction).then(|| inner.direction.is_rtl());
|
||||
let disabled = !inner.enabled;
|
||||
child::scope(ui, disabled, direction, |child| {
|
||||
child::scope(ui, id_salt, disabled, direction, |child| {
|
||||
let _restore = Restore { ctx: &ctx, installed: Some(Env::install_for(child, inner)) };
|
||||
body(child)
|
||||
})
|
||||
|
||||
@@ -9,10 +9,11 @@ use super::{PanelDrag, PanelKey, PanelTitle, Titles};
|
||||
use crate::buttons::IconButton;
|
||||
use crate::contract::overlay::Anchor;
|
||||
use crate::contract::{
|
||||
Access, Bind, BindMut, BodyCx, Common, Component, Container, ContainerProps, ContextMenuRequest, Cx, HeaderEdge, HeaderParts, HeaderRegion,
|
||||
Access, Bind, BindMut, BodyCx, Common, Component, Container, ContainerProps, ContextMenuRequest, Cx, Gap, HeaderEdge, HeaderParts, HeaderRegion,
|
||||
ListDrag, MeasureCx, MemoProps, MemoView, Name, Outbox, Outcome, OverlayUi, Overlays as _, PaintUi, PartsCx, Pointer as _, PopupKind, Props as _,
|
||||
Role, ShowCollection as _, ShowContainer as _, Sizing, WordSetCx as _,
|
||||
};
|
||||
use crate::layout_components::Row;
|
||||
use crate::menu::{GroupItem, Menu, PanelMenu, PanelRow};
|
||||
use crate::runtime::commands::{CommandSet as _, CommandState, Commands as _};
|
||||
use crate::runtime::env::Label;
|
||||
@@ -504,14 +505,11 @@ impl<P: Clone + Hash + Send + Sync + PartialEq + Debug + 'static> Container for
|
||||
let started = drop::started(&cx.env);
|
||||
let mut extra = self.extra.take();
|
||||
let env = crate::runtime::env::Env::of(ui);
|
||||
let rtl = env.direction.is_rtl();
|
||||
let (line, gap) = (env.tokens.size.panel_header, env.tokens.space.gap.item);
|
||||
ui.set_min_height(line);
|
||||
ui.set_min_height(env.tokens.size.panel_header);
|
||||
let surface_pressed = cx.surface_pressed();
|
||||
let layout = if rtl { egui::Layout::right_to_left(egui::Align::Min) } else { egui::Layout::left_to_right(egui::Align::Min) };
|
||||
let mut sent = Vec::new();
|
||||
let strip = crate::runtime::env::flow_scope(ui, egui::UiBuilder::new().layout(layout), |ui| {
|
||||
ui.spacing_mut().item_spacing.x = gap; // design: allow[style-mutation](the header lays its title cells with an egui layout and sets that layout's gap)
|
||||
let mut strip = None;
|
||||
let _ = Row::new().fill_width().gap(Gap::Item).show(ui, |ui, _| {
|
||||
if let Some(hook) = extra.as_mut() {
|
||||
hook.run(ui, cx);
|
||||
}
|
||||
@@ -526,9 +524,8 @@ impl<P: Clone + Hash + Send + Sync + PartialEq + Debug + 'static> Container for
|
||||
};
|
||||
// The env around the tab row holds only values its key follows (the shell, the dock's arrangement, a
|
||||
// floating window's layer), no handle or context rebuilt each pass, so it replays at rest.
|
||||
MemoView::new(("tabs", cx.id), row, tab_row::<P>).show(ui)
|
||||
})
|
||||
.inner;
|
||||
strip = MemoView::new(("tabs", cx.id), row, tab_row::<P>).show(ui);
|
||||
});
|
||||
self.extra = extra;
|
||||
// A press on the group's empty body focuses its panel, as in Photoshop.
|
||||
if surface_pressed && let Some(strip) = strip {
|
||||
|
||||
@@ -214,8 +214,9 @@ layout_component!(
|
||||
/// Children row-major over a column list: a child in flow takes the next cell.
|
||||
///
|
||||
/// The cells are the grid's craft children. An egui container shown in the body (`ui.horizontal`, an
|
||||
/// `egui::Grid`) is no cell: egui places it below the cells placed so far, its children are not cells, and the
|
||||
/// next child takes the next cell. Content that should take a cell goes in a craft body (`Row`).
|
||||
/// `egui::Grid`) is no cell: egui places it below the cells placed so far, and its children are not cells. The
|
||||
/// rows after it are placed over it, since the grid's rows do not count it: show it as the last child, or put it
|
||||
/// in a craft body (`Row`), which takes a cell.
|
||||
Grid,
|
||||
GridStyleKey,
|
||||
[]
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
config_version = 1
|
||||
|
||||
[[exception]]
|
||||
name = "dock-header-gap"
|
||||
kind = "marker"
|
||||
rule = "style-mutation"
|
||||
path = "crates/ui-panels/src/docking/group.rs"
|
||||
reason = "The header lays its title cells with an egui layout and sets that layout gap."
|
||||
count = 1
|
||||
|
||||
[[exception]]
|
||||
name = "iconic-story-stage"
|
||||
kind = "marker"
|
||||
|
||||
Reference in new issue
Block a user