From 9fc5af4f0c2f3a70c1c57b9ba0fe294ab937ec43 Mon Sep 17 00:00:00 2001 From: dubisdev Date: Sat, 14 Jan 2023 01:32:40 +0100 Subject: [PATCH] feat: added settings `alt+s` --- README.md | 2 +- package.json | 1 + settings.html | 16 +- src-tauri/Cargo.lock | 260 ++++++++++++++++++++--- src-tauri/Cargo.toml | 15 +- src-tauri/src/main.rs | 1 + src-tauri/tauri.conf.json | 14 ++ src/components/Settings/Checkbox.tsx | 35 +++ src/components/Settings/index.module.css | 78 +++++++ src/components/Settings/index.tsx | 80 +++++-- src/main.tsx | 2 + src/utils/configureSettingsListener.ts | 10 + src/utils/configureSettingsStorage.ts | 34 +++ src/utils/settingsPageLauncher.ts | 4 + 14 files changed, 496 insertions(+), 56 deletions(-) create mode 100644 src/components/Settings/Checkbox.tsx create mode 100644 src/components/Settings/index.module.css create mode 100644 src/utils/configureSettingsListener.ts create mode 100644 src/utils/configureSettingsStorage.ts diff --git a/README.md b/README.md index 8a07a92..8f3e122 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,11 @@

- ## Available Shortcuts - `Alt+M` to toggle the visibility of the app - `Alt+Q` to quit the app +- `Alt+S` to open the settings - `Enter` to copy the result to the clipboard ## Future Features diff --git a/package.json b/package.json index 2c19a14..4beda0e 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "mathjs": "11.5.0", "react": "18.2.0", "react-dom": "18.2.0", + "tauri-settings": "0.3.0", "the-new-css-reset": "1.8.2", "zustand": "4.3.1" }, diff --git a/settings.html b/settings.html index b5477e3..5cecaff 100644 --- a/settings.html +++ b/settings.html @@ -8,12 +8,22 @@ RunMath Settings + -

