Skip to content
Open
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
44 changes: 44 additions & 0 deletions src/main/lifecycle/startup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ vi.mock('../../shared/logger', () => ({
logWarn: (...a: unknown[]) => logWarnMock(...a),
}));

const isLinuxMock = vi.fn(() => false);
vi.mock('../../shared/platform', () => ({
isLinux: () => isLinuxMock(),
}));

function createMb() {
return {
on: vi.fn(),
Expand All @@ -38,6 +43,7 @@ function createMb() {
setIgnoreDoubleClickEvents: vi.fn(),
on: vi.fn(),
popUpContextMenu: vi.fn(),
setContextMenu: vi.fn(),
},
};
}
Expand All @@ -63,6 +69,44 @@ describe('main/lifecycle/startup.ts', () => {
expect(appQuitMock).toHaveBeenCalled();
expect(logWarnMock).toHaveBeenCalled();
});

it('uses setContextMenu on Linux', () => {
isLinuxMock.mockReturnValueOnce(true);
const mb = createMb();
const contextMenu = {} as Electron.Menu;

initializeAppLifecycle(mb as unknown as Menubar, contextMenu, 'gitify');

const readyHandler = (mb.on as unknown as ReturnType<typeof vi.fn>).mock
.calls[0]?.[1];
expect(readyHandler).toBeDefined();
readyHandler?.();

expect(mb.tray.setContextMenu).toHaveBeenCalledWith(contextMenu);
expect(mb.tray.on).not.toHaveBeenCalledWith(
'right-click',
expect.any(Function),
);
});

it('uses popUpContextMenu on non-Linux platforms', () => {
isLinuxMock.mockReturnValueOnce(false);
const mb = createMb();
const contextMenu = {} as Electron.Menu;

initializeAppLifecycle(mb as unknown as Menubar, contextMenu, 'gitify');

const readyHandler = (mb.on as unknown as ReturnType<typeof vi.fn>).mock
.calls[0]?.[1];
expect(readyHandler).toBeDefined();
readyHandler?.();

expect(mb.tray.setContextMenu).not.toHaveBeenCalled();
expect(mb.tray.on).toHaveBeenCalledWith(
'right-click',
expect.any(Function),
);
});
});

describe('handleProtocolURL', () => {
Expand Down
17 changes: 14 additions & 3 deletions src/main/lifecycle/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Menubar } from 'menubar';
import { APPLICATION } from '../../shared/constants';
import { EVENTS } from '../../shared/events';
import { logInfo, logWarn } from '../../shared/logger';
import { isLinux } from '../../shared/platform';

import { sendRendererEvent } from '../events';

Expand All @@ -27,9 +28,19 @@ export function initializeAppLifecycle(

mb.tray.setIgnoreDoubleClickEvents(true);

mb.tray.on('right-click', (_event, bounds) => {
mb.tray.popUpContextMenu(contextMenu, { x: bounds.x, y: bounds.y });
});
if (isLinux()) {
// Linux trays go through libappindicator / StatusNotifierItem (D-Bus),
// where Electron only emits 'right-click' if no context menu is set.
// setContextMenu hands the menu to the host indicator so it renders
// natively on right-click. Don't use this on macOS — there
// setContextMenu intercepts left-click too and would break the
// menubar window toggle.
mb.tray.setContextMenu(contextMenu);
} else {
mb.tray.on('right-click', (_event, bounds) => {
mb.tray.popUpContextMenu(contextMenu, { x: bounds.x, y: bounds.y });
});
}
});

preventSecondInstance(mb, protocol);
Expand Down
72 changes: 72 additions & 0 deletions src/main/menu.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,78 @@ describe('main/menu.ts', () => {
expect(menubar.app.quit).toHaveBeenCalled();
});

it('toggle menu item shows the window when hidden', () => {
const showWindow = vi.fn();
const hideWindow = vi.fn();
const mb = {
app: { quit: vi.fn() },
window: { isVisible: () => false },
showWindow,
hideWindow,
} as unknown as Menubar;
const builder = new MenuBuilder(mb);
builder.buildMenu();
const template = (Menu.buildFromTemplate as Mock).mock.calls.slice(
-1,
)[0][0] as TemplateItem[];

const item = template.find(
(i) => i.label === `Toggle ${APPLICATION.NAME}`,
);
item?.click?.();

expect(showWindow).toHaveBeenCalled();
expect(hideWindow).not.toHaveBeenCalled();
});

it('toggle menu item hides the window when visible', () => {
const showWindow = vi.fn();
const hideWindow = vi.fn();
const mb = {
app: { quit: vi.fn() },
window: { isVisible: () => true },
showWindow,
hideWindow,
} as unknown as Menubar;
const builder = new MenuBuilder(mb);
builder.buildMenu();
const template = (Menu.buildFromTemplate as Mock).mock.calls.slice(
-1,
)[0][0] as TemplateItem[];

const item = template.find(
(i) => i.label === `Toggle ${APPLICATION.NAME}`,
);
item?.click?.();

expect(hideWindow).toHaveBeenCalled();
expect(showWindow).not.toHaveBeenCalled();
});

it('toggle menu item shows the window when no window exists yet', () => {
const showWindow = vi.fn();
const hideWindow = vi.fn();
const mb = {
app: { quit: vi.fn() },
window: undefined,
showWindow,
hideWindow,
} as unknown as Menubar;
const builder = new MenuBuilder(mb);
builder.buildMenu();
const template = (Menu.buildFromTemplate as Mock).mock.calls.slice(
-1,
)[0][0] as TemplateItem[];

const item = template.find(
(i) => i.label === `Toggle ${APPLICATION.NAME}`,
);
item?.click?.();

expect(showWindow).toHaveBeenCalled();
expect(hideWindow).not.toHaveBeenCalled();
});

it('developer submenu includes expected static accelerators', () => {
const template = buildAndGetTemplate();
const devEntry = template.find(
Expand Down
11 changes: 11 additions & 0 deletions src/main/menu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ export default class MenuBuilder {
*/
buildMenu(): Menu {
const contextMenu = Menu.buildFromTemplate([
{
label: `Toggle ${APPLICATION.NAME}`,
click: () => {
if (this.menubar.window?.isVisible()) {
this.menubar.hideWindow();
} else {
this.menubar.showWindow();
}
},
},
{ type: 'separator' },
this.checkForUpdatesMenuItem,
this.noUpdateAvailableMenuItem,
this.updateAvailableMenuItem,
Expand Down
Loading