mirror of
https://github.com/storytold/craft-libs.git
synced 2026-12-01 02:36:54 +01:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
d1c5c2ebca
commit
a0f1e13586
5 files changed
+55
-35
No files matched your search
@@ -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]
|
||||
|
||||
@@ -38,11 +38,6 @@ pub(crate) fn icon_text(svg: &[u8]) -> Result<String, String> {
|
||||
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<Vec<String>, 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<Vec<(Class, String)>, String> {
|
||||
let text = icon_text(svg)?;
|
||||
|
||||
@@ -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<Galley>, Vec<(Range<usize>, 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<Galley>, Vec<(Range<usize>, Tone)>),
|
||||
full: &str,
|
||||
kept: Range<usize>,
|
||||
wrap: Wrap,
|
||||
max_width: f32,
|
||||
) -> (Arc<Galley>, Vec<(Range<usize>, Tone)>) {
|
||||
fn fit(layout: &dyn Fn(&[Piece]) -> Cut, full: &str, kept: Range<usize>, wrap: Wrap, max_width: f32) -> Cut {
|
||||
let text = full.get(kept.clone()).unwrap_or_default();
|
||||
let ends: Vec<usize> = 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<Galley>, Vec<(Range<usize>, Tone)>);
|
||||
|
||||
/// A piece of a cut run: a byte range of its text kept, or the ellipsis.
|
||||
enum Piece {
|
||||
Keep(Range<usize>),
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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::<Vec<_>>();
|
||||
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::<Vec<_>>(), [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, std::sync::Arc<ColorImage>)>>,
|
||||
) -> 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::<Vec<_>>();
|
||||
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<Color32> {
|
||||
let Some(svg) = icon(name) else { return Vec::new() };
|
||||
|
||||
Reference in new issue
Block a user