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 path = require('node:path');
|
||||||
|
|
||||||
const { menu } = require('./menu');
|
const { menu } = require('./menu');
|
||||||
|
const { settingsManager } = require('./settings-manager');
|
||||||
const { newtonFs } = require('./fs');
|
const { newtonFs } = require('./fs');
|
||||||
|
|
||||||
|
|
||||||
@ -15,16 +16,19 @@ let args = [];
|
|||||||
// Note: https://tinydew4.gitbooks.io/electron/content/api/browser-window.html
|
// Note: https://tinydew4.gitbooks.io/electron/content/api/browser-window.html
|
||||||
const createWindow = (startType = "build", debug = false, args = []) => {
|
const createWindow = (startType = "build", debug = false, args = []) => {
|
||||||
this.args = args;
|
this.args = args;
|
||||||
|
let data = settingsManager.getConfig();
|
||||||
|
|
||||||
const win = new BrowserWindow({
|
const win = new BrowserWindow({
|
||||||
title: "Newton",
|
title: "Newton",
|
||||||
width: 800,
|
x: data["config"]["main_window_x"],
|
||||||
height: 600,
|
y: data["config"]["main_window_y"],
|
||||||
minWidth: 800,
|
width: data["config"]["main_window_width"],
|
||||||
minHeight: 600,
|
height: data["config"]["main_window_height"],
|
||||||
|
minWidth: data["config"]["main_window_min_width"],
|
||||||
|
minHeight: data["config"]["main_window_min_height"],
|
||||||
show: false,
|
show: false,
|
||||||
transparent: true,
|
transparent: true,
|
||||||
icon: path.join(__dirname, ICON_PATH),
|
icon: settingsManager.getIconPath(),
|
||||||
webPreferences: {
|
webPreferences: {
|
||||||
preload: path.join(__dirname, 'preload.js'),
|
preload: path.join(__dirname, 'preload.js'),
|
||||||
contextIsolation: true,
|
contextIsolation: true,
|
||||||
@ -75,5 +79,6 @@ module.exports = {
|
|||||||
newton: {
|
newton: {
|
||||||
createWindow: createWindow,
|
createWindow: createWindow,
|
||||||
fs: newtonFs,
|
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 HOME_DIR = os.homedir();
|
||||||
const LSP_CONFIG_PATH = `${BASE_PATH}/resources/lsp-servers-config.json`;
|
const BASE_PATH = '../dist/app';
|
||||||
let window = null;
|
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) => {
|
const getFileContents = (_path, useRelativePath = false) => {
|
||||||
console.log(`Getting Contents For: ${_path}`);
|
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) => {
|
const saveFile = (fpath, content) => {
|
||||||
fs.writeFile(fpath, content, (err) => {
|
fs.writeFile(fpath, content, (err) => {
|
||||||
if (!err) return
|
if (!err) return
|
||||||
@ -52,7 +62,7 @@ const openFiles = (startPath) => {
|
|||||||
dialog.showOpenDialog(
|
dialog.showOpenDialog(
|
||||||
{
|
{
|
||||||
title: "Open File(s):",
|
title: "Open File(s):",
|
||||||
defaultPath: (startPath) ? startPath : os.homedir(),
|
defaultPath: (startPath) ? startPath : HOME_DIR,
|
||||||
filters: [
|
filters: [
|
||||||
{ name: "All Files", extensions: ["*"] },
|
{ name: "All Files", extensions: ["*"] },
|
||||||
{ name: "c", extensions: [".h", ".c"] },
|
{ name: "c", extensions: [".h", ".c"] },
|
||||||
@ -90,8 +100,10 @@ module.exports = {
|
|||||||
openFiles: openFiles,
|
openFiles: openFiles,
|
||||||
saveFile: saveFile,
|
saveFile: saveFile,
|
||||||
saveFileAs: saveFileAs,
|
saveFileAs: saveFileAs,
|
||||||
|
getIconPath: getIconPath,
|
||||||
getFileContents: getFileContents,
|
getFileContents: getFileContents,
|
||||||
getLspConfigData: getLspConfigData,
|
getLspConfigData: getLspConfigData,
|
||||||
|
getSettingsConfigData: getSettingsConfigData,
|
||||||
setWindow: setWindow
|
setWindow: setWindow
|
||||||
}
|
}
|
||||||
};
|
};
|
@ -52,10 +52,10 @@ app.whenReady().then(() => {
|
|||||||
loadArgs();
|
loadArgs();
|
||||||
loadHandlers();
|
loadHandlers();
|
||||||
|
|
||||||
|
newton.settings.loadsettings();
|
||||||
let window = newton.createWindow(startType, isDebug, args);
|
let window = newton.createWindow(startType, isDebug, args);
|
||||||
|
|
||||||
newton.fs.setWindow(window);
|
newton.fs.setWindow(window);
|
||||||
})
|
});
|
||||||
|
|
||||||
app.on('activate', () => {
|
app.on('activate', () => {
|
||||||
if (BrowserWindow.getAllWindows().length === 0) {
|
if (BrowserWindow.getAllWindows().length === 0) {
|
||||||
@ -67,7 +67,9 @@ app.on('window-all-closed', () => {
|
|||||||
if (process.platform !== 'darwin') {
|
if (process.platform !== 'darwin') {
|
||||||
app.quit()
|
app.quit()
|
||||||
};
|
};
|
||||||
})
|
|
||||||
|
newton.settings.saveConfig();
|
||||||
|
});
|
||||||
|
|
||||||
process.on('uncaughtException', (error) => {
|
process.on('uncaughtException', (error) => {
|
||||||
if (error.message != null) {
|
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