From a3e55c61218123b9514cab9fc44eab87eb0b6087 Mon Sep 17 00:00:00 2001 From: dragonwocky Date: Sun, 19 Jul 2020 16:24:34 +1000 Subject: [PATCH] taken control of createWindow --- repo/core/icons/alwaysontop_off.svg | 1 + repo/core/icons/alwaysontop_on.svg | 1 + repo/core/icons/close.svg | 1 + repo/core/{ => icons}/mac+linux.png | Bin repo/core/icons/maximise_off.svg | 1 + repo/core/icons/maximise_on.svg | 1 + repo/core/icons/minimise.svg | 1 + repo/core/{ => icons}/windows.ico | Bin repo/core/mod.js | 14 +++-- repo/core/tray.js | 22 ++++++- repo/core/window.js | 85 ++++++++++++++++++++++++++++ 11 files changed, 119 insertions(+), 8 deletions(-) create mode 100644 repo/core/icons/alwaysontop_off.svg create mode 100644 repo/core/icons/alwaysontop_on.svg create mode 100644 repo/core/icons/close.svg rename repo/core/{ => icons}/mac+linux.png (100%) create mode 100644 repo/core/icons/maximise_off.svg create mode 100644 repo/core/icons/maximise_on.svg create mode 100644 repo/core/icons/minimise.svg rename repo/core/{ => icons}/windows.ico (100%) create mode 100644 repo/core/window.js diff --git a/repo/core/icons/alwaysontop_off.svg b/repo/core/icons/alwaysontop_off.svg new file mode 100644 index 0000000..e731f9b --- /dev/null +++ b/repo/core/icons/alwaysontop_off.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/repo/core/icons/alwaysontop_on.svg b/repo/core/icons/alwaysontop_on.svg new file mode 100644 index 0000000..b9befcd --- /dev/null +++ b/repo/core/icons/alwaysontop_on.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/repo/core/icons/close.svg b/repo/core/icons/close.svg new file mode 100644 index 0000000..7268fb0 --- /dev/null +++ b/repo/core/icons/close.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/repo/core/mac+linux.png b/repo/core/icons/mac+linux.png similarity index 100% rename from repo/core/mac+linux.png rename to repo/core/icons/mac+linux.png diff --git a/repo/core/icons/maximise_off.svg b/repo/core/icons/maximise_off.svg new file mode 100644 index 0000000..e79979f --- /dev/null +++ b/repo/core/icons/maximise_off.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/repo/core/icons/maximise_on.svg b/repo/core/icons/maximise_on.svg new file mode 100644 index 0000000..038b21e --- /dev/null +++ b/repo/core/icons/maximise_on.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/repo/core/icons/minimise.svg b/repo/core/icons/minimise.svg new file mode 100644 index 0000000..e40cd84 --- /dev/null +++ b/repo/core/icons/minimise.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/repo/core/windows.ico b/repo/core/icons/windows.ico similarity index 100% rename from repo/core/windows.ico rename to repo/core/icons/windows.ico diff --git a/repo/core/mod.js b/repo/core/mod.js index 295e0d7..d8e90e5 100644 --- a/repo/core/mod.js +++ b/repo/core/mod.js @@ -7,7 +7,8 @@ const defaults = { openhidden: false, maximized: false, - tray: false, + close_to_tray: false, + frameless: true, hotkey: 'CmdOrCtrl+Shift+A', }; @@ -24,10 +25,13 @@ module.exports = { options: [], hacks: { 'main/main.js': require('./tray.js')(defaults), - 'renderer/preload.js': function (store) { - const data = store({ name: 'dragonwocky' }); - console.log(data.name); - data.name = 'tom'; + 'main/createWindow.js': require('./window.js')(defaults), + 'renderer/preload.js': function (store, __exports) { + const window = require('electron').remote.getCurrentWindow(); + document.defaultView.addEventListener('keyup', (event) => { + if (event.code === 'F5') window.reload(); + // if (event.code === 'F4' && event.altKey) window.close(); + }); }, }, }; diff --git a/repo/core/tray.js b/repo/core/tray.js index 0423260..0a9175d 100644 --- a/repo/core/tray.js +++ b/repo/core/tray.js @@ -8,7 +8,7 @@ let tray; module.exports = (defaults) => - function (store) { + function (store, __exports) { const electron = require('electron'), path = require('path'), is_mac = process.platform === 'darwin', @@ -18,9 +18,9 @@ module.exports = (defaults) => electron.app.on('ready', () => { tray = new electron.Tray( is_win - ? path.normalize(`${__dirname}/windows.ico`) + ? path.resolve(`${__dirname}/icons/windows.ico`) : new electron.nativeImage.createFromPath( - path.normalize(`${__dirname}/mac+linux.png`) + path.resolve(`${__dirname}/icons/mac+linux.png`) ).resize({ width: 16, height: 16, @@ -31,10 +31,20 @@ module.exports = (defaults) => { type: 'normal', label: 'Bug Report', + click: () => { + electron.shell.openExternal( + 'https://github.com/dragonwocky/notion-enhancer/issues/new?labels=bug&template=bug-report.md' + ); + }, }, { type: 'normal', label: 'Feature Request', + click: () => { + electron.shell.openExternal( + 'https://github.com/dragonwocky/notion-enhancer/issues/new?labels=enhancement&template=feature-request.md' + ); + }, }, { type: 'separator', @@ -42,6 +52,11 @@ module.exports = (defaults) => { type: 'normal', label: 'Docs', + click: () => { + electron.shell.openExternal( + 'https://github.com/dragonwocky/notion-enhancer/tree/js' + ); + }, }, { type: 'normal', @@ -70,6 +85,7 @@ module.exports = (defaults) => windows.forEach((win) => [win.isFocused() && win.blur(), win.hide()]); if (is_mac) electron.app.hide(); } + tray.on('click', () => { const windows = electron.BrowserWindow.getAllWindows(); if (windows.some((win) => win.isVisible())) hideWindows(); diff --git a/repo/core/window.js b/repo/core/window.js new file mode 100644 index 0000000..82ddc49 --- /dev/null +++ b/repo/core/window.js @@ -0,0 +1,85 @@ +/* + * notion-enhancer + * (c) 2020 dragonwocky + * (c) 2020 TarasokUA + * (https://dragonwocky.me/) under the MIT license + */ + +module.exports = (defaults) => + function (store, __exports) { + const electron = require('electron'), + allWindows = electron.BrowserWindow.getAllWindows, + createWindow = __exports.createWindow, + path = require('path'), + is_mac = process.platform === 'darwin', + settings = store(defaults), + helpers = require('../../pkg/helpers.js'), + __notion = helpers.getNotion(); + + __exports.createWindow = function (relativeUrl) { + if (!relativeUrl) relativeUrl = ''; + const window_state = require(`${__notion.replace( + /\\/g, + '/' + )}/app/node_modules/electron-window-state/index.js`)({ + defaultWidth: 1320, + defaultHeight: 860, + }), + rect = { + x: window_state.x, + y: window_state.y, + width: window_state.width, + height: window_state.height, + }, + focused_window = electron.BrowserWindow.getFocusedWindow(); + if (focused_window && !focused_window.isMaximized()) { + rect.x = focused_window.getPosition()[0] + 20; + rect.y = focused_window.getPosition()[1] + 20; + rect.width = focused_window.getSize()[0]; + rect.height = focused_window.getSize()[1]; + } + const window = new electron.BrowserWindow({ + show: false, + backgroundColor: '#ffffff', + titleBarStyle: 'hiddenInset', + frame: !settings.frameless, + webPreferences: { + preload: path.resolve(`${__notion}/app/renderer/index.js`), + webviewTag: true, + session: electron.session.fromPartition('persist:notion'), + }, + ...rect, + }); + window.once('ready-to-show', function () { + if ( + !settings.openhidden || + allWindows().some((win) => win.isVisible() && win.id != window.id) + ) { + window.show(); + if (settings.maximized) window.maximize(); + if ( + (focused_window && focused_window.isFullScreen()) || + window_state.isFullScreen + ) + window.setFullScreen(true); + } + }); + let intended_quit = false; + window.on('close', (e) => { + if ( + intended_quit || + !settings.close_to_tray || + allWindows().length > 1 + ) { + window_state.saveState(window); + window = null; + } else { + e.preventDefault(); + window.hide(); + } + }); + electron.app.on('before-quit', () => (intended_quit = true)); + window.loadURL(__exports.getIndexUrl(relativeUrl)); + return window; + }; + };