finish settings

This commit is contained in:
Joseph Montanaro 2023-04-25 22:09:20 -07:00
parent 31532cd76e
commit 0d37814cf4
5 changed files with 113 additions and 14 deletions

View File

@ -0,0 +1,62 @@
<script>
import { createEventDispatcher } from 'svelte';
import Setting from './Setting.svelte';
export let title;
export let value;
export let unit = '';
export let min = null;
export let max = null;
export let decimal = false;
const dispatch = createEventDispatcher();
let error = null;
function validate(event) {
let v = event.target.value;
if (v === '') {
error = null;
return;
}
let num = parseFloat(v);
if (Number.isNaN(num)) {
error = `"${v}" is not a number`;
}
else if (num % 1 !== 0 && !decimal) {
error = `${num} is not a whole number`;
}
else if (min && num < min) {
error = `Too low (minimum ${min})`;
}
else if (max && num > max) {
error = `Too large (maximum ${max})`
}
else {
error = null;
value = num;
dispatch('update', {value})
}
}
</script>
<Setting {title} {error}>
<div slot="input">
{#if unit}
<span class="mr-2">{unit}:</span>
{/if}
<div class="tooltip tooltip-error" class:tooltip-open={error !== null} data-tip={error}>
<input
type="text"
class="input input-sm input-bordered text-right max-w-[4rem]"
class:input-error={error}
value={value}
on:input="{validate}"
/>
</div>
</div>
<slot name="description" slot="description"></slot>
</Setting>

View File

@ -0,0 +1,18 @@
<script>
import { slide } from 'svelte/transition';
import ErrorAlert from '../ErrorAlert.svelte';
export let title;
export let error = null;
</script>
<div class="divider"></div>
<div class="flex justify-between">
<h3 class="text-lg font-bold">{title}</h3>
<slot name="input"></slot>
</div>
<p class="mt-3">
<slot name="description"></slot>
</p>

View File

@ -0,0 +1,22 @@
<script>
import { createEventDispatcher } from 'svelte';
import Setting from './Setting.svelte';
export let title;
export let value;
const dispatch = createEventDispatcher();
</script>
<Setting title="Start minimized">
<input
slot="input"
type="checkbox"
class="toggle toggle-success"
bind:checked={value}
on:change={e => dispatch('update', {value: e.target.checked})}
/>
<slot name="description" slot="description"></slot>
</Setting>

3
src/ui/settings/index.js Normal file
View File

@ -0,0 +1,3 @@
export { default as Setting } from './Setting.svelte';
export { default as ToggleSetting } from './ToggleSetting.svelte';
export { default as NumericSetting } from './NumericSetting.svelte';

View File

@ -6,18 +6,11 @@
import Link from '../ui/Link.svelte'; import Link from '../ui/Link.svelte';
import ErrorAlert from '../ui/ErrorAlert.svelte'; import ErrorAlert from '../ui/ErrorAlert.svelte';
// import Setting from '../ui/settings/Setting.svelte'; // import Setting from '../ui/settings/Setting.svelte';
import { Setting, NumericSetting } from '../ui/settings'; import { Setting, ToggleSetting, NumericSetting } from '../ui/settings';
async function save() { async function save() {
try {
await invoke('save_config', {config: $appState.config}); await invoke('save_config', {config: $appState.config});
} }
catch (e) {
}
}
window.state = $appState;
</script> </script>
@ -27,12 +20,13 @@
<div class="mx-auto mt-3 max-w-md"> <div class="mx-auto mt-3 max-w-md">
<h2 class="text-2xl font-bold text-center">Settings</h2> <h2 class="text-2xl font-bold text-center">Settings</h2>
<Setting title="Start minimized"> <ToggleSetting title="Start minimized" bind:value={$appState.config.start_minimized} on:update={save}>
<input slot="input" type="checkbox" bind:checked="{$appState.config.start_minimized}" class="toggle toggle-success" /> <svelte:fragment slot="description">
<svelte:fragment slot="description">Minimize to the system tray at startup.</svelte:fragment> Minimize to the system tray at startup.
</Setting> </svelte:fragment>
</ToggleSetting>
<NumericSetting title="Re-hide delay" bind:value={$appState.config.rehide_ms} min={0} unit="Milliseconds"> <NumericSetting title="Re-hide delay" bind:value={$appState.config.rehide_ms} min={0} unit="Milliseconds" on:update={save}>
<svelte:fragment slot="description"> <svelte:fragment slot="description">
How long to wait after a request is approved/denied before minimizing How long to wait after a request is approved/denied before minimizing
the window to tray. Only applicable if the window was minimized the window to tray. Only applicable if the window was minimized