From 1be756a10f00f090afc6a90500b92b55a731d6bb Mon Sep 17 00:00:00 2001 From: Commander07 <45269106+Commander07@users.noreply.github.com> Date: Wed, 17 Jan 2024 09:34:21 +0100 Subject: [PATCH] feat: prompt for launcher on first launch --- src/gui.rs | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 2 ++ 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/src/gui.rs b/src/gui.rs index a74a720..2de8475 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -194,6 +194,91 @@ fn Settings<'a>(cx: Scope<'a, SettingsProps<'a>>) -> Element { }) } +#[derive(Props, PartialEq)] +struct LauncherProps<'a> { + config: &'a UseRef, + first_launch: &'a UseState, + config_path: &'a PathBuf, + error: &'a UseRef>, + b64_id: String, +} + +fn Launcher<'a>(cx: Scope<'a, LauncherProps<'a>>) -> Element { + let mut vanilla = None; + let mut multimc = None; + let mut prism = None; + let launcher = cx.props.config.with(|cfg| cfg.launcher.clone()); + match &*launcher { + "vanilla" => vanilla = Some("true"), + "multimc-MultiMC" => multimc = Some("true"), + "multimc-PrismLauncher" => prism = Some("true"), + _ => {} + } + cx.render(rsx! { + div { + class: "version-inner-container", + style: "width: 25.25vw;", + div { + class: "container", + style: "width: 24vw;", + form { + id: "settings", + onsubmit: move |event| { + cx.props.config.with_mut(|cfg| cfg.launcher = event.data.values["launcher-select"].clone()); + let res = std::fs::write(cx.props.config_path, serde_json::to_vec(&*cx.props.config.read()).unwrap()); + match res { + Ok(_) => {}, + Err(e) => { + cx.props.error.set(Some(format!("{:#?}", e) + " (Failed to write config!)")); + }, + } + cx.props.first_launch.set(false); + }, + div { + class: "label", + span { + "Launcher:" + } + select { + name: "launcher-select", + id: "launcher-select", + form: "settings", + class: "credits-button", + if super::get_minecraft_folder().is_dir() { + rsx!(option { + value: "vanilla", + selected: vanilla, + "Vanilla" + }) + } + if super::get_multimc_folder("MultiMC").is_ok() { + rsx!(option { + value: "multimc-MultiMC", + selected: multimc, + "MultiMC" + }) + } + if super::get_multimc_folder("PrismLauncher").is_ok() { + rsx!(option { + value: "multimc-PrismLauncher", + selected: prism, + "Prism Launcher" + }) + } + } + } + input { + r#type: "submit", + value: "Continue", + class: "install-button", + id: "save" + } + } + } + } + }) +} + fn feature_change( installer_profile: &UseRef, modify: &UseState, @@ -585,6 +670,7 @@ pub(crate) fn App<'a>(cx: Scope<'a, AppProps>) -> Element<'a> { let branches = &cx.props.branches; let config: &UseRef = use_ref(cx, || cx.props.config.clone()); let settings: &UseState = use_state(cx, || false); + let first_launch: &UseState = use_state(cx, || config.with(|x| x.first_launch.unwrap_or(true))); let cog = String::from("data:image/png;base64,") + include_str!("assets/cog_icon.png.base64"); let err: &UseRef> = use_ref(cx, || None); let name = use_ref(cx, || String::default()); @@ -622,7 +708,19 @@ pub(crate) fn App<'a>(cx: Scope<'a, AppProps>) -> Element<'a> { } } } - } else { + } else if **first_launch { + rsx!(div { + class: "fake-body", + Launcher { + config: config, + first_launch: first_launch, + config_path: &cx.props.config_path, + error: err, + b64_id: engine::general_purpose::URL_SAFE_NO_PAD.encode(modpack_source) + } + }) + } + else { rsx!{ button { class: "settings-button", diff --git a/src/main.rs b/src/main.rs index 6363f76..9d89d2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -212,6 +212,7 @@ trait Downloadable { #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] struct Config { launcher: String, + first_launch: Option, // option for backwars compatibiliy } #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] @@ -1577,6 +1578,7 @@ fn main() { } else { config = Config { launcher: String::from("vanilla"), + first_launch: Some(true), }; fs::create_dir_all(config_path.parent().unwrap()).expect("Failed to create config dir!"); fs::write(&config_path, serde_json::to_vec(&config).unwrap())