- Just a settings page -
+
diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 0b700ec..675c747 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -38,17 +38,6 @@ version = "1.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2cb2f989d18dd141ab8ae82f64d1a8cdd37e0840f73a406896cf5e99502fab61" -[[package]] -name = "app" -version = "0.1.0" -dependencies = [ - "serde", - "serde_json", - "tauri", - "tauri-build", - "tauri-plugin-window-state", -] - [[package]] name = "atk" version = "0.15.1" @@ -238,6 +227,12 @@ dependencies = [ "smallvec", ] +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "cfg-if" version = "1.0.0" @@ -353,7 +348,7 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -362,7 +357,7 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "crossbeam-utils", ] @@ -372,7 +367,7 @@ version = "0.8.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -492,7 +487,7 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "dirs-sys-next", ] @@ -504,7 +499,7 @@ checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" dependencies = [ "libc", "redox_users", - "winapi", + "winapi 0.3.9", ] [[package]] @@ -546,7 +541,7 @@ version = "0.8.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -574,7 +569,7 @@ version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4e884668cd0c7480504233e951174ddc3b382f7c2666e3b7310b5c4e7b0c37f9" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "redox_syscall", "windows-sys", @@ -620,6 +615,41 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fsevent" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ab7d1bd1bd33cc98b0889831b72da23c0aa4df9cec7e0702f46ecea04b35db6" +dependencies = [ + "bitflags", + "fsevent-sys", +] + +[[package]] +name = "fsevent-sys" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f41b048a94555da0f42f1d632e2e19510084fb8e303b0daa2816e733fb3644a0" +dependencies = [ + "libc", +] + +[[package]] +name = "fuchsia-zircon" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" +dependencies = [ + "bitflags", + "fuchsia-zircon-sys", +] + +[[package]] +name = "fuchsia-zircon-sys" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" + [[package]] name = "futf" version = "0.1.5" @@ -803,7 +833,7 @@ version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "wasi 0.9.0+wasi-snapshot-preview1", ] @@ -814,7 +844,7 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "wasi 0.11.0+wasi-snapshot-preview1", ] @@ -846,7 +876,7 @@ dependencies = [ "gobject-sys", "libc", "system-deps 6.0.3", - "winapi", + "winapi 0.3.9", ] [[package]] @@ -1115,13 +1145,42 @@ dependencies = [ "cfb", ] +[[package]] +name = "inotify" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4816c66d2c8ae673df83366c18341538f234a26d65a9ecea5c348b453ac1d02f" +dependencies = [ + "bitflags", + "inotify-sys", + "libc", +] + +[[package]] +name = "inotify-sys" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb" +dependencies = [ + "libc", +] + [[package]] name = "instant" version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", +] + +[[package]] +name = "iovec" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" +dependencies = [ + "libc", ] [[package]] @@ -1190,6 +1249,16 @@ dependencies = [ "treediff", ] +[[package]] +name = "kernel32-sys" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" +dependencies = [ + "winapi 0.2.8", + "winapi-build", +] + [[package]] name = "kuchiki" version = "0.8.1" @@ -1208,6 +1277,12 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" +[[package]] +name = "lazycell" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" + [[package]] name = "libc" version = "0.2.139" @@ -1239,7 +1314,7 @@ version = "0.4.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -1248,7 +1323,7 @@ version = "0.5.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff50ecb28bb86013e935fb6683ab1f6d3a20016f123c76fd4c27470076ac30f5" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "generator", "scoped-tls", "serde", @@ -1325,6 +1400,49 @@ dependencies = [ "adler", ] +[[package]] +name = "mio" +version = "0.6.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4afd66f5b91bf2a3bc13fad0e21caedac168ca4c707504e75585648ae80e4cc4" +dependencies = [ + "cfg-if 0.1.10", + "fuchsia-zircon", + "fuchsia-zircon-sys", + "iovec", + "kernel32-sys", + "libc", + "log", + "miow", + "net2", + "slab", + "winapi 0.2.8", +] + +[[package]] +name = "mio-extras" +version = "2.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52403fe290012ce777c4626790c8951324a2b9e3316b3143779c72b029742f19" +dependencies = [ + "lazycell", + "log", + "mio", + "slab", +] + +[[package]] +name = "miow" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebd808424166322d4a38da87083bfddd3ac4c131334ed55856112eb06d46944d" +dependencies = [ + "kernel32-sys", + "net2", + "winapi 0.2.8", + "ws2_32-sys", +] + [[package]] name = "ndk" version = "0.6.0" @@ -1353,6 +1471,17 @@ dependencies = [ "jni-sys", ] +[[package]] +name = "net2" +version = "0.2.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74d0df99cfcd2530b2e694f6e17e7f37b8e26bb23983ac530c0c97408837c631" +dependencies = [ + "cfg-if 0.1.10", + "libc", + "winapi 0.3.9", +] + [[package]] name = "new_debug_unreachable" version = "1.0.4" @@ -1365,6 +1494,24 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb" +[[package]] +name = "notify" +version = "4.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae03c8c853dba7bfd23e571ff0cff7bc9dceb40a4cd684cd1681824183f45257" +dependencies = [ + "bitflags", + "filetime", + "fsevent", + "fsevent-sys", + "inotify", + "libc", + "mio", + "mio-extras", + "walkdir", + "winapi 0.3.9", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -1372,7 +1519,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" dependencies = [ "overload", - "winapi", + "winapi 0.3.9", ] [[package]] @@ -1517,7 +1664,7 @@ version = "0.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba1ef8814b5c993410bb3adfad7a5ed269563e4a2f90c41f5d85be7fb47133bf" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "redox_syscall", "smallvec", @@ -1901,7 +2048,19 @@ version = "0.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" dependencies = [ - "winapi", + "winapi 0.3.9", +] + +[[package]] +name = "runmath" +version = "1.0.0" +dependencies = [ + "serde", + "serde_json", + "tauri", + "tauri-build", + "tauri-plugin-fs-watch", + "tauri-plugin-window-state", ] [[package]] @@ -2110,7 +2269,7 @@ version = "0.10.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "82e6b795fe2e3b1e845bafcb27aa35405c4d47cdfc92af5fc8d3002f76cebdc0" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest", ] @@ -2414,6 +2573,19 @@ dependencies = [ "tauri-utils", ] +[[package]] +name = "tauri-plugin-fs-watch" +version = "0.1.0" +source = "git+https://github.com/tauri-apps/plugins-workspace?branch=dev#f6ab641e08ae95f5541626d99b6115f9cc6f8db4" +dependencies = [ + "log", + "notify", + "serde", + "serde_json", + "tauri", + "thiserror", +] + [[package]] name = "tauri-plugin-window-state" version = "0.1.0" @@ -2500,12 +2672,12 @@ version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5cdb1ef4eaeeaddc8fbd371e5017057064af0911902ef36b39801f67cc6d79e4" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "fastrand", "libc", "redox_syscall", "remove_dir_all", - "winapi", + "winapi 0.3.9", ] [[package]] @@ -2625,7 +2797,7 @@ version = "0.1.37" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "pin-project-lite", "tracing-attributes", "tracing-core", @@ -2793,7 +2965,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" dependencies = [ "same-file", - "winapi", + "winapi 0.3.9", "winapi-util", ] @@ -2894,6 +3066,12 @@ dependencies = [ "windows-metadata", ] +[[package]] +name = "winapi" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" + [[package]] name = "winapi" version = "0.3.9" @@ -2904,6 +3082,12 @@ dependencies = [ "winapi-x86_64-pc-windows-gnu", ] +[[package]] +name = "winapi-build" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" + [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" @@ -2916,7 +3100,7 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" dependencies = [ - "winapi", + "winapi 0.3.9", ] [[package]] @@ -3105,6 +3289,16 @@ dependencies = [ "windows-implement", ] +[[package]] +name = "ws2_32-sys" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d59cefebd0c892fa2dd6de581e937301d8552cb44489cdff035c6187cb63fa5e" +dependencies = [ + "winapi 0.2.8", + "winapi-build", +] + [[package]] name = "x11" version = "2.20.1" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 40d5d78..2e44d7a 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -1,11 +1,11 @@ [package] -name = "app" -version = "0.1.0" -description = "A Tauri App" -authors = ["you"] -license = "" +name = "runmath" +version = "1.0.0" +description = "Do math, quickly 🚀" +authors = ["David Jiménez"] +license = "MIT" repository = "" -default-run = "app" +default-run = "runmath" edition = "2021" rust-version = "1.57" @@ -17,8 +17,9 @@ tauri-build = { version = "1.2.1", features = [] } [dependencies] serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } -tauri = { version = "1.2.1", features = ["clipboard-write-text", "global-shortcut-all", "process-exit", "window-create", "window-hide", "window-set-focus", "window-show", "window-start-dragging", "window-unminimize"] } +tauri = { version = "1.2.1", features = ["clipboard-write-text", "fs-create-dir", "fs-read-dir", "fs-read-file", "fs-write-file", "global-shortcut-all", "path-all", "process-exit", "window-close", "window-create", "window-hide", "window-set-focus", "window-show", "window-start-dragging", "window-unminimize"] } tauri-plugin-window-state = "0.1" +tauri-plugin-fs-watch = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" } [features] # by default Tauri runs in production mode diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 9650106..f215fdd 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -7,6 +7,7 @@ fn main() { let context = tauri::generate_context!(); tauri::Builder::default() .plugin(tauri_plugin_window_state::Builder::default().build()) + .plugin(tauri_plugin_fs_watch::init()) .menu(if cfg!(target_os = "macos") { tauri::Menu::os_default(&context.package_info().name) } else { diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index fad5da8..d12e4b7 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -24,10 +24,24 @@ "window": { "create": true, "hide": true, + "close": true, "unminimize": true, "setFocus": true, "show": true, "startDragging": true + }, + "fs": { + "createDir": true, + "readDir": true, + "readFile": true, + "writeFile": true, + "scope": [ + "$APPCONFIG", + "$APPCONFIG/*" + ] + }, + "path": { + "all": true } }, "bundle": { diff --git a/src/components/Settings/Checkbox.tsx b/src/components/Settings/Checkbox.tsx new file mode 100644 index 0000000..e8529ba --- /dev/null +++ b/src/components/Settings/Checkbox.tsx @@ -0,0 +1,35 @@ +import { useState } from "react"; + +type CheckboxProps = { + checked: boolean; + onChange: (newValue: boolean) => any; + className?: string; +}; + +export const Checkbox = (props: CheckboxProps) => { + const { checked, onChange, className } = props; + + const [isChecked, setIsChecked] = useState(() => checked); + + const toggleChecked = () => { + setIsChecked((prev) => !prev); + }; + + const handleKeyDown = (event: React.KeyboardEvent) => { + if (event.key === "Enter") { + toggleChecked(); + onChange(!isChecked); + } + }; + + return ( + onChange(e.target.checked)} + className={className} + onKeyDown={handleKeyDown} + onInput={toggleChecked} + /> + ); +}; diff --git a/src/components/Settings/index.module.css b/src/components/Settings/index.module.css new file mode 100644 index 0000000..e0264bc --- /dev/null +++ b/src/components/Settings/index.module.css @@ -0,0 +1,78 @@ +.settingsContainer { + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + height: 100%; + width: 100%; + gap: 1em; +} + +.settingItem { + padding: 0.5em; + border: 1px solid white; + width: 100%; + display: inline-flex; + gap: 0.5em; + justify-content: space-between; + align-items: center; +} + +.settingTextZone { + width: 80%; + align-self: flex-start; + display: flex; + flex-direction: column; + justify-content: center; + align-items: flex-start; + gap: 0.25em; +} + +.settingTitle { + font-size: large; + font-weight: bold; +} + +.settingDescription { + font-size: small; + font-weight: normal; +} + +.settingValue { + margin: 0 auto; + font-size: 1em; + font-weight: bold; +} + +.buttonarea { + display: flex; + flex-direction: row; + justify-content: space-between; + align-items: center; + width: 60%; + gap: 1em; +} + +.save { + margin: 0 auto; + font-size: 1em; + font-weight: bold; + color: white; + background-color: #1f6dbb; + border: 1px solid #ffffff; + border-radius: 0.25em; + padding: 0.5em; + cursor: pointer; +} + +.reset { + margin: 0 auto; + font-size: 1em; + font-weight: bold; + color: white; + background-color: #1f6dbb; + border: 1px solid #ffffff; + border-radius: 0.25em; + padding: 0.5em; + cursor: pointer; +} diff --git a/src/components/Settings/index.tsx b/src/components/Settings/index.tsx index 13541c1..46bba38 100644 --- a/src/components/Settings/index.tsx +++ b/src/components/Settings/index.tsx @@ -1,18 +1,74 @@ +// import { Checkbox } from "./Checkbox"; +import styles from "./index.module.css"; +import { settings, resetSettings } from "@utils/configureSettingsStorage"; + +const SETTINGS = [ + // { + // name: "Start RunMath on Windows Start", + // description: + // "RunMath will start automatically when you turn on your computer", + // SettingElement: () => ( + // + // settings.setCache("runOnWindowsStart", newValue) + // } + // className={styles.settingValue} + // /> + // ), + // }, + { + name: "Background Color", + description: "Change the background color of the app", + SettingElement: () => ( + + settings.setCache("background", event.target.value) + } + className={styles.settingValue} + /> + ), + }, +] as const; + export const Settings = () => { + const handleResetSettings = async () => { + await resetSettings(); + window.location.reload(); + }; + + const handleSaveSettings = async () => { + await settings.syncCache(); + import("@tauri-apps/api/window").then((module) => + module.getCurrent().close() + ); + }; + return ( - <> -
-

