chrisbing / electorn-capture-screen

electron capture screen
MIT License
225 stars 65 forks source link

Always show getAllDisplays of undefined in utils.js #18

Open xj124456 opened 4 years ago

xj124456 commented 4 years ago

use electron-vue electron 7.1.7

I see the screen module in the Renderer process, is this reason? and hot to fix it

gary-dgc commented 4 years ago

Here is a workaround for this issue

in main process, listen following message

  ipcMain.on('capture-message', (event, arg) => {
    const {api, ...rest} = arg
    let rtv = null
    switch (api) {
      case 'getCurrentScreen':
        const {x, y} = rest
        const display = screen.getAllDisplays().filter(d => d.bounds.x === x && d.bounds.y === y)[0]
        rtv = display
        break
      case 'isCursorInCurrentWindow':
        const {
          x: winX, y: winY, width, height
        } = rest
        const { x: ptX, y: ptY } = screen.getCursorScreenPoint()
        rtv = ptX >= winX && ptX <= winX + width && ptY >= winY && ptY <= winY + height
        break
      default:
        break
    }
    event.returnValue = rtv
  })

in render process utils.js

import { ipcRenderer, remote } from 'electron'

let currentWindow = remote.getCurrentWindow()

export const getCurrentScreen = () => {
  let { x, y } = currentWindow.getBounds()
  const rtv = ipcRenderer.sendSync('capture-message', {api: 'getCurrentScreen', x, y})
  return rtv
}

export const isCursorInCurrentWindow = () => {
  let bound = currentWindow.getBounds()
  const rtv = ipcRenderer.sendSync('capture-message', {api: 'isCursorInCurrentWindow', ...bound})
  return rtv
}
lyh4556 commented 4 years ago

试验了下,这个错误没了,但是点击截图按钮没效果了,不知道是不是我写的有问题

lyh4556 commented 4 years ago

const useCapture = () => { globalShortcut.register('Esc', () => { if (captureWins) { captureWins.forEach(win => win.close()) captureWins = [] } })

globalShortcut.register('CmdOrCtrl+Shift+A', captureScreen)

ipcMain.on('capture-screen', (e, { type = 'start', screenId } = {}) => {
    if (type === 'start') {
        captureScreen()
    } else if (type === 'complete') {
        // nothing
    } else if (type === 'select') {
        captureWins.forEach(win => win.webContents.send('capture-screen', { type: 'select', screenId }))
    }
})

ipcMain.on('capture-message', (event, arg) => {
    console.log(11111)
    const { api, ...rest } = arg
    let rtv = null
    switch (api) {
        case 'getCurrentScreen':
            const { x, y } = rest
            const display = screen.getAllDisplays().filter(d => d.bounds.x === x && d.bounds.y === y)[0]
            rtv = display
            break
        case 'isCursorInCurrentWindow':
            const {
                x: winX, y: winY, width, height,
            } = rest
            const { x: ptX, y: ptY } = screen.getCursorScreenPoint()
            rtv = ptX >= winX && ptX <= winX + width && ptY >= winY && ptY <= winY + height
            break
        default:
            break
    }
    event.returnValue = rtv
})

}

gary-dgc commented 4 years ago

you miss the modification in utils.js, it's just a simple sync-communication between main and render processes

import { ipcRenderer, remote } from 'electron'
let currentWindow = remote.getCurrentWindow()

export const getCurrentScreen = () => {
  let { x, y } = currentWindow.getBounds()
  const rtv = ipcRenderer.sendSync('capture-message', {api: 'getCurrentScreen', x, y})
  return rtv
}

export const isCursorInCurrentWindow = () => {
  let bound = currentWindow.getBounds()
  const rtv = ipcRenderer.sendSync('capture-message', {api: 'isCursorInCurrentWindow', ...bound})
  return rtv
}