forked from spentak/electron
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: implement clipboard APIs without the remote module (electro…
- Loading branch information
Showing
10 changed files
with
118 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
'use strict' | ||
|
||
if (process.platform === 'linux' && process.type === 'renderer') { | ||
// On Linux we could not access clipboard in renderer process. | ||
const { getRemote } = require('@electron/internal/renderer/remote') | ||
module.exports = getRemote('clipboard') | ||
} else { | ||
const clipboard = process.atomBinding('clipboard') | ||
const clipboard = process.atomBinding('clipboard') | ||
|
||
// Read/write to find pasteboard over IPC since only main process is notified | ||
// of changes | ||
if (process.platform === 'darwin' && process.type === 'renderer') { | ||
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils') | ||
if (process.type === 'renderer') { | ||
const ipcRendererUtils = require('@electron/internal/renderer/ipc-renderer-internal-utils') | ||
const clipboardUtils = require('@electron/internal/common/clipboard-utils') | ||
|
||
clipboard.readFindText = (...args) => ipcRendererUtils.invokeSync('ELECTRON_BROWSER_CLIPBOARD_READ_FIND_TEXT', ...args) | ||
clipboard.writeFindText = (...args) => ipcRendererUtils.invokeSync('ELECTRON_BROWSER_CLIPBOARD_WRITE_FIND_TEXT', ...args) | ||
const makeRemoteMethod = function (method) { | ||
return (...args) => { | ||
args = clipboardUtils.serialize(args) | ||
const result = ipcRendererUtils.invokeSync('ELECTRON_BROWSER_CLIPBOARD', method, ...args) | ||
return clipboardUtils.deserialize(result) | ||
} | ||
} | ||
|
||
module.exports = clipboard | ||
if (process.platform === 'linux') { | ||
// On Linux we could not access clipboard in renderer process. | ||
for (const method of Object.keys(clipboard)) { | ||
clipboard[method] = makeRemoteMethod(method) | ||
} | ||
} else if (process.platform === 'darwin') { | ||
// Read/write to find pasteboard over IPC since only main process is notified of changes | ||
clipboard.readFindText = makeRemoteMethod('readFindText') | ||
clipboard.writeFindText = makeRemoteMethod('writeFindText') | ||
} | ||
} | ||
|
||
module.exports = clipboard |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
'use strict' | ||
|
||
module.exports = process.atomBinding('native_image') | ||
const { nativeImage } = process.atomBinding('native_image') | ||
|
||
module.exports = nativeImage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict' | ||
|
||
const { nativeImage, NativeImage } = process.atomBinding('native_image') | ||
|
||
const objectMap = function (source, mapper) { | ||
const sourceEntries = Object.entries(source) | ||
const targetEntries = sourceEntries.map(([key, val]) => [key, mapper(val)]) | ||
return Object.fromEntries(targetEntries) | ||
} | ||
|
||
const serialize = function (value) { | ||
if (value instanceof NativeImage) { | ||
return { | ||
buffer: value.toBitmap(), | ||
size: value.getSize(), | ||
__ELECTRON_SERIALIZED_NativeImage__: true | ||
} | ||
} else if (Array.isArray(value)) { | ||
return value.map(serialize) | ||
} else if (value instanceof Buffer) { | ||
return value | ||
} else if (value instanceof Object) { | ||
return objectMap(value, serialize) | ||
} else { | ||
return value | ||
} | ||
} | ||
|
||
const deserialize = function (value) { | ||
if (value && value.__ELECTRON_SERIALIZED_NativeImage__) { | ||
return nativeImage.createFromBitmap(value.buffer, value.size) | ||
} else if (Array.isArray(value)) { | ||
return value.map(deserialize) | ||
} else if (value instanceof Buffer) { | ||
return value | ||
} else if (value instanceof Object) { | ||
return objectMap(value, deserialize) | ||
} else { | ||
return value | ||
} | ||
} | ||
|
||
module.exports = { | ||
serialize, | ||
deserialize | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters