Skip to content

Commit

Permalink
feat: add setting to disable global shortcuts (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
jely2002 committed Jul 30, 2021
1 parent 5b98aaf commit 29e1713
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 17 deletions.
12 changes: 3 additions & 9 deletions main.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down Expand Up @@ -27,6 +27,8 @@ function sendLogToRenderer(log, isErr) {
}

function startCriticalHandlers(env) {
env.win = win;

win.on('maximize', () => {
win.webContents.send("maximized", true)
});
Expand Down Expand Up @@ -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()
Expand Down
27 changes: 26 additions & 1 deletion modules/persistence/Settings.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -44,6 +47,7 @@ class Settings {
data.proxy,
data.rateLimit,
data.autoFillClipboard,
data.globalShortcut,
data.spoofUserAgent,
data.validateCertificate,
data.enableEncoding,
Expand Down Expand Up @@ -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;
Expand All @@ -101,6 +106,7 @@ class Settings {

//Prevent installing already downloaded updates on app close.
this.env.appUpdater.setUpdateSetting(settings.updateApplication);
this.setGlobalShortcuts();
}

serialize() {
Expand All @@ -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,
Expand All @@ -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;
14 changes: 10 additions & 4 deletions renderer/renderer.html
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,6 @@ <h3>Performance</h3>
<input type="range" class="form-range w-50 d-block align-middle" min="1" max="32" id="maxConcurrent">
<button type="button" class="btn btn-dark" id="defaultConcurrent">Reset to default</button>
</div>
<div class="mb-1">
<input class="check-input" type="checkbox" value="" id="autoFillClipboard">
<label class="check-label" for="autoFillClipboard">Automatically fill in copied links</label>
</div>
<hr/>
<h3>Appearance</h3>
<div class="mb-4">
Expand Down Expand Up @@ -296,6 +292,16 @@ <h3>Network</h3>
<input id="ratelimitSetting" class="form-control" type="number" value="" placeholder="Rate-limit in KB/s">
</div>
<hr/>
<h3>Input</h3>
<div class="mb-1">
<input class="check-input" type="checkbox" value="" id="autoFillClipboard">
<label class="check-label" for="autoFillClipboard">Automatically fill in copied links</label>
</div>
<div class="mb-1">
<input class="check-input" type="checkbox" value="" id="globalShortcut">
<label class="check-label" for="globalShortcut">Enable global paste & download shortcuts</label>
</div>
<hr/>
<h3>Output</h3>
<div class="mb-3">
<label for="outputFormat" class="form-label">Attempt to use output format:</label>
Expand Down
2 changes: 2 additions & 0 deletions renderer/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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");
Expand Down
4 changes: 2 additions & 2 deletions tests/Settings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion tests/test-settings.json
Original file line number Diff line number Diff line change
@@ -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"}

0 comments on commit 29e1713

Please sign in to comment.