Skip to content

Commit

Permalink
✨ Remember last position
Browse files Browse the repository at this point in the history
  • Loading branch information
lmachens committed Nov 30, 2021
1 parent babe4d4 commit df126ca
Show file tree
Hide file tree
Showing 3 changed files with 467 additions and 46 deletions.
15 changes: 13 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const { app, BrowserWindow, screen } = require("electron");
const path = require("path");
const Store = require("electron-store");

const store = new Store();

const createWindow = () => {
const win = new BrowserWindow({
Expand All @@ -18,7 +21,7 @@ const createWindow = () => {
win.webContents.setWindowOpenHandler((props) => {
const json = props.features.substr("website=".length);
const website = JSON.parse(decodeURIComponent(json));

const position = store.get(`${website.id}-position`);
return {
action: "allow",
overrideBrowserWindowOptions: {
Expand All @@ -35,12 +38,13 @@ const createWindow = () => {
webPreferences: {
preload: path.join(__dirname, "childPreload.js"),
},
x: position?.[0],
y: position?.[1],
},
};
});

const windowOptions = {};

win.webContents.on("did-create-window", (childWindow, props) => {
const website = JSON.parse(decodeURIComponent(props.options.website));
if (website.clickThrough) {
Expand All @@ -54,6 +58,12 @@ const createWindow = () => {
childWindow.setIgnoreMouseEvents(true);
});
}

childWindow.on("moved", (event, data) => {
const position = childWindow.getPosition();
store.set(`${website.id}-position`, position);
});

windowOptions[childWindow.id] = website;
});

Expand Down Expand Up @@ -84,6 +94,7 @@ const createWindow = () => {
});
}, 10);
};

app.whenReady().then(() => {
createWindow();

Expand Down
Loading

0 comments on commit df126ca

Please sign in to comment.