Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

Commit

Permalink
fixes and design changes (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
Naomi Calabretta committed Jul 30, 2020
1 parent 24dcddd commit 424075d
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 34 deletions.
12 changes: 2 additions & 10 deletions src/browser_window.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class BrowserWindow extends electron.BrowserWindow {
// We do not call super to get an actual BrowserWindow from electron and not mess with native casts (broke GTK modals)
const window = new electron.BrowserWindow(options);
BrowserWindow._bindAndReplace(window, BrowserWindow.setBackgroundColor);
BrowserWindow._bindAndReplace(window, BrowserWindow.getBlur);
BrowserWindow._bindAndReplace(window, BrowserWindow.setBlur);
Main.getInstance().init(window, options);
if(typeof _backgroundColor !== "undefined")
window.webContents.on('dom-ready', () => window.setBackgroundColor(_backgroundColor));
if(typeof options.blur !== "undefined")
window.setBlur(options.blur);
return window;
}

Expand All @@ -55,14 +55,6 @@ class BrowserWindow extends electron.BrowserWindow {
else return callback();
}

static setBlur(blur){
return Main.getInstance().setBlur(this, blur);
}

static getBlur(){
return Main.getInstance().getBlur(this);
}

static _bindAndReplace(object, method){
const boundFunction = method.bind(object);
Object.defineProperty(object, method.name, {
Expand Down
12 changes: 10 additions & 2 deletions src/platforms/_platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@

module.exports = class Platform{

static init(win, _options){}
// override this or just use it idk
static init(win, _options){
Object.defineProperty(win, "getBlur", {
get: () => () => this.getBlur(win)
});
Object.defineProperty(win, "setBlur", {
get: () => (blur) => this.setBlur(win, blur)
});
}

static setBlur(win, bool){}

static getBlur(win){}

}
}
53 changes: 44 additions & 9 deletions src/platforms/darwin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ module.exports = class Darwin extends Platform {

static init(win, _options){
this._wrapVibrancy(win, _options.vibrancy || null);

Object.defineProperty(win, "setBlur", {
get: () => (blur) => this.setBlur(win, blur)
});
}

static setBlur(win, bool){
Expand All @@ -31,32 +35,63 @@ module.exports = class Darwin extends Platform {
return Promise.resolve(win.getVibrancy() === null);
}

/**
* Note for everyone, not just developers and contributors
* the win.vibrancy variable should always return a string.
* It can return null but only initially, when the vibrancy is not set.
* It MUST NOT return null afterwards, even if win.setBlur(false) has been called.
* win.vibrancy defines the vibrancy effect to be used when blurring.
*/
static _wrapVibrancy(win, vibrancyInitialValue = "fullscreen-ui"){
// Wrap the original setVibrancy
const originalFunction = win.setVibrancy;
let _vibrancy = vibrancyInitialValue;
let _vibrancyInternal = _vibrancy;

// Set an array of two values
// [
// Vibrancy value to be reported,
// Current applied value (can be null in case of blur = false)
// ]
let _vibrancy = [vibrancyInitialValue, vibrancyInitialValue];

// Here we define the actual win.vibrancy variable
Object.defineProperty(win, "vibrancy", {
get: () => _vibrancy,
set: async (_newVibrancy) => {
if(typeof _newVibrancy === "undefined") return;
// Yeet any undefined value
if(typeof _newVibrancy === "undefined")
return;

// Handle Electron handling "" vibrancy as null (disables stuff)
if(_newVibrancy === "")
_newVibrancy = null;

// If the new vibrancy is null, set the internal vibrancy to null
// and call the original function to reflect the change, then return.
if(_newVibrancy === null){
originalFunction(null);
_vibrancyInternal = null;
_vibrancy[1] = null;
return;
}
_vibrancyInternal = _newVibrancy;
_vibrancy = _vibrancyInternal;
if(_vibrancyInternal !== null) originalFunction(_vibrancy);

// Set the new vibrancy to the nominal one
_vibrancy[0] = _newVibrancy;
// Update the actual vibrancy if it's not null
if(_vibrancy[1] !== null){
_vibrancy[1] = _vibrancy[0];
originalFunction(_vibrancy);
}
}
});

// Bind the new wrapped setVibrancy function and apply it
const boundFunction = ((vibrancy) => {this.vibrancy = vibrancy;}).bind(win);
Object.defineProperty(win, "setVibrancy", {
get: () => boundFunction
});
Object.defineProperty(win, "getVibrancy", {
get: () => _vibrancyInternal

// Now we need an exposed method to get the correct blur status
Object.defineProperty(win, "getBlur", {
get: () => _vibrancy[1] === null
});
}
}
16 changes: 13 additions & 3 deletions src/platforms/win32.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ module.exports = class Win32 extends Platform {
this._defineDWM(win);

if(typeof win.blurType === "undefined")
this._defineBlurType(win);
this._defineBlurType(win, _options.blurType);

super.init(win, _options);
}

static setBlur(win, bool){
Expand Down Expand Up @@ -70,8 +72,16 @@ module.exports = class Win32 extends Platform {
});
}

static _defineBlurType(win){
let _blurType = null;
/**
* Note for everyone, not just developers and contributors
* the win.blurType variable should always return a string
* between "acrylic", "blurbehind" and "transparent".
* It can return null but only initially, when the blurType is not set.
* It MUST NOT return null afterwards, even if win.setBlur(false) has been called.
* win.blurType defines the blurring effect to be used when blurring.
*/
static _defineBlurType(win, _defaultValue = null){
let _blurType = _defaultValue;
Object.defineProperty(win, "blurType", {
get: () => _blurType,
set: async (_newBlurType) => {
Expand Down
17 changes: 10 additions & 7 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ function spawnWindow(){
title: "Glasstron test window",
autoHideMenuBar: true,
frame: false, // this is a requirement for transparent windows it seems
show: false,
show: true,
blur: false,
blurType: "blurbehind",
vibrancy: "fullscreen-ui",
webPreferences: {
nodeIntegration: true
}
Expand All @@ -30,7 +33,6 @@ function spawnWindow(){
win.webContents.loadURL(`file://${__dirname}/index.html`);

if(process.platform === "win32"){
win.blurType = "blurbehind";
electron.ipcMain.on("blurTypeChange", (e, value) => {
const win = electron.BrowserWindow.fromWebContents(e.sender);
if(win !== null){
Expand All @@ -44,25 +46,26 @@ function spawnWindow(){
});
}

win.setBlur(true);

electron.ipcMain.on("blurToggle", async (e, value) => {
const win = electron.BrowserWindow.fromWebContents(e.sender);
if(win !== null){
await win.setBlur(value);
e.sender.send("blurToggled", await win.getBlur());
e.sender.send("blurStatus", await win.getBlur());
}
});

electron.ipcMain.on("blurQuery", async (e, value) => {
e.sender.send("blurStatus", await win.getBlur());
});

electron.ipcMain.on("close", () => {
electron.app.quit()
});

electron.ipcMain.on("minimize", (e) => {
const win = electron.BrowserWindow.fromWebContents(e.sender);
win.minimize();
});

win.show();

return win;
}
8 changes: 5 additions & 3 deletions test/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ const wb = new (require('windowbar'))(
.on('minimize', () => electron.ipcRenderer.send("minimize"))
.appendTo(document.getElementById("windowbar"));

let toggled = true;
electron.ipcRenderer.on("blurToggled", (e, res) => {
let toggled = null;
electron.ipcRenderer.send("blurQuery");

electron.ipcRenderer.on("blurStatus", (e, res) => {
toggled = res;
document.getElementById("toggle").innerHTML = "Toggle " + (toggled ? "off" : "on");
});
Expand All @@ -37,4 +39,4 @@ if(process.platform === "win32"){
});

document.getElementById("win32").classList.remove("hidden");
}
}

0 comments on commit 424075d

Please sign in to comment.