Skip to content

Commit

Permalink
Merge pull request #138 from tareqimbasher/fix/125
Browse files Browse the repository at this point in the history
Fixes #125: Main menu becoming empty when opening a dialog window
  • Loading branch information
tareqimbasher authored Dec 18, 2023
2 parents 57c13c7 + 3f2ef99 commit 106a936
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,25 @@ export class ElectronEventHandlerBackgroundService extends WithDisposables imple
public start(): Promise<void> {
const bootstrapChannel = new ChannelInfo("main-menu-bootstrap");

const sendBootstrapDataToMain = () => this.electronIpcGateway.send(bootstrapChannel, {
menuItems: this.mainMenuService?.items.map(i => this.mapToMenuItemDto(i))
});
const sendBootstrapDataToMain = () => {
// If no main menu service is registered for the app/window then don't send the event to main process
if (!this.mainMenuService) {
return;
}

this.addDisposable(
this.electronIpcGateway.subscribe(bootstrapChannel, (message, channel) => sendBootstrapDataToMain())
);
try {
this.electronIpcGateway.send(bootstrapChannel, {
menuItems: this.mainMenuService.items.map(i => this.mapToMenuItemDto(i))
});
} catch (err) {
// ignore, Main process event handler might not be setup yet.
}
};

this.addDisposable(this.electronIpcGateway.subscribe(bootstrapChannel, () => sendBootstrapDataToMain()));

// Send right away to take care of any race-condition that might occur.
try {
sendBootstrapDataToMain();
} catch (err) {
// ignore, Main process event handler might not be setup yet.
}
sendBootstrapDataToMain();

return Promise.resolve();
}
Expand All @@ -67,7 +72,7 @@ export class ElectronEventHandlerBackgroundService extends WithDisposables imple
}

private mapToShortcutDto(shortcut: Shortcut) {
return {
return {
name: shortcut.name,
isEnabled: shortcut.isEnabled,
keyCombo: shortcut.keyCombo.asArray
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,24 @@ export class MainMenuManager {
const mainMenuBootstrapChannelName = "main-menu-bootstrap";

const sendBootstrapEvent = () => {
const allBrowsers = BrowserWindow.getAllWindows();
let allBrowsers: Electron.BrowserWindow[];

if (allBrowsers.length < 1) {
console.warn(`There are no active browser windows to send '${mainMenuBootstrapChannelName}'`);
return;
try {
allBrowsers = BrowserWindow.getAllWindows();

if (allBrowsers.length < 1) {
console.warn(`There are no active browser windows to send '${mainMenuBootstrapChannelName}'`);
return;
}
} catch (err) {
console.error("Unexpected error evaluating browser windows: ", err);
}

allBrowsers[0].webContents.send(mainMenuBootstrapChannelName);
try {
allBrowsers[0].webContents.send(mainMenuBootstrapChannelName);
} catch (err) {
// ignore, Renderer process event handler might not be setup yet.
}
};

ipcMain.handle(mainMenuBootstrapChannelName, (event, ...args) => {
Expand All @@ -37,11 +47,7 @@ export class MainMenuManager {
ipcMain.handle("AppActivatedEvent", (event, ...args) => sendBootstrapEvent());

// Send right away to take care of any race-condition that might occur.
try {
sendBootstrapEvent();
} catch (err) {
// ignore, Renderer process event handler might not be setup yet.
}
sendBootstrapEvent();
}

private static rebuildMenu() {
Expand Down

0 comments on commit 106a936

Please sign in to comment.