Merge pull request #2602 from ludovic111/feat/launcher-packs-ui
🤖 Agent: launcher-features-1 checked this: base main after #2601 merged, clean merge with main, launcher CI green; fmt, clippy -D warnings and the launcher tests pass locally. Queued for merge.
This commit is contained in:
@@ -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 <name>]`.
|
||||
**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 <name>]`.
|
||||
- **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 <instance> <tag|latest> keep it on one installed version, or follow updates
|
||||
horizon-launcher worlds [list] [--instance <name>] an instance's worlds
|
||||
horizon-launcher worlds backup|copy|delete <world> [--instance <name>] delete moves it to the trash
|
||||
horizon-launcher packs [list] [--kind resource|shader] [--instance <name>] resource and shader packs, on or off
|
||||
horizon-launcher packs add <file> | on|off|remove <pack> [--kind shader]
|
||||
horizon-launcher screenshots [list] [--instance <name>] an instance's screenshots, the newest first
|
||||
horizon-launcher screenshots delete <name> [--instance <name>] moves it to the trash
|
||||
horizon-launcher multiplayer [list] [--instance <name>] the servers in an instance's Multiplayer list
|
||||
|
||||
+10
-2
@@ -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<String>,
|
||||
/// 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<Div> {
|
||||
fn switch(id: impl Into<SharedString>, on: bool, t: &Theme) -> Stateful<Div> {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -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<PathBuf>,
|
||||
resource: Vec<(Pack, Option<Arc<Image>>)>,
|
||||
shader: Vec<Pack>,
|
||||
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<Result<String, String>>,
|
||||
}
|
||||
|
||||
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<String, String>) {
|
||||
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<PathBuf>) {
|
||||
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<Self>) {
|
||||
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<Self>) -> Option<Stateful<Div>> {
|
||||
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::<ExternalPaths>(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<Arc<Image>>,
|
||||
cx: &mut Context<Self>,
|
||||
) -> 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user