Initial wiring of managing settings
This commit is contained in:
parent
89b68d0422
commit
8b01314f43
@ -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
|
||||
}
|
||||
};
|
30
newton/fs.js
30
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
|
||||
}
|
||||
};
|
@ -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) {
|
||||
|
34
newton/settings-manager.js
Normal file
34
newton/settings-manager.js
Normal file
@ -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,
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user