launcher: import a file dropped anywhere on the window
A file dropped on the window goes where it belongs: - a shared instance becomes a new instance, which gets selected on Home; - a Java modpack goes to the Modpacks import; - a resource or shader pack goes into the selected instance; - a data pack points to the world rows. A note in the corner says what happened, until it is closed. The Packs section and the world rows still take their own drops, and now stop the drop there so it isn't imported twice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -92,6 +92,14 @@ The opening, then Play ([clip](docs/opening.webm)):
|
||||
a world goes without `playerdata/`, `stats/` and `advancements/`, and its level.dat without the
|
||||
player's UUID. `horizon-launcher instance export [<instance>] [--format mrpack|zip] [--world
|
||||
<w>]… [--out <file>]`.
|
||||
- **Import by dropping a file**: a file dropped anywhere on the window is imported for what it is
|
||||
(`horizon_launcher::import`). A shared instance (a `.mrpack` that needs no mod loader, such as
|
||||
Export writes, or a game folder `.zip`) becomes a new instance, opened on Home. Its `.mrpack`
|
||||
downloads come from Modrinth with their SHA-512 checked, and only what an instance keeps is
|
||||
taken, inside its folder. A Java modpack goes to the Modpacks import, and a resource or shader
|
||||
pack into the selected instance. A data pack is pointed at the world rows, and the Packs section
|
||||
and the world rows keep taking their own drops. A note in the corner says what happened.
|
||||
`horizon-launcher instance import <file>`.
|
||||
- **Worlds**: Home lists the selected instance's worlds (picture, mode, version, last played,
|
||||
size). **Back up** zips one into the instance's `backups/` as the game's Make Backup does
|
||||
(`<date>_<time>_<folder>.zip`); **Copy** puts "<name> (1)" beside it; **Delete** moves it to the
|
||||
|
||||
@@ -99,6 +99,8 @@ impl Launcher {
|
||||
.drag_over::<ExternalPaths>(move |s, _, _, _| s.border_color(t.accent))
|
||||
.on_drop(cx.listener(move |this, paths: &ExternalPaths, _, cx| {
|
||||
this.add_world_packs(path.clone(), paths.paths().to_vec());
|
||||
// Not the window's import as well.
|
||||
cx.stop_propagation();
|
||||
cx.notify();
|
||||
}))
|
||||
.child(row)
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
//! Files dropped anywhere on the window (`horizon_launcher::import`): a shared instance becomes
|
||||
//! a new instance, a Java modpack goes to the Modpacks import, a resource or shader pack into
|
||||
//! the selected instance. The Packs section and the world rows take their own drops first.
|
||||
use super::*;
|
||||
use gpui::ExternalPaths;
|
||||
use horizon_launcher::import::{self, Dropped};
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(Default)]
|
||||
pub(super) struct Importing {
|
||||
/// Shared instances being made.
|
||||
busy: usize,
|
||||
/// What the last drop did, or why not.
|
||||
note: Option<Result<String, String>>,
|
||||
}
|
||||
|
||||
impl Launcher {
|
||||
pub(super) fn import_dropped(&mut self, files: Vec<PathBuf>, cx: &mut Context<Self>) {
|
||||
for file in files {
|
||||
let name = file
|
||||
.file_name()
|
||||
.map(|n| n.to_string_lossy().into_owned())
|
||||
.unwrap_or_default();
|
||||
match import::what(&file) {
|
||||
Err(e) => self.importing.note = Some(Err(e)),
|
||||
Ok(Dropped::Instance) => self.import_instance(file, cx),
|
||||
Ok(Dropped::Modpack) => {
|
||||
self.show_modpack(None);
|
||||
self.pack_job(move |log| horizon_launcher::modpacks::import(&file, log));
|
||||
self.importing.note = Some(Ok(format!(
|
||||
"{name} is a Java modpack: importing it on the Modpacks page."
|
||||
)));
|
||||
}
|
||||
Ok(Dropped::Pack(_)) => {
|
||||
let Some(i) = self.selected_instance().cloned() else {
|
||||
continue;
|
||||
};
|
||||
self.importing.note =
|
||||
Some(horizon_launcher::packs::add(&i.home, &file).map(|p| {
|
||||
format!(
|
||||
"Added {} to {}'s packs: switch it on under Packs.",
|
||||
p.title, i.name
|
||||
)
|
||||
}));
|
||||
self.game_packs.stale();
|
||||
}
|
||||
Ok(Dropped::DataPack) => {
|
||||
self.importing.note = Some(Err(format!(
|
||||
"{name} is a data pack: drop it on a world on Home, or install it into \
|
||||
one from there."
|
||||
)))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn import_instance(&mut self, file: PathBuf, cx: &mut Context<Self>) {
|
||||
self.importing.busy += 1;
|
||||
self.importing.note = None;
|
||||
let task = cx
|
||||
.background_executor()
|
||||
.spawn(async move { import::import_instance(&file, &|_, _| {}) });
|
||||
cx.spawn(async move |this, cx| {
|
||||
let made = task.await;
|
||||
let _ = this.update(cx, |this, cx| {
|
||||
this.importing.busy -= 1;
|
||||
this.importing.note = Some(made.map(|i| {
|
||||
this.reload_instances();
|
||||
this.select_instance(&i);
|
||||
this.go(Page::Play);
|
||||
format!("Made the instance {} from the shared file.", i.name)
|
||||
}));
|
||||
cx.notify();
|
||||
});
|
||||
})
|
||||
.detach();
|
||||
}
|
||||
|
||||
/// The root's drop target: every drop the sections don't take.
|
||||
pub(super) fn import_drop_target(
|
||||
&self,
|
||||
root: Stateful<Div>,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Stateful<Div> {
|
||||
root.on_drop(cx.listener(|this, paths: &ExternalPaths, _, cx| {
|
||||
this.import_dropped(paths.paths().to_vec(), cx);
|
||||
cx.notify();
|
||||
}))
|
||||
}
|
||||
|
||||
/// What the last drop did, in a corner over the page until closed.
|
||||
pub(super) fn import_note(&self, cx: &mut Context<Self>) -> Option<Div> {
|
||||
let t = self.theme;
|
||||
let line = match (&self.importing.note, self.importing.busy) {
|
||||
(_, n) if n > 0 => text(theme::LABEL)
|
||||
.text_color(t.ink_soft)
|
||||
.child("Making the instance from the shared file…"),
|
||||
(Some(Ok(said)), _) => text(theme::LABEL)
|
||||
.text_color(t.ink_soft)
|
||||
.child(said.clone()),
|
||||
(Some(Err(e)), _) => text(theme::LABEL).text_color(t.danger).child(e.clone()),
|
||||
(None, _) => return None,
|
||||
};
|
||||
Some(
|
||||
div()
|
||||
.absolute()
|
||||
.bottom(px(24.))
|
||||
.right(px(24.))
|
||||
.w(px(440.))
|
||||
.child(
|
||||
plate(&t)
|
||||
.w_full()
|
||||
.flex()
|
||||
.items_center()
|
||||
.gap(px(12.))
|
||||
.px(px(16.))
|
||||
.py(px(12.))
|
||||
.border_1()
|
||||
.border_color(t.rule)
|
||||
.bg(t.page)
|
||||
.child(blocks::icon("download", 18., t.accent))
|
||||
.child(div().flex_1().min_w_0().child(line))
|
||||
.child(button("import-close", "Close", Kind::Ghost, &t).on_click(
|
||||
cx.listener(|this, _: &ClickEvent, _, cx| {
|
||||
this.importing.note = None;
|
||||
cx.notify();
|
||||
}),
|
||||
)),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -96,7 +96,7 @@ impl Launcher {
|
||||
}
|
||||
}
|
||||
|
||||
fn select_instance(&mut self, instance: &Instance) {
|
||||
pub(super) fn select_instance(&mut self, instance: &Instance) {
|
||||
self.settings.instance =
|
||||
(instance.kind != InstanceKind::Default).then(|| instance.id.clone());
|
||||
self.settings.save();
|
||||
|
||||
@@ -14,6 +14,7 @@ mod cull;
|
||||
mod datapacks_page;
|
||||
mod export_page;
|
||||
mod game_tools;
|
||||
mod import_page;
|
||||
mod instances_page;
|
||||
mod internet;
|
||||
mod markdown;
|
||||
@@ -243,6 +244,7 @@ struct Launcher {
|
||||
game_packs: packs_page::Packs,
|
||||
world_packs: datapacks_page::WorldPacks,
|
||||
export: Option<export_page::ExportPanel>,
|
||||
importing: import_page::Importing,
|
||||
/// 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.
|
||||
@@ -408,6 +410,7 @@ impl Launcher {
|
||||
game_packs: packs_page::Packs::default(),
|
||||
world_packs: datapacks_page::WorldPacks::default(),
|
||||
export: None,
|
||||
importing: Default::default(),
|
||||
offline_name: offline_name(),
|
||||
side_servers: horizon_launcher::servers::list(),
|
||||
art: art::Art::default(),
|
||||
@@ -3649,6 +3652,8 @@ impl Render for Launcher {
|
||||
root = root.child(dialog);
|
||||
}
|
||||
}
|
||||
root = self.import_drop_target(root, cx);
|
||||
root = root.children(self.import_note(cx));
|
||||
if let Some(dialog) = self.instance_dialog(cx) {
|
||||
root = root.child(dialog);
|
||||
}
|
||||
|
||||
@@ -25,7 +25,7 @@ impl Launcher {
|
||||
}
|
||||
|
||||
/// Runs a pipeline step on a thread; its lines and its end arrive as messages.
|
||||
fn pack_job(
|
||||
pub(super) fn pack_job(
|
||||
&mut self,
|
||||
job: impl FnOnce(&dyn Fn(String)) -> Result<Imported, String> + Send + 'static,
|
||||
) {
|
||||
|
||||
@@ -135,6 +135,8 @@ impl Launcher {
|
||||
.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());
|
||||
// Not the window's import as well.
|
||||
cx.stop_propagation();
|
||||
cx.notify();
|
||||
}))
|
||||
.child(header);
|
||||
|
||||
Reference in New Issue
Block a user