aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/ui/src/stores/settings.svelte.ts
blob: 989172c3646b2ab0250c945f03fd1fa6e368d999 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { invoke } from "@tauri-apps/api/core";
import type { LauncherConfig, JavaInstallation } from "../types";
import { uiState } from "./ui.svelte";

export class SettingsState {
  settings = $state<LauncherConfig>({
    min_memory: 1024,
    max_memory: 2048,
    java_path: "java",
    width: 854,
    height: 480,
  });
  javaInstallations = $state<JavaInstallation[]>([]);
  isDetectingJava = $state(false);

  async loadSettings() {
    try {
      const result = await invoke<LauncherConfig>("get_settings");
      this.settings = result;
    } catch (e) {
      console.error("Failed to load settings:", e);
    }
  }

  async saveSettings() {
    try {
      await invoke("save_settings", { config: this.settings });
      uiState.setStatus("Settings saved!");
    } catch (e) {
      console.error("Failed to save settings:", e);
      uiState.setStatus("Error saving settings: " + e);
    }
  }

  async detectJava() {
    this.isDetectingJava = true;
    try {
      this.javaInstallations = await invoke("detect_java");
      if (this.javaInstallations.length === 0) {
        uiState.setStatus("No Java installations found");
      } else {
        uiState.setStatus(`Found ${this.javaInstallations.length} Java installation(s)`);
      }
    } catch (e) {
      console.error("Failed to detect Java:", e);
      uiState.setStatus("Error detecting Java: " + e);
    } finally {
      this.isDetectingJava = false;
    }
  }

  selectJava(path: string) {
    this.settings.java_path = path;
  }
}

export const settingsState = new SettingsState();