ui-contract: clippy 1.100 clean: a row unit reports how it leaves as one (cut_first, rank) pair; the items' place_rows takes the detail through ItemCx::take_detail and a cell_width helper; face and whole-paste types named; tests return directly and take the last visibility by reduce

This commit is contained in:
bflatastic
2026-11-22 22:36:07 +07:00
parent 1222194118
commit f812df100a
10 changed files with 36 additions and 31 deletions
@@ -92,6 +92,13 @@ impl<'r, K> ItemCx<'_, 'r, K> {
self.detail = Some(Box::new(body));
}
/// The detail body the item gave, for a row that takes it: one of auto height (`auto`).
pub(crate) fn take_detail(&mut self, auto: bool) -> Option<ItemTip<'r>> {
let detail = self.detail.take();
debug_assert!(detail.is_none() || auto, "an item's detail needs an auto-height row; a grid and a fixed-height row ignore it");
detail.filter(|_| auto)
}
/// The opener for the item's part `part`: after the collection is shown, `CollectionResponse::opener` gives the
/// part's response (its click this pass and its rect), and a layer-5 menu opens from it with `opened_by`.
pub fn part_opener(&mut self, part: impl Into<craft_behavior::PartRef>) -> ItemOpener {
+9 -12
View File
@@ -477,12 +477,7 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
let item = (self.item)(&key, &mut icx);
let mut item = comp.dress(item, &state);
let tip = icx.tip.take();
let detail = icx.detail.take();
debug_assert!(
detail.is_none() || layout.is_auto(at),
"an item's detail needs an auto-height row; a grid and a fixed-height row ignore it"
);
let detail = detail.filter(|_| layout.is_auto(at));
let detail = icx.take_detail(layout.is_auto(at));
let openers = std::mem::take(&mut icx.openers);
drop(icx);
item = Props::id(item, item_id);
@@ -555,10 +550,7 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
}
}
if let Some(column) = &fit {
let (_, cells) = crate::runtime::measure::cell_widths(&ctx, || item.measure_in(measure_ui, Proposal::Ideal));
if let Some((_, width)) = cells.iter().find(|(held, _)| held == column) {
fit_widest = fit_widest.max(*width);
}
fit_widest = fit_widest.max(cell_width(&ctx, &mut item, measure_ui, column).unwrap_or_default());
}
}
let leaf = leaf_height.map_or(row, |height| Rect::from_min_size(row.min, Vec2::new(row.width(), height)));
@@ -566,8 +558,7 @@ impl<C: Collection> ShapeHooks<C> for ItemsHooks<'_, '_, C> {
let row_node = if C::MEMBERS { super::members::lead_node(id, &key) } else { item_id };
shown_rows.push(ShownRow::new(key.clone(), row_node, leaf, shown_cells.clone()).with_hover(state.hovered));
let Some((mut row_ui, installed)) = row_ui else {
debug_assert!(tip.is_none(), "a member of a collection's run takes no rich tooltip");
debug_assert!(detail.is_none(), "a member of a collection's run takes no detail body");
debug_assert!(tip.is_none() && detail.is_none(), "a member of a collection's run takes no rich tooltip and no detail body");
let shown = shown_rows.len() - 1;
members.push(Member { at, key, item, row, clip, flags, position, renaming: state.renaming, openers, shown });
continue;
@@ -864,6 +855,12 @@ impl<C: Collection> ItemsHooks<'_, '_, C> {
}
}
/// The width `item` gives its cell in `column` at its ideal width in `ui`; `None` when it has no cell there.
fn cell_width(ctx: &Context, item: &mut impl crate::contract::Show, ui: &Ui, column: &craft_behavior::ColumnKey) -> Option<f32> {
let (_, cells) = crate::runtime::measure::cell_widths(ctx, || item.measure_in(ui, Proposal::Ideal));
cells.into_iter().find(|(held, _)| held == column).map(|(_, width)| width)
}
/// Apply the collection's wheel displacement, the page and end moves a descendant left untaken and any reveal left by
/// a descendant.
fn scroll_rows(core: &mut CxCore<'_>, rect: Rect, scrolls: bool, mut offset: f64, maximum: f64) -> f64 {
+1 -1
View File
@@ -295,7 +295,7 @@ fn at_most(region: Rect, rtl: bool, width: f32) -> Rect {
/// from what it takes in no room down to what it takes with every leaf at its least.
fn report_unit(ctx: &egui::Context, shrink: Option<Shrink>, id: egui::Id, size: Vec2) {
match shrink {
Some(Shrink::Count(row)) => crate::layout::report_shrink(ctx, row, id, size.x, size.x, None, false, None),
Some(Shrink::Count(row)) => crate::layout::report_shrink(ctx, row, id, size.x, size.x, None, (false, None)),
Some(Shrink::Least(row)) => crate::layout::report_least(ctx, row, id, size.x),
_ => {}
}
+3 -3
View File
@@ -1749,9 +1749,9 @@ fn measure_step(
let ideal = steps::measure(style, &input, content, Vec2::ZERO, resolved).x;
let whole = steps::whole(style, content, Vec2::ZERO, resolved);
let least = ask(&mut *t, Proposal::Min, None, Vec2::ZERO).0.x;
crate::layout::report_shrink(ctx, row, id, size.x, ideal, Some((whole, least)), t.cut_first(), None);
crate::layout::report_shrink(ctx, row, id, size.x, ideal, Some((whole, least)), (t.cut_first(), None));
}
Some(crate::layout::Shrink::Count(row)) => crate::layout::report_shrink(ctx, row, id, size.x, size.x, None, false, t.fit_rank()),
Some(crate::layout::Shrink::Count(row)) => crate::layout::report_shrink(ctx, row, id, size.x, size.x, None, (false, t.fit_rank())),
Some(crate::layout::Shrink::Least(row)) if shape == ShapeKind::Body => crate::layout::report_least(ctx, row, id, size.x),
_ => {}
}
@@ -1759,7 +1759,7 @@ fn measure_step(
// the units narrow to leave it, and it takes what they leave.
if let Some(crate::layout::Shrink::Count(row)) = env.shrink.filter(|_| fill_body) {
let content = measured.x + t.overflow();
crate::layout::report_shrink(ctx, row, id, size.x.min(content), content, Some((content, content)), false, t.fit_rank());
crate::layout::report_shrink(ctx, row, id, size.x.min(content), content, Some((content, content)), (false, t.fit_rank()));
}
// A component shown at a rect fills it: the rect is its size, and a leaf that measures nothing (a content
// painter) still shows there.
+4 -6
View File
@@ -430,7 +430,7 @@ pub(crate) struct ShrinkReports(IdMap<Vec<Reported>>);
pub(crate) struct LeastReports(IdMap<IdMap<f32>>);
/// Reports the widths of the unit `leaf` of the row `row` that counts them: a leaf's whole and least widths, or `None`
/// for a body.
/// for a body, and how it leaves a row too narrow for it: whether its content is made to be cut, and its rank.
pub(crate) fn report_shrink(
ctx: &egui::Context,
row: Id,
@@ -438,8 +438,7 @@ pub(crate) fn report_shrink(
took: f32,
ideal: f32,
whole_least: Option<(f32, f32)>,
cut_first: bool,
rank: Option<u8>,
(cut_first, rank): (bool, Option<u8>),
) {
let (whole, least) = whole_least.map_or((ideal, ideal), |(whole, least)| (whole.min(ideal), least.min(ideal)));
let widths = ShrinkUnit { ideal, least: least.min(whole) };
@@ -708,8 +707,7 @@ fn shrink_in_turns(units: &[ShrinkUnit], whole: &[f32], first: &[bool], fixed: f
fn shrink_or_drop(
units: &[ShrinkUnit],
whole: &[f32],
first: &[bool],
ranks: &[Option<u8>],
(first, ranks): (&[bool], &[Option<u8>]),
(fixed, gap): (f32, f32),
room: f32,
order: ShrinkOrder,
@@ -824,7 +822,7 @@ fn run_shrinking<R>(ui: &mut Ui, id: Id, mut body: impl FnMut(&mut Ui) -> R) ->
let width_at = |leaf: &Reported, level: usize| leaf.levels.get(level).copied().unwrap_or_default();
let held: f32 = levelled.iter().zip(&levels).map(|(leaf, level)| width_at(leaf, *level)).sum();
let widths = if room.is_finite() {
shrink_or_drop(&units, &whole, &first, &ranks, (fixed + held, gap), room, order, drops)
shrink_or_drop(&units, &whole, (&first, &ranks), (fixed + held, gap), room, order, drops)
} else {
units.iter().map(|unit| ShrinkCap { width: unit.ideal, dropped: false, narrowed: false }).collect()
};
+3 -1
View File
@@ -6,13 +6,15 @@ use std::fmt;
use std::hash::{Hash, Hasher};
use std::sync::Arc;
type Paint = dyn Fn(&ContentPainterCx<'_>, Rect) + Send + Sync;
/// The one host painter a value carries: two faces with one key are the same value.
#[derive(Clone, crate::contract::MemoProps, craft_ui_macros::PlainData)]
pub struct FacePaint {
key: u64,
#[memo_props(covered_by = "key", reason = "the key names what the painter paints")]
#[plain_data(assume = "a face is replaced, never written: its painter is shared")]
paint: Arc<dyn Fn(&ContentPainterCx<'_>, Rect) + Send + Sync>,
paint: Arc<Paint>,
}
impl FacePaint {
@@ -375,7 +375,7 @@ fn apply(input: &mut AdapterIn<'_>, state: &mut TextState, text: &mut String, wi
}
EditCommand::Paste => {
if let Some(pasted) = keys::pasted_text(ctx, input.id) {
if input.options.whole_paste.as_ref().is_some_and(|whole| whole(&*pasted)) {
if input.options.whole_paste.as_ref().is_some_and(|whole| whole(&pasted)) {
replace_selection(ctx, widget, text, (0, text.chars().count()), pasted.trim());
} else {
let pasted = if input.options.multiline() { pasted.to_string() } else { pasted.replace(['\n', '\r'], " ") };
@@ -49,6 +49,9 @@ pub enum TabKey {
Reports,
}
/// Whether a pasted text is whole ([`TextOptions::whole_paste`]).
type WholePaste = dyn Fn(&str) -> bool;
/// How a text part behaves beyond its codec.
#[derive(Clone)]
pub struct TextOptions {
@@ -114,7 +117,7 @@ pub struct TextOptions {
/// Set from the codec by `PartsCx::text`, never by hosts: a pasted text that this says is whole replaces all the
/// text instead of the selection. It gets the paste as copied; the codec trims it.
// PLAN: no `Send + Sync`: `Codec` and `PartsCx::text`'s codec are not `Send`.
pub whole_paste: Option<Arc<dyn Fn(&str) -> bool>>,
pub whole_paste: Option<Arc<WholePaste>>,
}
impl Default for TextOptions {
@@ -91,8 +91,7 @@ fn tapped(fingers: usize, hold: f64, end: TouchPhase, drift: f32) -> Vec<(u8, Po
}
frames.frame(all(end, fingers), &mut show);
frames.idle(2, &mut show);
let taps = taps.borrow().clone();
taps
taps.borrow().clone()
}
#[trial]
@@ -121,8 +120,7 @@ fn tapped_beside_a_modal(opens: bool) -> Vec<(u8, Pos2)> {
frames.idle(1, &mut show);
frames.frame(all(TouchPhase::End, 2), &mut show);
frames.idle(2, &mut show);
let taps = taps.borrow().clone();
taps
taps.borrow().clone()
}
#[trial]
@@ -859,11 +859,11 @@ fn a_palette_hides_while_the_root_shows_a_modal_or_popup_and_returns_when_it_clo
frame(false, false);
}
assert_eq!((0..2).find_map(|_| visibility(&frame(false, false))), None, "no layer: the palette stays as it is");
let hidden = (0..3).filter_map(|_| visibility(&frame(true, false))).last();
let hidden = (0..3).filter_map(|_| visibility(&frame(true, false))).reduce(|_, latest| latest);
assert_eq!(hidden, Some(false), "the palette hides under the modal");
let out = frame(true, true);
assert_eq!(builder_titled(&out, "second").1.visible, Some(false), "a palette opened meanwhile opens hidden");
let back = (0..3).filter_map(|_| visibility(&frame(false, false))).last();
let back = (0..3).filter_map(|_| visibility(&frame(false, false))).reduce(|_, latest| latest);
assert_eq!(back, Some(true), "the palette returns once the modal closed");
}