Compare commits

...

2 Commits

Author SHA1 Message Date
Jan Prochazka
c5fb6a6237 #290 removed experimental not working code 2022-06-02 14:53:16 +02:00
Jan Prochazka
a1cfa4f94b mutliple windows WIP #290 2022-06-02 14:50:04 +02:00
7 changed files with 63 additions and 23 deletions

View File

@@ -22,6 +22,7 @@ const configRootPath = path.join(app.getPath('userData'), 'config-root.json');
let initialConfig = {};
let apiLoaded = false;
let mainModule;
let winCounter = 0;
const isMac = () => os.platform() == 'darwin';
@@ -34,8 +35,8 @@ try {
// 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.
let mainWindow;
let mainMenu;
const mainWindows = {};
const mainMenus = {};
let runCommandOnLoad = null;
log.transports.file.level = 'debug';
@@ -134,6 +135,7 @@ ipcMain.on('update-commands', async (event, arg) => {
}
});
ipcMain.on('quit-app', async (event, arg) => {
app.quit();
if (isMac()) {
app.quit();
} else {
@@ -155,6 +157,9 @@ ipcMain.on('app-started', async (event, arg) => {
runCommandOnLoad = null;
}
});
ipcMain.on('new-window', async (event, arg) => {
createWindow();
});
ipcMain.on('window-action', async (event, arg) => {
if (!mainWindow) {
return;
@@ -214,18 +219,18 @@ ipcMain.on('window-action', async (event, arg) => {
}
});
ipcMain.handle('showOpenDialog', async (event, options) => {
ipcMain.handle('showOpenDialog', async (event, { winid, options }) => {
const res = electron.dialog.showOpenDialogSync(mainWindow, options);
return res;
});
ipcMain.handle('showSaveDialog', async (event, options) => {
ipcMain.handle('showSaveDialog', async (event, { winid, options }) => {
const res = electron.dialog.showSaveDialogSync(mainWindow, options);
return res;
});
ipcMain.handle('showItemInFolder', async (event, path) => {
ipcMain.handle('showItemInFolder', async (event, { winid, path }) => {
electron.shell.showItemInFolder(path);
});
ipcMain.handle('openExternal', async (event, url) => {
ipcMain.handle('openExternal', async (event, { winid, url }) => {
electron.shell.openExternal(url);
});
@@ -268,6 +273,7 @@ function createWindow() {
nodeIntegration: true,
contextIsolation: false,
spellcheck: false,
additionalArguments: [`--winid=${++winCounter}`],
},
});
@@ -332,7 +338,7 @@ function createWindow() {
mainModule = main;
apiLoaded = true;
}
mainModule.setElectronSender(mainWindow.webContents);
mainModule.addElectronSender(mainWindow.webContents);
loadMainWindow();
@@ -341,8 +347,8 @@ function createWindow() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainModule.removeElectronSender(mainWindow.webContents);
mainWindow = null;
mainModule.setElectronSender(null);
});
}

View File

@@ -4,6 +4,7 @@ module.exports = ({ editMenu }) => [
submenu: [
{ command: 'new.connection', hideDisabled: true },
{ command: 'new.sqliteDatabase', hideDisabled: true },
{ command: 'new.window', hideDisabled: true },
{ divider: true },
{ command: 'new.query', hideDisabled: true },
{ command: 'new.freetable', hideDisabled: true },

View File

@@ -155,8 +155,17 @@ function useAllControllers(app, electron) {
useController(app, electron, '/apps', apps);
}
function setElectronSender(electronSender) {
socket.setElectronSender(electronSender);
function addElectronSender(electronSender) {
socket.addElectronSender(electronSender);
}
function removeElectronSender(electronSender) {
socket.removeElectronSender(electronSender);
}
module.exports = { start, useAllControllers, setElectronSender, configController: config };
module.exports = {
start,
useAllControllers,
addElectronSender,
removeElectronSender,
configController: config,
};

View File

@@ -1,5 +1,5 @@
let sseResponse = null;
let electronSender = null;
let electronSenders = [];
let init = [];
module.exports = {
@@ -7,18 +7,25 @@ module.exports = {
sseResponse = value;
setInterval(() => this.emit('ping'), 29 * 1000);
},
setElectronSender(value) {
electronSender = value;
addElectronSender(value) {
electronSenders = [...electronSenders, value];
},
removeElectronSender(value) {
electronSenders = electronSenders.filter(x => x != value);
},
emit(message, data) {
if (electronSender) {
if (electronSenders.length > 0) {
if (init.length > 0) {
for (const item of init) {
electronSender.send(item.message, item.data == null ? null : item.data);
for (const sender of electronSenders) {
sender.send(item.message, item.data == null ? null : item.data);
}
}
init = [];
}
electronSender.send(message, data == null ? null : data);
for (const sender of electronSenders) {
sender.send(message, data == null ? null : data);
}
} else if (sseResponse) {
if (init.length > 0) {
for (const item of init) {

View File

@@ -467,6 +467,15 @@ registerCommand({
onClick: () => getElectron().send('quit-app'),
});
registerCommand({
id: 'new.window',
category: 'New',
name: 'New Window',
keyText: 'CtrlOrCommand+N',
testEnabled: () => getElectron() != null,
onClick: () => getElectron().send('new-window'),
});
registerCommand({
id: 'app.logout',
category: 'App',

View File

@@ -38,7 +38,7 @@ export async function apiCall(route: string, args: {} = undefined) {
const electron = getElectron();
if (electron) {
const resp = await electron.invoke(route.replace('/', '-'), args);
const resp = await electron.invokeApi(route.replace('/', '-'), args);
return processApiResponse(route, args, resp);
} else {
const resp = await fetch(`${resolveApi()}/${route}`, {

View File

@@ -1,32 +1,40 @@
class ElectronApi {
private ipcRenderer = getIpcRenderer();
winid = null;
constructor() {}
constructor() {
this.winid = window.process?.argv?.find(x => x.startsWith('winid='))?.substring('winid='.length);
}
send(msg, args = null) {
this.ipcRenderer.send(msg, args);
}
async showOpenDialog(options) {
const res = await this.ipcRenderer.invoke('showOpenDialog', options);
const res = await this.invoke('showOpenDialog', options);
return res;
}
async showSaveDialog(options) {
const res = await this.ipcRenderer.invoke('showSaveDialog', options);
const res = await this.invoke('showSaveDialog', options);
return res;
}
async showItemInFolder(path) {
const res = await this.ipcRenderer.invoke('showItemInFolder', path);
const res = await this.invoke('showItemInFolder', path);
return res;
}
async openExternal(url) {
await this.ipcRenderer.invoke('openExternal', url);
await this.invoke('openExternal', url);
}
async invoke(route, args) {
const res = await this.ipcRenderer.invoke(route, { winid: this.winid, args });
return res;
}
async invokeApi(route, args) {
const res = await this.ipcRenderer.invoke(route, args);
return res;
}