diff --git a/package.json b/package.json index 2a9d974..cd5a226 100644 --- a/package.json +++ b/package.json @@ -2,12 +2,12 @@ "devDependencies": { "electron": "^32.2.0", "electron-builder": "^25.1.8", - "electron-webpack": "^2.8.2", "electron-builder-squirrel-windows": "^25.1.8", + "electron-webpack": "^2.8.2", "webpack": "^4.47.0" }, "name": "Deepl-Linux-Electron", - "version": "1.5.0", + "version": "1.6.0", "scripts": { "dev": "electron-webpack dev", "compile": "electron-webpack", @@ -21,6 +21,7 @@ }, "license": "MIT", "dependencies": { + "@electron/remote": "^2.1.2", "electron-store": "^8.2.0", "source-map-support": "^0.5.19" } diff --git a/src/main/index.js b/src/main/index.js index aa0f3a5..a126e1c 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -26,6 +26,7 @@ const { Tray, clipboard } = require('electron'); +require('@electron/remote/main').initialize(); var win = null; var appQuitting = false; const appName = 'Deepl-Linux-Electron'; @@ -39,6 +40,7 @@ app.setAboutPanelOptions({ app.on('ready', function() { let Menu = require('electron').Menu; + //console.log(app.getPath('userData')); isRemoveLineBreaks = store.get('remove_line_breaks'); isHiddenOnStartup = store.get('hidden_on_startup'); windowWidth = store.get('window_width'); @@ -54,10 +56,12 @@ app.on('ready', function() { width: 280, webPreferences: { nodeIntegration: true, - enableRemoteModule: true // https://github.com/electron/electron/issues/16558#issuecomment-703143446 + enableRemoteModule: true, + preload: path.join(__static, 'hotkey.js') } }) - // hotkeySettingsWindow.webContents.openDevTools(); + // hotkeySettingsWindow.webContents.openDevTools(); + require('@electron/remote/main').enable(hotkeySettingsWindow.webContents) hotkeySettingsWindow.loadFile(path.join(__static, 'hotkey.html')) } @@ -70,9 +74,11 @@ app.on('ready', function() { width: 200, webPreferences: { nodeIntegration: true, - enableRemoteModule: true + enableRemoteModule: true, + preload: path.join(__static, 'window-size.js') } }) + require('@electron/remote/main').enable(settingsWindowSize.webContents) settingsWindowSize.loadFile(path.join(__static, 'window-size.html')) } }, { @@ -143,9 +149,11 @@ app.on('ready', function() { title: "Deepl-Linux-Electron", width: 800, height: 600, - //webPreferences: { - // preload: path.join(__static, 'preload.js') - //}, + webPreferences: { + nodeIntegration: true, + enableRemoteModule: true + //preload: path.join(__static, 'preload.js') + }, show: !isHiddenOnStartup }); diff --git a/static/hotkey.html b/static/hotkey.html index e5f23ce..172fec4 100644 --- a/static/hotkey.html +++ b/static/hotkey.html @@ -7,57 +7,9 @@
- - + +
- diff --git a/static/hotkey.js b/static/hotkey.js new file mode 100644 index 0000000..683ed14 --- /dev/null +++ b/static/hotkey.js @@ -0,0 +1,55 @@ +const { + ipcRenderer +} = require('electron') +const remote = require('@electron/remote') + +window.addEventListener('load', (event) => { + ipcRenderer.on('set-hotkey-reply', (event, isOk, arg) => { + if (isOk) { + window.localStorage.setItem('shortKey', arg); + console.log('=============== saved :', arg); + } + var w = remote.getCurrentWindow(); + console.log("wwww:", w); + w.close(); + }); + + loadHotkey(); +}); + +async function loadHotkey() { + let shortKey = window.localStorage.shortKey; + console.log("========load hotkey:", shortKey); + if (!shortKey) shortKey = 'Control+Alt+D'; + document.getElementById("hotkey").value = shortKey; +} + +function saveHotkey() { + const hotkey = document.getElementById("hotkey").value; + ipcRenderer.send('set-hotkey', hotkey); + //console.log(hotkey, "key pressed"); +} + +function keyUp(event) { + const keyCode = event.keyCode; + const key = event.key; + const charCode = event.code; + + if ((keyCode >= 16 && keyCode <= 18) || keyCode === 91) return; + + const value = []; + event.ctrlKey ? value.push("Control") : null; + event.shiftKey ? value.push("Shift") : null; + event.isAlt ? value.push("Alt") : null; + value.push(key.toUpperCase()); + + document.getElementById("hotkey").value = value.join("+"); +} + +document.addEventListener('DOMContentLoaded', function() { + const hotkeyInput = document.getElementById('hotkey'); + hotkeyInput.addEventListener('keyup', keyUp); + + const hotkeySave = document.getElementById('save'); + hotkeySave.addEventListener('click', saveHotkey); +}); diff --git a/static/window-size.html b/static/window-size.html index 7c20bd8..ae8e152 100644 --- a/static/window-size.html +++ b/static/window-size.html @@ -15,58 +15,7 @@

- + - diff --git a/static/window-size.js b/static/window-size.js new file mode 100644 index 0000000..76c4966 --- /dev/null +++ b/static/window-size.js @@ -0,0 +1,55 @@ +const { + ipcRenderer +} = require('electron'); +const remote = require('@electron/remote'); + +window.addEventListener('load', (event) => { + ipcRenderer.on('set-window-size-reply', (event, isOk, argWidth, argHeight) => { + if (isOk) { + window.localStorage.setItem('window_width', argWidth); + window.localStorage.setItem('window_height', argHeight); + console.log('=============== saved :', argWidth, 'x', 'argHeight'); + } + var w = remote.getCurrentWindow(); + w.close(); + window.setSize(argWidth, argHeight, true) + }); + + loadWindowsSize(); +}); + +async function loadWindowsSize() { + let windowWidth = window.localStorage.window_width; + let windowHeight = window.localStorage.window_height; + document.getElementById("windowWidth").value = windowWidth; + document.getElementById("windowHeight").value = windowHeight; + console.log("========load window size:", windowWidth, 'x', windowHeight); +} + +function saveWindowsSize() { + console.log('=============== start :'); + const windowWidth = document.getElementById("windowWidth").value; + const windowHeight = document.getElementById("windowHeight").value; + ipcRenderer.send('set-window-size', windowWidth, windowHeight); +} + +function validateMax() { + if (this.max) this.value = Math.min(parseInt(this.max), parseInt(this.value) || 0); +} + +function validateMin() { + if (this.min) this.value = Math.max(parseInt(this.min), parseInt(this.value) || 0); +} + +document.addEventListener('DOMContentLoaded', function() { + const wsSave = document.getElementById('save'); + wsSave.addEventListener('click', saveWindowsSize); + + const inputWindowWidth = document.getElementById('windowWidth') + inputWindowWidth.addEventListener('change', validateMax); + inputWindowWidth.addEventListener('change', validateMin); + + const inputWindowHeight = document.getElementById('windowHeight') + inputWindowHeight.addEventListener('change', validateMax); + inputWindowHeight.addEventListener('change', validateMin); +});