feature/tauri-v2
ts 128 lines 4.32 KB
Raw
1 import { writable } from "svelte/store";
2 import Mousetrap from "mousetrap";
3 import { getKeybinds } from "./commands";
4 import { path } from "@tauri-apps/api";
5 import { loadTheme } from "./themehandler";
6 import { load, Store } from "@tauri-apps/plugin-store";
7 import { setEditorFontFamily, setEditorFontSize, setEditorLineHeight, setEditorTabSize } from "../lib/Editor.svelte";
8 import { watch } from "@tauri-apps/plugin-fs";
9 import { info } from "tauri-plugin-log-api";
10 import { setTerminalState } from "../lib/Statusbar.svelte";
11 import { termOptions, updateTermOptions } from "../lib/Terminal.svelte";
12 import * as fs from "@tauri-apps/plugin-fs"
13 import { invoke } from "@tauri-apps/api/core";
14
15 export const systemfonts = writable([]);
16 export const editorfont = writable("");
17 export const windowtheme = writable("");
18 export const autosave = writable(false);
19
20 // mousetrap is outdated and i hate the lowercase keymaps but cba to go into the code and fix everything so this will do
21 function parseKeybind(keybind: string) {
22 const keys = keybind.split("+");
23 const keymap = {
24 "Shift": "shift",
25 "Control": "ctrl",
26 "Alt": "alt",
27 "Meta": "meta",
28 "Backspace": "backspace",
29 "Tab": "tab",
30 "Enter": "enter",
31 "Capslock": "capslock",
32 "Escape": "escape",
33 "Space": "space",
34 "Pageup": "pageup",
35 "Pagedown": "pagedown",
36 "Home": "home",
37 "Delete": "del",
38 "End": "end",
39 "+": "up",
40 "-": "down"
41 }
42 for (const key of keys) {
43 if (keymap[key]) {
44 keybind = keybind.replace(key, keymap[key]);
45 }
46 else {
47 keybind = keybind.replace(key, key.toLowerCase());
48 }
49 }
50 return keybind;
51 }
52
53 export async function getShortcuts() {
54 info("Intializing shortcut bindings...", {file: "config.ts", line: 50});
55 const shortcuts = getKeybinds();
56 for (const shortcut of shortcuts) {
57 // skip binding shorcuts that are disabled
58 if (shortcut.disabled === "true") {
59 info(`The keybind "${shortcut.keybind}" is disabled. Skipping and/or falling back to default...`, {file: "config.ts", line: 55});
60 continue;
61 }
62 const keybind = parseKeybind(shortcut.keybind);
63 Mousetrap.bind(keybind, async (e) => {
64 e.preventDefault();
65 await fireAction(shortcut.command);
66 });
67 }
68 info("Shortcuts loaded successfully.", {file: "config.ts", line: 64});
69 }
70
71 async function fireAction(callback: () => Promise<void>, args = []) {
72 await callback();
73 return false;
74 }
75
76 export let appSettings: Store;
77
78 export async function loadDefaultSettings() {
79 const appdataLocal = await path.appLocalDataDir();
80 appSettings = await load(`${appdataLocal}default_settings.json`);
81
82 await watch(
83 `${appdataLocal}default_settings.json`,
84 () => {
85 appSettings;
86 }
87 )
88
89 await getShortcuts();
90 await loadTheme(await appSettings.get("svara.theme"));
91
92 appSettings.onKeyChange("editor.fontSize", (value: number) => {
93 setEditorFontSize(value);
94 })
95 appSettings.onKeyChange("editor.fontFamily", (value: string) => {
96 setEditorFontFamily(value);
97 })
98 appSettings.onKeyChange("editor.lineHeight", (value: string) => {
99 setEditorLineHeight(value);
100 })
101 appSettings.onKeyChange("editor.tabSize", (value: number) => {
102 setEditorTabSize(value);
103 })
104 appSettings.onKeyChange("svara.theme", (value: string) => {
105 loadTheme(value);
106 })
107 appSettings.onKeyChange("svara.useExternalTerminal", (value: string) => {
108 setTerminalState(value);
109 })
110 appSettings.onKeyChange("terminal.internal", (value: any) => {
111 termOptions.set(value);
112 updateTermOptions();
113 })
114 setTerminalState(await appSettings.get("svara.useExternalTerminal"))
115 termOptions.set(await appSettings.get("terminal.internal"));
116 info("Settings initialized", {file: "config.ts", line: 97});
117 }
118
119 export async function openLogFiles() {
120 const log_dir = await path.appConfigDir();
121 let recent_log = (await fs.readDir(`${log_dir}logs`)).at(-1);
122 await invoke("open_in_explorer", {path: `${log_dir}logs${path.sep}${recent_log.name}`});
123 }
124
125 export function getTime() {
126 let time = new Intl.DateTimeFormat('en-US', {dateStyle: "short", timeStyle: "long"}).format();
127 return time;
128 }