From 8b01314f430ebca3f31b5905217a87217012b927 Mon Sep 17 00:00:00 2001 From: itdominator <1itdominator@gmail.com> Date: Sat, 7 Jun 2025 23:38:42 -0500 Subject: [PATCH] Initial wiring of managing settings --- newton/app.js | 15 ++++++++++----- newton/fs.js | 30 +++++++++++++++++++++--------- newton/main.js | 10 ++++++---- newton/settings-manager.js | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 newton/settings-manager.js diff --git a/newton/app.js b/newton/app.js index 1d4d322..be91a15 100644 --- a/newton/app.js +++ b/newton/app.js @@ -2,6 +2,7 @@ const { BrowserWindow } = require('electron'); const path = require('node:path'); const { menu } = require('./menu'); +const { settingsManager } = require('./settings-manager'); const { newtonFs } = require('./fs'); @@ -15,16 +16,19 @@ let args = []; // Note: https://tinydew4.gitbooks.io/electron/content/api/browser-window.html const createWindow = (startType = "build", debug = false, args = []) => { this.args = args; + let data = settingsManager.getConfig(); const win = new BrowserWindow({ title: "Newton", - width: 800, - height: 600, - minWidth: 800, - minHeight: 600, + x: data["config"]["main_window_x"], + y: data["config"]["main_window_y"], + width: data["config"]["main_window_width"], + height: data["config"]["main_window_height"], + minWidth: data["config"]["main_window_min_width"], + minHeight: data["config"]["main_window_min_height"], show: false, transparent: true, - icon: path.join(__dirname, ICON_PATH), + icon: settingsManager.getIconPath(), webPreferences: { preload: path.join(__dirname, 'preload.js'), contextIsolation: true, @@ -75,5 +79,6 @@ module.exports = { newton: { createWindow: createWindow, fs: newtonFs, + settings: settingsManager } }; \ No newline at end of file diff --git a/newton/fs.js b/newton/fs.js index fb622d6..e731f4f 100644 --- a/newton/fs.js +++ b/newton/fs.js @@ -5,11 +5,26 @@ const os = require('os') -const BASE_PATH = '../dist/app'; -const LSP_CONFIG_PATH = `${BASE_PATH}/resources/lsp-servers-config.json`; -let window = null; +const HOME_DIR = os.homedir(); +const BASE_PATH = '../dist/app'; +const CONFIG_PATH = path.join(HOME_DIR, "/.config/newton/"); +const SETTINGS_CONFIG_PATH = path.join(CONFIG_PATH, "/settings.json"); +const LSP_CONFIG_PATH = path.join(BASE_PATH, "/resources/lsp-servers-config.json") +let window = null; +const getIconPath = () => { + return path.join(CONFIG_PATH, "./icons/newton.png"); +} + +const getSettingsConfigData = () => { + return getFileContents(SETTINGS_CONFIG_PATH); +} + +const getLspConfigData = () => { + const config = getFileContents(LSP_CONFIG_PATH, useRelativePath = true); + return config.replaceAll("{user.home}", HOME_DIR); +} const getFileContents = (_path, useRelativePath = false) => { console.log(`Getting Contents For: ${_path}`); @@ -25,11 +40,6 @@ const getFileContents = (_path, useRelativePath = false) => { } } -const getLspConfigData = () => { - const config = getFileContents(LSP_CONFIG_PATH, true); - return config.replaceAll("{user.home}", os.homedir()); -} - const saveFile = (fpath, content) => { fs.writeFile(fpath, content, (err) => { if (!err) return @@ -52,7 +62,7 @@ const openFiles = (startPath) => { dialog.showOpenDialog( { title: "Open File(s):", - defaultPath: (startPath) ? startPath : os.homedir(), + defaultPath: (startPath) ? startPath : HOME_DIR, filters: [ { name: "All Files", extensions: ["*"] }, { name: "c", extensions: [".h", ".c"] }, @@ -90,8 +100,10 @@ module.exports = { openFiles: openFiles, saveFile: saveFile, saveFileAs: saveFileAs, + getIconPath: getIconPath, getFileContents: getFileContents, getLspConfigData: getLspConfigData, + getSettingsConfigData: getSettingsConfigData, setWindow: setWindow } }; \ No newline at end of file diff --git a/newton/main.js b/newton/main.js index 5ccfb3f..fe95813 100644 --- a/newton/main.js +++ b/newton/main.js @@ -52,10 +52,10 @@ app.whenReady().then(() => { loadArgs(); loadHandlers(); + newton.settings.loadsettings(); let window = newton.createWindow(startType, isDebug, args); - newton.fs.setWindow(window); -}) +}); app.on('activate', () => { if (BrowserWindow.getAllWindows().length === 0) { @@ -67,7 +67,9 @@ app.on('window-all-closed', () => { if (process.platform !== 'darwin') { app.quit() }; -}) + + newton.settings.saveConfig(); +}); process.on('uncaughtException', (error) => { if (error.message != null) { @@ -87,4 +89,4 @@ process.on('unhandledRejection', function(error = {}) { if (error.stack != null) { console.log(error.stack); } -}); \ No newline at end of file +}); diff --git a/newton/settings-manager.js b/newton/settings-manager.js new file mode 100644 index 0000000..a1f1efa --- /dev/null +++ b/newton/settings-manager.js @@ -0,0 +1,34 @@ +const { newtonFs } = require('./fs'); + + +let config = {}; + + +const loadsettings = () => { + config = JSON.parse( + newtonFs.getSettingsConfigData() + ); +} + +const getConfig = () => { + return config; +} + +const saveConfig = () => { + console.log(config); +} + +const getIconPath = () => { + return newtonFs.getIconPath(); +} + + + +module.exports = { + settingsManager: { + getIconPath: getIconPath, + loadsettings: loadsettings, + getConfig: getConfig, + saveConfig: saveConfig, + } +};