From f812df100a18e60ab5824443be392822f487decc Mon Sep 17 00:00:00 2001 From: bflatastic Date: Sun, 22 Nov 2026 22:36:07 +0700 Subject: [PATCH] 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 --- crates/ui-contract/src/contract/cx/item.rs | 7 +++++++ .../ui-contract/src/contract/items/hooks.rs | 21 ++++++++----------- crates/ui-contract/src/contract/lazy.rs | 2 +- crates/ui-contract/src/contract/run.rs | 6 +++--- crates/ui-contract/src/layout/body.rs | 10 ++++----- crates/ui-contract/src/primitives/face.rs | 4 +++- .../src/primitives/text_adapter.rs | 2 +- .../ui-contract/src/primitives/text_input.rs | 5 ++++- .../ui-contract/tests/it/runtime/fingers.rs | 6 ++---- .../ui-contract/tests/it/runtime/windows.rs | 4 ++-- 10 files changed, 36 insertions(+), 31 deletions(-) diff --git a/crates/ui-contract/src/contract/cx/item.rs b/crates/ui-contract/src/contract/cx/item.rs index d4f585a68..4c8d96800 100644 --- a/crates/ui-contract/src/contract/cx/item.rs +++ b/crates/ui-contract/src/contract/cx/item.rs @@ -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> { + 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) -> ItemOpener { diff --git a/crates/ui-contract/src/contract/items/hooks.rs b/crates/ui-contract/src/contract/items/hooks.rs index 5b21e322c..53687a19c 100644 --- a/crates/ui-contract/src/contract/items/hooks.rs +++ b/crates/ui-contract/src/contract/items/hooks.rs @@ -477,12 +477,7 @@ impl ShapeHooks 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 ShapeHooks 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 ShapeHooks 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 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 { + 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 { diff --git a/crates/ui-contract/src/contract/lazy.rs b/crates/ui-contract/src/contract/lazy.rs index 2ebc31d76..32cc51745 100644 --- a/crates/ui-contract/src/contract/lazy.rs +++ b/crates/ui-contract/src/contract/lazy.rs @@ -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, 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), _ => {} } diff --git a/crates/ui-contract/src/contract/run.rs b/crates/ui-contract/src/contract/run.rs index b57669e22..3cd4a61e5 100644 --- a/crates/ui-contract/src/contract/run.rs +++ b/crates/ui-contract/src/contract/run.rs @@ -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. diff --git a/crates/ui-contract/src/layout/body.rs b/crates/ui-contract/src/layout/body.rs index 8ff048404..f72f08328 100644 --- a/crates/ui-contract/src/layout/body.rs +++ b/crates/ui-contract/src/layout/body.rs @@ -430,7 +430,7 @@ pub(crate) struct ShrinkReports(IdMap>); pub(crate) struct LeastReports(IdMap>); /// 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, + (cut_first, rank): (bool, Option), ) { 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], + (first, ranks): (&[bool], &[Option]), (fixed, gap): (f32, f32), room: f32, order: ShrinkOrder, @@ -824,7 +822,7 @@ fn run_shrinking(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() }; diff --git a/crates/ui-contract/src/primitives/face.rs b/crates/ui-contract/src/primitives/face.rs index 8dbd08097..6a68d0d31 100644 --- a/crates/ui-contract/src/primitives/face.rs +++ b/crates/ui-contract/src/primitives/face.rs @@ -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, Rect) + Send + Sync>, + paint: Arc, } impl FacePaint { diff --git a/crates/ui-contract/src/primitives/text_adapter.rs b/crates/ui-contract/src/primitives/text_adapter.rs index 78c1ac256..1d16a065f 100644 --- a/crates/ui-contract/src/primitives/text_adapter.rs +++ b/crates/ui-contract/src/primitives/text_adapter.rs @@ -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'], " ") }; diff --git a/crates/ui-contract/src/primitives/text_input.rs b/crates/ui-contract/src/primitives/text_input.rs index 9562a8a61..c7069ea7c 100644 --- a/crates/ui-contract/src/primitives/text_input.rs +++ b/crates/ui-contract/src/primitives/text_input.rs @@ -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 bool>>, + pub whole_paste: Option>, } impl Default for TextOptions { diff --git a/crates/ui-contract/tests/it/runtime/fingers.rs b/crates/ui-contract/tests/it/runtime/fingers.rs index 6b30b2953..a2b0c70bd 100644 --- a/crates/ui-contract/tests/it/runtime/fingers.rs +++ b/crates/ui-contract/tests/it/runtime/fingers.rs @@ -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] diff --git a/crates/ui-contract/tests/it/runtime/windows.rs b/crates/ui-contract/tests/it/runtime/windows.rs index 1164e15b6..21dc816d9 100644 --- a/crates/ui-contract/tests/it/runtime/windows.rs +++ b/crates/ui-contract/tests/it/runtime/windows.rs @@ -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"); }