feat: more options
This commit is contained in:
192
src/stores.ts
192
src/stores.ts
@ -1,3 +1,4 @@
|
||||
import { Ref, computed, ref } from 'vue';
|
||||
import { defineStore } from 'pinia';
|
||||
import { listen } from '@tauri-apps/api/event';
|
||||
import { open } from '@tauri-apps/plugin-dialog';
|
||||
@ -75,101 +76,118 @@ export const usePkgStore = defineStore('pkg', {
|
||||
},
|
||||
});
|
||||
|
||||
export const usePrfStore = defineStore('prf', {
|
||||
state: (): { prf: Profile | null; list: ProfileMeta[] } => {
|
||||
return {
|
||||
prf: null,
|
||||
list: [],
|
||||
};
|
||||
},
|
||||
getters: {
|
||||
current: (state) => state.prf,
|
||||
isPkgEnabled: (state) => (pkg: Package | undefined) =>
|
||||
pkg !== undefined && state.prf?.data.mods.includes(pkgKey(pkg)),
|
||||
cfg: (state) => (key: string) => state.prf?.data.cfg[key],
|
||||
},
|
||||
actions: {
|
||||
setupListeners() {
|
||||
listen<InstallStatus>('install-end', async () => {
|
||||
await this.reload();
|
||||
});
|
||||
},
|
||||
export const usePrfStore = defineStore('prf', () => {
|
||||
const current: Ref<Profile | null> = ref(null);
|
||||
const list: Ref<ProfileMeta[]> = ref([]);
|
||||
|
||||
async prompt() {
|
||||
const exePath = await open({
|
||||
multiple: false,
|
||||
directory: false,
|
||||
filters: [
|
||||
{
|
||||
name: 'mu3.exe or chusanApp.exe',
|
||||
extensions: ['exe'],
|
||||
},
|
||||
],
|
||||
});
|
||||
if (exePath !== null) {
|
||||
await this.create(exePath);
|
||||
}
|
||||
},
|
||||
const isPkgEnabled = (pkg: Package | undefined) =>
|
||||
computed(
|
||||
() =>
|
||||
pkg !== undefined &&
|
||||
current.value !== null &&
|
||||
current.value?.data.mods.includes(pkgKey(pkg))
|
||||
);
|
||||
|
||||
async create(exePath: string) {
|
||||
try {
|
||||
await invoke('init_profile', { exePath });
|
||||
await this.reload();
|
||||
await this.reloadList();
|
||||
} catch (e) {
|
||||
this.prf = null;
|
||||
}
|
||||
const reload = async () => {
|
||||
current.value = await invoke('get_current_profile');
|
||||
if (current.value !== null) {
|
||||
changePrimaryColor(current.value.game);
|
||||
}
|
||||
};
|
||||
|
||||
if (this.prf !== null) {
|
||||
const pkgs = usePkgStore();
|
||||
pkgs.reloadAll();
|
||||
}
|
||||
},
|
||||
const save = async () => {
|
||||
await invoke('save_current_profile');
|
||||
};
|
||||
|
||||
async switchTo(game: Game, name: string) {
|
||||
await invoke('load_profile', { game, name });
|
||||
await this.reload();
|
||||
if (this.prf !== null) {
|
||||
const pkgs = usePkgStore();
|
||||
pkgs.reloadAll();
|
||||
}
|
||||
},
|
||||
const cfg = <T extends string | boolean | number>(key: string, dflt: T) =>
|
||||
computed({
|
||||
get() {
|
||||
return (current.value?.data.cfg[key] as T | undefined) ?? dflt;
|
||||
},
|
||||
async set(value) {
|
||||
if (value !== undefined) {
|
||||
await invoke('set_cfg', { key, value: value });
|
||||
await reload();
|
||||
await save();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
async save() {
|
||||
await invoke('save_current_profile');
|
||||
},
|
||||
const prompt = async () => {
|
||||
const exePath = await open({
|
||||
multiple: false,
|
||||
directory: false,
|
||||
filters: [
|
||||
{
|
||||
name: 'mu3.exe or chusanApp.exe',
|
||||
extensions: ['exe'],
|
||||
},
|
||||
],
|
||||
});
|
||||
if (exePath !== null) {
|
||||
await create(exePath);
|
||||
}
|
||||
};
|
||||
|
||||
async reload() {
|
||||
this.prf = await invoke('get_current_profile');
|
||||
if (this.prf !== null) {
|
||||
changePrimaryColor(this.prf.game);
|
||||
}
|
||||
},
|
||||
const create = async (exePath: string) => {
|
||||
try {
|
||||
await invoke('init_profile', { exePath });
|
||||
await reload();
|
||||
await reloadList();
|
||||
} catch (e) {
|
||||
current.value = null;
|
||||
}
|
||||
|
||||
async reloadList() {
|
||||
const raw = (await invoke('list_profiles')) as [Game, string][];
|
||||
if (current.value !== null) {
|
||||
const pkgs = usePkgStore();
|
||||
pkgs.reloadAll();
|
||||
}
|
||||
};
|
||||
|
||||
this.list = raw.map(([game, name]) => {
|
||||
return {
|
||||
game,
|
||||
name,
|
||||
};
|
||||
});
|
||||
},
|
||||
const switchTo = async (game: Game, name: string) => {
|
||||
await invoke('load_profile', { game, name });
|
||||
await reload();
|
||||
if (current.value !== null) {
|
||||
const pkgs = usePkgStore();
|
||||
pkgs.reloadAll();
|
||||
}
|
||||
};
|
||||
|
||||
async togglePkg(pkg: Package | undefined, enable: boolean) {
|
||||
if (pkg === undefined) {
|
||||
return;
|
||||
}
|
||||
await invoke('toggle_package', { key: pkgKey(pkg), enable });
|
||||
await this.reload();
|
||||
await this.save();
|
||||
},
|
||||
const reloadList = async () => {
|
||||
const raw = (await invoke('list_profiles')) as [Game, string][];
|
||||
|
||||
async setCfg(key: string, value: string | boolean | number) {
|
||||
await invoke('set_cfg', { key, value });
|
||||
await this.reload();
|
||||
await this.save();
|
||||
},
|
||||
},
|
||||
list.value = raw.map(([game, name]) => {
|
||||
return {
|
||||
game,
|
||||
name,
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
const togglePkg = async (pkg: Package | undefined, enable: boolean) => {
|
||||
if (pkg === undefined) {
|
||||
return;
|
||||
}
|
||||
await invoke('toggle_package', { key: pkgKey(pkg), enable });
|
||||
await reload();
|
||||
await save();
|
||||
};
|
||||
|
||||
listen<InstallStatus>('install-end', async () => {
|
||||
await reload();
|
||||
});
|
||||
|
||||
return {
|
||||
current,
|
||||
list,
|
||||
isPkgEnabled,
|
||||
reload,
|
||||
save,
|
||||
cfg,
|
||||
prompt,
|
||||
create,
|
||||
switchTo,
|
||||
reloadList,
|
||||
togglePkg,
|
||||
};
|
||||
});
|
||||
|
Reference in New Issue
Block a user