Settings

-
+
+

Settings

+ + {SETTINGS.map(({ SettingElement, description, name }) => ( + + ))} -
- {/* A color selector */} - - - +
+ + +
+
); }; diff --git a/src/main.tsx b/src/main.tsx index ca76e0c..200f57c 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -14,3 +14,5 @@ import("@utils/configureExitOnCloseWindow"); import("@utils/configureShortcuts").then((module) => module.configureShortcuts() ); +import("@utils/configureSettingsStorage"); +import("@utils/configureSettingsListener"); diff --git a/src/utils/configureSettingsListener.ts b/src/utils/configureSettingsListener.ts new file mode 100644 index 0000000..101458a --- /dev/null +++ b/src/utils/configureSettingsListener.ts @@ -0,0 +1,10 @@ +import { settings } from "./configureSettingsStorage"; + +// get the color on startup +const newColor = await settings.get("background"); +document.body.style.setProperty("--bg-color", newColor); + +window.addEventListener("settings-changed", async () => { + const newColor = await settings.get("background"); + document.body.style.setProperty("--bg-color", newColor); +}); diff --git a/src/utils/configureSettingsStorage.ts b/src/utils/configureSettingsStorage.ts new file mode 100644 index 0000000..75e3cf1 --- /dev/null +++ b/src/utils/configureSettingsStorage.ts @@ -0,0 +1,34 @@ +import { SettingsManager } from "tauri-settings"; +import { watch } from "tauri-plugin-fs-watch-api"; + +type Schema = { + /** + * A string that can contain a css color name or a hex color code + */ + background: string; + /** + * Whether the app should run on windows start + */ + runOnWindowsStart: boolean; +}; + +const settingsManager = new SettingsManager( + { runOnWindowsStart: false, background: "#a3c8ff" }, // default settings + { prettify: true } +); + +// checks whether the settings file exists and created it if not +// loads the settings if it exists +await settingsManager.initialize(); + +export const resetSettings = async () => { + settingsManager.settings = settingsManager.default; + await settings.syncCache(); +}; + +// watch the settings file for changes +watch(settingsManager.path, {}, () => { + window.dispatchEvent(new CustomEvent("settings-changed")); +}); + +export const settings = settingsManager; diff --git a/src/utils/settingsPageLauncher.ts b/src/utils/settingsPageLauncher.ts index 75004ea..bcd371a 100644 --- a/src/utils/settingsPageLauncher.ts +++ b/src/utils/settingsPageLauncher.ts @@ -3,6 +3,10 @@ import { TauriEvent } from "@tauri-apps/api/event"; export const createSettingsPage = () => { const webview = new WebviewWindow("settings-page", { + minWidth: 400, + minHeight: 500, + maxWidth: 800, + maxHeight: 800, title: "RunMath Settings", visible: false, url: "settings.html",