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

This commit is contained in:
aiongg
2026-11-16 13:22:31 +00:00
parent 3af946a321
commit 404e03bc63
5 changed files with 47 additions and 27 deletions
+7 -3
View File
@@ -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<String>,
/// Whether a numeric literal appears.
pub has_literal: bool,
@@ -140,9 +141,12 @@ fn args_of<'a>(args: impl IntoIterator<Item = &'a Expr>) -> 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
+8
View File
@@ -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"));
}
+3 -14
View File
@@ -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<Arc<[u8]>>; 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<Arc<[u8]>>; 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.
+12 -10
View File
@@ -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<CaptionIcons> {
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<CaptionIcons>)>(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<CaptionIcons>)>| {
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
})
}
+17
View File
@@ -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<const N: usize>(theme: &str, icons: [&str; N]) -> [Option<Vec<u8>>; 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"];