feat(titlebar): handle window button clicks

This commit is contained in:
dragonwocky 2024-01-24 14:46:52 +11:00
parent f085362774
commit 6e9f0a420f
Signed by: dragonwocky
GPG Key ID: 7998D08F7D7BD7A8
2 changed files with 31 additions and 1 deletions

View File

@ -15,20 +15,24 @@ const minimizeLabel = "Minimize window",
closeLabel = "Close window";
const createWindowButtons = () => {
const { html } = globalThis.__enhancerApi,
const { html, sendMessage, invokeInWorker } = globalThis.__enhancerApi,
$minimize = html`<${TopbarButton}
onclick=${() => sendMessage("notion-enhancer:titlebar", "minimize")}
aria-label="${minimizeLabel}"
icon="minus"
/>`,
$maximize = html`<${TopbarButton}
onclick=${() => sendMessage("notion-enhancer:titlebar", "maximize")}
aria-label="${maximizeLabel}"
icon="maximize"
/>`,
$unmaximize = html`<${TopbarButton}
onclick=${() => sendMessage("notion-enhancer:titlebar", "unmaximize")}
aria-label="${unmaximizeLabel}"
icon="minimize"
/>`,
$close = html`<${TopbarButton}
onclick=${() => sendMessage("notion-enhancer:titlebar", "close")}
class="!hover:(bg-red-600 text-white)"
aria-label="${closeLabel}"
icon="x"
@ -37,6 +41,17 @@ const createWindowButtons = () => {
html`<${Tooltip}><b>${maximizeLabel}</b><//>`.attach($maximize, "bottom");
html`<${Tooltip}><b>${unmaximizeLabel}</b><//>`.attach($unmaximize, "bottom");
html`<${Tooltip}><b>${closeLabel}</b><//>`.attach($close, "bottom");
const resizeWindow = async () => {
const isMaximized = await invokeInWorker("notion-enhancer:titlebar", {
query: "is-maximized",
});
$maximize.style.display = isMaximized ? "none" : "";
$unmaximize.style.display = isMaximized ? "" : "none";
};
window.addEventListener("resize", resizeWindow);
resizeWindow();
return html`<div>${$minimize}${$maximize}${$unmaximize}${$close}</div>`;
};

View File

@ -10,4 +10,19 @@ module.exports = async ({}, db) => {
Object.assign((globalThis.__notionConfig ??= {}), {
titlebarStyle: "hidden",
});
const { ipcMain, BrowserWindow } = require("electron");
ipcMain.on("notion-enhancer:titlebar", ({ sender }, message) => {
const window = BrowserWindow.fromWebContents(sender);
if (message === "minimize") window.minimize();
if (message === "maximize") window.maximize();
if (message === "unmaximize") window.unmaximize();
if (message === "close") window.close();
});
ipcMain.handle("notion-enhancer:titlebar", ({ sender }, message) => {
if (message?.query !== "is-maximized") return;
const window = BrowserWindow.fromWebContents(sender);
return window.isMaximized();
});
};