From 404e03bc63f963e063eb46d77d5c52f3c63872fe Mon Sep 17 00:00:00 2001 From: aiongg Date: Mon, 16 Nov 2026 13:22:31 +0000 Subject: [PATCH] design-lint: contract/platform, contract/memory: the freedesktop icon lookup lives in craft-window (theme_svgs), so caption icons need no target cfg; desktop icons kept in a session slot; the memory rule's scope_builder test reads the builder argument, not the closure body --- crates/design-lint/src/contract/sites.rs | 10 +++++++--- crates/design-lint/tests/egui_rules.rs | 8 ++++++++ crates/ui-shell/src/shell/caption_icons.rs | 17 +++-------------- crates/ui-shell/src/shell/title_bar/bar.rs | 22 ++++++++++++---------- crates/window/src/desktop.rs | 17 +++++++++++++++++ 5 files changed, 47 insertions(+), 27 deletions(-) diff --git a/crates/design-lint/src/contract/sites.rs b/crates/design-lint/src/contract/sites.rs index aab348a23..e2684c8a2 100644 --- a/crates/design-lint/src/contract/sites.rs +++ b/crates/design-lint/src/contract/sites.rs @@ -18,7 +18,8 @@ pub(crate) const EGUI_ROOTS: [&str; 7] = ["egui", "epaint", "emath", "ecolor", " #[derive(Debug, Default)] pub(crate) struct Args { pub count: usize, - /// Every identifier inside the arguments, nested ones included. + /// Every identifier inside the arguments, nested ones included, except inside a closure argument: its body is + /// read as sites of its own, so a call there does not count as an argument of this one. pub idents: Vec, /// Whether a numeric literal appears. pub has_literal: bool, @@ -140,9 +141,12 @@ fn args_of<'a>(args: impl IntoIterator) -> Args { let mut out = Args::default(); for e in args { out.count += 1; - out.has_closure |= matches!(e, Expr::Closure(_)); + let closure = matches!(e, Expr::Closure(_)); + out.has_closure |= closure; let tokens = e.to_token_stream(); - summarize(tokens.clone(), &mut out.idents); + if !closure { + summarize(tokens.clone(), &mut out.idents); + } out.has_literal |= has_number(tokens); } out diff --git a/crates/design-lint/tests/egui_rules.rs b/crates/design-lint/tests/egui_rules.rs index d4c1d9f2a..5b6bc5fe2 100644 --- a/crates/design-lint/tests/egui_rules.rs +++ b/crates/design-lint/tests/egui_rules.rs @@ -103,3 +103,11 @@ fn family_modules_neither_import_runtime_egui_nor_build_egui_text() { let shell = analyze_contract(&src, Path::new("crates/x/src/shell/app.rs"), &layered, false); assert!(shell.iter().all(|f| f.rule != "contract/runtime-egui"), "layer 7 hosts the adapter"); } + +#[test] +fn a_salt_inside_a_scope_closure_is_not_the_scope_builders() { + let inner = "fn f(ui: &mut Ui) {\n ui.scope_builder(UiBuilder::new().max_rect(r), |ui| Button::new(\"a\").id_salt(\"b\").show(ui));\n}\n"; + assert!(!rules_at(inner, "src/ui/a.rs", &scope()).contains(&"contract/memory")); + let salted = "fn f(ui: &mut Ui) {\n ui.scope_builder(UiBuilder::new().id_salt(\"b\"), |ui| ());\n}\n"; + assert!(rules_at(salted, "src/ui/a.rs", &scope()).contains(&"contract/memory")); +} diff --git a/crates/ui-shell/src/shell/caption_icons.rs b/crates/ui-shell/src/shell/caption_icons.rs index e77ed4b57..2d3042ed4 100644 --- a/crates/ui-shell/src/shell/caption_icons.rs +++ b/crates/ui-shell/src/shell/caption_icons.rs @@ -34,7 +34,6 @@ impl CaptionIcon { } /// The icon name the freedesktop themes give the glyph. - #[cfg_attr(not(target_os = "linux"), allow(dead_code))] fn theme_name(self) -> &'static str { match self { CaptionIcon::Minimize => "window-minimize-symbolic", @@ -121,20 +120,10 @@ impl std::fmt::Debug for CaptionIcons { } } -/// The four glyph SVGs of icon theme `theme` (with its parents and `hicolor`), in [`CaptionIcon::ALL`] order. -#[cfg(target_os = "linux")] +/// The four glyph SVGs of icon theme `theme` (with its parents and `hicolor`), in [`CaptionIcon::ALL`] order; none +/// where the platform has no icon themes. fn theme_files(theme: &str) -> [Option>; 4] { - use craft_window::desktop::{IconTheme, base_dirs, read_small}; - let Some(found) = IconTheme::load(theme, &base_dirs(&|key: &str| std::env::var(key).ok())) else { - return Default::default(); - }; - CaptionIcon::ALL.map(|icon| found.find_svg(icon.theme_name()).and_then(|path| read_small(&path)).map(Arc::<[u8]>::from)) -} - -/// The freedesktop lookup is Linux's; elsewhere every glyph is the family's own. -#[cfg(not(target_os = "linux"))] -fn theme_files(_theme: &str) -> [Option>; 4] { - Default::default() + craft_window::desktop::theme_svgs(theme, CaptionIcon::ALL.map(CaptionIcon::theme_name)).map(|file| file.map(Arc::<[u8]>::from)) } /// Bytes drawing one caption icon may allocate, its target included. diff --git a/crates/ui-shell/src/shell/title_bar/bar.rs b/crates/ui-shell/src/shell/title_bar/bar.rs index fc4206c89..a805409b9 100644 --- a/crates/ui-shell/src/shell/title_bar/bar.rs +++ b/crates/ui-shell/src/shell/title_bar/bar.rs @@ -15,6 +15,7 @@ use crate::primitives::{RectSide, RuleKind}; use crate::runtime::access::Name; use crate::runtime::egui::pass; use crate::runtime::env::Env; +use crate::runtime::memory; use crate::runtime::windows::{self, WindowAction, WindowHost}; use crate::shell::app_shell::RegionBody; use crate::shell::{Caption, CaptionButton, CaptionIcons, Frame, ShellWords, SlotSize}; @@ -287,14 +288,15 @@ impl Container for TitleBar<'_> { /// `CaptionIcons::for_desktop` once per desktop value and kept for the app's bars while the desktop stays the same. fn desktop_icons(ctx: &egui::Context) -> Option { let desktop = WindowHost::of(ctx).desktop()?; - let key = egui::Id::unique("craft_ui.title_bar.desktop_icons"); - let kept = ctx.data(|data| data.get_temp::<(Desktop, Option)>(key)); - if let Some((seen, icons)) = &kept - && *seen == desktop - { - return icons.clone(); - } - let icons = CaptionIcons::for_desktop(&desktop, kept.and_then(|(_, icons)| icons)); - ctx.data_mut(|data| data.insert_temp(key, (desktop, icons.clone()))); - icons + let key = memory::unique("craft_ui.title_bar.desktop_icons"); + memory::with_session(ctx, key, |kept: &mut Option<(Desktop, Option)>| { + if let Some((seen, icons)) = kept.as_ref() + && *seen == desktop + { + return icons.clone(); + } + let icons = CaptionIcons::for_desktop(&desktop, kept.take().and_then(|(_, icons)| icons)); + *kept = Some((desktop, icons.clone())); + icons + }) } diff --git a/crates/window/src/desktop.rs b/crates/window/src/desktop.rs index f69840a16..599cd3772 100644 --- a/crates/window/src/desktop.rs +++ b/crates/window/src/desktop.rs @@ -50,6 +50,23 @@ impl Desktop { } } +/// The SVG file of each freedesktop icon name in `icons`, looked up in icon theme `theme` with its parents and +/// `hicolor` and read only when small (`read_small`). Icon themes are Linux's: elsewhere every entry is `None`. +pub fn theme_svgs(theme: &str, icons: [&str; N]) -> [Option>; N] { + #[cfg(target_os = "linux")] + { + let Some(found) = IconTheme::load(theme, &base_dirs(&|key: &str| std::env::var(key).ok())) else { + return [const { None }; N]; + }; + icons.map(|icon| found.find_svg(icon).and_then(|path| read_small(&path))) + } + #[cfg(not(target_os = "linux"))] + { + let _ = (theme, icons); + [const { None }; N] + } +} + #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd", target_os = "netbsd", test))] /// Compositors and window managers that tile, matched against the `XDG_CURRENT_DESKTOP` entries. const TILING: &[&str] = &["sway", "hyprland", "i3", "niri", "river", "bspwm", "awesome", "dwl", "qtile", "herbstluftwm", "leftwm", "xmonad", "dwm"];