Skip to content

Commit

Permalink
✨ Fade out on hover if clickthrough is active
Browse files Browse the repository at this point in the history
  • Loading branch information
lmachens committed Nov 30, 2021
1 parent af3a332 commit babe4d4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
11 changes: 11 additions & 0 deletions childPreload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { ipcRenderer } = require("electron");

window.addEventListener("DOMContentLoaded", () => {
document.body.style.transition = "opacity 0.2s ease-in";
ipcRenderer.on("mouseenter", () => {
document.body.style.opacity = 0.1;
});
ipcRenderer.on("mouseleave", () => {
document.body.style.opacity = 1;
});
});
41 changes: 36 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { app, BrowserWindow } = require("electron");
const { app, BrowserWindow, screen } = require("electron");
const path = require("path");

const createWindow = () => {
Expand All @@ -7,7 +7,6 @@ const createWindow = () => {
height: 640,
webPreferences: {
preload: path.join(__dirname, "preload.js"),
webgl: true,
},
frame: true,
resizable: false,
Expand All @@ -33,26 +32,58 @@ const createWindow = () => {
autoHideMenuBar: true,
maximizable: website.frame,
fullscreenable: website.frame,
webPreferences: {
preload: path.join(__dirname, "childPreload.js"),
},
},
};
});

const windowOptions = {};

win.webContents.on("did-create-window", (childWindow, props) => {
const website = JSON.parse(decodeURIComponent(props.options.website));
if (website.clickThrough) {
childWindow.setIgnoreMouseEvents(true, { forward: true });
childWindow.setIgnoreMouseEvents(true);

childWindow.on("focus", () => {
childWindow.setIgnoreMouseEvents(false);
});

childWindow.on("blur", () => {
childWindow.setIgnoreMouseEvents(true, { forward: true });
childWindow.setIgnoreMouseEvents(true);
});
}
windowOptions[childWindow.id] = website;
});
};

let prevMousePos = screen.getCursorScreenPoint();
setInterval(() => {
const mousePos = screen.getCursorScreenPoint();
if (mousePos.x === prevMousePos.x && mousePos.y === prevMousePos.y) {
return;
}
prevMousePos = mousePos;
const allWindows = BrowserWindow.getAllWindows();
const clickThroughWindows = allWindows.filter(
(singleWindow) => windowOptions[singleWindow.id]?.clickThrough
);
clickThroughWindows.forEach((singleWindow) => {
const bounds = singleWindow.getBounds();
const xInBounds =
mousePos.x <= bounds.x + bounds.width && mousePos.x >= bounds.x;
const yInBounds =
mousePos.y <= bounds.y + bounds.height && mousePos.y >= bounds.y;
const inBounds = xInBounds && yInBounds;
if (inBounds && !singleWindow.inBounds) {
singleWindow.webContents.send("mouseenter");
} else if (!inBounds && singleWindow.inBounds) {
singleWindow.webContents.send("mouseleave");
}
singleWindow.inBounds = inBounds;
});
}, 10);
};
app.whenReady().then(() => {
createWindow();

Expand Down
1 change: 0 additions & 1 deletion preload.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { createFormModalElement } = require("./lib/formModalElement");
const { getWebsites, addWebsite } = require("./lib/storage");
const { createWebsiteElement } = require("./lib/websiteElement");
const { remote } = require("electron");

window.addEventListener("DOMContentLoaded", () => {
const websitesElement = document.querySelector(".websites");
Expand Down

0 comments on commit babe4d4

Please sign in to comment.