From 29e17136731a41bf80f57ff79ce69bfa5efd23c6 Mon Sep 17 00:00:00 2001 From: Jelle Glebbeek Date: Fri, 30 Jul 2021 15:02:59 +0200 Subject: [PATCH] feat: add setting to disable global shortcuts (#144) --- main.js | 12 +++--------- modules/persistence/Settings.js | 27 ++++++++++++++++++++++++++- renderer/renderer.html | 14 ++++++++++---- renderer/renderer.js | 2 ++ tests/Settings.test.js | 4 ++-- tests/test-settings.json | 2 +- 6 files changed, 44 insertions(+), 17 deletions(-) diff --git a/main.js b/main.js index 06b2ae78..e58946f1 100644 --- a/main.js +++ b/main.js @@ -1,4 +1,4 @@ -const { app, BrowserWindow, ipcMain, dialog, Menu, shell, clipboard, globalShortcut } = require('electron'); +const { app, BrowserWindow, ipcMain, dialog, Menu, shell, clipboard } = require('electron'); const Environment = require('./modules/Environment'); const path = require('path'); const QueryManager = require("./modules/QueryManager"); @@ -27,6 +27,8 @@ function sendLogToRenderer(log, isErr) { } function startCriticalHandlers(env) { + env.win = win; + win.on('maximize', () => { win.webContents.send("maximized", true) }); @@ -64,14 +66,6 @@ function startCriticalHandlers(env) { if(appStarting) { appStarting = false; - globalShortcut.register('Shift+CommandOrControl+V', async () => { - win.webContents.send("addShortcut", clipboard.readText()); - }); - - globalShortcut.register('Shift+CommandOrControl+D', async () => { - win.webContents.send("downloadShortcut"); - }); - //Restore the videos from last session ipcMain.handle("restoreTaskList", () => { taskList.restore() diff --git a/modules/persistence/Settings.js b/modules/persistence/Settings.js index 613a58dc..e218e3cf 100644 --- a/modules/persistence/Settings.js +++ b/modules/persistence/Settings.js @@ -1,8 +1,9 @@ const os = require("os"); +const { globalShortcut, clipboard } = require('electron'); const fs = require("fs").promises; class Settings { - constructor(paths, env, outputFormat, audioOutputFormat, downloadPath, proxy, rateLimit, autoFillClipboard, spoofUserAgent, validateCertificate, enableEncoding, taskList, nameFormat, nameFormatMode, sizeMode, splitMode, maxConcurrent, updateBinary, updateApplication, cookiePath, statSend, downloadMetadata, downloadThumbnail, keepUnmerged, calculateTotalSize, theme) { + constructor(paths, env, outputFormat, audioOutputFormat, downloadPath, proxy, rateLimit, autoFillClipboard, globalShortcut, spoofUserAgent, validateCertificate, enableEncoding, taskList, nameFormat, nameFormatMode, sizeMode, splitMode, maxConcurrent, updateBinary, updateApplication, cookiePath, statSend, downloadMetadata, downloadThumbnail, keepUnmerged, calculateTotalSize, theme) { this.paths = paths; this.env = env this.outputFormat = outputFormat == null ? "none" : outputFormat; @@ -11,6 +12,7 @@ class Settings { this.proxy = proxy == null ? "" : proxy; this.rateLimit = rateLimit == null ? "" : rateLimit; this.autoFillClipboard = autoFillClipboard == null ? true : autoFillClipboard; + this.globalShortcut = globalShortcut == null ? true : globalShortcut; this.spoofUserAgent = spoofUserAgent == null ? true : spoofUserAgent; this.validateCertificate = validateCertificate == null ? false : validateCertificate; this.enableEncoding = enableEncoding == null ? false : enableEncoding; @@ -29,6 +31,7 @@ class Settings { this.cookiePath = cookiePath; this.statSend = statSend == null ? false : statSend; this.theme = theme == null ? "dark" : theme; + this.setGlobalShortcuts(); } static async loadFromFile(paths, env) { @@ -44,6 +47,7 @@ class Settings { data.proxy, data.rateLimit, data.autoFillClipboard, + data.globalShortcut, data.spoofUserAgent, data.validateCertificate, data.enableEncoding, @@ -78,6 +82,7 @@ class Settings { this.proxy = settings.proxy; this.rateLimit = settings.rateLimit; this.autoFillClipboard = settings.autoFillClipboard; + this.globalShortcut = settings.globalShortcut; this.spoofUserAgent = settings.spoofUserAgent; this.validateCertificate = settings.validateCertificate; this.enableEncoding = settings.enableEncoding; @@ -101,6 +106,7 @@ class Settings { //Prevent installing already downloaded updates on app close. this.env.appUpdater.setUpdateSetting(settings.updateApplication); + this.setGlobalShortcuts(); } serialize() { @@ -111,6 +117,7 @@ class Settings { proxy: this.proxy, rateLimit: this.rateLimit, autoFillClipboard: this.autoFillClipboard, + globalShortcut: this.globalShortcut, spoofUserAgent: this.spoofUserAgent, validateCertificate: this.validateCertificate, enableEncoding: this.enableEncoding, @@ -137,6 +144,24 @@ class Settings { save() { fs.writeFile(this.paths.settings, JSON.stringify(this.serialize()), "utf8").then(() => console.log("Saved settings file.")); } + + setGlobalShortcuts() { + if(globalShortcut == null) return; + if(!this.globalShortcut) { + globalShortcut.unregisterAll(); + } else { + if(!globalShortcut.isRegistered("Shift+CommandOrControl+V")) { + globalShortcut.register('Shift+CommandOrControl+V', async () => { + this.env.win.webContents.send("addShortcut", clipboard.readText()); + }); + } + if(!globalShortcut.isRegistered("Shift+CommandOrControl+D")) { + globalShortcut.register('Shift+CommandOrControl+D', async () => { + this.env.win.webContents.send("downloadShortcut"); + }); + } + } + } } module.exports = Settings; diff --git a/renderer/renderer.html b/renderer/renderer.html index 2cd50019..11969533 100644 --- a/renderer/renderer.html +++ b/renderer/renderer.html @@ -262,10 +262,6 @@

Performance

-
- - -

Appearance

@@ -296,6 +292,16 @@

Network


+

Input

+
+ + +
+
+ + +
+

Output

diff --git a/renderer/renderer.js b/renderer/renderer.js index 00b3a706..31147dd7 100644 --- a/renderer/renderer.js +++ b/renderer/renderer.js @@ -164,6 +164,7 @@ async function init() { updateBinary: $('#updateBinary').prop('checked'), updateApplication: $('#updateApplication').prop('checked'), autoFillClipboard: $('#autoFillClipboard').prop('checked'), + globalShortcut: $('#globalShortcut').prop('checked'), outputFormat: $('#outputFormat').val(), audioOutputFormat: $('#audioOutputFormat').val(), proxy: $('#proxySetting').val(), @@ -211,6 +212,7 @@ async function init() { $('#enableEncoding').prop('checked', settings.enableEncoding); $('#taskList').prop('checked', settings.taskList); $('#autoFillClipboard').prop('checked', settings.autoFillClipboard); + $('#globalShortcut').prop('checked', settings.globalShortcut); $('#ratelimitSetting').val(settings.rateLimit); $('#proxySetting').val(settings.proxy); $('#nameFormatCustom').val(settings.nameFormat).prop("disabled", settings.nameFormatMode === "custom"); diff --git a/tests/Settings.test.js b/tests/Settings.test.js index 15a8bc39..aed4b0b7 100644 --- a/tests/Settings.test.js +++ b/tests/Settings.test.js @@ -2,8 +2,8 @@ const fs = require('fs').promises; const os = require("os"); const Settings = require('../modules/persistence/Settings'); const env = {version: "2.0.0-test1", app: {getPath: jest.fn().mockReturnValue("test/path")}}; -const defaultSettingsInstance = new Settings({settings: "tests/test-settings.json"}, env, "none", "none", "test/path", "", "", true, true, false, false, true, "%(title).200s-(%(height)sp%(fps).0d).%(ext)s", "%(title).200s-(%(height)sp%(fps).0d).%(ext)s", "click", "49", 8, true, true, "C:\\Users\\user\\cookies.txt", false, true, false, false, true, "dark"); -const defaultSettings = "{\"outputFormat\":\"none\",\"audioOutputFormat\":\"none\",\"downloadPath\":\"test/path\",\"proxy\":\"\",\"rateLimit\":\"\",\"autoFillClipboard\":true,\"spoofUserAgent\":true,\"validateCertificate\":false,\"enableEncoding\":false,\"taskList\":true,\"nameFormat\":\"%(title).200s-(%(height)sp%(fps).0d).%(ext)s\",\"nameFormatMode\":\"%(title).200s-(%(height)sp%(fps).0d).%(ext)s\",\"sizeMode\":\"click\",\"splitMode\":\"49\",\"maxConcurrent\":8,\"defaultConcurrent\":8,\"updateBinary\":true,\"updateApplication\":true,\"statSend\":false,\"downloadMetadata\":true,\"downloadThumbnail\":false,\"keepUnmerged\":false,\"calculateTotalSize\":true,\"theme\":\"dark\",\"version\":\"2.0.0-test1\"}" +const defaultSettingsInstance = new Settings({settings: "tests/test-settings.json"}, env, "none", "none", "test/path", "", "", true, true, true, false, false, true, "%(title).200s-(%(height)sp%(fps).0d).%(ext)s", "%(title).200s-(%(height)sp%(fps).0d).%(ext)s", "click", "49", 8, true, true, "C:\\Users\\user\\cookies.txt", false, true, false, false, true, "dark"); +const defaultSettings = "{\"outputFormat\":\"none\",\"audioOutputFormat\":\"none\",\"downloadPath\":\"test/path\",\"proxy\":\"\",\"rateLimit\":\"\",\"autoFillClipboard\":true,\"globalShortcut\":true,\"spoofUserAgent\":true,\"validateCertificate\":false,\"enableEncoding\":false,\"taskList\":true,\"nameFormat\":\"%(title).200s-(%(height)sp%(fps).0d).%(ext)s\",\"nameFormatMode\":\"%(title).200s-(%(height)sp%(fps).0d).%(ext)s\",\"sizeMode\":\"click\",\"splitMode\":\"49\",\"maxConcurrent\":8,\"defaultConcurrent\":8,\"updateBinary\":true,\"updateApplication\":true,\"statSend\":false,\"downloadMetadata\":true,\"downloadThumbnail\":false,\"keepUnmerged\":false,\"calculateTotalSize\":true,\"theme\":\"dark\",\"version\":\"2.0.0-test1\"}" describe('Load settings from file', () => { beforeEach(() => { diff --git a/tests/test-settings.json b/tests/test-settings.json index b324d6cd..a9fdfc91 100644 --- a/tests/test-settings.json +++ b/tests/test-settings.json @@ -1 +1 @@ -{"outputFormat":"none","audioOutputFormat":"none","downloadPath": "test/path","proxy": "","rateLimit": "","autoFillClipboard":true,"spoofUserAgent":true,"validateCertificate": false,"enableEncoding": false,"taskList":true,"nameFormat":"%(title).200s-(%(height)sp%(fps).0d).%(ext)s","nameFormatMode":"%(title).200s-(%(height)sp%(fps).0d).%(ext)s","sizeMode":"click","splitMode":"49","maxConcurrent":8,"defaultConcurrent":8,"updateBinary":true,"updateApplication":true,"cookiePath":"C:\\Users\\user\\cookies.txt","statSend":false,"downloadMetadata":true,"downloadThumbnail":false,"keepUnmerged":false,"calculateTotalSize":true,"theme": "dark","version":"2.0.0-test1"} +{"outputFormat":"none","audioOutputFormat":"none","downloadPath": "test/path","proxy": "","rateLimit": "","autoFillClipboard":true,"globalShortcut":true,"spoofUserAgent":true,"validateCertificate": false,"enableEncoding": false,"taskList":true,"nameFormat":"%(title).200s-(%(height)sp%(fps).0d).%(ext)s","nameFormatMode":"%(title).200s-(%(height)sp%(fps).0d).%(ext)s","sizeMode":"click","splitMode":"49","maxConcurrent":8,"defaultConcurrent":8,"updateBinary":true,"updateApplication":true,"cookiePath":"C:\\Users\\user\\cookies.txt","statSend":false,"downloadMetadata":true,"downloadThumbnail":false,"keepUnmerged":false,"calculateTotalSize":true,"theme": "dark","version":"2.0.0-test1"}