diff --git a/launcher/src/cull.rs b/launcher/src/cull.rs new file mode 100644 index 000000000..41df05610 --- /dev/null +++ b/launcher/src/cull.rs @@ -0,0 +1,119 @@ +//! Long lists in the page's scroll area (the hundred releases on Versions and Patch notes) draw +//! only the rows near what is on screen. A row far off screen becomes an empty block as tall as +//! it was the last time it was drawn, so the page keeps its length and the scrollbar its place. +use gpui::{div, px, Bounds, Div, IntoElement, ParentElement, Pixels, ScrollHandle, Styled}; +use std::{cell::RefCell, rc::Rc}; + +#[derive(Clone, Default)] +pub struct Cull(Rc>); + +/// Where each row was in the last frame, and the scroll offset and the viewport's width then +/// (a narrower window wraps text, and the heights no longer hold). +#[derive(Default)] +struct Drawn { + rows: Vec>, + offset: Pixels, + width: Pixels, +} + +impl Cull { + /// A column for `rows` rows that remembers where they were drawn. Rows come from `row(i)`, or + /// an empty block when row `i` was more than a screen away from the viewport last frame and + /// `keep(i)` is false (a row being installed or showing an error is always drawn). + pub fn column( + &self, + handle: &ScrollHandle, + rows: usize, + gap: f32, + keep: impl Fn(usize) -> bool, + mut row: impl FnMut(usize) -> E, + ) -> Div { + let far = self.far(handle, rows); + let skipped: Vec> = (0..rows).map(|i| far(i).filter(|_| !keep(i))).collect(); + let gaps = skipped.iter().any(Option::is_some); + let state = self.0.clone(); + let scroll = handle.clone(); + div() + .flex() + .flex_col() + .children(skipped.iter().enumerate().map(|(i, skip)| match skip { + Some(height) => div().flex_none().h(*height).mb(px(gap)).into_any_element(), + None => row(i).into_any_element(), + })) + .on_children_prepainted(move |bounds, window, _| { + let view = scroll.bounds(); + // A jump of more than a screen (the scrollbar dragged) can bring an empty block + // into view: draw again, with the rows where they are now. + if gaps + && bounds + .iter() + .zip(&skipped) + .any(|(b, skip)| skip.is_some() && !away(*b, px(0.), view)) + { + window.refresh(); + } + let mut drawn = state.borrow_mut(); + drawn.rows = bounds; + drawn.offset = scroll.offset().y; + drawn.width = view.size.width; + }) + } + + /// For row `i`: its height when it was more than a screen away from the viewport. + fn far(&self, handle: &ScrollHandle, rows: usize) -> impl Fn(usize) -> Option { + let drawn = self.0.borrow(); + let view = handle.bounds(); + // The rows moved by however far the page scrolled since they were drawn. + let moved = handle.offset().y - drawn.offset; + let known = + drawn.rows.len() == rows && view.size.height > px(0.) && drawn.width == view.size.width; + let rows = if known { + drawn.rows.clone() + } else { + Vec::new() + }; + move |i| { + let b = *rows.get(i)?; + away(b, moved, view).then_some(b.size.height) + } + } +} + +/// Whether a row drawn at `row`, since moved down by `moved`, is more than a screen above or +/// below `view`. +fn away(row: Bounds, moved: Pixels, view: Bounds) -> bool { + row.bottom() + moved < view.top() - view.size.height + || row.top() + moved > view.bottom() + view.size.height +} + +#[cfg(test)] +mod tests { + use super::*; + use gpui::{point, size}; + + fn at(y: f32, h: f32) -> Bounds { + Bounds::new(point(px(0.), px(y)), size(px(10.), px(h))) + } + + #[test] + fn only_rows_more_than_a_screen_away_are_skipped() { + let view = at(100., 500.); + assert!(!away(at(100., 100.), px(0.), view)); + assert!(!away(at(1000., 100.), px(0.), view)); + assert!(away(at(1200., 100.), px(0.), view)); + assert!(away(at(-600., 100.), px(0.), view)); + // Scrolled 700 px down since: the row that was far below is now on screen. + assert!(!away(at(1200., 100.), px(-700.), view)); + } + + #[test] + fn nothing_is_skipped_before_the_rows_are_known() { + let cull = Cull::default(); + cull.0.borrow_mut().rows = (0..10).map(|i| at(i as f32 * 2000., 100.)).collect(); + // No viewport yet (the first frame), or another number of rows: every row is drawn. + let far = cull.far(&ScrollHandle::new(), 10); + assert!((0..10).all(|i| far(i).is_none())); + let far = cull.far(&ScrollHandle::new(), 11); + assert!((0..11).all(|i| far(i).is_none())); + } +} diff --git a/launcher/src/main.rs b/launcher/src/main.rs index 365c952f4..cfee78d63 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -5,6 +5,7 @@ mod art; mod blocks; mod cli; mod collection; +mod cull; mod game_tools; mod internet; mod markdown; @@ -123,6 +124,8 @@ struct Launcher { /// How many times the page changed, for the page transition. switches: usize, content_scroll: Scrollbar, + /// The rows of the page's long list (Versions, Patch notes) last drawn. + long_list: cull::Cull, opening: Option, updates: Updates, checked: Option, @@ -256,6 +259,7 @@ impl Launcher { page: Page::from_env(), switches: 0, content_scroll: Scrollbar::default(), + long_list: cull::Cull::default(), opening: (!settings.skip_opening).then(|| { let focus = cx.focus_handle(); window.focus(&focus); @@ -357,6 +361,7 @@ impl Launcher { self.page = page; self.switches += 1; self.content_scroll = Scrollbar::default(); + self.long_list = cull::Cull::default(); } self.picker_open = false; } @@ -2974,23 +2979,36 @@ impl Launcher { Load::Ready(r) if r.is_empty() => return page.child(centered("No releases yet.", &t)), Load::Ready(r) => r.clone(), }; - let mut list = div().flex().flex_col(); - for release in releases.into_iter() { - let tag = release.tag.clone(); - let installed = self.installed.contains(&tag); - let selected = self.selected() == Some(&tag); - let channel = backend::UpdateChannel::of(&release).label(); - let mut meta = vec![channel.to_string(), release.date.clone()]; - if let Some((_, size)) = release.asset { - meta.push(megabytes(size)); - } - let summary = release - .name - .split_once(": ") - .map(|(_, rest)| rest.to_string()) - .unwrap_or_else(|| release.name.clone()); - let right: gpui::AnyElement = - if let Some((_, p)) = self.installing.as_ref().filter(|(t, _)| *t == tag) { + // A hundred releases: only the rows near the screen are drawn. + let keep = |i: usize| { + let tag = &releases[i].tag; + self.installing.as_ref().is_some_and(|(t, _)| t == tag) + || self.install_error.as_ref().is_some_and(|(t, _)| t == tag) + || self.confirm_remove.as_ref() == Some(tag) + }; + let list = self.long_list.clone().column( + &self.content_scroll.handle, + releases.len(), + 8., + keep, + |i| { + let release = releases[i].clone(); + let tag = release.tag.clone(); + let installed = self.installed.contains(&tag); + let selected = self.selected() == Some(&tag); + let channel = backend::UpdateChannel::of(&release).label(); + let mut meta = vec![channel.to_string(), release.date.clone()]; + if let Some((_, size)) = release.asset { + meta.push(megabytes(size)); + } + let summary = release + .name + .split_once(": ") + .map(|(_, rest)| rest.to_string()) + .unwrap_or_else(|| release.name.clone()); + let right: gpui::AnyElement = if let Some((_, p)) = + self.installing.as_ref().filter(|(t, _)| *t == tag) + { div() .flex() .items_center() @@ -3067,76 +3085,77 @@ impl Launcher { } b.into_any_element() }; - let status = if selected { - Some(caps("In use").text_color(t.accent)) - } else if installed { - Some(caps("Installed").text_color(t.ink_soft)) - } else { - None - }; - let error = self - .install_error - .as_ref() - .filter(|(t, _)| *t == tag) - .map(|(_, e)| e.clone()); - let id = SharedString::from(format!("release-{tag}")); - let row = div() - .id(id.clone()) - .relative() - .flex() - .flex_col() - .gap(px(10.)) - .py(px(14.)) - .px(px(16.)) - .mb(px(8.)) - .bg(t.panel) - .border_2() - .border_color(t.outline) - .children(blocks::edges(t.bevel, t.lip, theme::BEVEL)) - .child(hover_layer(&id, t.raised, 0., 0.)) - .child( - div() - .relative() - .flex() - .items_center() - .gap(px(16.)) - .child( - div() - .flex() - .flex_col() - .flex_1() - .min_w_0() - .gap(px(4.)) - .child(version_tag(&tag).text_color(t.ink)) - .child( - text(theme::UI) - .text_color(t.muted) - .truncate() - .child(summary), - ), - ) - .child( - div() - .flex() - .flex_col() - .items_end() - .gap(px(2.)) - .flex_none() - .children(status) - .child(caps(meta.join(" · ")).text_color(t.muted)), - ) - .child( - div() - .flex() - .justify_end() - .w(px(190.)) - .flex_none() - .child(right), - ), - ) - .children(error.map(|e| notice(e, &t))); - list = list.child(track_hover(row, id)); - } + let status = if selected { + Some(caps("In use").text_color(t.accent)) + } else if installed { + Some(caps("Installed").text_color(t.ink_soft)) + } else { + None + }; + let error = self + .install_error + .as_ref() + .filter(|(t, _)| *t == tag) + .map(|(_, e)| e.clone()); + let id = SharedString::from(format!("release-{tag}")); + let row = div() + .id(id.clone()) + .relative() + .flex() + .flex_col() + .gap(px(10.)) + .py(px(14.)) + .px(px(16.)) + .mb(px(8.)) + .bg(t.panel) + .border_2() + .border_color(t.outline) + .children(blocks::edges(t.bevel, t.lip, theme::BEVEL)) + .child(hover_layer(&id, t.raised, 0., 0.)) + .child( + div() + .relative() + .flex() + .items_center() + .gap(px(16.)) + .child( + div() + .flex() + .flex_col() + .flex_1() + .min_w_0() + .gap(px(4.)) + .child(version_tag(&tag).text_color(t.ink)) + .child( + text(theme::UI) + .text_color(t.muted) + .truncate() + .child(summary), + ), + ) + .child( + div() + .flex() + .flex_col() + .items_end() + .gap(px(2.)) + .flex_none() + .children(status) + .child(caps(meta.join(" · ")).text_color(t.muted)), + ) + .child( + div() + .flex() + .justify_end() + .w(px(190.)) + .flex_none() + .child(right), + ), + ) + .children(error.map(|e| notice(e, &t))); + track_hover(row, id) + }, + ); page.child(list) } @@ -3198,14 +3217,18 @@ impl Launcher { } Load::Ready(r) => r.clone(), }; - let mut list = div().flex().flex_col(); - for release in releases.iter() { - let summary = release - .name - .split_once(": ") - .map(|(_, rest)| rest.to_string()) - .unwrap_or_default(); - list = list.child( + let list = self.long_list.clone().column( + &self.content_scroll.handle, + releases.len(), + 10., + |_| false, + |i| { + let release = &releases[i]; + let summary = release + .name + .split_once(": ") + .map(|(_, rest)| rest.to_string()) + .unwrap_or_default(); div() .flex() .flex_col() @@ -3247,9 +3270,9 @@ impl Launcher { } else { markdown::render(&release.body, &t) }), - ), - ); - } + ) + }, + ); page.child(list) } } diff --git a/launcher/src/mods_page.rs b/launcher/src/mods_page.rs index f2dde2447..9cb834797 100644 --- a/launcher/src/mods_page.rs +++ b/launcher/src/mods_page.rs @@ -220,6 +220,22 @@ impl Launcher { Event::Loaded(l) => loaded.push(l), } } + // `HORIZON_LAUNCHER_PROJECT=` (with `HORIZON_LAUNCHER_PAGE=mods`): opens that + // project's page once a search finds it, for screenshots. + if self.mods.browse.open.is_none() { + if let Ok(want) = std::env::var("HORIZON_LAUNCHER_PROJECT") { + if let Some(p) = self + .mods + .results + .iter() + .find(|p| p.id == want || p.slug == want) + .cloned() + { + std::env::remove_var("HORIZON_LAUNCHER_PROJECT"); + self.mods_open(p); + } + } + } self.mods_check(to_check); for l in loaded { self.mods_loaded(l); @@ -848,14 +864,7 @@ fn source_switch(t: &Theme) -> Stateful
{ .text_color(t.muted) .child("CurseForge (coming soon)"), ); - segments( - "mods-source", - &SOURCES, - SourceId::Modrinth, - t, - |_, _, _| {}, - ) - .child(soon) + segments("mods-source", &SOURCES, SourceId::Modrinth, t, |_, _, _| {}).child(soon) } #[cfg(test)]