electron-captureview
Advanced tools
Comparing version 1.0.3 to 1.0.4
@@ -7,275 +7,47 @@ "use strict"; | ||
var _electron = require("electron"); | ||
var _events = require("./events"); | ||
let captureWins = []; | ||
let captureWinIds = []; | ||
let displays = []; | ||
let displayHash = {}; | ||
let command = 'screencapture -x '; | ||
let deletecommand = 'rm -rf '; | ||
const path = require('os').homedir() + `/Desktop/`; | ||
const captureURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080` : `file://${require('path').join(__dirname, '../renderer/index.html')}`; | ||
var _events2 = _interopRequireDefault(_events); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const platform = require('os').platform(); | ||
let hotKey = 'shift+option+a'; | ||
const { | ||
exec | ||
} = require('child_process'); | ||
const fs = require('fs'); | ||
let state = fs.existsSync(require('os').homedir() + `/Desktop`); | ||
let captureView = {}; | ||
exports.default = { | ||
startScreenshot, | ||
useCapture, | ||
updateShortCutKey | ||
}; | ||
function useCapture(options) { | ||
captureView = options; | ||
hotKey = captureView.globalShortCut ? captureView.globalShortCut : hotKey; | ||
_electron.globalShortcut.register(hotKey, () => { | ||
if (typeof captureView.onShowByShortCut === 'function') { | ||
captureView.onShowByShortCut(); | ||
} | ||
startScreenshot(); | ||
}); | ||
createCaptureWins(); | ||
initListern(); | ||
return captureWins; | ||
} | ||
function updateShortCutKey(newHotKey) { | ||
_electron.globalShortcut.unregister(hotKey); | ||
hotKey = newHotKey; | ||
_electron.globalShortcut.register(hotKey, () => { | ||
startScreenshot(); | ||
}); | ||
} | ||
function createCaptureBrowserWindow(display) { | ||
let captureWin = new _electron.BrowserWindow({ | ||
width: display.bounds.width, | ||
height: display.bounds.height, | ||
x: display.bounds.x, | ||
y: display.bounds.y, | ||
webPreferences: { | ||
nodeIntegration: true, | ||
webviewTag: true, | ||
webSecurity: process.env.NODE_ENV === 'production' | ||
}, | ||
fullscreen: platform === 'win32' || undefined, | ||
resizable: false, | ||
enableLargerThanScreen: true, | ||
skipTaskbar: true, | ||
show: false, | ||
movable: false, | ||
frame: false, | ||
transparent: true, | ||
focusable: true | ||
}); | ||
captureWin.setAlwaysOnTop(true, 'screen-saver'); | ||
captureWin.setVisibleOnAllWorkspaces(true); | ||
captureWin.setFullScreenable(true); | ||
captureWin.hide(); | ||
captureWin.loadURL(captureURL); | ||
captureWin.on('ready-to-show', () => { | ||
captureWin.hide(); | ||
}); | ||
captureWin.on('show', () => { | ||
_electron.globalShortcut.register('Esc', () => { | ||
reset(); | ||
}); | ||
}); | ||
captureWin.on('closed', () => { | ||
_electron.globalShortcut.unregister('Esc'); | ||
captureWin = null; | ||
}); | ||
return captureWin; | ||
} | ||
function setScreenInfo(displays) { | ||
if (!state) { | ||
fs.mkdirSync(require('os').homedir() + `/Desktop`); | ||
class captureView { | ||
constructor(options) { | ||
this.multiScreen = !!options ? !!options.multiScreen : false; | ||
this.globalShortCut = !!options ? options.globalShortCut || 'shift+option+A' : 'shift+option+A'; | ||
this.devTools = !!options ? !!options.devTools : false; | ||
this.fileprefix = !!options ? options.fileprefix || 'screen_shot' : 'screen_shot'; | ||
this.onClose = !!options ? options.onClose || null : null; | ||
this.onShow = !!options ? options.onShow || null : null; | ||
this.onShowByShortCut = !!options ? options.onShowByShortCut || null : null; | ||
this.Mosaic = !!options ? !!options.Mosaic : false; | ||
this.Text = !!options ? !!options.Text : false; | ||
this.useCapture(); | ||
} | ||
displays.forEach(display => { | ||
displayHash[display.id] = display.id + '_' + new Date().getTime(); | ||
command = command + path + displayHash[display.id] + '.png '; | ||
deletecommand = deletecommand + path + displayHash[display.id] + '.png '; | ||
}); | ||
console.log('setScreenInfo', command); | ||
} | ||
function createCaptureWins() { | ||
if (captureWins.length) { | ||
console.log('截图窗口已存在,不重新创建'); | ||
return; | ||
useCapture() { | ||
global.captureView = this; | ||
this.captureWins = _events2.default.useCapture(this); | ||
} | ||
if (platform === 'darwin') { | ||
const { | ||
screen | ||
} = require('electron'); | ||
displays = screen.getAllDisplays(); | ||
setScreenInfo(displays); | ||
captureWins = displays.map(display => { | ||
return createCaptureBrowserWindow(display); | ||
}); | ||
console.log('MACOS 截图窗口初始化成功', displays.length); | ||
} else { | ||
const { | ||
screen | ||
} = require('electron'); | ||
const cursor = screen.getCursorScreenPoint(); | ||
const display = screen.getDisplayNearestPoint(cursor); | ||
captureWins[0] = createCaptureBrowserWindow(display); | ||
console.log('linux 截图窗口初始化成功', display); | ||
open() { | ||
_events2.default.startScreenshot(); | ||
} | ||
if (captureView.devTools) { | ||
captureWins.forEach(d => { | ||
d.webContents.openDevTools(); | ||
}); | ||
} else { | ||
captureWins.forEach(d => { | ||
d.webContents.closeDevTools(); | ||
}); | ||
close() { | ||
_events2.default.reset(); | ||
} | ||
return captureWins; | ||
} | ||
function initListern() { | ||
const { | ||
screen | ||
} = require('electron'); | ||
screen.on('display-added', () => { | ||
console.log('display-added'); | ||
reset(); | ||
}); | ||
screen.on('display-removed', () => { | ||
console.log('display-removed'); | ||
reset(); | ||
}); | ||
_electron.ipcMain.on('SCREENSHOT::CLOSE', () => { | ||
reset(); | ||
}); | ||
_electron.ipcMain.on('SCREENSHOT::HIDE', () => { | ||
if (captureWins) { | ||
captureWins.forEach(win => win.hide()); | ||
} | ||
}); | ||
_electron.ipcMain.on('SCREENSHOT::CREATE', () => { | ||
createCaptureWins(); | ||
}); | ||
_electron.ipcMain.on('SCREENSHOT::START', () => { | ||
console.log('IpcMain...... SCREENSHOT::START', captureWins.length); | ||
startScreenshot(); | ||
}); | ||
_electron.globalShortcut.register(hotKey, () => { | ||
startScreenshot(); | ||
}); | ||
} | ||
function reset() { | ||
if (typeof captureView.onClose === 'function') { | ||
captureView.onClose(); | ||
updateShortCutKey(newHotKey) { | ||
_events2.default.updateShortCutKey(newHotKey); | ||
} | ||
if (captureWins) { | ||
captureWins.forEach(win => { | ||
win.close(); | ||
win = null; | ||
}); | ||
displayHash = {}; | ||
captureWins = []; | ||
displays = []; | ||
captureWinIds = []; | ||
setMultiScreen(option) { | ||
this.multiScreen = option; | ||
} | ||
if (platform === 'darwin') { | ||
exec(deletecommand, (error, stdout, stderr) => { | ||
if (error) throw error; | ||
deletecommand = 'rm -rf '; | ||
command = 'screencapture -x '; | ||
state = fs.existsSync(require('os').homedir() + `/Desktop`); | ||
createCaptureWins(); | ||
}); | ||
} else { | ||
createCaptureWins(); | ||
} | ||
} | ||
function startScreenshot() { | ||
if (typeof captureView.onShow === 'function') { | ||
console.log(captureView.onShow.toString()); | ||
captureView.onShow(); | ||
} | ||
if (platform === 'darwin') { | ||
startMacScreenshot(); | ||
} else { | ||
startLinuxScreenshot(); | ||
} | ||
} | ||
function startMacScreenshot() { | ||
state = fs.existsSync(require('os').homedir() + `/Desktop`); | ||
if (!state) { | ||
fs.mkdirSync(require('os').homedir() + `/Desktop`); | ||
} | ||
exec(command, (error, stdout, stderr) => { | ||
if (error) throw error; | ||
if (captureView.multiScreen) { | ||
captureWins.forEach(captureWin => { | ||
const _win = displays.filter(d => d.bounds.x === captureWin.getBounds().x && d.bounds.y === captureWin.getBounds().y)[0]; | ||
captureWin.setSize(_win.bounds.width, _win.bounds.height); | ||
captureWin.webContents.send('SCREENSHOT::OPEN_MAC', _win.bounds.width, _win.bounds.height, _win.scaleFactor, displayHash[_win.id]); | ||
}); | ||
} else { | ||
const { | ||
screen | ||
} = require('electron'); | ||
const cursor = screen.getCursorScreenPoint(); | ||
const display = screen.getDisplayNearestPoint(cursor); | ||
const _win = captureWins.filter(d => d.getBounds().x === display.bounds.x && d.getBounds().y === display.bounds.y)[0]; | ||
_win.webContents.send('SCREENSHOT::OPEN_MAC', display.bounds.width, display.bounds.height, display.scaleFactor, displayHash[display.id], { | ||
mosaic: captureView.Mosaic, | ||
text: captureView.Text, | ||
fileprefix: captureView.fileprefix | ||
}); | ||
} | ||
}); | ||
} | ||
function startLinuxScreenshot() { | ||
const captureWin = captureWins[0]; | ||
captureWin.webContents.send('SCREENSHOT::OPEN_Linux', { | ||
mosaic: captureView.Mosaic, | ||
text: captureView.Text, | ||
fileprefix: captureView.fileprefix | ||
}); | ||
} | ||
exports.default = captureView; |
{ | ||
"name": "electron-captureview", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "'a electron screen capture'", | ||
@@ -5,0 +5,0 @@ "main": "./main/captureview.js", |
6
6
210181
687