Skip to content

Commit

Permalink
feat: screenshot capture logic rework
Browse files Browse the repository at this point in the history
  • Loading branch information
KochiyaOcean committed Aug 11, 2022
1 parent 1be42f8 commit 6981426
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const dbg = require('./lib/debug')
require('./lib/updater')
proxy.setMaxListeners(30)
require('./lib/tray')
require('./lib/screenshot')

// Disable HA
if (config.get('poi.misc.disablehwaccel', false)) {
Expand Down
2 changes: 1 addition & 1 deletion assets/js/webview-preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ window.capture = async function (toClipboard) {
})
.then((dataURL) => {
const ss = window.ipc.access('screenshot')
if (ss && ss.onScreenshotCaptured) ss.onScreenshotCaptured({ dataURL, toClipboard })
if (ss && ss.onScreenshotCaptured) ss.onScreenshotCaptured(dataURL, toClipboard)
return true
})
.catch(() => false)
Expand Down
13 changes: 13 additions & 0 deletions lib/screenshot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ipcMain, Rectangle, ResizeOptions, webContents } from 'electron'

ipcMain.handle(
'screenshot::get',
async (event, id: number, rect: Rectangle, actualSize?: ResizeOptions) => {
const webContent = webContents.fromId(id)
if (webContent) {
const image = await webContent.capturePage(rect)
return (actualSize ? image.resize(actualSize) : image).toDataURL()
}
return undefined
},
)
30 changes: 16 additions & 14 deletions views/components/info/control.es
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs-extra'
import path from 'path-extra'
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { shell, clipboard, nativeImage } from 'electron'
import { shell, clipboard, nativeImage, ipcRenderer } from 'electron'
import * as remote from '@electron/remote'
import { Button, Position } from '@blueprintjs/core'
import { connect } from 'react-redux'
Expand Down Expand Up @@ -98,28 +98,30 @@ export class PoiControl extends Component {
this.handleCapturePageOverWebContent(toClipboard)
}

handleCapturePageOverWebContent = (toClipboard) => {
handleCapturePageOverWebContent = async (toClipboard) => {
const { width, height } = getStore('layout.webview')
const webContentId = getStore('layout.webview.ref.view').getWebContentsId()
const actualSize = { width: Math.round(width), height: Math.round(height) }
const rect = {
x: 0,
y: 0,
width: Math.floor(width * devicePixelRatio),
height: Math.floor(height * devicePixelRatio),
}
remote.webContents
.fromId(getStore('layout.webview.ref.view').getWebContentsId())
.capturePage(rect)
.then((image) => {
this.handleScreenshotCaptured({
dataURL: image
.resize({ width: Math.floor(width), height: Math.floor(height) })
.toDataURL(),
toClipboard,
})
})
try {
const dataURL =
(await ipcRenderer.invoke('screenshot::get', webContentId, rect, actualSize)) ||
(await remote.webContents.fromId(webContentId).capturePage(rect))
.resize(actualSize)
.toDataURL()
this.handleScreenshotCaptured(dataURL, toClipboard)
} catch (err) {
console.error(err)
window.error(this.props.t('Failed to save the screenshot'))
}
}

handleScreenshotCaptured = ({ dataURL, toClipboard }) => {
handleScreenshotCaptured = (dataURL, toClipboard) => {
const screenshotPath = config.get(
'poi.misc.screenshot.path',
remote.getGlobal('DEFAULT_SCREENSHOT_PATH'),
Expand Down

0 comments on commit 6981426

Please sign in to comment.