From a0f1e1358697188a4ece97114009ad8eb577eab2 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sat, 28 Nov 2026 18:39:11 +0000 Subject: [PATCH] ui-contract: clippy clean (dead split_runs, named cut type, trial lint scopes, icon trial split by encoding) Co-Authored-By: Claude Opus 5.5 --- .../ui-contract/src/primitives/flag_mark.rs | 9 ++- crates/ui-contract/src/primitives/svg_tree.rs | 5 -- crates/ui-contract/src/primitives/text.rs | 13 ++-- .../src/testing/host_key_trials.rs | 2 + crates/ui-contract/src/testing/icon_trials.rs | 61 ++++++++++++------- 5 files changed, 55 insertions(+), 35 deletions(-) diff --git a/crates/ui-contract/src/primitives/flag_mark.rs b/crates/ui-contract/src/primitives/flag_mark.rs index 9a3a0e71f3..41532b2754 100644 --- a/crates/ui-contract/src/primitives/flag_mark.rs +++ b/crates/ui-contract/src/primitives/flag_mark.rs @@ -80,11 +80,18 @@ pub fn paint_galley_flags( #[cfg(all(feature = "testing", any(test, feature = "suite")))] pub(crate) mod trials { - use super::*; + #![allow( + clippy::expect_used, + clippy::panic, + reason = "trial code: clippy.toml allows expect and panic in tests, and trials are compiled into the library" + )] + + use super::{FlagMark, paint_flag_mark}; use crate::primitives::{Draw, DrawEnv, PaintRecord, Span, TextLayout, TextRun, Wrap}; use crate::style::Ink; use crate::testing::{Frames, trial}; use craft_theme::{Contrast, Mode, variant::Tone}; + use egui::{Color32, Pos2, Shape, Stroke, vec2}; use std::cell::RefCell; #[trial] diff --git a/crates/ui-contract/src/primitives/svg_tree.rs b/crates/ui-contract/src/primitives/svg_tree.rs index a4e0891d7c..0b492c1ddf 100644 --- a/crates/ui-contract/src/primitives/svg_tree.rs +++ b/crates/ui-contract/src/primitives/svg_tree.rs @@ -38,11 +38,6 @@ pub(crate) fn icon_text(svg: &[u8]) -> Result { Ok(text.to_owned()) } -/// Splits consecutive non-shared top-level parts of equal class into complete SVG documents. -pub(crate) fn split_runs(svg: &[u8]) -> Result, String> { - Ok(classified_runs(svg)?.into_iter().map(|(_, document)| document).collect()) -} - /// Splits complete run documents while retaining the class chosen during grouping. pub(super) fn classified_runs(svg: &[u8]) -> Result, String> { let text = icon_text(svg)?; diff --git a/crates/ui-contract/src/primitives/text.rs b/crates/ui-contract/src/primitives/text.rs index e25af10e83..6e96632ae0 100644 --- a/crates/ui-contract/src/primitives/text.rs +++ b/crates/ui-contract/src/primitives/text.rs @@ -658,7 +658,7 @@ impl TextRun<'_> { /// The run on one row, cut where its wrap says (the end for `Elide`) with one ellipsis, keeping the most characters /// that fit `max_width`; whole when it fits. - fn layout_cut(&self, ctx: &Context, env: &TextEnv<'_>, full: &str, max_width: f32) -> (Arc, Vec<(Range, Tone)>) { + fn layout_cut(&self, ctx: &Context, env: &TextEnv<'_>, full: &str, max_width: f32) -> Cut { let (job, flags) = self.job(env, self.spans.iter().map(|s| (s, s.text.as_ref(), true)), f32::INFINITY); let whole = text_layout::layout(ctx, job); if whole.size().x <= max_width { @@ -686,13 +686,7 @@ impl TextRun<'_> { /// The cut of `kept` (bytes of `full`) at `wrap`'s place that keeps the most characters within `max_width`, found /// by bisection over the character count; at least the ellipsis alone. - fn fit( - layout: &dyn Fn(&[Piece]) -> (Arc, Vec<(Range, Tone)>), - full: &str, - kept: Range, - wrap: Wrap, - max_width: f32, - ) -> (Arc, Vec<(Range, Tone)>) { + fn fit(layout: &dyn Fn(&[Piece]) -> Cut, full: &str, kept: Range, wrap: Wrap, max_width: f32) -> Cut { let text = full.get(kept.clone()).unwrap_or_default(); let ends: Vec = text.char_indices().map(|(at, _)| kept.start + at).chain([kept.end]).collect(); let total = ends.len() - 1; @@ -747,6 +741,9 @@ const ELLIPSIS: &str = "\u{2026}"; /// The ellipsis followed by a right-to-left mark. const RTL_ELLIPSIS: &str = "\u{2026}\u{200f}"; +/// A laid-out cut of a text and the tone of each of its ranges. +type Cut = (Arc, Vec<(Range, Tone)>); + /// A piece of a cut run: a byte range of its text kept, or the ellipsis. enum Piece { Keep(Range), diff --git a/crates/ui-contract/src/testing/host_key_trials.rs b/crates/ui-contract/src/testing/host_key_trials.rs index dba67a90d5..00a0d8dddd 100644 --- a/crates/ui-contract/src/testing/host_key_trials.rs +++ b/crates/ui-contract/src/testing/host_key_trials.rs @@ -1,5 +1,7 @@ //! Trials of the keys and presses a host's own egui widgets keep while the runtime is installed on the whole window. +#![allow(clippy::expect_used, reason = "trial code: clippy.toml allows expect in tests, and trials are compiled into the library")] + use crate::contract::{Common, Component, Container, ContainerProps, Cx, Mount, Overlay, Props as _, Role, ShowContainer as _}; use crate::runtime::commands::{ AppCatalog, Binding, CommandDef, CommandInput, CommandSet as _, EditCommands, NavCommand, QuitCommand, Scope, TypingRule, diff --git a/crates/ui-contract/src/testing/icon_trials.rs b/crates/ui-contract/src/testing/icon_trials.rs index 318ff308c3..8eecd1faf9 100644 --- a/crates/ui-contract/src/testing/icon_trials.rs +++ b/crates/ui-contract/src/testing/icon_trials.rs @@ -241,27 +241,7 @@ fn an_app_icons_parts_paint_in_document_order_and_on_and_off_parts_only_in_their let mut frames = Frames::new(); frames.record_paint(); let uploads = RefCell::new(Vec::new()); - let mut paint = |name: &'static str, mode| { - let spec = IconSpec::svg(IconId::from(std::borrow::Cow::Borrowed(name))).color(StyleColor(ink)); - let record = RefCell::new(PaintRecord::default()); - frames.frame(Vec::new(), |ui| { - let env = DrawEnv { tokens: &tokens, direction: Direction::Ltr, ink: Ink::OnPanel, mark: FlagMark::default() }; - Draw::new(ui.painter(), &record, env).data(mode).icon(Rect::from_min_size(pos2(10.0, 10.0), vec2(24.0, 24.0)), &spec); - }); - let Some(output) = frames.take_paint() else { return Vec::new() }; - let shapes = output.shapes.iter().map(|clipped| clipped.shape.clone()).collect::>(); - let quads = textured(&shapes); - for (texture, _) in &quads { - if let Some(deltas) = output.textures_delta.set.get(texture) { - for delta in deltas { - let egui::ImageData::Color(image) = &delta.image; - uploads.borrow_mut().push((*texture, std::sync::Arc::clone(image))); - } - } - } - output.drop_without_applying_deltas(); - quads - }; + let mut paint = |name: &'static str, mode| icon_quads(&mut frames, &tokens, ink, name, mode, &uploads); let enabled = paint(ORDERED_NAME, DataMode::Full); // design: allow[raw-colour/const](a white tint preserves the fixed square's colour) assert_eq!(enabled.iter().map(|(_, tint)| *tint).collect::>(), [ink, Color32::WHITE, ink]); @@ -306,8 +286,17 @@ fn an_app_icons_parts_paint_in_document_order_and_on_and_off_parts_only_in_their } assert!(texel(&silhouette, 2, 18).a() == 0, "rotation moves the horizontal rectangle"); assert!(texel(&silhouette, 10, 16).a() < texel(&silhouette, 13, 19).a(), "rounded rectangles trim the corner"); +} +#[trial] +fn encoded_app_icons_paint_through_draw_and_invalid_siblings_stay_out_of_the_index() { // Encoding conversion must serve both split documents and the stroke-scaled original silhouette. + let tokens = style::tokens(Mode::Light, Contrast::Standard); + let ink = ink_color(&tokens, Ink::OnPanel); + let mut frames = Frames::new(); + frames.record_paint(); + let uploads = RefCell::new(Vec::new()); + let mut paint = |name: &'static str, mode| icon_quads(&mut frames, &tokens, ink, name, mode, &uploads); const UTF16_NAME: &str = concat!("zz_w1_", "utf16"); const LATIN1_NAME: &str = concat!("zz_w1_", "latin1"); const MIXED_NAME: &str = concat!("zz_w1_", "mixed"); @@ -354,6 +343,36 @@ fn an_app_icons_parts_paint_in_document_order_and_on_and_off_parts_only_in_their assert_eq!(mask(UTF16_NAME), mask(LATIN1_NAME), "encodings preserve the same geometry"); } +/// The textured quads `name` paints in `mode` under `ink`, first to last; the images they upload go to `uploads`. +fn icon_quads( + frames: &mut Frames, + tokens: &UiTokens, + ink: Color32, + name: &'static str, + mode: DataMode, + uploads: &RefCell)>>, +) -> Vec<(TextureId, Color32)> { + let spec = IconSpec::svg(IconId::from(std::borrow::Cow::Borrowed(name))).color(StyleColor(ink)); + let record = RefCell::new(PaintRecord::default()); + frames.frame(Vec::new(), |ui| { + let env = DrawEnv { tokens, direction: Direction::Ltr, ink: Ink::OnPanel, mark: FlagMark::default() }; + Draw::new(ui.painter(), &record, env).data(mode).icon(Rect::from_min_size(pos2(10.0, 10.0), vec2(24.0, 24.0)), &spec); + }); + let Some(output) = frames.take_paint() else { return Vec::new() }; + let shapes = output.shapes.iter().map(|clipped| clipped.shape.clone()).collect::>(); + let quads = textured(&shapes); + for (texture, _) in &quads { + if let Some(deltas) = output.textures_delta.set.get(texture) { + for delta in deltas { + let egui::ImageData::Color(image) = &delta.image; + uploads.borrow_mut().push((*texture, std::sync::Arc::clone(image))); + } + } + } + output.drop_without_applying_deltas(); + quads +} + /// The tints of the textured shapes `name` paints under the tint `color`, first to last. fn painted(name: &'static str, color: Color32) -> Vec { let Some(svg) = icon(name) else { return Vec::new() };