Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #125: Main menu becoming empty when opening a dialog window #138

Merged
merged 1 commit into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading