TagSpaces is an offline, open source, document manager with tagging support
refactoring electron functionalities, adding event for managing the global shortcuts
Ilian Sapundshiev12d781c9a28d
diff --git a/data/electron/electron.api.js b/data/electron/electron.api.jsindex a47c0e653..1a001311f 100644--- a/data/electron/electron.api.js+++ b/data/electron/electron.api.js@@ -50,7 +50,7 @@ define(function(require, exports, module) {}}- function initMainMenu() {+ function initElectronIntegration() {var Menu = remote.Menu;var template = [{@@ -401,6 +401,9 @@ define(function(require, exports, module) {var audioEvent = new CustomEvent('resume', {'detail': arg});window.dispatchEvent(audioEvent);});++ ipcRenderer.send('global-shortcuts-enabled', TSCORE.Config.getEnableGlobalKeyboardShortcuts());+}// Brings the TagSpaces window on top of the windows@@ -1035,7 +1038,7 @@ define(function(require, exports, module) {}// Platform specific calls- exports.initMainMenu = initMainMenu;+ exports.initElectronIntegration = initElectronIntegration;exports.showMainWindow = showMainWindow;exports.watchDirectory = watchDirectory; // Platform specificdiff --git a/data/electron/main.js b/data/electron/main.jsindex e72f8d8fc..cbcbcc469 100644--- a/data/electron/main.js+++ b/data/electron/main.js@@ -20,6 +20,15 @@ let trayIcon = null;const isOSX = /^darwin/.test(process.platform);+let path = require('path');+let applicationPath = path.dirname(__dirname);+let indexPath = 'file://' + applicationPath + '/index.html';++// Keep a global reference of the window object, if you don't, the window will+// be closed automatically when the JavaScript object is garbage collected.+let mainWindow = null;+let newWindow = null;+//handling start parameter//console.log(JSON.stringify(process.argv));process.argv.forEach((arg, count) => {@@ -36,24 +45,18 @@ process.argv.forEach((arg, count) => {console.log("Opening file: " + arg);startupFilePath = arg;}-});-if (portableMode) {- startupFilePath = undefined;-}--ipcMain.on('quit-application', (event, arg) => {- app.quit();+ if (portableMode) {+ startupFilePath = undefined;+ }});-let path = require('path');-let applicationPath = path.dirname(__dirname);-let indexPath = 'file://' + applicationPath + '/index.html';--// Keep a global reference of the window object, if you don't, the window will-// be closed automatically when the JavaScript object is garbage collected.-let mainWindow = null;-let newWindow = null;+process.on('uncaughtException', (error) => {+ if (error.stack) {+ console.error('error:', error.stack);+ }+ reloadApp();+});// Quit when all windows are closed.app.on('window-all-closed', () => {@@ -69,48 +72,61 @@ app.on('will-quit', () => {globalShortcut.unregisterAll();});-ipcMain.on("new-win", () => {- newWindow = new BrowserWindow({width: 1280, height: 768});+app.on('ready', (event) => {- let startupParameter = "";- if (startupFilePath) {- startupParameter = "?open=" + encodeURIComponent(startupFilePath);- }+ ipcMain.on('global-shortcuts-enabled', (e, arg) => {+ if(arg) {+ globalShortcut.register('CommandOrControl+Alt+P', resumePlayback);+ globalShortcut.register('CommandOrControl+Alt+N', newTextFile);+ globalShortcut.register('CommandOrControl+Alt+D', getNextFile);+ globalShortcut.register('CommandOrControl+Alt+A', getPreviousFile);+ globalShortcut.register('CommandOrControl+Alt+W', showTagSpaces);+ } else {+ globalShortcut.unregisterAll();+ }+ });- newWindow.setMenu(null);- newWindow.loadURL(indexPath + startupParameter);+ ipcMain.on("new-win", () => {+ newWindow = new BrowserWindow({width: 1280, height: 768});- if (debugMode) {- newWindow.webContents.openDevTools();- }+ let startupParameter = "";+ if (startupFilePath) {+ startupParameter = "?open=" + encodeURIComponent(startupFilePath);+ }- newWindow.once('ready-to-show', () => {- newWindow.show();+ newWindow.setMenu(null);+ newWindow.loadURL(indexPath + startupParameter);++ if (debugMode) {+ newWindow.webContents.openDevTools();+ }++ newWindow.once('ready-to-show', () => {+ newWindow.show();+ });++ ipcMain.on('win-close', (e, arg) => {+ newWindow.hide();+ });++ ipcMain.on('close', (e, arg) => {+ newWindow.hide();+ });});ipcMain.on('win-close', (e, arg) => {- newWindow.hide();+ mainWindow.hide();});ipcMain.on('close', (e, arg) => {- newWindow.hide();+ mainWindow.hide();});-});-ipcMain.on('relaunch-app', reloadApp);+ ipcMain.on('relaunch-app', reloadApp);-function reloadApp() {- if (mainWindow) {- mainWindow.loadURL(indexPath);- }- if (newWindow) {- newWindow.loadURL(indexPath);- }-}--app.on('ready', (event) => {- //console.log(app.getLocale());- //console.log(app.getAppPath());+ ipcMain.on('quit-application', (event, arg) => {+ app.quit();+ });// Load the previous state with fallback to defaultslet mainWindowState = windowStateKeeper({@@ -151,10 +167,6 @@ app.on('ready', (event) => {mainWindow = null;});- ipcMain.on('close', (e, arg) => {- mainWindow.hide();- });-mainWindow.webContents.on('crashed', () => {const options = {type: 'info',@@ -172,10 +184,6 @@ app.on('ready', (event) => {});});- ipcMain.on('win-close', (e, arg) => {- mainWindow.hide();- });-let focusedWindow = BrowserWindow.getFocusedWindow();let trayIconPath;@@ -222,7 +230,7 @@ app.on('ready', (event) => {type: 'separator'},{- label: 'Open Next File (' + ctrlName + '+Alt+S)',+ label: 'Open Next File (' + ctrlName + '+Alt+D)',click: getNextFile},{@@ -257,103 +265,95 @@ app.on('ready', (event) => {let title = 'TagSpaces App';let trayMenu = Menu.buildFromTemplate(trayMenuTemplate);- // trayIcon.setToolTip(title);- // trayIcon.setTitle(title);- trayIcon.setContextMenu(trayMenu);-- globalShortcut.register('CommandOrControl+Alt+P', resumePlayback);-- globalShortcut.register('CommandOrControl+Alt+N', newTextFile);-- globalShortcut.register('CommandOrControl+Alt+S', getNextFile);- globalShortcut.register('CommandOrControl+Alt+A', getPreviousFile);+ trayIcon.setContextMenu(trayMenu);- globalShortcut.register('CommandOrControl+Alt+W', showTagSpaces);+});- function showTagSpaces() {- if (mainWindow) {- mainWindow.restore();- mainWindow.show();- } else {- newWindow.restore();- newWindow.show();- }- //mainWindow.webContents.send("showing-tagspaces", "tagspaces");+function reloadApp() {+ if (mainWindow) {+ mainWindow.loadURL(indexPath);}+ if (newWindow) {+ newWindow.loadURL(indexPath);+ }+}- function newTextFile() {- if (mainWindow) {- mainWindow.show();- mainWindow.webContents.send("file", "text");- } else {- newWindow.show();- newWindow.webContents.send("file", "text");- }+function showTagSpaces() {+ if (mainWindow) {+ mainWindow.restore();+ mainWindow.show();+ } else {+ newWindow.restore();+ newWindow.show();}+ //mainWindow.webContents.send("showing-tagspaces", "tagspaces");+}- function newHTMLFile() {- if (mainWindow) {- mainWindow.show();- mainWindow.webContents.send("file", "html");- } else {- newWindow.show();- newWindow.webContents.send("file", "html");- }+function newTextFile() {+ if (mainWindow) {+ mainWindow.show();+ mainWindow.webContents.send("file", "text");+ } else {+ newWindow.show();+ newWindow.webContents.send("file", "text");}+}- function newMDFile() {- if (mainWindow) {- mainWindow.show();- mainWindow.webContents.send("file", "markdown");- } else {- newWindow.show();- newWindow.webContents.send("file", "markdown");- }+function newHTMLFile() {+ if (mainWindow) {+ mainWindow.show();+ mainWindow.webContents.send("file", "html");+ } else {+ newWindow.show();+ newWindow.webContents.send("file", "html");}+}- function newAudioFile() {- if (mainWindow) {- mainWindow.show();- mainWindow.webContents.send("file", "audio");- } else {- newWindow.show();- newWindow.webContents.send("file", "audio");- }+function newMDFile() {+ if (mainWindow) {+ mainWindow.show();+ mainWindow.webContents.send("file", "markdown");+ } else {+ newWindow.show();+ newWindow.webContents.send("file", "markdown");}+}- function getNextFile() {- if (mainWindow) {- mainWindow.show();- mainWindow.webContents.send("file", "next-file");- } else {- newWindow.show();- newWindow.webContents.send("file", "next-file");- }+function newAudioFile() {+ if (mainWindow) {+ mainWindow.show();+ mainWindow.webContents.send("file", "audio");+ } else {+ newWindow.show();+ newWindow.webContents.send("file", "audio");}+}- function getPreviousFile() {- if (mainWindow) {- mainWindow.show();- mainWindow.webContents.send("file", "previous-file");- } else {- newWindow.show();- newWindow.webContents.send("file", "previous-file");- }+function getNextFile() {+ if (mainWindow) {+ mainWindow.show();+ mainWindow.webContents.send("file", "next-file");+ } else {+ newWindow.show();+ newWindow.webContents.send("file", "next-file");}+}- function resumePlayback() {- if (mainWindow) {- mainWindow.webContents.send("play-pause", true);- } else {- newWindow.webContents.send("play-pause", true);- }+function getPreviousFile() {+ if (mainWindow) {+ mainWindow.show();+ mainWindow.webContents.send("file", "previous-file");+ } else {+ newWindow.show();+ newWindow.webContents.send("file", "previous-file");}-});+}-process.on('uncaughtException', (error) => {- if (error.stack) {- console.error('error:', error.stack);+function resumePlayback() {+ if (mainWindow) {+ mainWindow.webContents.send("play-pause", true);+ } else {+ newWindow.webContents.send("play-pause", true);}- reloadApp();-});\ No newline at end of file+}diff --git a/data/js/core.api.js b/data/js/core.api.jsindex 24e3d4e4e..5728b6f61 100644--- a/data/js/core.api.js+++ b/data/js/core.api.js@@ -145,7 +145,7 @@ define(function(require, exports, module) {switchInterfaceLanguage(language).then(function() {if (isNode || isElectron) {- tsIOApi.initMainMenu();+ tsIOApi.initElectronIntegration();}if (tsSettings.Settings.tagspacesList.length < 1) {diff --git a/data/templates/OptionsDialog.html b/data/templates/OptionsDialog.htmlindex 52ae632f1..804359563 100644--- a/data/templates/OptionsDialog.html+++ b/data/templates/OptionsDialog.html@@ -312,6 +312,14 @@<p class="help-block"><span data-i18n="ns.dialogs:keyBindingInstructions4"></span> <code>,</code>.</p></div>+ <div class="form-group">+ <div class="col-sm-12 checkbox">+ <label for="enableGlobalKeyboardShortcutsCheckbox">+ <input type="checkbox" id="enableGlobalKeyboardShortcutsCheckbox"/>+ <span data-i18n="ns.dialogs:enableGlobalKeyboardShortcuts"></span> (restart needed)+ </label>+ </div>+ </div><div class='form-group'><label class="col-sm-4 control-label" data-i18n="ns.dialogs:selectAllFiles"></label>@@ -410,14 +418,6 @@<input type='text' class='form-control' id='renamingFileKeyBinding' placeholder='f2 | f2'/></div></div>- <div class="form-group">- <div class="col-sm-12 checkbox">- <label for="enableGlobalKeyboardShortcutsCheckbox">- <input type="checkbox" id="enableGlobalKeyboardShortcutsCheckbox"/>- <span data-i18n="ns.dialogs:enableGlobalKeyboardShortcuts"></span>- </label>- </div>- </div></form></div></div>