Skip to content

Commit

Permalink
feat(developer): take screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed Jul 23, 2024
1 parent ab2f7f7 commit c4b43c9
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/electron/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const { autoUpdater } = require('electron-updater');
const { onFirstRunMaybe } = require('./first-run');
const path = require('node:path');
const log = require('electron-log');
const fs = require('node:fs');
const os = require('node:os');

log.initialize();
autoUpdater.logger = log;
Expand Down Expand Up @@ -50,6 +52,11 @@ const contextMenu = Menu.buildFromTemplate([
accelerator:
process.platform === 'darwin' ? 'Alt+Cmd+I' : 'Ctrl+Shift+I',
},
{
label: 'Take Screenshot',
accelerator: 'CommandOrControl+S',
click: () => takeScreenshot(),
},
],
},
{ type: 'separator' },
Expand All @@ -62,17 +69,17 @@ const contextMenu = Menu.buildFromTemplate([
},
]);

const mb = menubar({
icon: idleIcon,
index: `file://${__dirname}/index.html`,
browserWindow: browserWindowOpts,
preloadWindow: true,
showDockIcon: false,
});

app.whenReady().then(async () => {
await onFirstRunMaybe();

const mb = menubar({
icon: idleIcon,
index: `file://${__dirname}/index.html`,
browserWindow: browserWindowOpts,
preloadWindow: true,
showDockIcon: false,
});

mb.on('ready', () => {
autoUpdater.checkForUpdatesAndNotify();

Expand Down Expand Up @@ -168,3 +175,15 @@ app.whenReady().then(async () => {
app.setLoginItemSettings(settings);
});
});

function takeScreenshot() {
const date = new Date();
const dateStr = date.toISOString().replace(/:/g, '-');

const capturedPicFilePath = `${os.homedir()}/${dateStr}-gitify-screenshot.png`;
mb.window.capturePage().then((img) => {
fs.writeFile(capturedPicFilePath, img.toPNG(), () =>
log.info(`Screenshot saved ${capturedPicFilePath}`),
);
});
}

0 comments on commit c4b43c9

Please sign in to comment.