Fit slide browser thumbnails to window width and anchor scrollbar

This commit is contained in:
Dandaedre 2026-11-20 06:58:41 +01:00
parent ce78d13d11
commit ec5d8a2458
6 changed files with 121 additions and 52 deletions

2
Cargo.lock generated
View File

@ -2212,7 +2212,7 @@ dependencies = [
[[package]]
name = "pdf-presenter"
version = "0.1.37"
version = "0.1.38"
dependencies = [
"bytemuck",
"chrono",

View File

@ -4,7 +4,7 @@ default-members = ["crates/pdf-presenter"]
resolver = "3"
[workspace.package]
version = "0.1.37"
version = "0.1.38"
edition = "2024"
license = "MIT"
repository = "https://proxy.dandaedre.com/gitea/idle-org/pdf-presenter"

View File

@ -892,6 +892,7 @@ pub fn presenter(ui: &mut Ui, s: &mut Studio, native: &mut UiState) {
}
}
pub fn browser(ui: &mut Ui, s: &mut Studio, native: &mut UiState) {
let browser_width = ui.available_width();
let locale = ui.ctx().clone();
ui.painter()
.rect_filled(ui.max_rect(), 0., ui.visuals().panel_fill);
@ -1002,61 +1003,75 @@ pub fn browser(ui: &mut Ui, s: &mut Studio, native: &mut UiState) {
}
));
}
let size = s.settings.thumbnail_size;
let cols = (ui.available_width() / (size + 16.)).floor().max(1.) as usize;
// Reserve a stable gutter, so neither thumbnail sizing nor scrollbar placement
// depends on how much of the last row is populated.
ui.spacing_mut().scroll = egui::style::ScrollStyle::solid();
let width = (browser_width - ui.spacing().scroll.allocated_width()).max(1.);
let gap = 16.;
let cols = ((width + gap) / (s.settings.thumbnail_size + gap))
.floor()
.max(1.) as usize;
let size = ((width - gap * (cols - 1) as f32) / cols as f32).max(1.);
let rows = s.count().div_ceil(cols);
s.visible_thumbs.clear();
egui::ScrollArea::vertical().show_rows(ui, size * 0.5625 + 50., rows, |ui, range| {
for row in range {
ui.horizontal(|ui| {
for col in 0..cols {
let page = row * cols + col;
if page >= s.count() {
break;
}
s.visible_thumbs.insert(page);
ui.vertical(|ui| {
let (rect, response) =
ui.allocate_exact_size(Vec2::new(size, size * 0.5625), Sense::click());
native.image(ui, s, page, rect);
if s.browser_selected == page || s.requested == page {
ui.painter().rect_stroke(
rect,
4.,
egui::Stroke::new(
2_f32,
if s.requested == page {
ui.visuals().hyperlink_color
} else {
Color32::GOLD
},
),
egui::StrokeKind::Inside,
egui::ScrollArea::vertical()
.max_width(browser_width)
.auto_shrink([false, false])
.scroll_bar_visibility(egui::scroll_area::ScrollBarVisibility::AlwaysVisible)
.show_rows(ui, size * 0.5625 + 50., rows, |ui, range| {
ui.spacing_mut().item_spacing.x = gap;
for row in range {
ui.horizontal(|ui| {
for col in 0..cols {
let page = row * cols + col;
if page >= s.count() {
break;
}
s.visible_thumbs.insert(page);
ui.vertical(|ui| {
let (rect, response) = ui.allocate_exact_size(
Vec2::new(size, size * 0.5625),
Sense::click(),
);
}
if response.double_clicked() && !s.change_on_click {
s.browser_preview = Some((s.generation, page));
}
if response.clicked() {
s.browser_selected = page;
if s.change_on_click {
s.actions.push(Action::Go(page));
native.image(ui, s, page, rect);
if s.browser_selected == page || s.requested == page {
ui.painter().rect_stroke(
rect,
4.,
egui::Stroke::new(
2_f32,
if s.requested == page {
ui.visuals().hyperlink_color
} else {
Color32::GOLD
},
),
egui::StrokeKind::Inside,
);
}
}
ui.label(format!(
"{}{}",
page + 1,
if s.requested == page {
tr(&locale, " · CURRENT")
} else {
""
if response.double_clicked() && !s.change_on_click {
s.browser_preview = Some((s.generation, page));
}
));
});
}
});
}
});
if response.clicked() {
s.browser_selected = page;
if s.change_on_click {
s.actions.push(Action::Go(page));
}
}
ui.label(format!(
"{}{}",
page + 1,
if s.requested == page {
tr(&locale, " · CURRENT")
} else {
""
}
));
});
}
});
}
});
if let Some((_, page)) = s.browser_preview {
// Keep preview requests in the browser's bounded asynchronous loading path.
s.visible_thumbs.insert(page);

View File

@ -723,3 +723,47 @@ fn browser_double_click_previews_without_live_navigation() {
}
}
}
#[test]
fn browser_thumbnails_fill_resized_window_without_stretching_tiles() {
fn collect(shape: &egui::Shape, rects: &mut Vec<Rect>) {
match shape {
egui::Shape::Rect(r) if r.fill == egui::Color32::from_rgb(18, 28, 31) => {
rects.push(r.rect)
}
egui::Shape::Vec(shapes) => shapes.iter().for_each(|s| collect(s, rects)),
_ => {}
}
}
let ctx = Context::default();
let mut s = studio();
s.deck.as_mut().unwrap().names = (0..1000).map(|i| format!("Slide {i}")).collect();
let mut native = UiState::default();
for width in [500., 900., 1600.] {
let mut right = 0.;
let mut output = ctx.run_ui(input(Vec2::new(width, 900.)), |ui| {
right = ui.max_rect().right();
presenter_ui::browser(ui, &mut s, &mut native);
});
for _ in 0..3 {
output = ctx.run_ui(input(Vec2::new(width, 900.)), |ui| {
right = ui.max_rect().right();
presenter_ui::browser(ui, &mut s, &mut native);
});
}
let mut rects = vec![];
for shape in &output.shapes {
collect(&shape.shape, &mut rects);
}
assert!(!rects.is_empty());
let max_right = rects.iter().map(Rect::right).fold(0., f32::max);
assert!(
(max_right + egui::style::ScrollStyle::solid().allocated_width() - right).abs() < 1.
);
for rect in &rects {
assert!((rect.height() / rect.width() - 0.5625).abs() < 0.001);
assert!(rect.right() <= right);
}
assert!(s.visible_thumbs.len() < s.count());
}
}

View File

@ -2,6 +2,12 @@
## Unreleased
### 0.1.38
- Keep the All slides scrollbar at the right edge with a stable gutter. Fit
thumbnail columns to the available window width, using Preview size as the
preferred density while retaining slide aspect ratios and virtualized rows.
### 0.1.37
- Double-click a thumbnail in All slides to open a resizable preview without

View File

@ -264,3 +264,7 @@ The rule targets only All slides, not presenter or audience windows.
In All slides, double-click a thumbnail to open a larger, resizable preview inside
the browser. It does not change the presentation. Close it with its X or Escape.
When Change on click is enabled, clicks switch the live slide and do not open previews.
The All slides grid fills the browser width and adapts as you resize it. Preview
size sets the preferred thumbnail density; tiles share the available row width,
with slides fitted without distortion. The scrollbar stays at the right edge.