Merge PR #3559: Type: wire Recent Fonts into the font menu (#204)

docs/scorecard.md: ours (regenerated at combine).
This commit is contained in:
aiongg committed 2026-10-22 02:21:48 +00:00
commit 995eae3123
6 files changed
+169 -23

No files matched your search

-1
View File
@@ -1059,7 +1059,6 @@ pub const HIDDEN_UNTIL_IMPLEMENTED: &[&str] = &[
"plugIns.allowScriptsToConnect",
"plugIns.generatorEnabled",
"type.textEngine",
"type.recentFonts",
"enhancedControls.scrubbySliderAcceleration",
"rawDefaults.colorSpace",
"rawDefaults.bitDepth",
+23
View File
@@ -1466,6 +1466,26 @@ impl PhotocraftApp {
self.session.prefs.edit(|p| p.file_handling.recent_files = r);
}
/// Records a picked type family for the font menu's Recent group, capped by
/// Preferences ▸ Type ▸ Recent Fonts (0 keeps no list), most recent first, de-duplicated.
pub fn note_recent_font(&mut self, family: &str) {
let cap = self.session.prefs().type_.recent_fonts.min(50) as usize;
let r = &mut self.ui.recent_fonts;
r.retain(|f| f != family);
if cap == 0 {
r.clear();
return;
}
r.insert(0, family.to_string());
r.truncate(cap);
}
/// Preferences ▸ Type ▸ Recent Fonts: a smaller cap (or 0) applies to the existing list at
/// once, not only after the next pick.
pub fn sync_recent_fonts(&mut self) {
self.ui.recent_fonts.truncate(self.session.prefs().type_.recent_fonts.min(50) as usize);
}
/// File › Open Recent › Clear Recent File List.
pub fn clear_recent(&mut self) {
self.ui.recent_files.clear();
@@ -2203,6 +2223,9 @@ impl eframe::App for PhotocraftApp {
text_field_menu::show(&ctx);
// Each painting tool keeps its own brush (#218), so switch the active tool's brush in
// before anything this frame reads it (the cursor, the options bar, a stroke).
// Preferences ▸ Type ▸ Recent Fonts: a smaller cap (or 0) trims the existing list at
// once, not only after the next pick.
self.sync_recent_fonts();
paint_mouse::sync_tool_brush(self);
// A unit typed into a value field (`1in` in a px field) converts at the active document's
// resolution, as in Photoshop.
+5
View File
@@ -1220,6 +1220,10 @@ pub struct UiState {
/// Recently opened file paths, most-recent first (File › Open Recent). Capped; de-duplicated.
#[serde(default)]
pub recent_files: Vec<String>,
/// Recently picked type families, most-recent first (the font menu's Recent group). Capped by
/// Preferences ▸ Type ▸ Recent Fonts; de-duplicated.
#[serde(default)]
pub recent_fonts: Vec<String>,
/// Inline type editing session, if any.
#[serde(default)]
pub text_edit: Option<TextEdit>,
@@ -1465,6 +1469,7 @@ impl Default for UiState {
group_tools: Vec::new(),
spring: None,
recent_files: Vec::new(),
recent_fonts: Vec::new(),
text_edit: None,
caret_blink_time: 0.0,
text_edit_return_tool: None,
+61 -6
View File
@@ -13,6 +13,7 @@ struct State {
button: egui::Id,
popup: egui::Id,
preview: FontPreview,
recents: Vec<String>,
}
fn harness() -> Harness<'static, State> {
@@ -20,16 +21,21 @@ 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, button: egui::Id::NULL, popup: egui::Id::NULL, preview };
let state = State { current: "Family 00".into(), families, button: egui::Id::NULL, 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.button = ui.make_persistent_id(egui::IdSalt::new("type-font"));
s.popup = s.button.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,
);
@@ -332,14 +338,15 @@ fn arrows_on_the_field_step_through_the_families_after_a_pick() {
fn the_first_arrow_after_a_pick_keeps_the_focus_between_neighbours() {
for (enter, picked, stepped) in [(false, "Family 05", "Family 06"), (true, "Family 04", "Family 05")] {
let families: Vec<String> = (0..20).map(|i| format!("Family {i:02}")).collect();
let state = State { current: "Family 03".into(), families, button: egui::Id::NULL, popup: egui::Id::NULL, preview: FontPreview::Off };
let state =
State { current: "Family 03".into(), families, button: egui::Id::NULL, popup: egui::Id::NULL, preview: FontPreview::Off, recents: Vec::new() };
let mut h = Harness::builder().with_size(vec2(600.0, 900.0)).build_ui_state(
|ui, s: &mut State| {
let _ = ui.button("Above");
s.button = ui.make_persistent_id(egui::IdSalt::new("type-font"));
s.popup = s.button.with("popup");
let fams = s.families.clone();
font_picker_in(ui, &mut s.current, 170.0, &fams, s.preview);
font_picker_in(ui, &mut s.current, 170.0, &fams, s.preview, &s.recents);
let _ = ui.button("Below");
},
state,
@@ -647,12 +654,14 @@ fn localized_font_names_label_the_menu_but_pick_the_family() {
button: egui::Id::NULL,
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,
);
@@ -687,7 +696,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));
@@ -698,6 +707,28 @@ fn font_menu_rows_search_both_names_and_sort_by_label() {
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}");
}
#[test]
fn wheel_over_the_closed_font_button_steps_through_the_families() {
let mut h = harness();
@@ -717,6 +748,30 @@ fn wheel_over_the_closed_font_button_steps_through_the_families() {
assert_eq!(h.state().current, "Family 00", "stops at the first family");
}
#[test]
fn wheel_over_the_closed_button_keeps_a_stable_order_with_recents() {
let mut h = harness_with_recents(FontPreview::Medium, vec![]);
let button = h.query_all_by_role(Role::ComboBox).next().unwrap().rect().center();
h.hover_at(button);
h.run();
let wheel = |h: &mut Harness<'static, State>, y: f32| {
h.event(egui::Event::MouseWheel { unit: egui::MouseWheelUnit::Line, delta: vec2(0.0, y), phase: egui::TouchPhase::Move, modifiers: Modifiers::NONE });
h.run();
};
wheel(&mut h, -1.0);
assert_eq!(h.state().current, "Family 01");
// The caller (the options bar) records the picked family as the most recent one.
h.state_mut().recents.insert(0, "Family 01".into());
h.run();
wheel(&mut h, -1.0);
assert_eq!(h.state().current, "Family 02", "the next notch keeps going, not back to the recent family");
h.state_mut().recents.insert(0, "Family 02".into());
h.run();
wheel(&mut h, -1.0);
assert_eq!(h.state().current, "Family 03");
assert!(popup_rect(&h).is_none(), "the list stays closed");
}
#[test]
fn wheel_family_index_clamps_to_the_ends() {
assert_eq!(super::wheel_family_index(0, 3, -5.0), Some(2));
+7 -4
View File
@@ -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 }));
}
}
@@ -518,12 +520,12 @@ 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 {
if !crate::type_tool::fonts_ready(ui, 140.0) {
return false;
}
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;
}
@@ -617,7 +619,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();
}
+73 -12
View File
@@ -1361,8 +1361,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()
@@ -1372,6 +1374,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
}
@@ -1546,8 +1558,8 @@ pub(crate) fn fonts_ready(ui: &mut egui::Ui, width: f32) -> bool {
}
/// 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.
@@ -1555,8 +1567,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
@@ -1568,6 +1587,7 @@ pub(crate) fn family_picker_in(
width: f32,
families: &[String],
preview: photocraft_engine::prefs::FontPreview,
recents: &[String],
) -> bool {
let mut changed = false;
// A font picked from the open menu (click or Enter): the field keeps the focus afterwards.
@@ -1612,7 +1632,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 {
@@ -1711,9 +1731,10 @@ pub(crate) fn family_picker_in(
// the frame. Cancel it, so the arrow only steps the font.
ui.memory_mut(|m| m.move_focus(egui::FocusDirection::None));
}
// The wheel's step: a negative notch is the next family.
// The wheel's step: a negative notch is the next family. Steps use a stable order (no
// recents hoisting), so recording the picked family cannot make the next step jump back.
let notches = f32::from(u8::from(up)) - f32::from(u8::from(down));
let rows = font_menu_rows(families, labels, "");
let rows = font_menu_rows(families, labels, "", &[]);
if let Some(i) = rows.iter().position(|(f, _)| *f == current)
&& let Some(n) = wheel_family_index(i, rows.len(), notches)
&& let Some((f, _)) = rows.get(n)
@@ -1733,7 +1754,10 @@ pub(crate) fn family_picker_in(
ui.input_mut(|i| i.smooth_scroll_delta = egui::Vec2::ZERO);
}
let notches = crate::widgets::wheel_notches_of(ui, response.response.id.with("family-wheel"), hovered, 1.0);
let rows = font_menu_rows(families, labels, "");
// The wheel steps in a stable order: hoisting the family it just picked as recent
// would change the order between notches and can jump back to the previous family.
// The open menu still shows the recent families first.
let rows = font_menu_rows(families, labels, "", &[]);
if let Some(i) = rows.iter().position(|(f, _)| *f == current)
&& let Some(n) = wheel_family_index(i, rows.len(), notches)
&& let Some((f, _)) = rows.get(n)
@@ -1911,7 +1935,8 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
}
// Font family and style menus (one placeholder in their place while the fonts load).
if fonts_ready(ui, 170.0 + ui.spacing().item_spacing.x + 110.0) {
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()) };
@@ -2342,7 +2367,8 @@ fn type_sections(app: &mut PhotocraftApp, ui: &mut egui::Ui, character: bool, pa
let mut ready = true;
row(ui, &mut |ui| {
ready = fonts_ready(ui, full);
if ready && font_picker(ui, &mut fam, full, app.session.prefs().type_.font_preview) {
if ready && 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}));
}
@@ -2652,6 +2678,41 @@ 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");
}
/// A smaller Recent Fonts cap (or 0) applies to the existing list at once, without waiting
/// for the next pick (CodeRabbit review of #3559).
#[test]
fn lowering_the_recent_fonts_cap_applies_at_once() {
let mut app = app();
for f in ["A", "B", "C"] {
app.note_recent_font(f);
}
app.run("prefs.set", json!({"path": "type.recentFonts", "value": 2})).unwrap();
app.sync_recent_fonts();
assert_eq!(app.ui.recent_fonts, ["C", "B"], "the smaller cap trims the tail without a pick");
app.run("prefs.set", json!({"path": "type.recentFonts", "value": 0})).unwrap();
app.sync_recent_fonts();
assert!(app.ui.recent_fonts.is_empty(), "0 clears the list");
}
fn layer_text(app: &PhotocraftApp) -> String {
let ed = app.ui.text_edit.as_ref().unwrap();
current_text(app, LayerId(ed.layer)).unwrap()