diff --git a/crates/ui-egui/src/home.rs b/crates/ui-egui/src/home.rs index 6e039b304..64c0b7d90 100644 --- a/crates/ui-egui/src/home.rs +++ b/crates/ui-egui/src/home.rs @@ -860,7 +860,8 @@ fn start_content(app: &mut SolveApp, ui: &mut egui::Ui, action: &mut Option) -> String { } } +/// Kind column for a recent file: Design for `.sc`/legacy, else STEP, STL, … (#9081). +fn recent_kind(app: &SolveApp, path: &str) -> String { + let ext = std::path::Path::new(path).extension().and_then(|e| e.to_str()).unwrap_or(""); + if solidcraft_engine::io::is_design_ext(ext) || ext.eq_ignore_ascii_case("json") { + return app.tr("home.design_workspace").to_string(); + } + if solidcraft_engine::io::is_drawing_path(path) { + return app.tr_label("Drawing").to_string(); + } + let label = match ext.to_ascii_lowercase().as_str() { + "step" | "stp" => "STEP", + "igs" | "iges" => "IGES", + "stl" => "STL", + "3mf" => "3MF", + "obj" => "OBJ", + "glb" | "gltf" => "glTF", + "fcstd" => "FreeCAD", + "3dm" => "Rhino", + other if !other.is_empty() => return other.to_ascii_uppercase(), + _ => return app.tr("home.design_workspace").to_string(), + }; + label.to_string() +} + fn recent_time(app: &SolveApp, path: &str) -> String { let Some(time) = app.home.opened_at.get(path) else { return String::new() }; let now = web_time::SystemTime::now().duration_since(web_time::UNIX_EPOCH).map(|d| d.as_secs()).unwrap_or(*time); @@ -1112,6 +1138,31 @@ mod tests { assert_eq!(abbreviated_path("/disk/design.solidcraft", None), "/disk/design.solidcraft"); } + #[test] + fn recent_kind_names_imports_by_extension() { + let app = crate::test_support::sample_app(); + assert_eq!(recent_kind(&app, "/tmp/plate.sc"), "Design"); + assert_eq!(recent_kind(&app, "/tmp/plate.solidcraft"), "Design"); + assert_eq!(recent_kind(&app, "/tmp/plate.step"), "STEP"); + assert_eq!(recent_kind(&app, "/tmp/plate.STL"), "STL"); + assert_eq!(recent_kind(&app, "/tmp/plate.3mf"), "3MF"); + assert_eq!(recent_kind(&app, "/tmp/plate.obj"), "OBJ"); + } + + #[test] + fn middle_ellipsis_keeps_both_ends_of_a_path() { + let ctx = crate::fonts::test_context(); + let mut shown = String::new(); + crate::test_support::frame(&ctx, egui::RawInput::default(), |ui| { + let font = egui::FontId::monospace(12.0); + let full = "~/Projects/exports/out/plate.step"; + shown = crate::ui_kit::ellipsize_middle(ui, full, font, 80.0); + }); + assert!(shown.contains('…'), "{shown}"); + assert!(shown.ends_with("plate.step") || shown.ends_with(".step"), "{shown}"); + assert!(shown.starts_with('~') || shown.starts_with("~/P"), "{shown}"); + } + #[test] fn recent_times_use_compact_labels_and_calendar_dates() { let app = crate::test_support::sample_app(); diff --git a/crates/ui-egui/src/ui_kit/file_view.rs b/crates/ui-egui/src/ui_kit/file_view.rs index 57fafc3ad..6ed018212 100644 --- a/crates/ui-egui/src/ui_kit/file_view.rs +++ b/crates/ui-egui/src/ui_kit/file_view.rs @@ -1,5 +1,8 @@ //! File/start presentation; callers retain commands, caches and recovery ownership. -use super::{metrics::dimension, text::paint_text}; +use super::{ + metrics::dimension, + text::{paint_text, paint_text_aligned}, +}; use crate::theme::Tokens; use egui::{Align2, FontId, InnerResponse, Rect, Response, Sense, Stroke, TextureId, Ui, Vec2, pos2, vec2}; @@ -301,7 +304,15 @@ impl FileView { t.form_ink, false, ); - paint_text(ui, Rect::from_min_max(pos2(left, half), content.max), row.path, FontId::monospace(t.font_body), t.file_dim, false); + paint_text_aligned( + ui, + Rect::from_min_max(pos2(left, half), content.max), + row.path, + FontId::monospace(t.font_body), + t.file_dim, + false, + true, + ); } else { let when_width = t.space_24 * 5.0; let workspace_width = t.form_label_width * 2.0; @@ -318,13 +329,14 @@ impl FileView { t.form_ink, false, ); - paint_text( + paint_text_aligned( ui, Rect::from_min_max(pos2(path_left, content.top()), pos2((when_left - t.space_14).max(path_left), content.bottom())), row.path, FontId::monospace(t.font_body), t.file_dim, false, + true, ); paint_text( ui, diff --git a/crates/ui-egui/src/ui_kit/mod.rs b/crates/ui-egui/src/ui_kit/mod.rs index 6a10498d1..121e49853 100644 --- a/crates/ui-egui/src/ui_kit/mod.rs +++ b/crates/ui-egui/src/ui_kit/mod.rs @@ -94,6 +94,8 @@ pub(crate) use sphere::{SphereTile, sphere_cursor, sphere_grid, sphere_grid_drag pub(crate) use stages::{StageAction, StageBar}; pub(crate) use status::*; pub(crate) use table::*; +#[cfg(test)] +pub(crate) use text::ellipsize_middle; pub(crate) use text_menu::menu_open as text_menu_open; pub(crate) use tree::*; pub(crate) use value_focus::*; diff --git a/crates/ui-egui/src/ui_kit/text.rs b/crates/ui-egui/src/ui_kit/text.rs index 483a2a0ff..d58b63952 100644 --- a/crates/ui-egui/src/ui_kit/text.rs +++ b/crates/ui-egui/src/ui_kit/text.rs @@ -2,15 +2,55 @@ use crate::theme::Tokens; use egui::Ui; +/// Shorten `text` with an ellipsis in the middle so both ends stay visible (#9081). +pub(crate) fn ellipsize_middle(ui: &Ui, text: &str, font: egui::FontId, max_width: f32) -> String { + if !(max_width.is_finite() && max_width > 0.0) { + return String::new(); + } + let width = |s: &str| ui.fonts_mut(|fonts| fonts.layout_no_wrap(s.to_owned(), font.clone(), ui.visuals().text_color()).size().x); + if width(text) <= max_width + 0.5 { + return text.to_owned(); + } + let chars: Vec = text.chars().collect(); + if chars.len() < 4 { + return "…".into(); + } + let mut best = "…".to_string(); + let mut lo = 1usize; + let mut hi = chars.len() / 2; + while lo <= hi { + let keep = (lo + hi) / 2; + let left: String = chars.iter().take(keep).collect(); + let right: String = chars.iter().skip(chars.len() - keep).collect(); + let candidate = format!("{left}…{right}"); + if width(&candidate) <= max_width + 0.5 { + best = candidate; + lo = keep + 1; + } else if keep == 0 { + break; + } else { + hi = keep - 1; + } + } + best +} + /// Paint one pixel-aligned text run with the requested interface weight. /// Clipped, device-pixel-snapped weighted text. Tokens: `font_section_tracking; caller supplies font/ink tokens`. pub(super) fn paint_text(ui: &Ui, rect: egui::Rect, text: &str, font: egui::FontId, ink: egui::Color32, spaced: bool) { + paint_text_aligned(ui, rect, text, font, ink, spaced, false); +} + +/// Like [`paint_text`], but with `middle` an ellipsis sits in the middle so a path's file name +/// stays visible (#9081). +pub(super) fn paint_text_aligned(ui: &Ui, rect: egui::Rect, text: &str, font: egui::FontId, ink: egui::Color32, spaced: bool, middle: bool) { let size = font.size; - let mut format = egui::TextFormat::simple(font, ink); + let mut format = egui::TextFormat::simple(font.clone(), ink); if spaced { format.extra_letter_spacing = size * Tokens::get().font_section_tracking; } - let mut job = egui::text::LayoutJob::single_section(text.to_string(), format); + let shown = if middle { ellipsize_middle(ui, text, font, rect.width().max(0.0)) } else { text.to_owned() }; + let mut job = egui::text::LayoutJob::single_section(shown, format); job.wrap.max_width = rect.width().max(0.0); job.wrap.max_rows = 1; let galley = ui.fonts_mut(|fonts| fonts.layout_job(job));