diff --git a/docs/launcher-commands.md b/docs/launcher-commands.md index a5c7b17fb..432b76e72 100644 --- a/docs/launcher-commands.md +++ b/docs/launcher-commands.md @@ -134,11 +134,11 @@ MCP: `account_offline`. ## `horizon-launcher settings` -Show the launcher's settings, or set one (the Settings page): selected-version TAG, minecraft-dir PATH, server-dir PATH ("default" resets a folder), install-new-versions on|off, opening-animation on|off, proxy http://host:port ("default" removes it). +Show the launcher's settings, or set one (the Settings page): selected-version TAG, minecraft-dir PATH, server-dir PATH ("default" resets a folder), install-new-versions on|off, opening-animation on|off, proxy http://host:port ("default" removes it), while-playing stay|minimize|hide (what the window does while the game runs). horizon-launcher settings [KEY] [VALUE] -- `KEY` (selected-version, minecraft-dir, server-dir, install-new-versions, opening-animation, proxy) +- `KEY` (selected-version, minecraft-dir, server-dir, install-new-versions, opening-animation, proxy, while-playing) - `VALUE`: The new value (with KEY) MCP: `settings`. diff --git a/launcher/README.md b/launcher/README.md index 6077e6a80..ad96eece1 100644 --- a/launcher/README.md +++ b/launcher/README.md @@ -147,8 +147,10 @@ page `servers.json` (the list), `servers//` (the servers created there) an - **Settings**: install new Horizon versions automatically (on unless you turn it off; the selected version only moves to the new one if you were on the newest), the opening animation, - the game's extra command-line arguments, an HTTP proxy for downloads, and the launcher's own - version and update state. + what the window does while you play (stays by default; Minimise, or Hide on macOS, puts it away + when the game starts and brings it back to the front when the game closes; `settings + while-playing stay|minimize|hide`), the game's extra command-line arguments, an HTTP proxy for + downloads, and the launcher's own version and update state. - **Updates**: there is one channel. Every release is published as the release, GitHub's latest, once its smoke test passed (docs/release-flow.md), and the launcher installs it. The game pre-releases of the old Alpha channel (v0.2.1 on) stay on the Versions page to install diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index dbd54aeee..3d900bd87 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -2515,6 +2515,51 @@ pub enum Appearance { System, } +/// What the launcher's window does while the game runs, as the player picks it in Settings. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "lowercase")] +pub enum WhilePlaying { + /// The window stays where it is. + #[default] + Stay, + /// The window goes to the Dock or the taskbar, and comes back when the game closes. + Minimize, + /// The launcher hides (macOS only: elsewhere it minimises), and comes back when the game + /// closes. + Hide, +} + +impl WhilePlaying { + pub const ALL: [WhilePlaying; 3] = [Self::Stay, Self::Minimize, Self::Hide]; + + /// Its name in `settings.json` and on the command line. + pub fn name(self) -> &'static str { + match self { + Self::Stay => "stay", + Self::Minimize => "minimize", + Self::Hide => "hide", + } + } + + pub fn parse(name: &str) -> Option { + let name = name.trim().to_ascii_lowercase(); + let name = match name.as_str() { + "minimise" => "minimize", + "keep" | "open" | "off" => "stay", + n => n, + }; + Self::ALL.into_iter().find(|w| w.name() == name) + } + + /// What this computer does for it: only macOS can hide a whole app. + pub fn here(self) -> Self { + match self { + Self::Hide if !cfg!(target_os = "macos") => Self::Minimize, + w => w, + } + } +} + #[derive(Default, serde::Serialize, serde::Deserialize)] pub struct Settings { pub selected: Option, @@ -2556,6 +2601,9 @@ pub struct Settings { /// The player hid the sidebar's partner banner. The Servers page keeps its own. #[serde(default)] pub partner_hidden: bool, + /// What the window does while the game runs. Stays by default. + #[serde(default)] + pub while_playing: WhilePlaying, } impl Settings { @@ -2574,6 +2622,12 @@ impl Settings { { fields.remove("appearance"); } + if fields + .get("while_playing") + .is_some_and(|w| serde_json::from_value::(w.clone()).is_err()) + { + fields.remove("while_playing"); + } } serde_json::from_value(value).unwrap_or_default() } @@ -2887,6 +2941,30 @@ mod tests { assert_eq!(CHECK_EVERY.as_secs(), 30 * 60); } + #[test] + fn the_window_stays_while_playing_unless_picked() { + assert_eq!(Settings::parse("{}").while_playing, WhilePlaying::Stay); + let hide = Settings::parse(r#"{"while_playing": "hide", "game_args": "--x"}"#); + assert_eq!(hide.while_playing, WhilePlaying::Hide); + let future = Settings::parse(r#"{"while_playing": "fade", "game_args": "--x"}"#); + assert_eq!( + (future.while_playing, future.game_args.as_str()), + (WhilePlaying::Stay, "--x") + ); + assert_eq!( + WhilePlaying::parse("Minimise"), + Some(WhilePlaying::Minimize) + ); + assert_eq!(WhilePlaying::parse("off"), Some(WhilePlaying::Stay)); + assert_eq!(WhilePlaying::parse("close"), None); + for w in WhilePlaying::ALL { + let text = serde_json::to_string(&w).unwrap(); + assert_eq!(text, format!("\"{}\"", w.name())); + } + let here = WhilePlaying::Hide.here(); + assert_eq!(here == WhilePlaying::Hide, cfg!(target_os = "macos")); + } + #[test] fn the_appearance_is_dark_unless_picked_and_an_unknown_one_keeps_the_rest() { assert_eq!(Settings::parse("{}").appearance, Appearance::Dark); diff --git a/launcher/src/cli.rs b/launcher/src/cli.rs index 60e46e8e1..246538b08 100644 --- a/launcher/src/cli.rs +++ b/launcher/src/cli.rs @@ -549,7 +549,8 @@ enum Cmd { }, /// Show the launcher's settings, or set one (the Settings page): selected-version TAG, /// minecraft-dir PATH, server-dir PATH ("default" resets a folder), install-new-versions - /// on|off, opening-animation on|off, proxy http://host:port ("default" removes it) + /// on|off, opening-animation on|off, proxy http://host:port ("default" removes it), + /// while-playing stay|minimize|hide (what the window does while the game runs) Settings { #[arg(value_parser = SETTING_KEYS)] key: Option, @@ -686,13 +687,14 @@ pub fn command() -> clap::Command { pub const AI_COMMANDS: [&str; 2] = ["mcp", "agent-config"]; /// The keys `settings` sets. -const SETTING_KEYS: [&str; 6] = [ +const SETTING_KEYS: [&str; 7] = [ "selected-version", "minecraft-dir", "server-dir", "install-new-versions", "opening-animation", "proxy", + "while-playing", ]; fn on_off(value: &str) -> Result { @@ -711,6 +713,10 @@ fn set_setting(s: &mut backend::Settings, key: &str, value: &str) -> Result<(), "server-dir" => s.server_dir = folder(value), "install-new-versions" => s.install_new_versions = Some(on_off(value)?), "opening-animation" => s.skip_opening = !on_off(value)?, + "while-playing" => { + s.while_playing = backend::WhilePlaying::parse(value) + .ok_or_else(|| format!("{value}: say stay, minimize or hide"))? + } "proxy" => { if value == "default" || value.is_empty() { s.proxy = None; @@ -736,6 +742,7 @@ fn settings_json(s: &backend::Settings) -> serde_json::Value { "game_args": s.game_args, "proxy": s.proxy, "desktop_entry": s.desktop_entry.unwrap_or(true), + "while_playing": s.while_playing.name(), "no_ai": backend::no_ai(), }) } diff --git a/launcher/src/main.rs b/launcher/src/main.rs index cb68372b6..352ae7e98 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -32,6 +32,7 @@ mod scrollbar; mod servers_page; mod theme; mod titlebar; +mod while_playing; mod worlds_page; use gpui::{ @@ -247,6 +248,8 @@ struct Launcher { export: Option, importing: import_page::Importing, console: console_page::Console, + /// What the window did when the game started (`while_playing.rs`). + aside: while_playing::Aside, /// The name the game made up for an offline player (`horizon-player.txt`), for the top bar. offline_name: Option, /// The player's servers, for the sidebar until the Servers page has read them. @@ -414,6 +417,7 @@ impl Launcher { export: None, importing: Default::default(), console: Default::default(), + aside: while_playing::Aside::new(window.window_handle()), offline_name: offline_name(), side_servers: horizon_launcher::servers::list(), art: art::Art::default(), @@ -606,6 +610,7 @@ impl Launcher { Ok(r) => { self.client = Some(r); self.client_run = run.map(|id| (id, Instant::now())); + self.step_aside(cx); } Err(e) => self.client_error = Some(e), } @@ -723,6 +728,7 @@ impl Launcher { } Job::Client => { self.client = None; + self.come_back(cx); // The game saved: last played, sizes and pictures changed. self.worlds.stale(); self.multiplayer.stale(); @@ -2662,6 +2668,7 @@ impl Launcher { Some(appearance.into_any_element()), &t, )) + .child(self.while_playing_row(n + 3, cx)) .child(index_row( n + 3, "Show the opening", diff --git a/launcher/src/while_playing.rs b/launcher/src/while_playing.rs new file mode 100644 index 000000000..d39309f7c --- /dev/null +++ b/launcher/src/while_playing.rs @@ -0,0 +1,92 @@ +//! What the window does while the game runs (`backend::WhilePlaying`): stay, minimise, or hide +//! (macOS), and come back to the front when the game closes, with how the run ended. +use super::*; +use backend::WhilePlaying; +use gpui::AnyWindowHandle; + +pub(super) struct Aside { + /// The launcher's window. + window: AnyWindowHandle, + /// What the window did when the game started, to undo when it closes. + stepped: Option, +} + +impl Aside { + pub(super) fn new(window: AnyWindowHandle) -> Self { + Aside { + window, + stepped: None, + } + } +} + +impl Launcher { + /// The game started: get out of its way as the player asked. Read from `settings.json`, so + /// `horizon-launcher settings while-playing …` counts at once. + pub(super) fn step_aside(&mut self, cx: &mut Context) { + self.settings.while_playing = Settings::load().while_playing; + let how = self.settings.while_playing.here(); + if how == WhilePlaying::Stay { + return; + } + let window = self.aside.window; + self.aside.stepped = Some(how); + cx.defer(move |cx| match how { + WhilePlaying::Hide => cx.hide(), + _ => { + let _ = window.update(cx, |_, window, _| window.minimize_window()); + } + }); + } + + /// The game closed: back to the front, where its play time, errors and log are. + pub(super) fn come_back(&mut self, cx: &mut Context) { + if self.aside.stepped.take().is_none() { + return; + } + let window = self.aside.window; + cx.defer(move |cx| { + cx.activate(true); + let _ = window.update(cx, |_, window, _| window.activate_window()); + }); + } + + /// Settings' row for it. + pub(super) fn while_playing_row(&self, n: usize, cx: &mut Context) -> Div { + let t = self.theme; + let mut options = vec![ + (WhilePlaying::Stay, "Stay"), + (WhilePlaying::Minimize, "Minimise"), + ]; + if cfg!(target_os = "macos") { + options.push((WhilePlaying::Hide, "Hide")); + } + let current = self.settings.while_playing.here(); + let picker = segments("while-playing", &options, current, &t, { + let this = cx.entity().downgrade(); + move |choice, _, cx| { + let _ = this.update(cx, |this, cx| { + this.settings.while_playing = choice; + this.settings.save(); + cx.notify(); + }); + } + }); + let said = match current { + WhilePlaying::Stay => "The launcher stays open while you play.", + WhilePlaying::Minimize => { + "The launcher goes down when the game starts, and comes back when it closes." + } + WhilePlaying::Hide => { + "The launcher hides when the game starts, and comes back when it closes." + } + }; + index_row( + n, + "While playing", + Some(div().child(said).into_any_element()), + Some(picker.into_any_element()), + &t, + ) + } +}