From fd681cef5641fbb02d45f043e60fe87333608d17 Mon Sep 17 00:00:00 2001 From: purocean Date: Tue, 27 Oct 2020 17:42:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=20Electron=20=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E4=B8=AD=E5=BC=80=E5=90=AF=E7=BC=A9=E6=94=BE=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/useful/env.ts | 37 +++++++++++++++++++++++++++++++--- frontend/src/useful/startup.ts | 35 ++++++++++++++++++++++++++++++++ src/app.ts | 3 ++- 3 files changed, 71 insertions(+), 4 deletions(-) diff --git a/frontend/src/useful/env.ts b/frontend/src/useful/env.ts index cb56b5c9..b7589d3c 100644 --- a/frontend/src/useful/env.ts +++ b/frontend/src/useful/env.ts @@ -12,10 +12,41 @@ const isElectron = !!(nodeProcess?.versions?.electron) const openAlwaysOnTopWindow = (url: string, target = '_blank') => { if (isElectron) { - const opener: any = window.open(url, target, 'frame=true,alwaysOnTop=true') + const opener: any = window.open(url, target, 'nodeIntegration=yes,frame=true,alwaysOnTop=true,enableRemoteModule=true') - // 让弹出的窗口置顶 - opener.eval("require('electron').remote.getCurrentWindow().setAlwaysOnTop(true)") + const preload = ` + // 在 Electron 环境中开启鼠标滚轮缩放页面 + const webContents = require('electron').remote.getCurrentWebContents() + + const changeZoomFactor = zoomIn => { + const currentZoomFactor = webContents.getZoomFactor() + const factor = currentZoomFactor + (zoomIn ? 0.1 : -0.1) + + if (factor > 0.2 && factor < 3) { + webContents.setZoomFactor(factor) + } + } + + window.addEventListener('keydown', e => { + if (e.ctrlKey) { + if (e.key === '0') { + webContents.setZoomFactor(1) + } else if (e.key === '=') { + changeZoomFactor(true) + } else if (e.key === '-') { + changeZoomFactor(false) + } + } + }) + + window.addEventListener('mousewheel', e => { + if (e.ctrlKey) { + changeZoomFactor(e.deltaY < 0) + } + }) + ` + + opener.eval(preload) return opener } else { diff --git a/frontend/src/useful/startup.ts b/frontend/src/useful/startup.ts index ccfac5d0..f770a683 100644 --- a/frontend/src/useful/startup.ts +++ b/frontend/src/useful/startup.ts @@ -2,6 +2,7 @@ import { useStore } from 'vuex' import { useBus } from './bus' import RunPlugin from '@/plugins/RunPlugin' import { copyText } from './copy-text' +import env from './env' export default function startup () { const bus = useBus() @@ -19,4 +20,38 @@ export default function startup () { }) bus.on('copy-text', copyText) + + // 在 Electron 环境中开启缩放页面功能 + if (env.isElectron) { + const webContents = env.require('electron').remote.getCurrentWebContents() + + const changeZoomFactor = (zoomIn: boolean) => { + const currentZoomFactor = webContents.getZoomFactor() + const factor = currentZoomFactor + (zoomIn ? 0.1 : -0.1) + + if (factor > 0.2 && factor < 3) { + webContents.setZoomFactor(factor) + } + } + + window.addEventListener('keydown', e => { + if (e.ctrlKey) { + if (e.key === '0') { + webContents.setZoomFactor(1) + } else if (e.key === '=') { + changeZoomFactor(true) + } else if (e.key === '-') { + changeZoomFactor(false) + } + } + }) + + window.addEventListener('mousewheel', event => { + const e = event as WheelEvent + + if (e.ctrlKey) { + changeZoomFactor(e.deltaY < 0) + } + }) + } } diff --git a/src/app.ts b/src/app.ts index af8d4375..f2f33ff5 100644 --- a/src/app.ts +++ b/src/app.ts @@ -52,7 +52,8 @@ const createWindow = () => { backgroundColor: '#282a2b', webPreferences: { webSecurity: false, - nodeIntegration: true + nodeIntegration: true, + enableRemoteModule: true, }, })