Add pointer-operated scrollbars throughout the launcher
This commit is contained in:
@@ -88,6 +88,9 @@ page `servers.json` (the list), `servers/<name>/` (the servers created there) an
|
||||
(`{"discord": "...", "sponsor": "...", "website": "..."}`), so they change without a launcher release. Offline,
|
||||
the launcher uses the same values built in; an empty address hides its button.
|
||||
|
||||
Long pages, dialogs, the server console and data previews have a visible scrollbar when
|
||||
their content overflows. Drag the thumb or click the track to navigate without a mouse wheel.
|
||||
|
||||
## Updates
|
||||
|
||||
The launcher looks for a newer Horizon release when it starts and every four hours while it
|
||||
|
||||
+24
-18
@@ -7,6 +7,8 @@ use horizon_telemetry::{Choices, Consent, Kind as DataKind};
|
||||
/// What the window keeps about data collection.
|
||||
pub(crate) struct Collection {
|
||||
pub consent: Consent,
|
||||
body_scroll: Scrollbar,
|
||||
json_scroll: Scrollbar,
|
||||
/// The question's switches, until the player answers.
|
||||
pub draft: Choices,
|
||||
/// "View what would be sent": a line on what it is, and the JSON.
|
||||
@@ -20,6 +22,8 @@ impl Collection {
|
||||
pub fn load() -> Self {
|
||||
Self {
|
||||
consent: telemetry::consent(),
|
||||
body_scroll: Scrollbar::default(),
|
||||
json_scroll: Scrollbar::default(),
|
||||
draft: Choices::NONE,
|
||||
view: None,
|
||||
note: None,
|
||||
@@ -42,7 +46,7 @@ fn kind_id(prefix: &str, kind: DataKind) -> &'static str {
|
||||
}
|
||||
|
||||
/// The exact JSON, in mono, scrolling inside a plate.
|
||||
fn json_view(view: &(String, String), t: &Theme) -> Div {
|
||||
fn json_view(view: &(String, String), t: &Theme, scroll: &Scrollbar) -> Div {
|
||||
div()
|
||||
.flex()
|
||||
.flex_col()
|
||||
@@ -53,20 +57,21 @@ fn json_view(view: &(String, String), t: &Theme) -> Div {
|
||||
.child(view.0.clone()),
|
||||
)
|
||||
.child(
|
||||
div()
|
||||
.id("data-json")
|
||||
.max_h(px(260.))
|
||||
.overflow_y_scroll()
|
||||
// GPUI lets wheel events reach scroll containers behind this one unless stopped.
|
||||
.on_scroll_wheel(|_, _, cx| cx.stop_propagation())
|
||||
.p(px(16.))
|
||||
.rounded(px(theme::RADIUS_PLATE))
|
||||
.bg(t.plate)
|
||||
.font_family(theme::MONO)
|
||||
.text_size(px(theme::CAPTION.px))
|
||||
.line_height(px(18.))
|
||||
.text_color(t.ink_soft)
|
||||
.children(view.1.lines().map(|l| div().child(l.to_string()))),
|
||||
scroll.wrap(
|
||||
div()
|
||||
.id("data-json")
|
||||
.max_h(px(260.))
|
||||
.overflow_y_scroll()
|
||||
.p(px(16.))
|
||||
.rounded(px(theme::RADIUS_PLATE))
|
||||
.bg(t.plate)
|
||||
.font_family(theme::MONO)
|
||||
.text_size(px(theme::CAPTION.px))
|
||||
.line_height(px(18.))
|
||||
.text_color(t.ink_soft)
|
||||
.children(view.1.lines().map(|l| div().child(l.to_string()))),
|
||||
t,
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -160,7 +165,7 @@ impl Launcher {
|
||||
cx.notify();
|
||||
})),
|
||||
)
|
||||
.children(self.collection.view.as_ref().map(|v| json_view(v, &t)));
|
||||
.children(self.collection.view.as_ref().map(|v| json_view(v, &t, &self.collection.json_scroll)));
|
||||
let answers = div()
|
||||
.flex()
|
||||
.flex_none()
|
||||
@@ -193,7 +198,7 @@ impl Launcher {
|
||||
.bg(t.page)
|
||||
.border_1()
|
||||
.border_color(t.rule)
|
||||
.child(body)
|
||||
.child(self.collection.body_scroll.wrap(body, &t))
|
||||
.child(answers);
|
||||
div()
|
||||
.id("consent")
|
||||
@@ -208,6 +213,7 @@ impl Launcher {
|
||||
.py(px(24.))
|
||||
.bg(t.page.opacity(0.92))
|
||||
.on_scroll_wheel(|_, _, cx| cx.stop_propagation())
|
||||
.occlude()
|
||||
.child(card)
|
||||
}
|
||||
|
||||
@@ -315,6 +321,6 @@ impl Launcher {
|
||||
.child(list)
|
||||
.child(text(theme::CAPTION).text_color(t.muted).child(telemetry::NEVER))
|
||||
.children(self.collection.note.clone().map(|n| text(theme::UI).text_color(t.ink).child(n)))
|
||||
.children(self.collection.view.as_ref().map(|v| json_view(v, &t)))
|
||||
.children(self.collection.view.as_ref().map(|v| json_view(v, &t, &self.collection.json_scroll)))
|
||||
}
|
||||
}
|
||||
|
||||
+25
-15
@@ -9,18 +9,20 @@ mod markdown;
|
||||
mod mcp;
|
||||
mod modpacks_page;
|
||||
mod perf;
|
||||
mod scrollbar;
|
||||
mod servers_page;
|
||||
mod theme;
|
||||
|
||||
use gpui::{
|
||||
div, ease_out_quint, prelude::*, px, relative, size, Animation, AnimationExt, AnyElement, App,
|
||||
Application, Bounds, ClickEvent, Context, Div, ElementId, FocusHandle, FontWeight, Hsla,
|
||||
KeyDownEvent, PathPromptOptions, ScrollHandle, SharedString, Stateful, Subscription,
|
||||
TitlebarOptions, Window, WindowBounds, WindowOptions,
|
||||
KeyDownEvent, PathPromptOptions, SharedString, Stateful, Subscription, TitlebarOptions, Window,
|
||||
WindowBounds, WindowOptions,
|
||||
};
|
||||
use horizon_launcher::account::{self, Accounts, SignIn, SignInState};
|
||||
use horizon_launcher::backend::{self, Job, Msg, Release, Running, Settings, Updates};
|
||||
use horizon_launcher::telemetry;
|
||||
use scrollbar::Scrollbar;
|
||||
use std::borrow::Cow;
|
||||
use std::collections::VecDeque;
|
||||
use std::sync::atomic::Ordering;
|
||||
@@ -103,6 +105,7 @@ struct Launcher {
|
||||
page: Page,
|
||||
/// How many times the page changed, for the page transition.
|
||||
switches: usize,
|
||||
content_scroll: Scrollbar,
|
||||
opening: Option<Opening>,
|
||||
updates: Updates,
|
||||
checked: Option<Instant>,
|
||||
@@ -223,6 +226,7 @@ impl Launcher {
|
||||
theme: Theme::for_appearance(window.appearance()),
|
||||
page: Page::Play,
|
||||
switches: 0,
|
||||
content_scroll: Scrollbar::default(),
|
||||
opening: (!settings.skip_opening).then(|| {
|
||||
let focus = cx.focus_handle();
|
||||
window.focus(&focus);
|
||||
@@ -314,6 +318,7 @@ impl Launcher {
|
||||
if page != self.page {
|
||||
self.page = page;
|
||||
self.switches += 1;
|
||||
self.content_scroll = Scrollbar::default();
|
||||
}
|
||||
self.picker_open = false;
|
||||
}
|
||||
@@ -2745,20 +2750,25 @@ impl Render for Launcher {
|
||||
.top(px(theme::RISE * (1. - ui)))
|
||||
.child(self.topbar(cx))
|
||||
.child(
|
||||
div()
|
||||
.id("content")
|
||||
.relative()
|
||||
.flex_1()
|
||||
.min_h_0()
|
||||
.overflow_y_scroll()
|
||||
.child(
|
||||
self.content_scroll
|
||||
.wrap(
|
||||
div()
|
||||
.flex()
|
||||
.px(px(theme::GUTTER))
|
||||
.pt(px(24.))
|
||||
.pb(px(48.))
|
||||
.child(content),
|
||||
),
|
||||
.id("content")
|
||||
.relative()
|
||||
.flex_1()
|
||||
.min_h_0()
|
||||
.overflow_y_scroll()
|
||||
.child(
|
||||
div()
|
||||
.flex()
|
||||
.px(px(theme::GUTTER))
|
||||
.pt(px(24.))
|
||||
.pb(px(48.))
|
||||
.child(content),
|
||||
),
|
||||
&t,
|
||||
)
|
||||
.flex_1(),
|
||||
)
|
||||
.child(self.footer(cx)),
|
||||
);
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
//! Pointer-operated scrollbars for the launcher's vertical scroll areas.
|
||||
use crate::theme::Theme;
|
||||
use gpui::{
|
||||
canvas, div, fill, point, prelude::*, px, size, Bounds, Div, HitboxBehavior, MouseButton,
|
||||
MouseDownEvent, MouseMoveEvent, MouseUpEvent, ScrollHandle, Stateful,
|
||||
};
|
||||
use std::{cell::Cell, rc::Rc};
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
pub(crate) struct Scrollbar {
|
||||
pub handle: ScrollHandle,
|
||||
/// Distance from the top of the thumb to the point grabbed by the pointer.
|
||||
grabbed: Rc<Cell<Option<f32>>>,
|
||||
}
|
||||
|
||||
/// Geometry in logical pixels. A minimum thumb size keeps long lists easy to grab.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
struct Geometry {
|
||||
thumb: f32,
|
||||
travel: f32,
|
||||
top: f32,
|
||||
max: f32,
|
||||
}
|
||||
|
||||
impl Geometry {
|
||||
fn new(height: f32, viewport: f32, max: f32, offset: f32) -> Option<Self> {
|
||||
if height <= 0. || viewport <= 0. || max <= 0. {
|
||||
return None;
|
||||
}
|
||||
let thumb = (height * viewport / (viewport + max)).max(32_f32.min(height / 2.));
|
||||
let travel = height - thumb;
|
||||
Some(Self {
|
||||
thumb,
|
||||
travel,
|
||||
top: (-offset / max).clamp(0., 1.) * travel,
|
||||
max,
|
||||
})
|
||||
}
|
||||
|
||||
fn offset(self, thumb_top: f32) -> f32 {
|
||||
if self.travel <= 0. {
|
||||
return 0.;
|
||||
}
|
||||
-(thumb_top / self.travel).clamp(0., 1.) * self.max
|
||||
}
|
||||
}
|
||||
|
||||
impl Scrollbar {
|
||||
/// Wrap the scroll container, reserving a narrow gutter so the bar never covers controls.
|
||||
pub fn wrap(&self, content: Stateful<Div>, theme: &Theme) -> Div {
|
||||
let state = self.clone();
|
||||
let track_color = theme.rule;
|
||||
let thumb_color = theme.muted;
|
||||
div()
|
||||
.relative()
|
||||
.flex()
|
||||
.flex_col()
|
||||
.min_h_0()
|
||||
.min_w_0()
|
||||
.child(
|
||||
content
|
||||
.track_scroll(&self.handle)
|
||||
.mr(px(16.))
|
||||
// Nested scroll areas must not also move the page behind them.
|
||||
.on_scroll_wheel(|_, _, cx| cx.stop_propagation()),
|
||||
)
|
||||
.child(
|
||||
canvas(
|
||||
move |bounds, window, _| {
|
||||
let geometry = Geometry::new(
|
||||
bounds.size.height.into(),
|
||||
state.handle.bounds().size.height.into(),
|
||||
state.handle.max_offset().height.into(),
|
||||
state.handle.offset().y.into(),
|
||||
);
|
||||
let hitbox =
|
||||
geometry.map(|_| window.insert_hitbox(bounds, HitboxBehavior::Normal));
|
||||
(state, geometry, hitbox)
|
||||
},
|
||||
move |bounds, (state, geometry, hitbox), window, _| {
|
||||
let Some(g) = geometry else {
|
||||
state.grabbed.set(None);
|
||||
return;
|
||||
};
|
||||
let hitbox = hitbox.unwrap();
|
||||
window.paint_quad(fill(bounds, track_color).corner_radii(px(3.)));
|
||||
window.paint_quad(
|
||||
fill(
|
||||
Bounds::new(
|
||||
point(bounds.left() + px(3.), bounds.top() + px(g.top)),
|
||||
size(bounds.size.width - px(6.), px(g.thumb)),
|
||||
),
|
||||
thumb_color,
|
||||
)
|
||||
.corner_radii(px(3.)),
|
||||
);
|
||||
window.on_mouse_event({
|
||||
let state = state.clone();
|
||||
move |ev: &MouseDownEvent, phase, window, cx| {
|
||||
if !phase.bubble()
|
||||
|| ev.button != MouseButton::Left
|
||||
|| !hitbox.is_hovered(window)
|
||||
{
|
||||
return;
|
||||
}
|
||||
let y = f32::from(ev.position.y - bounds.top());
|
||||
let grab = if (g.top..=g.top + g.thumb).contains(&y) {
|
||||
y - g.top
|
||||
} else {
|
||||
// Clicking the track centers the thumb at the pointer.
|
||||
let grab = g.thumb / 2.;
|
||||
let mut offset = state.handle.offset();
|
||||
offset.y = px(g.offset(y - grab));
|
||||
state.handle.set_offset(offset);
|
||||
grab
|
||||
};
|
||||
state.grabbed.set(Some(grab));
|
||||
cx.stop_propagation();
|
||||
window.refresh();
|
||||
}
|
||||
});
|
||||
window.on_mouse_event({
|
||||
let state = state.clone();
|
||||
move |ev: &MouseMoveEvent, phase, window, cx| {
|
||||
if !phase.capture() {
|
||||
return;
|
||||
}
|
||||
let Some(grab) = state.grabbed.get() else {
|
||||
return;
|
||||
};
|
||||
if ev.pressed_button != Some(MouseButton::Left) {
|
||||
state.grabbed.set(None);
|
||||
return;
|
||||
}
|
||||
let mut offset = state.handle.offset();
|
||||
offset.y =
|
||||
px(g.offset(f32::from(ev.position.y - bounds.top()) - grab));
|
||||
state.handle.set_offset(offset);
|
||||
cx.stop_propagation();
|
||||
window.refresh();
|
||||
}
|
||||
});
|
||||
window.on_mouse_event(move |ev: &MouseUpEvent, phase, window, cx| {
|
||||
if phase.capture()
|
||||
&& ev.button == MouseButton::Left
|
||||
&& state.grabbed.take().is_some()
|
||||
{
|
||||
cx.stop_propagation();
|
||||
window.refresh();
|
||||
}
|
||||
});
|
||||
},
|
||||
)
|
||||
.absolute()
|
||||
.right(px(2.))
|
||||
.top_0()
|
||||
.bottom_0()
|
||||
.w(px(12.)),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Geometry;
|
||||
|
||||
#[test]
|
||||
fn hides_without_overflow() {
|
||||
assert!(Geometry::new(200., 200., 0., 0.).is_none());
|
||||
assert!(Geometry::new(0., 200., 100., 0.).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn thumb_tracks_wheel_offsets_and_pointer_reaches_both_ends() {
|
||||
let g = Geometry::new(200., 200., 600., -300.).unwrap();
|
||||
assert_eq!(g.thumb, 50.);
|
||||
assert_eq!(g.top, 75.);
|
||||
assert_eq!(g.offset(g.top), -300.);
|
||||
assert_eq!(g.offset(-100.), 0.);
|
||||
assert_eq!(g.offset(1000.), -600.);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn long_lists_and_small_viewports_keep_geometry_bounded() {
|
||||
let g = Geometry::new(200., 200., 20000., -20000.).unwrap();
|
||||
assert_eq!(g.thumb, 32.);
|
||||
assert_eq!(g.top, g.travel);
|
||||
assert_eq!(g.offset(g.travel), -20000.);
|
||||
let tiny = Geometry::new(20., 20., 1000., -500.).unwrap();
|
||||
assert_eq!(tiny.thumb, 10.);
|
||||
assert_eq!(tiny.offset(10.), -1000.);
|
||||
}
|
||||
}
|
||||
@@ -125,7 +125,8 @@ pub(super) struct Servers {
|
||||
log: VecDeque<String>,
|
||||
log_next: u64,
|
||||
log_dir: Option<PathBuf>,
|
||||
console_scroll: ScrollHandle,
|
||||
console_scroll: Scrollbar,
|
||||
create_scroll: Scrollbar,
|
||||
/// The action under way, and what it printed.
|
||||
busy: Option<String>,
|
||||
job_log: VecDeque<String>,
|
||||
@@ -175,7 +176,8 @@ impl Servers {
|
||||
log: VecDeque::new(),
|
||||
log_next: 0,
|
||||
log_dir: None,
|
||||
console_scroll: ScrollHandle::new(),
|
||||
console_scroll: Scrollbar::default(),
|
||||
create_scroll: Scrollbar::default(),
|
||||
busy: None,
|
||||
job_log: VecDeque::new(),
|
||||
problem: None,
|
||||
@@ -548,7 +550,7 @@ impl Launcher {
|
||||
while s.log.len() > CONSOLE_KEEP {
|
||||
s.log.pop_front();
|
||||
}
|
||||
s.console_scroll.scroll_to_bottom();
|
||||
s.console_scroll.handle.scroll_to_bottom();
|
||||
}
|
||||
s.log_next = next;
|
||||
}
|
||||
@@ -701,7 +703,7 @@ impl Launcher {
|
||||
};
|
||||
let tx = self.servers.tx.clone();
|
||||
self.servers.log.push_back(format!("> {line}"));
|
||||
self.servers.console_scroll.scroll_to_bottom();
|
||||
self.servers.console_scroll.handle.scroll_to_bottom();
|
||||
std::thread::spawn(move || {
|
||||
let result = servers::Api::open(&dir)
|
||||
.ok_or_else(|| "The server isn't running.".to_string())
|
||||
@@ -786,6 +788,7 @@ impl Launcher {
|
||||
self.servers.text(Field::Seed).clear();
|
||||
*self.servers.text(Field::Players) = own_name(&self.accounts).unwrap_or_default();
|
||||
self.servers.problem = None;
|
||||
self.servers.create_scroll = Scrollbar::default();
|
||||
self.servers.create = Some(Create {
|
||||
new_world: true,
|
||||
copy: worlds.first().map(|w| w.1.clone()),
|
||||
@@ -1672,7 +1675,6 @@ impl Launcher {
|
||||
.px(px(20.))
|
||||
.py(px(12.))
|
||||
.overflow_y_scroll()
|
||||
.track_scroll(&self.servers.console_scroll)
|
||||
.font_family(theme::MONO)
|
||||
.text_size(px(theme::CAPTION.px))
|
||||
.line_height(px(19.))
|
||||
@@ -1685,6 +1687,7 @@ impl Launcher {
|
||||
}))
|
||||
})
|
||||
.children(lines);
|
||||
let log = self.servers.console_scroll.wrap(log, &t);
|
||||
let input = if running {
|
||||
self.field(
|
||||
Field::Command,
|
||||
@@ -2909,7 +2912,7 @@ impl Launcher {
|
||||
.bg(t.page)
|
||||
.border_1()
|
||||
.border_color(t.rule)
|
||||
.child(body)
|
||||
.child(self.servers.create_scroll.wrap(body, &t))
|
||||
.child(foot);
|
||||
Some(
|
||||
div()
|
||||
|
||||
Reference in New Issue
Block a user