From e5c3d7cff7e5db03ccf11daacf7570e3365ae47b Mon Sep 17 00:00:00 2001 From: Ludovic Marie <136255825+ludovic111@users.noreply.github.com> Date: Tue, 27 Oct 2026 02:26:42 +0100 Subject: [PATCH] Launcher: Packs section on Home, with drag and drop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Home lists the selected instance's resource and shader packs with a switch each (disabled while the game runs: it rewrites options.txt itself), Remove after a confirmation, and Add pack… or a .zip/.hshader dropped on the section. switch() now takes any id. Co-Authored-By: Claude Opus 5.5 --- launcher/README.md | 9 ++ launcher/src/main.rs | 12 +- launcher/src/packs_page.rs | 306 +++++++++++++++++++++++++++++++++++++ 3 files changed, 325 insertions(+), 2 deletions(-) create mode 100644 launcher/src/packs_page.rs diff --git a/launcher/README.md b/launcher/README.md index aeeb4f288..e1a9150d0 100644 --- a/launcher/README.md +++ b/launcher/README.md @@ -89,6 +89,13 @@ The opening, then Play ([clip](docs/opening.webm)): system's trash after a confirmation; **Open folder**. The same from the command line: `horizon-launcher worlds [list|backup|copy|delete] [--instance ]`. **Play** on a world starts the game straight into it. +- **Packs**: Home lists the selected instance's resource packs and shader packs, each with a switch. + The choice is the game's own: `resourcePacks` in options.txt (a pack turned on goes on top, as + the game's Resource Packs screen does) and `pickedShaderPack` in `horizon-graphics.txt` (one shader + pack at a time). **Add pack…** or a `.zip` / `.hshader` dropped on the section copies it into the + right folder; **Remove** turns it off and moves it to the trash after a confirmation. From the + command line: `horizon-launcher packs [list|add|on|off|remove] [--kind resource|shader] + [--instance ]`. - **Screenshots**: Home shows the selected instance's newest screenshots (what F2 saves in its `screenshots/` folder) and all of them on request. Clicking one opens it over the window (← and → step through) with **Copy** (the picture, to the clipboard), **Open**, **Show in folder** and @@ -202,6 +209,8 @@ and don't set `%ERRORLEVEL%` / `$LASTEXITCODE`. Scripts that need the exit code horizon-launcher instance version keep it on one installed version, or follow updates horizon-launcher worlds [list] [--instance ] an instance's worlds horizon-launcher worlds backup|copy|delete [--instance ] delete moves it to the trash + horizon-launcher packs [list] [--kind resource|shader] [--instance ] resource and shader packs, on or off + horizon-launcher packs add | on|off|remove [--kind shader] horizon-launcher screenshots [list] [--instance ] an instance's screenshots, the newest first horizon-launcher screenshots delete [--instance ] moves it to the trash horizon-launcher multiplayer [list] [--instance ] the servers in an instance's Multiplayer list diff --git a/launcher/src/main.rs b/launcher/src/main.rs index 67ef0ac39..0542d7504 100644 --- a/launcher/src/main.rs +++ b/launcher/src/main.rs @@ -20,6 +20,7 @@ mod modpacks_page; mod mods_page; mod mods_project; mod multiplayer_page; +mod packs_page; mod partner; mod perf; mod screenshots_page; @@ -237,6 +238,7 @@ struct Launcher { worlds: worlds_page::Worlds, multiplayer: multiplayer_page::Multiplayer, screenshots: screenshots_page::Screenshots, + game_packs: packs_page::Packs, /// 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. @@ -399,6 +401,7 @@ impl Launcher { worlds: worlds_page::Worlds::default(), multiplayer: multiplayer_page::Multiplayer::default(), screenshots: screenshots_page::Screenshots::default(), + game_packs: packs_page::Packs::default(), offline_name: offline_name(), side_servers: horizon_launcher::servers::list(), art: art::Art::default(), @@ -712,6 +715,7 @@ impl Launcher { self.worlds.stale(); self.multiplayer.stale(); self.screenshots.stale(); + self.game_packs.stale(); if let Some((id, started)) = self.client_run.take() { if let Err(e) = instances::record_play(&id, started.elapsed()) { eprintln!("Play time not saved: {e}"); @@ -1189,10 +1193,11 @@ fn button( } /// An on/off switch: a square knob sliding in a sunk track that turns violet when on. -fn switch(id: &'static str, on: bool, t: &Theme) -> Stateful
{ +fn switch(id: impl Into, on: bool, t: &Theme) -> Stateful
{ let t = *t; + let id: SharedString = id.into(); let knob = if on { t.on_face } else { t.muted }; - keyboard(div().id(id), &t) + keyboard(div().id(id.clone()), &t) .relative() .flex_none() .w(px(44.)) @@ -2204,6 +2209,9 @@ impl Launcher { if let Some(servers) = self.multiplayer_section(cx) { page = page.child(servers); } + if let Some(packs) = self.packs_section(cx) { + page = page.child(packs); + } if let Some(shots) = self.screenshots_section(cx) { page = page.child(shots); } diff --git a/launcher/src/packs_page.rs b/launcher/src/packs_page.rs new file mode 100644 index 000000000..b497afa44 --- /dev/null +++ b/launcher/src/packs_page.rs @@ -0,0 +1,306 @@ +//! The selected instance's resource packs and shader packs on Home (`horizon_launcher::packs`): +//! each turned on or off with one click, Remove (to the trash, after a confirmation), and +//! Add (a file picker) or a .zip or .hshader dropped anywhere on the section. +use super::*; +use gpui::{ExternalPaths, Image, ImageFormat}; +use horizon_launcher::packs::{self, Kind as PackKind, Pack}; +use std::path::PathBuf; +use std::sync::Arc; + +#[derive(Default)] +pub(super) struct Packs { + home: Option, + resource: Vec<(Pack, Option>)>, + shader: Vec, + fresh: bool, + /// The pack waiting for its removal to be confirmed: its kind and file. + confirm_remove: Option<(PackKind, String)>, + /// What the last action did, or why it failed. + note: Option>, +} + +impl Packs { + pub(super) fn stale(&mut self) { + self.fresh = false; + } +} + +impl Launcher { + fn load_packs_list(&mut self, home: PathBuf) { + if self.game_packs.home.as_ref() != Some(&home) { + self.game_packs = Packs { + home: Some(home.clone()), + ..Packs::default() + }; + } + if self.game_packs.fresh { + return; + } + // A few small reads per pack (pack.mcmeta, pack.png): fine while drawing. + self.game_packs.resource = packs::list(&home, PackKind::Resource) + .into_iter() + .map(|p| { + let icon = p + .icon + .clone() + .map(|png| Arc::new(Image::from_bytes(ImageFormat::Png, png))); + (p, icon) + }) + .collect(); + self.game_packs.shader = packs::list(&home, PackKind::Shader); + self.game_packs.fresh = true; + } + + fn pack_action(&mut self, act: impl FnOnce(&std::path::Path) -> Result) { + let Some(home) = self.game_packs.home.clone() else { + return; + }; + self.game_packs.note = Some(act(&home)); + self.game_packs.confirm_remove = None; + self.game_packs.stale(); + } + + /// Copies dropped or picked files into the instance's pack folders. + fn add_packs(&mut self, files: Vec) { + self.pack_action(|home| { + let mut added = Vec::new(); + for file in &files { + added.push(packs::add(home, file)?.title); + } + Ok(format!("Added {}: turn it on below.", added.join(", "))) + }); + } + + fn choose_packs(&mut self, cx: &mut Context) { + let paths = cx.prompt_for_paths(PathPromptOptions { + files: true, + directories: false, + multiple: true, + prompt: Some("Add".into()), + }); + cx.spawn(async move |this, cx| { + if let Ok(Ok(Some(paths))) = paths.await { + let _ = this.update(cx, |this, cx| { + this.add_packs(paths); + cx.notify(); + }); + } + }) + .detach(); + } + + /// Home's Packs section, for the selected instance. + pub(super) fn packs_section(&mut self, cx: &mut Context) -> Option> { + let t = self.theme; + let home = self.selected_instance()?.home.clone(); + self.load_packs_list(home.clone()); + let header = + div() + .flex() + .items_center() + .justify_between() + .mb(px(4.)) + .child(section_title("Packs", "package", &t)) + .child( + div() + .flex() + .items_center() + .gap(px(6.)) + .child(button("packs-add", "Add pack…", Kind::Ghost, &t).on_click( + cx.listener(|this, _: &ClickEvent, _, cx| { + this.choose_packs(cx); + cx.notify(); + }), + )) + .child( + button("packs-folder", "Open folder", Kind::Ghost, &t).on_click( + move |_, _, cx| { + let dir = home.join(PackKind::Resource.folder()); + let _ = std::fs::create_dir_all(&dir); + cx.open_with_system(&dir); + }, + ), + ), + ); + let mut section = div() + .id("packs") + .flex() + .flex_col() + .gap(px(8.)) + // The drop outline sits just outside the rows, which line up with the other sections. + .p(px(2.)) + .mx(px(-4.)) + .border_2() + .border_color(gpui::transparent_black()) + .drag_over::(move |s, _, _, _| s.border_color(t.accent).bg(t.well)) + .on_drop(cx.listener(|this, paths: &ExternalPaths, _, cx| { + this.add_packs(paths.paths().to_vec()); + cx.notify(); + })) + .child(header); + match &self.game_packs.note { + Some(Ok(m)) => { + section = section.child(text(theme::CAPTION).text_color(t.muted).child(m.clone())) + } + Some(Err(e)) => section = section.child(notice(e.clone(), &t)), + None => {} + } + if self.client.is_some() { + section = section.child(text(theme::CAPTION).text_color(t.muted).child( + "The game is running: change packs in its own menus, or here once it is closed.", + )); + } + let resource = self.game_packs.resource.clone(); + section = section.child(caps("Resource packs").text_color(t.muted).mt(px(4.))); + if resource.is_empty() { + section = section.child( + text(theme::CAPTION) + .text_color(t.muted) + .child("None yet: drop a .zip here, or find some on the Mods page."), + ); + } + for (i, (pack, icon)) in resource.into_iter().enumerate() { + section = section.child(self.pack_row(i, pack, icon, cx)); + } + let shader = self.game_packs.shader.clone(); + section = section.child(caps("Shader packs").text_color(t.muted).mt(px(8.))); + if shader.is_empty() { + section = section.child( + text(theme::CAPTION) + .text_color(t.muted) + .child("None yet: drop a .hshader or a shader pack .zip here."), + ); + } + for (i, pack) in shader.into_iter().enumerate() { + section = section.child(self.pack_row(i, pack, None, cx)); + } + Some(section) + } + + fn pack_row( + &mut self, + i: usize, + pack: Pack, + icon: Option>, + cx: &mut Context, + ) -> Div { + let t = self.theme; + let id = |what: &str| { + SharedString::from(format!("pack-{what}-{:?}-{i}", pack.kind).to_lowercase()) + }; + let picture = div() + .flex() + .flex_none() + .items_center() + .justify_center() + .size(px(40.)) + .border_2() + .border_color(t.outline) + .bg(t.well); + let picture = match icon { + Some(image) => picture.child(gpui::img(image).size(px(36.))), + None => picture.child(blocks::icon( + if pack.kind == PackKind::Shader { + "zap" + } else { + "package" + }, + 18., + t.muted, + )), + }; + let running = self.client.is_some(); + let mut actions = div().flex().items_center().gap(px(6.)); + let key = (pack.kind, pack.file.clone()); + if self.game_packs.confirm_remove.as_ref() == Some(&key) { + let gone = pack.clone(); + actions = actions + .child( + text(theme::CAPTION) + .text_color(t.danger) + .child("Move it to the trash?"), + ) + .child( + button(id("keep"), "Keep", Kind::Ghost, &t).on_click(cx.listener( + |this, _: &ClickEvent, _, cx| { + this.game_packs.confirm_remove = None; + cx.notify(); + }, + )), + ) + .child( + button(id("trash"), "Move to trash", Kind::Secondary, &t).on_click( + cx.listener(move |this, _: &ClickEvent, _, cx| { + let gone = gone.clone(); + this.pack_action(move |home| { + packs::remove(home, &gone) + .map(|()| format!("{} moved to the trash.", gone.title)) + }); + cx.notify(); + }), + ), + ); + } else { + let toggle = switch(id("toggle"), pack.enabled, &t); + let flip = pack.clone(); + let toggle = if running { + disabled(toggle) + } else { + toggle.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + let flip = flip.clone(); + this.pack_action(move |home| { + packs::set_enabled(home, &flip, !flip.enabled).map(|()| { + match flip.enabled { + true => format!("{} is off.", flip.title), + false => format!("{} is on.", flip.title), + } + }) + }); + cx.notify(); + })) + }; + let remove = button(id("remove"), "Remove", Kind::Ghost, &t); + let remove = if running { + disabled(remove) + } else { + remove.on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.game_packs.confirm_remove = Some(key.clone()); + cx.notify(); + })) + }; + actions = actions.child(toggle).child(remove); + } + let facts = match pack.description.is_empty() { + true => pack.file.clone(), + false if pack.title == pack.file => pack.description.clone(), + false => format!("{} · {}", pack.description, pack.file), + }; + plate(&t) + .flex() + .items_center() + .gap(px(14.)) + .px(px(14.)) + .py(px(8.)) + .child(picture) + .child( + div() + .flex() + .flex_col() + .flex_1() + .min_w_0() + .child( + blocks::pixel(14., FontWeight::MEDIUM) + .text_color(if pack.enabled { t.ink } else { t.muted }) + .truncate() + .child(pack.title.clone()), + ) + .child( + text(theme::CAPTION) + .text_color(t.muted) + .truncate() + .child(facts), + ), + ) + .child(actions) + } +}