From c58130f2db86970dedf22d6825a7ea93c0b49201 Mon Sep 17 00:00:00 2001 From: developomp Date: Mon, 31 Jan 2022 15:41:14 +0900 Subject: [PATCH] redesigned UI logic --- src/components/Spinner.svelte | 6 +- src/pages/Operation/Complete.svelte | 50 +++++---- src/pages/Operation/Install.svelte | 5 + src/pages/Operation/Remove.svelte | 5 + src/pages/Operation/index.svelte | 156 ++++++---------------------- src/pages/Operation/logic.ts | 78 ++++++++++++++ src/pages/Operation/store.ts | 26 +++++ src/pages/Operation/types.ts | 9 +- 8 files changed, 190 insertions(+), 145 deletions(-) create mode 100644 src/pages/Operation/Install.svelte create mode 100644 src/pages/Operation/Remove.svelte create mode 100644 src/pages/Operation/logic.ts create mode 100644 src/pages/Operation/store.ts diff --git a/src/components/Spinner.svelte b/src/components/Spinner.svelte index 9649ea2..b09b423 100644 --- a/src/components/Spinner.svelte +++ b/src/components/Spinner.svelte @@ -1,17 +1,17 @@
WBM back WBM left gear WBM right gear WBM ftont diff --git a/src/pages/Operation/Complete.svelte b/src/pages/Operation/Complete.svelte index b187a31..882f8fb 100644 --- a/src/pages/Operation/Complete.svelte +++ b/src/pages/Operation/Complete.svelte @@ -1,31 +1,45 @@ -

- You can also optionally setup - - { - shellOpen("https://github.com/War-Brokers-Mods/WBM#3-set-up-obs-optional") - }} - > - OBS overlays - - for WB statistics. -

+{#if operationType == OperationType.Install} +

+ You can also optionally setup + + { + shellOpen( + "https://github.com/War-Brokers-Mods/WBM#3-set-up-obs-optional" + ) + }} + > + OBS overlays + + for WB statistics. +

+{:else} +

Remove launch option if you're using macOS or linux.

+{/if} diff --git a/src/pages/Operation/Install.svelte b/src/pages/Operation/Install.svelte new file mode 100644 index 0000000..923dd6d --- /dev/null +++ b/src/pages/Operation/Install.svelte @@ -0,0 +1,5 @@ + + + + diff --git a/src/pages/Operation/Remove.svelte b/src/pages/Operation/Remove.svelte new file mode 100644 index 0000000..9e373cb --- /dev/null +++ b/src/pages/Operation/Remove.svelte @@ -0,0 +1,5 @@ + + + + diff --git a/src/pages/Operation/index.svelte b/src/pages/Operation/index.svelte index bdc5dce..fca91ec 100644 --- a/src/pages/Operation/index.svelte +++ b/src/pages/Operation/index.svelte @@ -1,31 +1,21 @@ @@ -108,58 +46,30 @@ {/if} -{#if operationType == OperationType.Install} -
- +
+ - {#if !wasButtonClicked} - + {#if !wasButtonClicked} + + {#if operationType == OperationType.Install} + + {:else} + {/if} - - - {#if wasButtonClicked} - - - {#if installSuccess} - - {/if} + {:else} + + {#if operationType == OperationType.Install} + + {:else} + {/if} -
-{:else} -
- - - {#if !wasButtonClicked} - - {/if} - - - {#if wasButtonClicked} - - - {#if installSuccess} - - {/if} - {/if} -
-{/if} + {/if} +
diff --git a/src/pages/Operation/logic.ts b/src/pages/Operation/logic.ts new file mode 100644 index 0000000..e45affb --- /dev/null +++ b/src/pages/Operation/logic.ts @@ -0,0 +1,78 @@ +import { invoke } from "@tauri-apps/api/tauri" +import { open as dialogOpen } from "@tauri-apps/api/dialog" + +import { COMMANDS } from "../../constants" +import store from "./store" + +import type { InstallErr, RemoveErr } from "./types" + +function buttonClicked() { + store.wasButtonClicked.set(true) + store.spinCog.set(true) +} + +/** + * Calls the install command in the backend. + * + * @param {string} gamePath - Absolute path to the game directory in the steam library. Leave it empty to use default location. + */ +export function install(gamePath: string = "") { + buttonClicked() + + if (!gamePath) { + store.gamePath.update((value) => { + gamePath = value + return value + }) + } + + invoke(COMMANDS.INSTALL, { gamePath }) + .then(() => { + store.wasInstallSuccessful.set(true) + }) + .catch((err: InstallErr) => { + console.error(err) + + store.lastInstallErr.set(err) + }) +} + +/** + * Calls the remove command in the backend. + * + * @param {string} gamePath - Absolute path to the game directory in the steam library. Leave it empty to use default location. + */ +export function remove(gamePath: string = "") { + buttonClicked() + + if (gamePath) { + store.gamePath.update((value) => { + gamePath = value + return value + }) + } + + invoke(COMMANDS.REMOVE, { gamePath }) + .then(() => { + store.wasRemoveSuccessful.set(true) + }) + .catch((err: RemoveErr) => { + console.error(err) + + store.lastRemoveErr.set(err) + }) +} + +/** + * Opens a file selection dialog for the user to manually select the game directory. + * Called when the default game location was not found. + * + * @param {(gamePath?: string) => void} f - Function that will run after selecting a directory. Expected to be either the {@link install} function or the {@link remove} function. + */ +export function selectGamePathAndRun(f: (gamePath?: string) => void) { + dialogOpen({ directory: true, multiple: false }).then((value: string) => { + store.gamePath.set(value) + + f(value) + }) +} diff --git a/src/pages/Operation/store.ts b/src/pages/Operation/store.ts new file mode 100644 index 0000000..7022a04 --- /dev/null +++ b/src/pages/Operation/store.ts @@ -0,0 +1,26 @@ +import { writable } from "svelte/store" +import type { InstallErr, RemoveErr } from "./types" + +const wasButtonClicked = writable(false) +const spinCog = writable(false) + +const gamePath = writable("") + +const lastInstallErr = writable(undefined) +const lastRemoveErr = writable(undefined) + +const wasInstallSuccessful = writable(false) +const wasRemoveSuccessful = writable(false) + +export default { + wasButtonClicked, + spinCog, + + gamePath, + + lastInstallErr, + lastRemoveErr, + + wasInstallSuccessful, + wasRemoveSuccessful, +} diff --git a/src/pages/Operation/types.ts b/src/pages/Operation/types.ts index 442cb11..3f4eae0 100644 --- a/src/pages/Operation/types.ts +++ b/src/pages/Operation/types.ts @@ -1,7 +1,6 @@ /** * Must be synced with `src-tauri/src/commands/install/types.rs` */ - export enum InstallErr { UnsupportedOS, FailedToGetGamePath, @@ -16,7 +15,15 @@ export enum InstallErr { LaunchOptionNotSet, } +/** + * Must be synced with `src-tauri/src/commands/remove/types.rs` + */ export enum RemoveErr { FailedToGetGamePath, GamePathNotValid, } + +export enum OperationType { + Install, + Remove, +}