mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-25 23:49:35 +00:00
Merge pull request #3335 from batistalucasv/fix/fontpicker-skip-idle-stops
font-manager: skip building FontPicker stops on idle closed frames
This commit is contained in:
@@ -730,6 +730,18 @@ pub struct FontPicker<'a> {
|
|||||||
host_picked: Option<String>,
|
host_picked: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// How many times a closed [`FontPicker`] built its stop list on `ctx`, for regression trials.
|
||||||
|
#[cfg(feature = "screen-testing")]
|
||||||
|
#[derive(Clone, Copy, Default)]
|
||||||
|
struct StopBuilds(u64);
|
||||||
|
|
||||||
|
/// How many times a closed [`FontPicker`] built its stop list on `ctx`.
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[cfg(feature = "screen-testing")]
|
||||||
|
pub fn stop_builds(ctx: &egui::Context) -> u64 {
|
||||||
|
ctx.data(|data| data.get_temp::<StopBuilds>(egui::Id::unique("font_picker_stop_builds")).unwrap_or_default().0)
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a> FontPicker<'a> {
|
impl<'a> FontPicker<'a> {
|
||||||
/// A picker named `label` over the family `family` of `catalog`.
|
/// A picker named `label` over the family `family` of `catalog`.
|
||||||
pub fn new(label: impl Into<Label>, family: Bind<'a, String>, catalog: &'a Arc<Catalog>) -> Self {
|
pub fn new(label: impl Into<Label>, family: Bind<'a, String>, catalog: &'a Arc<Catalog>) -> Self {
|
||||||
@@ -806,6 +818,10 @@ impl<'a> FontPicker<'a> {
|
|||||||
/// What the closed field's arrows and wheel step through: the host face and action rows in group order, then the
|
/// What the closed field's arrows and wheel step through: the host face and action rows in group order, then the
|
||||||
/// families in the list's narrowed order. A notice takes no step.
|
/// families in the list's narrowed order. A notice takes no step.
|
||||||
fn stops(&self, cx: &Cx<'_, Self>, settings: Option<&FontPickerSettings>, filters: &FontFilters) -> Vec<Stop> {
|
fn stops(&self, cx: &Cx<'_, Self>, settings: Option<&FontPickerSettings>, filters: &FontFilters) -> Vec<Stop> {
|
||||||
|
#[cfg(feature = "screen-testing")]
|
||||||
|
cx.response.ctx.data_mut(|data| {
|
||||||
|
data.get_temp_mut_or_default::<StopBuilds>(egui::Id::unique("font_picker_stop_builds")).0 += 1;
|
||||||
|
});
|
||||||
let folder = cx.env.folder();
|
let folder = cx.env.folder();
|
||||||
let host = rank::host_rows(self.host, "", &folder)
|
let host = rank::host_rows(self.host, "", &folder)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
@@ -985,52 +1001,75 @@ impl Component for FontPicker<'_> {
|
|||||||
let typing = cx.memory::<Typing>();
|
let typing = cx.memory::<Typing>();
|
||||||
typing.expanded.clear();
|
typing.expanded.clear();
|
||||||
typing.rested.clear();
|
typing.rested.clear();
|
||||||
|
// Collect stepping input before building the sorted stop list: idle frames carry neither arrow moves nor
|
||||||
|
// wheel ticks, so skipping `stops` avoids collating every installed family on every repaint.
|
||||||
|
let mut moves: Vec<bool> = Vec::new();
|
||||||
|
while let Some((step, mods)) = cx.intents().take_move() {
|
||||||
|
match step {
|
||||||
|
NavMove::Down if !mods.fine => moves.push(true),
|
||||||
|
NavMove::Up if !mods.fine => moves.push(false),
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let wheel = cx.take_wheel();
|
||||||
|
let wheel_along = wheel.as_ref().and_then(|wheel| {
|
||||||
|
if wheel.phase == WheelPhase::End {
|
||||||
|
None
|
||||||
|
} else if let StepResult::Set(along) =
|
||||||
|
wheel_step(&ValueSteps::new(0.0, -f64::MAX, f64::MAX, 1.0, 1.0).fine(1.0), ValueAxes::Both, wheel, cx.env.direction().is_rtl())
|
||||||
|
&& along != 0.0
|
||||||
|
{
|
||||||
|
Some((wheel.phase, along))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let wheel_ended = wheel.is_some_and(|wheel| wheel.phase == WheelPhase::End);
|
||||||
|
if moves.is_empty() && wheel_along.is_none() {
|
||||||
|
if wheel_ended {
|
||||||
|
cx.wheel_run(WheelPhase::End, None);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
let settings = read(&mut self.settings, cx);
|
let settings = read(&mut self.settings, cx);
|
||||||
let filters = read(&mut self.filters, cx).unwrap_or_default();
|
let filters = read(&mut self.filters, cx).unwrap_or_default();
|
||||||
let all = self.stops(cx, settings.as_ref(), &filters);
|
let all = self.stops(cx, settings.as_ref(), &filters);
|
||||||
let stops: Vec<usize> = (0..all.len()).collect();
|
let stops: Vec<usize> = (0..all.len()).collect();
|
||||||
let here = match self.current_host.as_deref() {
|
if !moves.is_empty() {
|
||||||
Some(key) => all.iter().position(|stop| matches!(stop, Stop::Host(host) if host == key)),
|
let here = match self.current_host.as_deref() {
|
||||||
None => cx.value().and_then(|current| all.iter().position(|stop| matches!(stop, Stop::Family(family) if family == current))),
|
Some(key) => all.iter().position(|stop| matches!(stop, Stop::Host(host) if host == key)),
|
||||||
};
|
None => cx.value().and_then(|current| all.iter().position(|stop| matches!(stop, Stop::Family(family) if family == current))),
|
||||||
let mut to = here;
|
};
|
||||||
while let Some((step, mods)) = cx.intents().take_move() {
|
let mut to = here;
|
||||||
match step {
|
for forward in moves {
|
||||||
NavMove::Down if !mods.fine => to = step_stop(&stops, to, true),
|
to = step_stop(&stops, to, forward);
|
||||||
NavMove::Up if !mods.fine => to = step_stop(&stops, to, false),
|
|
||||||
_ => {}
|
|
||||||
}
|
}
|
||||||
}
|
if to != here {
|
||||||
if to != here {
|
match to.and_then(|at| all.get(at)) {
|
||||||
match to.and_then(|at| all.get(at)) {
|
Some(Stop::Host(key)) => self.host_picked = Some(key.clone()),
|
||||||
Some(Stop::Host(key)) => self.host_picked = Some(key.clone()),
|
Some(Stop::Family(family)) => {
|
||||||
Some(Stop::Family(family)) => {
|
self.picked = Some(family.clone());
|
||||||
self.picked = Some(family.clone());
|
cx.set(family.clone());
|
||||||
cx.set(family.clone());
|
}
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
None => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// The wheel steps one stop a line down for a step forward, as one run the wheel's end commits.
|
// The wheel steps one stop a line down for a step forward, as one run the wheel's end commits.
|
||||||
if let Some(wheel) = cx.take_wheel() {
|
if wheel_ended {
|
||||||
if wheel.phase == WheelPhase::End {
|
cx.wheel_run(WheelPhase::End, None);
|
||||||
cx.wheel_run(wheel.phase, None);
|
} else if let Some((phase, along)) = wheel_along {
|
||||||
} else if let StepResult::Set(along) =
|
let mut stop = match self.current_host.as_deref() {
|
||||||
wheel_step(&ValueSteps::new(0.0, -f64::MAX, f64::MAX, 1.0, 1.0).fine(1.0), ValueAxes::Both, &wheel, cx.env.direction().is_rtl())
|
Some(key) => all.iter().position(|stop| matches!(stop, Stop::Host(host) if host == key)),
|
||||||
&& along != 0.0
|
None => cx.value().and_then(|current| all.iter().position(|stop| matches!(stop, Stop::Family(family) if family == current))),
|
||||||
{
|
};
|
||||||
let mut stop = match self.current_host.as_deref() {
|
for _ in 0..along.abs().round() as usize {
|
||||||
Some(key) => all.iter().position(|stop| matches!(stop, Stop::Host(host) if host == key)),
|
stop = step_stop(&stops, stop, along < 0.0);
|
||||||
None => cx.value().and_then(|current| all.iter().position(|stop| matches!(stop, Stop::Family(family) if family == current))),
|
}
|
||||||
};
|
match stop.and_then(|at| all.get(at)) {
|
||||||
for _ in 0..along.abs().round() as usize {
|
Some(Stop::Host(key)) => self.host_picked = Some(key.clone()),
|
||||||
stop = step_stop(&stops, stop, along < 0.0);
|
Some(Stop::Family(family)) => cx.wheel_run(phase, Some(family.clone())),
|
||||||
}
|
None => {}
|
||||||
match stop.and_then(|at| all.get(at)) {
|
|
||||||
Some(Stop::Host(key)) => self.host_picked = Some(key.clone()),
|
|
||||||
Some(Stop::Family(family)) => cx.wheel_run(wheel.phase, Some(family.clone())),
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1054,3 +1054,35 @@ fn the_list_loads_its_sample_faces_off_the_pass_and_once() {
|
|||||||
fx.frame(vec![]);
|
fx.frame(vec![]);
|
||||||
assert_eq!(jobs.waiting(), 0, "the list opened again loads no face");
|
assert_eq!(jobs.waiting(), 0, "the list opened again loads no face");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[trial]
|
||||||
|
fn the_closed_field_skips_building_stops_while_idle_and_steps_on_input() {
|
||||||
|
let all = listed(&three_faces(), &FontFilters::default(), &[]);
|
||||||
|
assert_eq!(all.len(), 3);
|
||||||
|
|
||||||
|
let (mut fx, seen) = rig(three_faces(), vec![]);
|
||||||
|
seen.borrow_mut().family = all[0].clone();
|
||||||
|
fx.frame(vec![]);
|
||||||
|
let before = craft_font_manager::picker::stop_builds(fx.ctx());
|
||||||
|
for _ in 0..5 {
|
||||||
|
fx.frame(vec![]);
|
||||||
|
}
|
||||||
|
assert_eq!(craft_font_manager::picker::stop_builds(fx.ctx()), before, "idle closed frames do not rebuild stops");
|
||||||
|
|
||||||
|
aim_at_field(&mut fx);
|
||||||
|
assert_eq!(craft_font_manager::picker::stop_builds(fx.ctx()), before, "focusing and hovering without stepping do not rebuild stops");
|
||||||
|
|
||||||
|
fx.key(Key::ArrowDown, Modifiers::NONE);
|
||||||
|
assert_eq!(seen.borrow().family, all[1], "ArrowDown steps to the next family");
|
||||||
|
let after_key = craft_font_manager::picker::stop_builds(fx.ctx());
|
||||||
|
assert_eq!(after_key, before + 1, "one ArrowDown builds stops once");
|
||||||
|
|
||||||
|
fx.frame(vec![wheel_lines(-1.0)]);
|
||||||
|
assert_eq!(seen.borrow().family, all[2], "wheel down steps to the following family");
|
||||||
|
let after_wheel = craft_font_manager::picker::stop_builds(fx.ctx());
|
||||||
|
assert_eq!(after_wheel, after_key + 1, "one wheel tick builds stops once");
|
||||||
|
|
||||||
|
fx.advance(GESTURE_GAP * 2.0).idle(3);
|
||||||
|
assert_eq!(seen.borrow().picks.last().map(|(f, _)| f.as_str()), Some(all[2].as_str()), "wheel end commits the stepped family");
|
||||||
|
assert_eq!(craft_font_manager::picker::stop_builds(fx.ctx()), after_wheel, "wheel end and idle frames do not rebuild stops");
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user