diff --git a/launcher/README.md b/launcher/README.md index cf968f93c..5e38e9fd9 100644 --- a/launcher/README.md +++ b/launcher/README.md @@ -97,6 +97,10 @@ page `servers.json` (the list), `servers//` (the servers created there) an 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. +Pages and Home's landscape fill the window as it resizes. Windows uses a matching title +bar with native dragging, resizing, Snap, +minimize, maximize/restore and close; macOS and Linux keep their system title bars. + ## Updates The launcher looks for a newer Horizon release when it starts and every 30 minutes while it diff --git a/launcher/src/art.rs b/launcher/src/art.rs index ecae80714..d61214c11 100644 --- a/launcher/src/art.rs +++ b/launcher/src/art.rs @@ -2,39 +2,36 @@ //! and the landscape behind Home (the vanilla grass block is `browser::images::grass_block`). //! GPUI samples images smoothly, so every image is baked at the window's scale with each texel a //! whole block of device pixels, and drawn at exactly its size: the edges stay sharp. Each image -//! is made once per appearance and scale, then reused. -use gpui::RenderImage; +//! is made once per appearance and scale, then reused; Home's landscape is made again only when +//! the window grows wider than it. +use gpui::{RenderImage, Window}; use horizon_theme as tokens; use std::collections::HashMap; use std::sync::Arc; -/// What an image is, to find it again. -#[derive(Clone, PartialEq, Eq, Hash)] -enum Key { - Stone { dark: bool, scale: u32 }, - Scene { dark: bool, scale: u32 }, -} - #[derive(Default)] pub struct Art { - made: HashMap>, + /// The stone texture for each appearance and scale. + stones: HashMap<(bool, u32), Arc>, + /// Home's landscape: its appearance and scale, and its width in texels. + scene: Option<((bool, u32), usize, Arc)>, + /// Landscapes replaced since the last frame, still in the window's sprite atlas. + replaced: Vec>, } /// The stone texture comes in squares of this many logical pixels (4 by 4 tiles). pub const STONE: f32 = tokens::TILE_TEXEL * (tokens::TILE_TEXELS * 4) as f32; -/// Home's landscape: texels of this many logical pixels, and its size in texels. +/// Home's landscape: texels of this many logical pixels, and its height in texels. Its width +/// grows in steps of `SCENE_STEP` texels, so resizing a window rarely makes another. pub const SCENE_TEXEL: f32 = 6.; -pub const SCENE_SIZE: (usize, usize) = (220, 56); +pub const SCENE_HEIGHT: usize = 56; +const SCENE_STEP: usize = 64; impl Art { /// The page's stone texture, for a window at `scale`. pub fn stone(&mut self, page: u32, dark: bool, scale: f32) -> Arc { - let key = Key::Stone { - dark, - scale: scale_key(scale), - }; - self.made - .entry(key) + self.stones + .entry((dark, scale_key(scale))) .or_insert_with(|| { let n = tokens::TILE_TEXELS * 4; bake(&opaque(&stone(page, n)), n, n, tokens::TILE_TEXEL, scale) @@ -42,20 +39,30 @@ impl Art { .clone() } - /// Home's landscape: a sunset in the dark appearance, a dawn in the light one. - pub fn scene(&mut self, dark: bool, scale: f32) -> Arc { - let key = Key::Scene { - dark, - scale: scale_key(scale), - }; - self.made - .entry(key) - .or_insert_with(|| { - let (w, h) = SCENE_SIZE; - let palette = if dark { &SUNSET } else { &DAWN }; - bake(&opaque(&scene(palette, 3, w, h)), w, h, SCENE_TEXEL, scale) - }) - .clone() + /// Home's landscape, at least `width` logical pixels wide (the banner clips the rest), and + /// its width: a sunset in the dark appearance, a dawn in the light one. + pub fn scene(&mut self, dark: bool, scale: f32, width: f32) -> (Arc, f32) { + let key = (dark, scale_key(scale)); + let texels = (width / SCENE_TEXEL).ceil().max(1.) as usize; + if !matches!(&self.scene, Some((k, w, _)) if *k == key && *w >= texels) { + let w = texels.next_multiple_of(SCENE_STEP); + let palette = if dark { &SUNSET } else { &DAWN }; + let texels = opaque(&scene(palette, 3, w, SCENE_HEIGHT)); + let image = bake(&texels, w, SCENE_HEIGHT, SCENE_TEXEL, scale); + if let Some((_, _, old)) = self.scene.replace((key, w, image)) { + self.replaced.push(old); + } + } + let (_, w, image) = self.scene.as_ref().expect("made above"); + (image.clone(), *w as f32 * SCENE_TEXEL) + } + + /// Frees the landscapes replaced since the last frame: GPUI keeps an image in the window's + /// sprite atlas until it is dropped there. + pub fn free_replaced(&mut self, window: &mut Window) { + for image in self.replaced.drain(..) { + let _ = window.drop_image(image); + } } } @@ -199,8 +206,14 @@ pub const DAWN: Palette = Palette { /// A blocky landscape, `w` by `h` texels: banded sky, a sun half set behind the far hills and cut /// into slices like Horizon's mark, flat clouds, three ridges of hills and a few square trees. +/// Each part draws from its own generator from left to right, so a wider landscape only adds +/// columns: the first `w` are the same at every width, and a resized window keeps its view. pub fn scene(p: &Palette, seed: u32, w: usize, h: usize) -> Vec { - let mut rng = Rng(seed.wrapping_mul(0x2545_f491) | 1); + let mut seeds = Rng(seed.wrapping_mul(0x2545_f491) | 1); + let mut rng = || { + seeds.next(); + Rng(seeds.0) + }; let mut out = vec![0; w * h]; let horizon = h * 62 / 100; let band = horizon as f32 / p.sky.len() as f32; @@ -223,20 +236,23 @@ pub fn scene(p: &Palette, seed: u32, w: usize, h: usize) -> Vec { } } }; - for _ in 0..4 { - let cx = (rng.next() * w as f32) as isize; - let cy = (horizon as f32 * (0.12 + rng.next() * 0.4)) as isize; - let cw = 10 + (rng.next() * 14.) as usize; - let under = p.sky[((cy as f32 / band) as usize + 1).min(p.sky.len() - 1)]; - let c = mix(under, 0xffffff, 0.12); - rect(&mut out, cx, cy, cw, 2, c); - rect(&mut out, cx + 3, cy - 2, cw.saturating_sub(6), 2, c); + // Four clouds in every stretch of `CLOUDS` texels. + const CLOUDS: usize = 220; + let mut clouds = rng(); + for stretch in 0..w.div_ceil(CLOUDS) { + for _ in 0..4 { + let cx = (stretch * CLOUDS) as isize + (clouds.next() * CLOUDS as f32) as isize; + let cy = (horizon as f32 * (0.12 + clouds.next() * 0.4)) as isize; + let cw = 10 + (clouds.next() * 14.) as usize; + let under = p.sky[((cy as f32 / band) as usize + 1).min(p.sky.len() - 1)]; + let c = mix(under, 0xffffff, 0.12); + rect(&mut out, cx, cy, cw, 2, c); + rect(&mut out, cx + 3, cy - 2, cw.saturating_sub(6), 2, c); + } } + // The sun, where it was when the landscape had one width (two thirds of 220 texels). let r = (h as f32 * 0.2) as isize; - let (cx, cy) = ( - (w as f32 * 0.68) as isize, - horizon as isize - (h as f32 * 0.11) as isize, - ); + let (cx, cy) = (149, horizon as isize - (h as f32 * 0.11) as isize); for dy in -r..=0 { let half = ((r * r - dy * dy) as f32).sqrt().round() as isize; let t = (dy + r) as f32 / r as f32; @@ -251,7 +267,7 @@ pub fn scene(p: &Palette, seed: u32, w: usize, h: usize) -> Vec { rect(&mut out, cx - half, cy + dy, (half * 2) as usize, 1, c); } } - let ridge = |rng: &mut Rng, base: f32, amp: f32, rough: f32| -> Vec { + let ridge = |mut rng: Rng, base: f32, amp: f32, rough: f32| -> Vec { let (mut y, mut v) = (base, 0.); (0..w) .map(|x| { @@ -277,7 +293,7 @@ pub fn scene(p: &Palette, seed: u32, w: usize, h: usize) -> Vec { ]; let mut near = Vec::new(); for (c, base, amp, rough, top) in layers { - let hs = ridge(&mut rng, base, amp, rough); + let hs = ridge(rng(), base, amp, rough); for (x, &y) in hs.iter().enumerate() { for yy in y.min(h)..h { out[yy * w + x] = c; @@ -288,9 +304,12 @@ pub fn scene(p: &Palette, seed: u32, w: usize, h: usize) -> Vec { } near = hs; } - for _ in 0..w / 26 { - let tx = 4 + (rng.next() * (w - 10) as f32) as isize; - let ty = near[tx as usize] as isize; + // A tree somewhere in every 26 texels of the near hills. + let mut trees = rng(); + for cell in 0..w.div_ceil(26) { + let tx = cell * 26 + 2 + (trees.next() * 22.) as usize; + let Some(&ty) = near.get(tx) else { break }; + let (tx, ty) = (tx as isize, ty as isize); rect(&mut out, tx - 2, ty - 8, 5, 5, p.trees); rect(&mut out, tx - 1, ty - 10, 3, 2, p.trees); rect(&mut out, tx, ty - 3, 1, 3, p.trees); @@ -320,4 +339,39 @@ mod tests { let size = image.size(0); assert_eq!((size.width.0, size.height.0), (16, 16)); } + + #[test] + fn the_landscape_covers_the_banner_and_is_made_again_only_when_it_grows() { + let mut art = Art::default(); + let stone = art.stone(0x0f0d17, true, 2.); + let (first, drawn) = art.scene(true, 2., 700.); + assert!(drawn >= 700.); + assert_eq!(first.size(0).width.0, (drawn * 2.) as i32); + assert_eq!( + first.size(0).height.0, + (SCENE_HEIGHT as f32 * SCENE_TEXEL * 2.) as i32 + ); + // A step wide: a few pixels more or a narrower window keep it. + for width in [drawn, 400.] { + assert!(Arc::ptr_eq(&first, &art.scene(true, 2., width).0)); + } + let (wider, wider_drawn) = art.scene(true, 2., 1900.); + assert!(!Arc::ptr_eq(&first, &wider) && wider_drawn >= 1900.); + assert!(Arc::ptr_eq(&art.replaced[0], &first)); + assert!(Arc::ptr_eq(&stone, &art.stone(0x0f0d17, true, 2.))); + } + + #[test] + fn a_wider_landscape_only_adds_columns() { + let (h, narrow, wide) = (SCENE_HEIGHT, 150, 400); + let a = scene(&SUNSET, 3, narrow, h); + let b = scene(&SUNSET, 3, wide, h); + for y in 0..h { + assert_eq!( + a[y * narrow..(y + 1) * narrow], + b[y * wide..y * wide + narrow], + "row {y}" + ); + } + } } diff --git a/launcher/src/main.rs b/launcher/src/main.rs index db2309dd6..9a97b8e8f 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -19,12 +19,13 @@ mod perf; mod scrollbar; mod servers_page; mod theme; +mod titlebar; use gpui::{ actions, div, ease_out_quint, prelude::*, px, relative, size, Animation, AnimationExt, AnyElement, App, Application, Bounds, ClickEvent, Context, Div, ElementId, FocusHandle, FontWeight, Hsla, KeyBinding, KeyDownEvent, Menu, MenuItem, PathPromptOptions, SharedString, - Stateful, Subscription, TitlebarOptions, Window, WindowBounds, WindowOptions, + Stateful, Subscription, Window, WindowBounds, WindowOptions, }; use horizon_launcher::account::{self, Accounts, SignIn, SignInState}; use horizon_launcher::backend::{self, Job, Msg, Release, Running, Settings, Updates}; @@ -1313,11 +1314,11 @@ fn megabytes(bytes: u64) -> String { // ----- Pages ------------------------------------------------------------------------------------- impl Launcher { - /// The top bar: the mark and the name over the sidebar, the pages with their icons, then - /// the account playing and the Settings button. + /// The top bar: the mark and the name over the sidebar (in the title bar on Windows), the + /// pages with their icons, then the account playing and the Settings button. fn topbar(&mut self, cx: &mut Context) -> Div { let t = self.theme; - let mut nav = div().flex().h_full().gap(px(2.)); + let mut nav = div().flex().flex_none().h_full().gap(px(2.)); for (page, label, icon) in Page::ALL { if page == Page::Settings { continue; @@ -1372,6 +1373,8 @@ impl Launcher { let account = keyboard(div().id("account"), &t) .relative() .flex() + .min_w_0() + .max_w(px(180.)) .items_center() .gap(px(10.)) .h(px(40.)) @@ -1395,8 +1398,10 @@ impl Launcher { div() .flex() .flex_col() + .min_w_0() .child( blocks::pixel(14., FontWeight::MEDIUM) + .truncate() .text_color(t.ink) .child(name), ) @@ -1448,25 +1453,29 @@ impl Launcher { .bg(t.side) .border_b_2() .border_color(t.outline) - .child( - div() - .flex() - .flex_none() - .items_center() - .gap(px(10.)) - .w(px(theme::SIDEBAR - 18.)) - .child(mark(24., &t)) - .child( - blocks::pixel(20., FontWeight::SEMIBOLD) - .text_color(t.ink) - .child("HORIZON"), - ), - ) + // Where the title bar is drawn here (Windows), it carries the mark and the name. + .when(!titlebar::DRAWN, |bar| { + bar.child( + div() + .flex() + .flex_none() + .items_center() + .gap(px(10.)) + .w(px(theme::SIDEBAR - 18.)) + .child(mark(24., &t)) + .child( + blocks::pixel(20., FontWeight::SEMIBOLD) + .text_color(t.ink) + .child("HORIZON"), + ), + ) + }) .child(nav) .child( div() .ml_auto() .flex() + .flex_none() .items_center() .gap(px(8.)) .child(account) @@ -1835,7 +1844,7 @@ impl Launcher { wrap } - fn play_page(&mut self, cx: &mut Context) -> Div { + fn play_page(&mut self, banner_width: f32, cx: &mut Context) -> Div { let t = self.theme; let Some(tag) = self.selected().cloned() else { // No hero without a version: the page starts below the top bar like the others. @@ -1994,7 +2003,7 @@ impl Launcher { // The banner: Horizon's landscape (`art.rs`) under a veil of the page colour, the hero and // the Play row over it. It reaches the content's edges and the top bar. - let scene = self.art.scene(t.dark, self.scale); + let (scene, scene_width) = self.art.scene(t.dark, self.scale, banner_width); let veil = |angle: f32, from: f32, to: f32, at: f32| { div() .absolute() @@ -2007,15 +2016,14 @@ impl Launcher { gpui::linear_color_stop(t.page.opacity(to), at), )) }; - let (w, h) = art::SCENE_SIZE; let banner = div() .relative() .flex() .flex_col() .justify_end() .min_h(px(340.)) - .mx(px(-32.)) - .px(px(32.)) + .mx(px(-theme::PAGE_GUTTER)) + .px(px(theme::PAGE_GUTTER)) .pb(px(28.)) .gap(px(24.)) .overflow_hidden() @@ -2027,8 +2035,8 @@ impl Launcher { .absolute() .bottom_0() .left_0() - .w(px(w as f32 * art::SCENE_TEXEL)) - .h(px(h as f32 * art::SCENE_TEXEL)), + .w(px(scene_width)) + .h(px(art::SCENE_HEIGHT as f32 * art::SCENE_TEXEL)), ) .child(veil(90., 0.82, 0., 0.62)) .child(veil(180., 0., 0.75, 1.)) @@ -3251,21 +3259,23 @@ impl Render for Launcher { self.friend_focused = self.friend_focus.is_focused(window); self.mods.note_focus(window); self.args_focused = self.args_focus.is_focused(window); + let size = window.viewport_size(); + let banner_width = f32::from(size.width) - theme::SIDEBAR - scrollbar::GUTTER; let content = match self.page { - Page::Play => self.play_page(cx), + Page::Play => self.play_page(banner_width, cx), Page::Versions => self.versions_page(cx), Page::Mods => self.mods_page(cx), Page::Servers => self.servers_page(cx), Page::Notes => self.notes_page(cx), Page::Settings => self.settings_page(cx), }; + self.art.free_replaced(window); // The new page fades in and rises a few pixels. let content = div() .relative() .w_full() // Let the page shrink inside the padded flex row so long notices can wrap. .min_w_0() - .max_w(px(1152.)) .child(content) .with_animation( ElementId::named_usize("page", self.switches), @@ -3277,7 +3287,6 @@ impl Render for Launcher { if self.opening.is_none() && window.focused(cx).is_none() { window.focus(&self.root_focus); } - let size = window.viewport_size(); let stone = self.art.stone(t.page_rgb, t.dark, window.scale_factor()); let topbar = self.topbar(cx); let sidebar = self.sidebar(cx); @@ -3333,7 +3342,9 @@ impl Render for Launcher { .child( div() .flex() - .px(px(32.)) + .w_full() + .min_w_0() + .px(px(theme::PAGE_GUTTER)) .pt(px(if self.page == Page::Play { 0. } else { @@ -3411,7 +3422,13 @@ impl Render for Launcher { }), ); } - root + div() + .flex() + .flex_col() + .size_full() + .bg(t.side) + .children(titlebar::render(&t, window)) + .child(root.flex_1().min_h_0()) } } @@ -3533,10 +3550,7 @@ fn main() { WindowOptions { window_bounds: Some(WindowBounds::Windowed(bounds)), window_min_size: Some(size(px(960.), px(600.))), - titlebar: Some(TitlebarOptions { - title: Some("Horizon".into()), - ..Default::default() - }), + titlebar: Some(titlebar::options()), app_id: Some("horizon-launcher".into()), ..Default::default() }, diff --git a/launcher/src/scrollbar.rs b/launcher/src/scrollbar.rs index 757d3525e..723c98cca 100644 --- a/launcher/src/scrollbar.rs +++ b/launcher/src/scrollbar.rs @@ -6,6 +6,9 @@ use gpui::{ }; use std::{cell::Cell, rc::Rc}; +/// Space reserved beside scroll content for the track and its padding. +pub const GUTTER: f32 = 16.; + #[derive(Clone, Default)] pub(crate) struct Scrollbar { pub handle: ScrollHandle, @@ -60,7 +63,7 @@ impl Scrollbar { .child( content .track_scroll(&self.handle) - .mr(px(16.)) + .mr(px(GUTTER)) // Nested scroll areas must not also move the page behind them. .on_scroll_wheel(|_, _, cx| cx.stop_propagation()), ) diff --git a/launcher/src/theme.rs b/launcher/src/theme.rs index 6425232c5..60b796709 100644 --- a/launcher/src/theme.rs +++ b/launcher/src/theme.rs @@ -24,6 +24,7 @@ pub const CONTROL: f32 = 34.; /// The top bar, and the sidebar of instances and servers. pub const TOPBAR: f32 = 56.; pub const SIDEBAR: f32 = 248.; +pub const PAGE_GUTTER: f32 = 32.; /// Motion: short and eased out, never bouncy. pub const QUICK: Duration = tokens::QUICK.duration(); diff --git a/launcher/src/titlebar.rs b/launcher/src/titlebar.rs new file mode 100644 index 000000000..3c2a78ea4 --- /dev/null +++ b/launcher/src/titlebar.rs @@ -0,0 +1,156 @@ +//! Custom Windows chrome. Other platforms keep their native window decorations. +use crate::theme::Theme; +use gpui::{Div, TitlebarOptions, Window}; + +/// Whether the launcher draws its own title bar (`render`) rather than the system. +pub const DRAWN: bool = cfg!(target_os = "windows"); + +pub fn options() -> TitlebarOptions { + TitlebarOptions { + title: Some("Horizon".into()), + appears_transparent: DRAWN, + ..Default::default() + } +} + +#[cfg(not(target_os = "windows"))] +pub fn render(_theme: &Theme, _window: &Window) -> Option
{ + None +} + +#[cfg(target_os = "windows")] +mod windows { + use super::*; + use crate::{blocks, mark}; + use gpui::{div, prelude::*, px, FontWeight, Stateful, WindowControlArea}; + + const HEIGHT: f32 = 32.; + const BUTTON_WIDTH: f32 = 46.; + const RESIZE_INSET: f32 = 5.; + + #[derive(Clone, Copy)] + enum Control { + Minimize, + Maximize, + Close, + } + + impl Control { + fn area(self) -> WindowControlArea { + match self { + Self::Minimize => WindowControlArea::Min, + Self::Maximize => WindowControlArea::Max, + Self::Close => WindowControlArea::Close, + } + } + + fn name(self) -> &'static str { + match self { + Self::Minimize => "window-minimize", + Self::Maximize => "window-maximize", + Self::Close => "window-close", + } + } + } + + fn control_icon(control: Control, maximized: bool, theme: &Theme) -> Div { + let square = || { + div() + .absolute() + .size(px(10.)) + .border_1() + .border_color(theme.ink_soft) + }; + let icon = div().relative().size(px(14.)); + match control { + Control::Minimize => icon.child( + div() + .absolute() + .left(px(2.)) + .top(px(7.)) + .w(px(10.)) + .h(px(1.)) + .bg(theme.ink_soft), + ), + Control::Maximize if maximized => icon + .child( + div() + .absolute() + .left(px(4.)) + .top_0() + .size(px(10.)) + .border_t_1() + .border_r_1() + .border_color(theme.ink_soft), + ) + .child(square().left(px(1.)).top(px(3.))), + Control::Maximize => icon.child(square().left(px(2.)).top(px(2.))), + Control::Close => { + let hover_color = theme.page; + icon.child( + blocks::icon("close", 14., theme.ink_soft) + .group_hover("window-close", move |style| style.text_color(hover_color)), + ) + } + } + } + + fn control_button(control: Control, theme: &Theme, maximized: bool) -> Stateful
{ + let theme = *theme; + div() + .id(control.name()) + .group(control.name()) + .flex() + .flex_none() + .items_center() + .justify_center() + .w(px(BUTTON_WIDTH)) + .h_full() + .text_color(theme.ink_soft) + // GPUI maps these to native non-client hit tests, retaining Snap Layouts, + // Alt+Space and close handling. Click callbacks would run the action twice. + .window_control_area(control.area()) + .hover(move |style| match control { + Control::Close => style.bg(theme.danger).text_color(theme.page), + _ => style.bg(theme.raised), + }) + .child(control_icon(control, maximized, &theme)) + } + + pub fn render(theme: &Theme, window: &Window) -> Option
{ + let drag = div() + .id("window-drag") + .flex() + .flex_1() + .h_full() + .items_center() + .gap(px(8.)) + .px(px(12.)) + .window_control_area(WindowControlArea::Drag) + .child(mark(16., theme)) + .child(blocks::pixel(13., FontWeight::MEDIUM).child("Horizon")); + Some( + div() + .flex() + .flex_none() + .items_center() + .h(px(HEIGHT)) + // Control hit tests run before native edge resizing in GPUI 0.2.2. + .when(!window.is_maximized(), |bar| { + bar.pt(px(RESIZE_INSET)).px(px(RESIZE_INSET)) + }) + .bg(theme.side) + .text_color(theme.muted) + .border_b_1() + .border_color(theme.outline) + .child(drag) + .children( + [Control::Minimize, Control::Maximize, Control::Close] + .map(|control| control_button(control, theme, window.is_maximized())), + ), + ) + } +} + +#[cfg(target_os = "windows")] +pub use windows::render;