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