Skip to content

Commit

Permalink
(fix) Fix writing images to clipboard on Linux platforms
Browse files Browse the repository at this point in the history
Electron has a [long standing bug](electron/electron#8151) since v2 that doesn't let write image data from the renderer process on Linux platforms.

Writing data from rendere process works fine on Windows, but the code is moved to main process anyway to be careful.
  • Loading branch information
notlmn committed Dec 27, 2018
1 parent e360636 commit c1bcd9b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "electron-clippy",
"version": "0.1.1",
"version": "0.1.2",
"description": "A simple clipboard manager build on Electron",
"license": "MIT",
"repository": "ramlmn/electron-clippy",
Expand Down
4 changes: 3 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ export const EVENT = {
SETTINGS_UPDATE: 'settings-update', // From main -> renderer
SETTINGS_CHANGE: 'settings-change', // From renderer -> main
SETTINGS_HIDE: 'settings-hide',
SETTINGS_SHOW: 'settings-show'
SETTINGS_SHOW: 'settings-show',

COPY_TO_CLIPBOARD: 'copy-to-clipboard'
};
15 changes: 14 additions & 1 deletion src/main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from 'fs';
import url from 'url';
import path from 'path';
import {promisify} from 'util';
import {app, Menu, BrowserWindow, ipcMain, globalShortcut, Tray} from 'electron';
import {app, Menu, BrowserWindow, ipcMain, globalShortcut, Tray, clipboard, nativeImage} from 'electron';
import AutoLaunch from 'auto-launch';
import {EVENT} from '../constants';
import ClipboardWatcher from './clipboard-watcher';
Expand Down Expand Up @@ -233,3 +233,16 @@ ipcMain.on(EVENT.ITEMS_SAVE, (event, items) => {
persistItems(items);
}
});

// Image data cannot be written from the renderer process on Linux platforms
// So data is sent here instead to be copied to clipboard
// Bug: https://github.com/electron/electron/issues/8151
ipcMain.on(EVENT.COPY_TO_CLIPBOARD, (event, data) => {
if (typeof data === 'string') {
clipboard.write({
image: nativeImage.createFromDataURL(data)
});
} else {
clipboard.write(data);
}
});
8 changes: 4 additions & 4 deletions src/renderer/components/clippy-items/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,16 @@ class ClippyItems extends ClippyElement {

if (selected) {
if (selected.type === 'image') {
const image = nativeImage.createFromDataURL(selected.buffer);

clipboard.write({image});
dispatch(EVENT.COPY_TO_CLIPBOARD, selected.buffer)
} else {
clipboard.write({
dispatch(EVENT.COPY_TO_CLIPBOARD, {
text: selected.text,
html: selected.html,
rtf: selected.rtf
});
}

this.handleNewItem(selected);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/renderer/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ ipcRenderer.on(EVENT.SETTINGS_UPDATE, (event, settings) => dispatch(EVENT.SETTIN
subscribe(EVENT.SETTINGS_CHANGE, settings => ipcRenderer.send(EVENT.SETTINGS_CHANGE, settings));

subscribe(EVENT.APP_HIDE, () => ipcRenderer.send(EVENT.APP_HIDE));
subscribe(EVENT.COPY_TO_CLIPBOARD, data => ipcRenderer.send(EVENT.COPY_TO_CLIPBOARD, data));

window.addEventListener('load', () => ipcRenderer.send(EVENT.APP_INIT));

0 comments on commit c1bcd9b

Please sign in to comment.