diff --git a/app/src/electron.js b/app/src/electron.js index bbd1ac7c..7473e43e 100644 --- a/app/src/electron.js +++ b/app/src/electron.js @@ -30,14 +30,14 @@ try { initialConfig = {}; } -let settingsJson = {}; -try { - const datadir = path.join(os.homedir(), 'dbgate-data'); - settingsJson = JSON.parse(fs.readFileSync(path.join(datadir, 'settings.json'), { encoding: 'utf-8' })); -} catch (err) { - console.log('Error loading settings.json:', err.message); - settingsJson = {}; -} +// let settingsJson = {}; +// try { +// const datadir = path.join(os.homedir(), 'dbgate-data'); +// settingsJson = JSON.parse(fs.readFileSync(path.join(datadir, 'settings.json'), { encoding: 'utf-8' })); +// } catch (err) { +// console.log('Error loading settings.json:', err.message); +// settingsJson = {}; +// } // Keep a global reference of the window object, if you don't, the window will // be closed automatically when the JavaScript object is garbage collected. @@ -199,7 +199,10 @@ function createWindow() { mainMenu = buildMenu(); mainWindow.setMenu(mainMenu); - function loadMainWindow() { + async function loadMainWindow() { + const settings = await main.configController.getSettings(); + console.log(settings); + const startUrl = process.env.ELECTRON_START_URL || url.format({ diff --git a/packages/api/src/controllers/config.js b/packages/api/src/controllers/config.js index d4221e21..69d71cbc 100644 --- a/packages/api/src/controllers/config.js +++ b/packages/api/src/controllers/config.js @@ -5,21 +5,24 @@ const { datadir } = require('../utility/directories'); const hasPermission = require('../utility/hasPermission'); const socket = require('../utility/socket'); const _ = require('lodash'); +const AsyncLock = require('async-lock'); const currentVersion = require('../currentVersion'); const platformInfo = require('../utility/platformInfo'); const connections = require('../controllers/connections'); -module.exports = { - settingsValue: {}, +const lock = new AsyncLock(); - async _init() { - try { - this.settingsValue = JSON.parse(await fs.readFile(path.join(datadir(), 'settings.json'), { encoding: 'utf-8' })); - } catch (err) { - this.settingsValue = {}; - } - }, +module.exports = { + // settingsValue: {}, + + // async _init() { + // try { + // this.settingsValue = JSON.parse(await fs.readFile(path.join(datadir(), 'settings.json'), { encoding: 'utf-8' })); + // } catch (err) { + // this.settingsValue = {}; + // } + // }, get_meta: true, async get() { @@ -40,24 +43,33 @@ module.exports = { getSettings_meta: true, async getSettings() { - return this.settingsValue; + try { + return JSON.parse(await fs.readFile(path.join(datadir(), 'settings.json'), { encoding: 'utf-8' })); + } catch (err) { + return {}; + } }, updateSettings_meta: true, async updateSettings(values) { if (!hasPermission(`settings/change`)) return false; - try { - const updated = { - ...this.settingsValue, - ...values, - }; - await fs.writeFile(path.join(datadir(), 'settings.json'), JSON.stringify(updated, undefined, 2)); - this.settingsValue = updated; - socket.emitChanged(`settings-changed`); - return updated; - } catch (err) { - return false; - } + + const res = await lock.acquire('update', async () => { + const currentValue = await this.getSettings(); + try { + const updated = { + ...currentValue, + ...values, + }; + await fs.writeFile(path.join(datadir(), 'settings.json'), JSON.stringify(updated, undefined, 2)); + // this.settingsValue = updated; + socket.emitChanged(`settings-changed`); + return updated; + } catch (err) { + return false; + } + }); + return res; }, changelog_meta: true, diff --git a/packages/api/src/controllers/databaseConnections.js b/packages/api/src/controllers/databaseConnections.js index 68dd2649..21baa660 100644 --- a/packages/api/src/controllers/databaseConnections.js +++ b/packages/api/src/controllers/databaseConnections.js @@ -110,7 +110,7 @@ module.exports = { msgtype: 'connect', connection: { ...connection, database }, structure: lastClosed ? lastClosed.structure : null, - globalSettings: config.settingsValue, + globalSettings: await config.getSettings(), }); return newOpened; }, diff --git a/packages/api/src/controllers/serverConnections.js b/packages/api/src/controllers/serverConnections.js index 47f7de29..673fefeb 100644 --- a/packages/api/src/controllers/serverConnections.js +++ b/packages/api/src/controllers/serverConnections.js @@ -69,7 +69,7 @@ module.exports = { if (newOpened.disconnected) return; this.close(conid, false); }); - subprocess.send({ msgtype: 'connect', ...connection, globalSettings: config.settingsValue }); + subprocess.send({ msgtype: 'connect', ...connection, globalSettings: await config.getSettings() }); return newOpened; }); return res; diff --git a/packages/api/src/main.js b/packages/api/src/main.js index c95eaae8..b3268cc7 100644 --- a/packages/api/src/main.js +++ b/packages/api/src/main.js @@ -145,4 +145,4 @@ function initializeElectronSender(electronSender) { socket.setElectronSender(electronSender); } -module.exports = { start, useAllControllers, initializeElectronSender }; +module.exports = { start, useAllControllers, initializeElectronSender, configController: config }; diff --git a/packages/web/src/forms/SettingsCheckboxField.svelte b/packages/web/src/forms/SettingsCheckboxField.svelte new file mode 100644 index 00000000..68e3d3bd --- /dev/null +++ b/packages/web/src/forms/SettingsCheckboxField.svelte @@ -0,0 +1,23 @@ + + + setFieldValue(name, !$values[name]) }} +> + + diff --git a/packages/web/src/forms/SettingsCheckboxFieldRaw.svelte b/packages/web/src/forms/SettingsCheckboxFieldRaw.svelte new file mode 100644 index 00000000..86064b2d --- /dev/null +++ b/packages/web/src/forms/SettingsCheckboxFieldRaw.svelte @@ -0,0 +1,17 @@ + + + diff --git a/packages/web/src/forms/SettingsFormProvider.svelte b/packages/web/src/forms/SettingsFormProvider.svelte new file mode 100644 index 00000000..b1950c00 --- /dev/null +++ b/packages/web/src/forms/SettingsFormProvider.svelte @@ -0,0 +1,42 @@ + + + + + diff --git a/packages/web/src/forms/SettingsTextField.svelte b/packages/web/src/forms/SettingsTextField.svelte new file mode 100644 index 00000000..aebcc501 --- /dev/null +++ b/packages/web/src/forms/SettingsTextField.svelte @@ -0,0 +1,15 @@ + + + + + diff --git a/packages/web/src/forms/SettingsTextFieldRaw.svelte b/packages/web/src/forms/SettingsTextFieldRaw.svelte new file mode 100644 index 00000000..f99875e5 --- /dev/null +++ b/packages/web/src/forms/SettingsTextFieldRaw.svelte @@ -0,0 +1,18 @@ + + + diff --git a/packages/web/src/settings/SettingsModal.svelte b/packages/web/src/settings/SettingsModal.svelte index 679eb6cc..c409d298 100644 --- a/packages/web/src/settings/SettingsModal.svelte +++ b/packages/web/src/settings/SettingsModal.svelte @@ -1,5 +1,6 @@ - +
Settings
{#if electron}
Appearance
- + showSnackbarInfo('Native menu settings will be applied after app restart')} + /> {/if}
Data grid
@@ -58,6 +60,7 @@ defaultValue="100" /> + @@ -76,11 +79,11 @@
- - + +
-
+