Skip to content

fix: ensure screen capture uses primary display source (close: #116) #117

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

Merged
merged 3 commits into from
Feb 19, 2025
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
76 changes: 76 additions & 0 deletions src/main/agent/device.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest';
import { screen, desktopCapturer } from 'electron';
import { Desktop } from './device';

// Mock dependencies
vi.mock('electron', () => ({
screen: {
getPrimaryDisplay: vi.fn(),
},
desktopCapturer: {
getSources: vi.fn(),
},
app: {
on: vi.fn(),
off: vi.fn(),
quit: vi.fn(),
exit: vi.fn(),
relaunch: vi.fn(),
},
}));

vi.mock('./execute', () => ({
execute: vi.fn(),
}));

vi.mock('@main/env', () => ({
isMacOS: false,
}));

describe('Desktop', () => {
let desktop: Desktop;

beforeEach(() => {
desktop = new Desktop();
vi.clearAllMocks();
});

afterEach(() => {
vi.resetAllMocks();
});

describe('screenshot', () => {
it('should capture screenshot successfully', async () => {
const mockDisplay = {
id: '1',
size: { width: 1920, height: 1080 },
};
const mockSource = {
display_id: '1',
thumbnail: {
toPNG: () => Buffer.from('mock-image'),
},
};

vi.mocked(screen.getPrimaryDisplay).mockReturnValue(mockDisplay as any);
vi.mocked(desktopCapturer.getSources).mockResolvedValueOnce([
mockSource as any,
]);

const result = await desktop.screenshot();

expect(result).toEqual({
base64: 'bW9jay1pbWFnZQ==',
width: 1920,
height: 1080,
});
expect(desktopCapturer.getSources).toHaveBeenCalledWith({
types: ['screen'],
thumbnailSize: {
width: 1920,
height: 1080,
},
});
});
});
});
6 changes: 4 additions & 2 deletions src/main/agent/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@ export class Desktop {
height: Math.round(height),
},
});
const primarySource = sources[0];
const screenshot = primarySource.thumbnail;
const primarySource = sources.find(
(source) => source.display_id === primaryDisplay.id.toString(),
);
const screenshot = primarySource!.thumbnail;

return {
base64: screenshot.toPNG().toString('base64'),
Expand Down
9 changes: 7 additions & 2 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
ipcMain,
session,
WebContentsView,
screen,
} from 'electron';
import squirrelStartup from 'electron-squirrel-startup';
import ElectronStore from 'electron-store';
Expand Down Expand Up @@ -131,8 +132,12 @@
session.defaultSession.setDisplayMediaRequestHandler(
(_request, callback) => {
desktopCapturer.getSources({ types: ['screen'] }).then((sources) => {
// Grant access to the first screen found.
callback({ video: sources[0], audio: 'loopback' });
const primaryDisplay = screen.getPrimaryDisplay();
const primarySource = sources.find(
(source) => source.display_id === primaryDisplay.id.toString(),

Check warning on line 137 in src/main/main.ts

View check run for this annotation

Codecov / codecov/patch

src/main/main.ts#L135-L137

Added lines #L135 - L137 were not covered by tests
);

callback({ video: primarySource!, audio: 'loopback' });

Check warning on line 140 in src/main/main.ts

View check run for this annotation

Codecov / codecov/patch

src/main/main.ts#L140

Added line #L140 was not covered by tests
});
},
{ useSystemPicker: false },
Expand Down
Loading