Let a date range be stated, and draw the axis at the scale it deserves

"Limit to range" did nothing, and the reason was not visible from the button.
It took its span from the timeline's zoom, which is zero until someone zooms —
so `zoomed_span` returned the whole library and the filter narrowed to
everything. The chip lit up and the grid did not change.

The range has ends now, shown and typed as `YYYY-MM-DD`. Seeding them from the
timeline is kept, because zooming to a fortnight and pressing the chip is the
fast path; the fields say which fortnight it landed on and let it be corrected.
Ends given backwards are swapped rather than refused — there is exactly one
range between two days — and the closing day is included, since "to the 5th"
means the whole of the 5th and a range ending at its midnight contains none of
it. `parse_date` refuses anything that is not a date rather than guessing at an
order, because the alternative is a library silently filtered to a span nobody
asked for.

The axis then follows the range. It used to keep drawing the full extent while
a range was on, because it was the only way back out; the typed ends are the
way back out now, so it is free to show what was asked about.

And bucket size is chosen by how many bars it makes rather than by fixed
cut-offs. Each zoom step halves the span, so under thresholds the bar count
halved with it until a boundary was crossed: fifteen years went 15 bars, 8,
then 46, 23, 11, and finally 6. Zooming in made the picture coarser, which is
the opposite of what zooming is for. Aiming at forty bars keeps the count in
the same neighbourhood at every level, and the test asserts the property
directly — halving a span never coarsens the bucket.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-08-23 12:40:14 +02:00
co-authored by Claude Opus 5
parent fbf286d504
commit 4b7648082e
6 changed files with 391 additions and 8 deletions
+118
View File
@@ -3542,6 +3542,20 @@ fn refresh_timeline(window: &AppWindow, catalog: &Catalog, ctl: &Rc<LibraryContr
}
};
// A chosen date range is what the axis draws, when there is one.
//
// It used to keep drawing the full extent while a range was on, because
// the axis was the only way back out: narrow it to the range and there
// would be nothing left to widen from. The range now has typed ends, and
// those are the way back out — so the axis is free to show the span the
// user actually asked about, at the granularity that span deserves. A
// fortnight drawn across fifteen years is one column; drawn across itself
// it is fourteen days.
let span = match (filter.captured_from, filter.captured_to) {
(Some(from), Some(to)) if to > from => (from, to),
_ => span,
};
// Zoom narrows the span around wherever the view sits rather than around
// the library's midpoint, so zooming in keeps what you were looking at.
let zoom = *ctl.timeline_zoom.borrow();
@@ -4652,6 +4666,52 @@ pub fn wire<F>(
f.captured_to = None;
}
w.set_library_range_active(on);
show_range(&w, &ctl);
refilter(&w, &ctl);
});
}
// TRACES: FR-CAT-6
// The ends of the range, typed.
//
// The chip alone used to be the whole control and it read its span from
// the timeline's zoom, which is zero until someone zooms — so "limit to
// range" set the range to the entire library and appeared to do nothing.
// The span is shown now, and these two fields are how it is corrected.
{
let weak = window.as_weak();
let ctl = ctl.clone();
window.on_library_range_edited(move |from, to| {
let Some(w) = weak.upgrade() else { return };
// Both ends, or neither. A range with one end parsed and the other
// silently dropped is a filter nobody asked for, and the grid
// going empty is a poor way to find out a date was mistyped.
let (Some(from), Some(to)) = (
dr_types::parse_date(from.as_str()),
dr_types::parse_date(to.as_str()),
) else {
w.set_library_range_invalid(true);
return;
};
// Typed the other way round is a slip, not an error: the user has
// said which two days they mean and there is exactly one range
// between them.
let (from, to) = if from <= to { (from, to) } else { (to, from) };
{
let mut f = ctl.filter.borrow_mut();
f.captured_from = Some(from);
// Inclusive of the last day. "To the 5th" means the whole of
// the 5th — `parse_date` returns its midnight, and a range
// ending there would exclude every photograph taken on the day
// the user named.
f.captured_to = Some(to + 86_400 - 1);
}
w.set_library_range_invalid(false);
w.set_library_range_active(true);
show_range(&w, &ctl);
refilter(&w, &ctl);
});
}
@@ -5114,6 +5174,46 @@ mod tests {
assert_eq!(y, 1969);
}
/// The axis follows a chosen range, so its granularity does too.
///
/// The complaint this fixes: a fortnight selected out of a fifteen-year
/// library was still drawn as one column of a year-per-bar axis, which is
/// the least useful picture of a fortnight available.
#[test]
fn a_chosen_range_decides_the_granularity() {
use dr_catalog::Granularity;
const DAY: i64 = 86_400;
// Fifteen years unfiltered: a bar per year, fifteen of them.
assert_eq!(Granularity::for_span(15 * 365 * DAY), Granularity::Year);
// Narrowed to a fortnight, the axis is handed the range rather than
// the extent, and a fortnight is drawn as fourteen days.
assert_eq!(Granularity::for_span(14 * DAY), Granularity::Day);
// A single day's shoot is drawn by the hour.
assert_eq!(Granularity::for_span(DAY), Granularity::Hour);
// The property rather than the thresholds, because the thresholds are
// no longer what decides it: a narrower range never produces a coarser
// bucket than a wider one.
let mut previous = i64::MAX;
for days in [3650, 730, 180, 60, 14, 3, 1] {
let g = Granularity::for_span(days * DAY);
let bucket = match g {
Granularity::Year => 365 * DAY,
Granularity::Month => 30 * DAY,
Granularity::Day => DAY,
Granularity::Hour => 3600,
};
assert!(
bucket <= previous,
"{days} days chose a coarser bucket than the range above it"
);
previous = bucket;
}
}
#[test]
fn bucket_labels_match_their_granularity() {
use dr_catalog::Granularity;
@@ -5554,3 +5654,21 @@ mod display_name_tests {
assert_eq!(without_extension(""), "");
}
}
/// TRACES: FR-CAT-6
/// Show the range the filter is actually using.
///
/// Pushed back rather than left as the user typed it, because the filter may
/// have adjusted it — ends given backwards are swapped, and the closing day is
/// extended to include itself. A field showing something other than what is
/// being filtered on is worse than one showing nothing.
fn show_range(window: &AppWindow, ctl: &Rc<LibraryController>) {
let f = ctl.filter.borrow();
let text = |t: Option<i64>| -> slint::SharedString {
t.map(dr_types::format_date).unwrap_or_default().into()
};
window.set_library_range_from(text(f.captured_from));
// The stored end is the last second of the closing day; naming that day is
// what the user typed and what they should read back.
window.set_library_range_to(text(f.captured_to.map(|t| t - 86_400 + 1)));
}