1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 13:07:58 +00:00

Application settings

This commit is contained in:
Romein van Buren 2023-01-20 15:35:16 +01:00
parent 6703950734
commit 6c77941ae9
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49
10 changed files with 234 additions and 20 deletions

View File

@ -1,21 +1,20 @@
<script>
import { onMount } from 'svelte';
import { OpenConnection } from '../wailsjs/go/app/App';
import { Environment, WindowSetTitle } from '../wailsjs/runtime';
import { WindowSetTitle } from '../wailsjs/runtime';
import BlankState from './components/blankstate.svelte';
import ContextMenu from './components/contextmenu.svelte';
import AddressBar from './organisms/addressbar/index.svelte';
import Connection from './organisms/connection/index.svelte';
import { busy, contextMenu, connections } from './stores';
import Settings from './organisms/settings/index.svelte';
import { busy, contextMenu, connections, environment, applicationSettings } from './stores';
import { controlKeyDown } from './utils';
let hosts = {};
let environment;
let activeHostKey = '';
let activeDbKey = '';
let activeCollKey = '';
let addressBarModalOpen = true;
let settingsModalOpen = false;
$: host = hosts[activeHostKey];
$: connection = $connections[activeHostKey];
@ -39,17 +38,20 @@
busy.end();
}
onMount(() => {
Environment().then(e => environment = e);
});
function keydown(e) {
if (controlKeyDown(e) && e.key === ',') {
settingsModalOpen = true;
e.preventDefault();
}
}
</script>
<svelte:window on:contextmenu|preventDefault />
<svelte:window on:contextmenu|preventDefault on:keydown={keydown} />
<div id="root" class="platform-{environment?.platform}">
<div id="root" class="platform-{$environment?.platform}">
<div class="titlebar"></div>
{#if environment}
{#if $environment && $applicationSettings}
<main class:empty={!host || !connection}>
<AddressBar bind:hosts bind:activeHostKey on:select={e => openConnection(e.detail)} bind:modalOpen={addressBarModalOpen} />
@ -63,6 +65,7 @@
{#key $contextMenu}
<ContextMenu {...$contextMenu} on:close={contextMenu.hide} />
{/key}
<Settings bind:show={settingsModalOpen} />
{/if}
</div>

View File

@ -8,15 +8,16 @@
import FindViewConfigModal from './find-viewconfig.svelte';
import { onMount } from 'svelte';
import Grid from '../../../components/grid.svelte';
import { applicationSettings } from '../../../stores';
export let collection;
const defaults = {
query: '{}',
sort: '{ "_id": 1 }',
sort: $applicationSettings.defaultSort || '{ "_id": 1 }',
fields: '{}',
skip: 0,
limit: 15,
limit: $applicationSettings.defaultLimit || 15,
};
let form = { ...defaults };
@ -72,7 +73,9 @@
async function refresh() {
await getViewConfig();
await submitQuery();
if ($applicationSettings.autosubmitQuery) {
await submitQuery();
}
}
function prev() {

View File

@ -1,7 +1,6 @@
<script>
import { onMount, tick } from 'svelte';
import { Hosts } from '../../../wailsjs/go/app/App';
import { Environment } from '../../../wailsjs/runtime';
import { input } from '../../actions';
import Modal from '../../components/modal.svelte';
import DatabaseList from './dblist.svelte';
@ -13,7 +12,6 @@
export let activeDbKey = '';
export let activeCollKey = '';
let environment;
let addressBarModalOpen = true;
let dbList;
@ -43,7 +41,6 @@
}
onMount(() => {
Environment().then(e => environment = e);
Hosts().then(h => hosts = h);
});
</script>

View File

@ -0,0 +1,37 @@
<script>
import { input } from '../../actions';
import Modal from '../../components/modal.svelte';
import { applicationSettings as settings } from '../../stores';
export let show = false;
</script>
<Modal title="Prefrences" bind:show>
<div class="prefs">
<label for="defaultLimit">Initial number of items to retrieve using one query (limit):</label>
<label class="field">
<input type="number" bind:value={$settings.defaultLimit} id="defaultLimit" use:input={{ autofocus: true }} />
<span class="label">items</span>
</label>
<label for="defaultSort">Default sort query</label>
<label class="field">
<input type="text" class="code" bind:value={$settings.defaultSort} id="defaultSort" use:input={{ json: true }} />
</label>
<label for="autosubmitQuery">Autosubmit query</label>
<span>
<input type="checkbox" id="autosubmitQuery" bind:checked={$settings.autosubmitQuery} />
<label for="autosubmitQuery">Query items automatically when you open a collection</label>
</span>
</div>
</Modal>
<style>
.prefs {
display: grid;
grid-template-columns: auto auto;
gap: 0.5rem;
align-items: center;
}
</style>

View File

@ -1,4 +1,6 @@
import { writable } from 'svelte/store';
import { Environment } from '../wailsjs/runtime/runtime';
import { Settings, UpdateSettings } from '../wailsjs/go/app/App';
export const busy = (() => {
const { update, subscribe } = writable(0);
@ -21,6 +23,7 @@ export const busy = (() => {
export const contextMenu = (() => {
const { set, subscribe } = writable();
return {
show: (evt, menu) => set(menu ? {
position: [ evt.clientX, evt.clientY ],
@ -32,3 +35,31 @@ export const contextMenu = (() => {
})();
export const connections = writable({});
export const applicationSettings = (() => {
const { set, subscribe } = writable({});
const reload = async() => {
const newSettings = await Settings();
set(newSettings);
return newSettings;
};
reload();
subscribe(newSettings => {
UpdateSettings(JSON.stringify(newSettings || {}));
});
return { reload, set, subscribe };
})();
export const environment = (() => {
const { set, subscribe } = writable({});
const reload = async() => {
const newEnv = await Environment();
set(newEnv);
return newEnv;
};
reload();
return { reload, subscribe };
})();

View File

@ -1,3 +1,6 @@
import { get } from 'svelte/store';
import { environment } from './stores';
export function resolveKeypath(object, path) {
// Get a value from an object with a JSON path, from Webdesq core
@ -19,3 +22,14 @@ export function resolveKeypath(object, path) {
return result;
}
export function controlKeyDown(event) {
const env = get(environment);
// @ts-ignore
if (env?.platform === 'darwin') {
return event?.metaKey;
}
else {
return event?.ctrlKey;
}
}

View File

@ -32,6 +32,10 @@ export function RemoveItemById(arg1:string,arg2:string,arg3:string,arg4:string):
export function RemoveItems(arg1:string,arg2:string,arg3:string,arg4:string,arg5:boolean):Promise<number>;
export function Settings():Promise<app.Settings>;
export function UpdateHost(arg1:string,arg2:string):Promise<void>;
export function UpdateItems(arg1:string,arg2:string,arg3:string,arg4:string):Promise<number>;
export function UpdateSettings(arg1:string):Promise<app.Settings>;

View File

@ -58,6 +58,10 @@ export function RemoveItems(arg1, arg2, arg3, arg4, arg5) {
return window['go']['app']['App']['RemoveItems'](arg1, arg2, arg3, arg4, arg5);
}
export function Settings() {
return window['go']['app']['App']['Settings']();
}
export function UpdateHost(arg1, arg2) {
return window['go']['app']['App']['UpdateHost'](arg1, arg2);
}
@ -65,3 +69,7 @@ export function UpdateHost(arg1, arg2) {
export function UpdateItems(arg1, arg2, arg3, arg4) {
return window['go']['app']['App']['UpdateItems'](arg1, arg2, arg3, arg4);
}
export function UpdateSettings(arg1) {
return window['go']['app']['App']['UpdateSettings'](arg1);
}

View File

@ -1,5 +1,21 @@
export namespace app {
export class Settings {
defaultLimit: number;
defaultSort: string;
autosubmitQuery: boolean;
static createFrom(source: any = {}) {
return new Settings(source);
}
constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.defaultLimit = source["defaultLimit"];
this.defaultSort = source["defaultSort"];
this.autosubmitQuery = source["autosubmitQuery"];
}
}
export class findResult {
total: number;
results: any;

View File

@ -1,18 +1,37 @@
package app
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
goRuntime "runtime"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
type Settings struct {
DefaultLimit int64 `json:"defaultLimit"`
DefaultSort string `json:"defaultSort"`
AutosubmitQuery bool `json:"autosubmitQuery"`
}
func NewSettings() Settings {
return Settings{
DefaultLimit: 20,
DefaultSort: `{ "_id": 1 }`,
AutosubmitQuery: true,
}
}
func appDataDirectory() (string, error) {
var err error
homeDir, err := os.UserHomeDir()
prefDir := ""
switch runtime.GOOS {
switch goRuntime.GOOS {
case "windows":
prefDir = filepath.Join(homeDir, "/AppData/Local/Mongodup")
case "darwin":
@ -35,3 +54,85 @@ func appDataFilePath(filename string) (string, error) {
path := filepath.Join(dir, filename)
return path, nil
}
func (a *App) Settings() Settings {
s := NewSettings()
filePath, err := appDataFilePath("settings.json")
if err != nil {
fmt.Println(err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Could not retrieve application settings, using defaults!",
Message: err.Error(),
})
return s
}
jsonData, err := ioutil.ReadFile(filePath)
if err != nil {
// It's ok if the file cannot be opened, for example if it is not accessible.
// Therefore no error is returned.
fmt.Println(err.Error())
return s
}
if len(jsonData) == 0 {
return s
} else {
err = json.Unmarshal(jsonData, &s)
if err != nil {
fmt.Println(err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.ErrorDialog,
Title: "Could not retrieve application settings, using defaults!",
Message: err.Error(),
})
}
return s
}
}
func (a *App) UpdateSettings(jsonData string) Settings {
s := a.Settings()
err := json.Unmarshal([]byte(jsonData), &s)
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "Malformed JSON",
Message: err.Error(),
})
return s
}
filePath, err := appDataFilePath("settings.json")
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "Could not update settings.json",
Message: err.Error(),
})
return s
}
newJson, err := json.MarshalIndent(s, "", "\t")
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "Could not marshal settings into JSON",
Message: err.Error(),
})
return s
}
err = ioutil.WriteFile(filePath, newJson, os.ModePerm)
if err != nil {
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Type: runtime.InfoDialog,
Title: "Could not update host list",
Message: err.Error(),
})
}
return s
}