From 26bce739d7c43d6a9c36acb5b9bf5c125c0cd0e1 Mon Sep 17 00:00:00 2001 From: Samuel Elliott Date: Thu, 23 Aug 2018 02:53:40 +0100 Subject: [PATCH] Add bd-windowHasFrame and bd-windowIsTransparent classes --- client/src/ui/bdui.js | 8 +++++++- .../bd/internal-settings/WindowPreferences.vue | 7 ++++--- common/modules/utils.js | 15 +++++++++++---- core/src/main.js | 7 ++++++- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/client/src/ui/bdui.js b/client/src/ui/bdui.js index 1ca7656a..fb298a17 100644 --- a/client/src/ui/bdui.js +++ b/client/src/ui/bdui.js @@ -25,13 +25,19 @@ export default class { }); if (hideButtonSetting.value) document.body.classList.add('bd-hideButton'); + const currentWindow = remote.getCurrentWindow(); + const windowOptions = currentWindow.__bd_options; + + if (!windowOptions.hasOwnProperty('frame') || windowOptions.frame) document.body.classList.add('bd-windowHasFrame'); + if (windowOptions.transparent) document.body.classList.add('bd-windowIsTransparent'); + this.pathCache = { isDm: null, server: DiscordApi.currentGuild, channel: DiscordApi.currentChannel }; - remote.getCurrentWindow().webContents.on('did-navigate-in-page', (e, url, isMainFrame) => { + currentWindow.webContents.on('did-navigate-in-page', (e, url, isMainFrame) => { const { currentGuild, currentChannel } = DiscordApi; if (!this.pathCache.server) diff --git a/client/src/ui/components/bd/internal-settings/WindowPreferences.vue b/client/src/ui/components/bd/internal-settings/WindowPreferences.vue index 545dda0b..d887c748 100644 --- a/client/src/ui/components/bd/internal-settings/WindowPreferences.vue +++ b/client/src/ui/components/bd/internal-settings/WindowPreferences.vue @@ -59,8 +59,7 @@ filePath() { try { return Globals.require.resolve(path.join(Globals.getPath('data'), 'window')); - } - catch (err) { + } catch (err) { FileUtils.writeJsonToFile(this.defaultFilePath, {}); return this.defaultFilePath; } @@ -79,11 +78,13 @@ if (event.category.id === 'default' && event.setting.id === 'transparent') { newPreferences.transparent = event.value; - if (event.value) delete newPreferences.backgroundColor; + if (event.value) newPreferences.backgroundColor = null; + else if (newPreferences.backgroundColor === null) delete newPreferences.backgroundColor; } if (event.category.id === 'default' && event.setting.id === 'background-colour') { newPreferences.backgroundColor = event.value; + if (event.value) newPreferences.transparent = false; } if (event.category.id === 'default' && event.setting.id === 'frame') { diff --git a/common/modules/utils.js b/common/modules/utils.js index 2635ff43..fa49f23a 100644 --- a/common/modules/utils.js +++ b/common/modules/utils.js @@ -132,16 +132,21 @@ export class Utils { * @param {Function} exclude A function to filter objects that shouldn't be cloned * @return {Any} The cloned value */ - static deepclone(value, exclude) { + static deepclone(value, exclude, cloned) { if (exclude && exclude(value)) return value; - if (typeof value === 'object') { - if (value instanceof Array) return value.map(i => this.deepclone(i, exclude)); + if (!cloned) cloned = new WeakMap(); + + if (typeof value === 'object' && value !== null) { + if (value instanceof Array) return value.map(i => this.deepclone(i, exclude, cloned)); + + if (cloned.has(value)) return cloned.get(value); const clone = Object.assign({}, value); + cloned.set(value, clone); for (const key in clone) { - clone[key] = this.deepclone(clone[key], exclude); + clone[key] = this.deepclone(clone[key], exclude, cloned); } return clone; @@ -159,6 +164,8 @@ export class Utils { if (exclude && exclude(object)) return; if (typeof object === 'object' && object !== null) { + if (Object.isFrozen(object)) return object; + const properties = Object.getOwnPropertyNames(object); for (const property of properties) { diff --git a/core/src/main.js b/core/src/main.js index dabbb572..ff7e85d3 100644 --- a/core/src/main.js +++ b/core/src/main.js @@ -74,7 +74,7 @@ class PatchedBrowserWindow extends BrowserWindow { super(options); - this.__bd_preload = []; + Object.defineProperty(this, '__bd_preload', {value: []}); if (originalOptions.webPreferences && originalOptions.webPreferences.preload) { this.__bd_preload.push(originalOptions.webPreferences.preload); @@ -82,6 +82,11 @@ class PatchedBrowserWindow extends BrowserWindow { if (userOptions.webPreferences && userOptions.webPreferences.preload) { this.__bd_preload.push(path.resolve(_dataPath, userOptions.webPreferences.preload)); } + + Object.defineProperty(this, '__bd_options', {value: options}); + Object.freeze(options); + Object.freeze(options.webPreferences); + Object.freeze(this.__bd_preload); } static get userWindowPreferences() {