Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

feat(ui): hoist recent families in the font menu (#204)

Reynold Hu6293fd87eb2e
diff --git a/crates/ui-egui/src/type_font_picker_tests.rs b/crates/ui-egui/src/type_font_picker_tests.rs
index 2fc600511..0ba91455e 100644
--- a/crates/ui-egui/src/type_font_picker_tests.rs
+++ b/crates/ui-egui/src/type_font_picker_tests.rs
@@ -12,6 +12,7 @@ struct State {
families: Vec<String>,
popup: egui::Id,
preview: FontPreview,
+ recents: Vec<String>,
}
fn harness() -> Harness<'static, State> {
@@ -19,15 +20,20 @@ fn harness() -> Harness<'static, State> {
}
fn harness_with(preview: FontPreview) -> Harness<'static, State> {
+ harness_with_recents(preview, Vec::new())
+}
+
+fn harness_with_recents(preview: FontPreview, recents: Vec<String>) -> Harness<'static, State> {
// Enough families that the unfiltered menu hits its maximum height and scrolls.
let mut families: Vec<String> = (0..60).map(|i| format!("Family {i:02}")).collect();
families.push("Zeta Sans".into());
- let state = State { current: "Family 00".into(), families, popup: egui::Id::NULL, preview };
+ let state = State { current: "Family 00".into(), families, popup: egui::Id::NULL, preview, recents };
let mut h = Harness::builder().with_size(vec2(600.0, 900.0)).build_ui_state(
|ui, s: &mut State| {
s.popup = ui.make_persistent_id(egui::IdSalt::new("type-font")).with("popup");
let fams = s.families.clone();
- font_picker_in(ui, &mut s.current, 170.0, &fams, s.preview);
+ let recents = s.recents.clone();
+ font_picker_in(ui, &mut s.current, 170.0, &fams, s.preview, &recents);
},
state,
);
@@ -413,12 +419,14 @@ fn localized_font_names_label_the_menu_but_pick_the_family() {
families: vec!["Arial".into(), "MS Gothic".into(), "Zeta Sans".into()],
popup: egui::Id::NULL,
preview: FontPreview::Medium,
+ recents: Vec::new(),
};
let mut h = Harness::builder().with_size(vec2(600.0, 900.0)).build_ui_state(
|ui, s: &mut State| {
s.popup = ui.make_persistent_id(egui::IdSalt::new("type-font")).with("popup");
let fams = s.families.clone();
- font_picker_in(ui, &mut s.current, 170.0, &fams, s.preview);
+ let recents = s.recents.clone();
+ font_picker_in(ui, &mut s.current, 170.0, &fams, s.preview, &recents);
},
state,
);
@@ -453,7 +461,7 @@ fn font_menu_rows_search_both_names_and_sort_by_label() {
let labels: HashMap<String, String> =
[("MS Gothic", "MS ゴシック"), ("Microsoft YaHei", "微软雅黑")].iter().map(|(a, b)| (a.to_string(), b.to_string())).collect();
let rows = |labels: Option<&HashMap<String, String>>, q: &str| -> Vec<(String, String)> {
- super::font_menu_rows(&families, labels, q).into_iter().map(|(f, l)| (f.clone(), l.to_string())).collect()
+ super::font_menu_rows(&families, labels, q, &[]).into_iter().map(|(f, l)| (f.clone(), l.to_string())).collect()
};
let english = rows(None, "");
assert!(english.iter().all(|(f, l)| f == l));
@@ -463,3 +471,25 @@ fn font_menu_rows_search_both_names_and_sort_by_label() {
assert_eq!(rows(Some(&labels), "雅黑"), [("Microsoft YaHei".to_string(), "微软雅黑".to_string())]);
assert!(rows(Some(&labels), "nothing").is_empty());
}
+
+/// Preferences ▸ Type ▸ Recent Fonts: recently used families move to the top (most recent first,
+/// once each); a query drops the hoisting so the list stays alphabetical while searching.
+#[test]
+fn recent_fonts_are_hoisted_to_the_top() {
+ let families: Vec<String> = ["Arial", "Baskerville", "Courier", "Zeta Sans"].iter().map(|s| s.to_string()).collect();
+ let recents: Vec<String> = vec!["Zeta Sans".into(), "Courier".into()];
+ let rows = |q: &str| -> Vec<String> { super::font_menu_rows(&families, None, q, &recents).into_iter().map(|(f, _)| f.clone()).collect() };
+ assert_eq!(rows(""), ["Zeta Sans", "Courier", "Arial", "Baskerville"], "recents hoist, most recent first, once each");
+ assert_eq!(rows("a"), ["Arial", "Baskerville", "Zeta Sans"], "a query drops the hoisting");
+}
+
+/// The menu itself draws the hoisted recents first.
+#[test]
+fn recent_fonts_appear_first_in_the_menu() {
+ let mut h = harness_with_recents(FontPreview::Medium, vec!["Zeta Sans".into(), "Family 05".into()]);
+ open(&mut h);
+ let zeta = h.get_by_label("Zeta Sans").rect().top();
+ let five = h.get_by_label("Family 05").rect().top();
+ let six = h.get_by_label("Family 06").rect().top();
+ assert!(zeta < five && five < six, "recents come first, in order: {zeta} {five} {six}");
+}
diff --git a/crates/ui-egui/src/type_panels_ui.rs b/crates/ui-egui/src/type_panels_ui.rs
index fbab3f84f..e2a5f5711 100644
--- a/crates/ui-egui/src/type_panels_ui.rs
+++ b/crates/ui-egui/src/type_panels_ui.rs
@@ -373,6 +373,8 @@ fn options_editor(app: &mut PhotocraftApp, ui: &mut egui::Ui, paragraph: bool, s
let defined = |attrs: &Value, k: &str| attrs.get(k).is_some();
// Preferences ▸ Type ▸ Font Preview Size sizes the family picker's samples.
let preview = app.session.prefs().type_.font_preview;
+ // The fonts the pickers hoist to the top (Preferences ▸ Type ▸ Recent Fonts).
+ let recents = app.ui.recent_fonts.clone();
// Preferences ▸ Units & Rulers ▸ Type: the model stores points; the fields may show pixels
// (by the document's resolution) or millimetres.
let (type_unit, unit_label) = crate::type_tool::type_unit(app);
@@ -399,7 +401,7 @@ fn options_editor(app: &mut PhotocraftApp, ui: &mut egui::Ui, paragraph: bool, s
ui.add_enabled_ui(on, |ui| match kind {
"font" => {
let mut f = v.as_str().unwrap_or("").to_string();
- if family_picker(ui, &format!("style-font-{paragraph}-{id}"), &mut f, preview) {
+ if family_picker(ui, &format!("style-font-{paragraph}-{id}"), &mut f, preview, &recents) {
set = Some(json!({ panel: f }));
}
}
@@ -508,9 +510,9 @@ fn options_editor(app: &mut PhotocraftApp, ui: &mut egui::Ui, paragraph: bool, s
}
/// Searchable family combo box.
-fn family_picker(ui: &mut egui::Ui, salt: &str, current: &mut String, preview: photocraft_engine::prefs::FontPreview) -> bool {
+fn family_picker(ui: &mut egui::Ui, salt: &str, current: &mut String, preview: photocraft_engine::prefs::FontPreview, recents: &[String]) -> bool {
let mut shown = if current.is_empty() { photocraft_text::fonts::DEFAULT_FAMILY.to_string() } else { current.clone() };
- let changed = crate::type_tool::family_picker_in(ui, salt, &mut shown, 140.0, &crate::type_tool::families(), preview);
+ let changed = crate::type_tool::family_picker_in(ui, salt, &mut shown, 140.0, &crate::type_tool::families(), preview, recents);
if changed {
*current = shown;
}
@@ -600,7 +602,8 @@ pub fn glyphs_panel(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
// Font family + style.
ui.horizontal(|ui| {
let mut f = family.clone();
- if family_picker(ui, "glyph-family", &mut f, app.session.prefs().type_.font_preview) {
+ if family_picker(ui, "glyph-family", &mut f, app.session.prefs().type_.font_preview, &app.ui.recent_fonts) {
+ app.note_recent_font(&f);
app.ui.type_panels.glyph_family = f;
app.ui.type_panels.glyph_style = tl!("Regular").into();
}
diff --git a/crates/ui-egui/src/type_tool.rs b/crates/ui-egui/src/type_tool.rs
index d80e234dd..efa2931e7 100644
--- a/crates/ui-egui/src/type_tool.rs
+++ b/crates/ui-egui/src/type_tool.rs
@@ -939,8 +939,10 @@ fn localized_families() -> Arc<HashMap<String, String>> {
/// The font menu's rows: each family with the label it shows (its localized name when `labels`
/// has one, else the family), matching `query` (lowercase) by either name. With localized labels
-/// the rows sort by label, so the menu stays alphabetical as shown.
-fn font_menu_rows<'a>(families: &'a [String], labels: Option<&'a HashMap<String, String>>, query: &str) -> Vec<(&'a String, &'a str)> {
+/// the rows sort by label, so the menu stays alphabetical as shown. Recently used families are
+/// hoisted to the top, most recent first (Preferences ▸ Type ▸ Recent Fonts); a query hides the
+/// hoisting so the list stays strictly alphabetical while searching.
+fn font_menu_rows<'a>(families: &'a [String], labels: Option<&'a HashMap<String, String>>, query: &str, recents: &[String]) -> Vec<(&'a String, &'a str)> {
let label = |f: &'a String| labels.and_then(|l| l.get(f)).map_or(f.as_str(), String::as_str);
let mut rows: Vec<(&String, &str)> = families
.iter()
@@ -950,6 +952,16 @@ fn font_menu_rows<'a>(families: &'a [String], labels: Option<&'a HashMap<String,
if labels.is_some_and(|l| !l.is_empty()) {
rows.sort_by_cached_key(|(_, l)| l.to_lowercase());
}
+ if query.is_empty() {
+ let mut at = 0;
+ for r in recents {
+ if let Some(i) = rows.iter().position(|(f, _)| *f == r) {
+ let row = rows.remove(i);
+ rows.insert(at, row);
+ at += 1;
+ }
+ }
+ }
rows
}
@@ -1022,8 +1034,8 @@ pub fn style_label(style: &str) -> String {
}
/// Searchable font-family combo box.
-fn font_picker(ui: &mut egui::Ui, current: &mut String, width: f32, preview: photocraft_engine::prefs::FontPreview) -> bool {
- family_picker_in(ui, "type-font", current, width, &families(), preview)
+fn font_picker(ui: &mut egui::Ui, current: &mut String, width: f32, preview: photocraft_engine::prefs::FontPreview, recents: &[String]) -> bool {
+ family_picker_in(ui, "type-font", current, width, &families(), preview, recents)
}
/// Maximum height of the font menu.
@@ -1031,8 +1043,15 @@ const FONT_MENU_HEIGHT: f32 = 460.0;
/// [`font_picker`] over a given family list (tests pass their own).
#[cfg(test)]
-fn font_picker_in(ui: &mut egui::Ui, current: &mut String, width: f32, families: &[String], preview: photocraft_engine::prefs::FontPreview) -> bool {
- family_picker_in(ui, "type-font", current, width, families, preview)
+fn font_picker_in(
+ ui: &mut egui::Ui,
+ current: &mut String,
+ width: f32,
+ families: &[String],
+ preview: photocraft_engine::prefs::FontPreview,
+ recents: &[String],
+) -> bool {
+ family_picker_in(ui, "type-font", current, width, families, preview, recents)
}
/// Shared by the Type options, Character, style and Glyphs panels. Selection changes still
@@ -1044,6 +1063,7 @@ pub(crate) fn family_picker_in(
width: f32,
families: &[String],
preview: photocraft_engine::prefs::FontPreview,
+ recents: &[String],
) -> bool {
let mut changed = false;
let search_id = ui.id().with(("font-search", salt));
@@ -1086,7 +1106,7 @@ pub(crate) fn family_picker_in(
d.insert_temp(search_id, q.clone());
});
let ql = q.to_lowercase();
- let rows = font_menu_rows(families, labels, &ql);
+ let rows = font_menu_rows(families, labels, &ql, recents);
if down || up {
let index = rows.iter().position(|(f, _)| *f == current);
let next = match index {
@@ -1304,7 +1324,8 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
app.ui.status_error = true;
}
}
- if font_picker(ui, &mut fam, 170.0, app.session.prefs().type_.font_preview) {
+ if font_picker(ui, &mut fam, 170.0, app.session.prefs().type_.font_preview, &app.ui.recent_fonts) {
+ app.note_recent_font(&fam);
app.ui.tool_options.type_font = fam.clone();
let st = styles(&fam);
style = if st.contains(&style) { style } else { st.first().cloned().unwrap_or_else(|| "Regular".into()) };
@@ -1616,7 +1637,8 @@ fn type_sections(app: &mut PhotocraftApp, ui: &mut egui::Ui, character: bool, pa
let w = field_width(full, 2, LABEL_W);
let mut fam = c.font_family.clone();
row(ui, &mut |ui| {
- if font_picker(ui, &mut fam, full, app.session.prefs().type_.font_preview) {
+ if font_picker(ui, &mut fam, full, app.session.prefs().type_.font_preview, &app.ui.recent_fonts) {
+ app.note_recent_font(&fam);
app.ui.tool_options.type_font = fam.clone();
apply(app, ui.ctx(), json!({"font": fam}));
}
@@ -1897,6 +1919,25 @@ mod tests {
app
}
+ /// Preferences ▸ Type ▸ Recent Fonts caps the recorded list; a re-pick moves to the front and
+ /// 0 keeps no list.
+ #[test]
+ fn note_recent_font_caps_and_de_duplicates() {
+ let mut app = app();
+ for f in ["A", "B", "C"] {
+ app.note_recent_font(f);
+ }
+ assert_eq!(app.ui.recent_fonts, ["C", "B", "A"]);
+ app.run("prefs.set", json!({"path": "type.recentFonts", "value": 2})).unwrap();
+ app.note_recent_font("D");
+ assert_eq!(app.ui.recent_fonts, ["D", "C"], "the cap trims the tail");
+ app.note_recent_font("C");
+ assert_eq!(app.ui.recent_fonts, ["C", "D"], "a re-pick moves to the front");
+ app.run("prefs.set", json!({"path": "type.recentFonts", "value": 0})).unwrap();
+ app.note_recent_font("E");
+ assert!(app.ui.recent_fonts.is_empty(), "0 keeps no list");
+ }
+
fn layer_text(app: &PhotocraftApp) -> String {
let ed = app.ui.text_edit.as_ref().unwrap();
current_text(app, LayerId(ed.layer)).unwrap()