From ae5697dbad45d7cc5dcb11cb786482e2a4a5ad10 Mon Sep 17 00:00:00 2001 From: pu-raihan Date: Thu, 28 Mar 2024 14:43:08 +0530 Subject: [PATCH] feat: Added custom titleBarverlay with close,minimize and maximize styles --- main.ts | 7 -- main/preload.ts | 36 ++++++++ main/registerIpcMainMessageListeners.ts | 22 +++++ src/components/WindowsTitleBar.vue | 105 +++++++++++++++++++++++- utils/messages.ts | 7 ++ 5 files changed, 169 insertions(+), 8 deletions(-) diff --git a/main.ts b/main.ts index 5727fd15..23140d29 100644 --- a/main.ts +++ b/main.ts @@ -102,13 +102,6 @@ export class Main { resizable: true, }; - if (!this.isMac) { - options.titleBarOverlay = { - color: '#FFFFFF', - height: 26, - }; - } - if (this.isDevelopment || this.isLinux) { Object.assign(options, { icon: this.icon }); } diff --git a/main/preload.ts b/main/preload.ts index e0a700e9..c4fc72d0 100644 --- a/main/preload.ts +++ b/main/preload.ts @@ -27,6 +27,42 @@ const ipc = { return ipcRenderer.send(IPC_MESSAGES.RELOAD_MAIN_WINDOW); }, + minimizeWindow() { + return ipcRenderer.send(IPC_MESSAGES.MINIMIZE_MAIN_WINDOW); + }, + + toggleMaximize() { + return ipcRenderer.send(IPC_MESSAGES.MAXIMIZE_MAIN_WINDOW); + }, + + isMaximized() { + return new Promise((resolve, reject) => { + ipcRenderer.send(IPC_MESSAGES.ISMAXIMIZED_MAIN_WINDOW); + ipcRenderer.once( + IPC_MESSAGES.ISMAXIMIZED_RESULT, + (_event, isMaximized) => { + resolve(isMaximized); + } + ); + }); + }, + + isFullscreen() { + return new Promise((resolve, reject) => { + ipcRenderer.send(IPC_MESSAGES.ISFULLSCREEN_MAIN_WINDOW); + ipcRenderer.once( + IPC_MESSAGES.ISFULLSCREEN_RESULT, + (_event, isFullscreen) => { + resolve(isFullscreen); + } + ); + }); + }, + + closeWindow() { + return ipcRenderer.send(IPC_MESSAGES.CLOSE_MAIN_WINDOW); + }, + async getCreds() { return (await ipcRenderer.invoke(IPC_ACTIONS.GET_CREDS)) as Creds; }, diff --git a/main/registerIpcMainMessageListeners.ts b/main/registerIpcMainMessageListeners.ts index 65c42039..4c3fbeec 100644 --- a/main/registerIpcMainMessageListeners.ts +++ b/main/registerIpcMainMessageListeners.ts @@ -20,6 +20,28 @@ export default function registerIpcMainMessageListeners(main: Main) { ipcMain.on(IPC_MESSAGES.RELOAD_MAIN_WINDOW, () => { main.mainWindow!.reload(); }); + + ipcMain.on(IPC_MESSAGES.MINIMIZE_MAIN_WINDOW, () => { + main.mainWindow!.minimize(); + }); + + ipcMain.on(IPC_MESSAGES.MAXIMIZE_MAIN_WINDOW, () => { + main.mainWindow!.isMaximized()?main.mainWindow!.unmaximize():main.mainWindow!.maximize() + }); + + ipcMain.on(IPC_MESSAGES.ISMAXIMIZED_MAIN_WINDOW, (event) => { + const isMaximized = main.mainWindow?.isMaximized() ?? false; + event.sender.send(IPC_MESSAGES.ISMAXIMIZED_RESULT, isMaximized); + }); + + ipcMain.on(IPC_MESSAGES.ISFULLSCREEN_MAIN_WINDOW, (event) => { + const isFullscreen = main.mainWindow?.isFullScreen() ?? false; + event.sender.send(IPC_MESSAGES.ISFULLSCREEN_RESULT, isFullscreen); + }); + + ipcMain.on(IPC_MESSAGES.CLOSE_MAIN_WINDOW, () => { + main.mainWindow!.close(); + }); ipcMain.on(IPC_MESSAGES.OPEN_EXTERNAL, (_, link: string) => { shell.openExternal(link).catch((err) => emitMainProcessError(err)); diff --git a/src/components/WindowsTitleBar.vue b/src/components/WindowsTitleBar.vue index e9705c28..d7d5b5f9 100644 --- a/src/components/WindowsTitleBar.vue +++ b/src/components/WindowsTitleBar.vue @@ -1,12 +1,61 @@ @@ -20,5 +69,59 @@ export default { dbPath: String, companyName: String, }, + data() { + return { + isMax: Boolean, + isFullscreen: Boolean, + }; + }, + mounted() { + this.getIsMaximized(); + this.getIsFullscreen(); + window.addEventListener('resize', this.getIsFullscreen); + document.addEventListener('webkitfullscreenchange', this.getIsFullscreen); + document.addEventListener('mozfullscreenchange', this.getIsFullscreen); + document.addEventListener('fullscreenchange', this.getIsFullscreen); + document.addEventListener('MSFullscreenChange', this.getIsFullscreen); + }, + destroyed() { + window.removeEventListener('resize', this.getIsFullscreen); + document.removeEventListener('webkitfullscreenchange', this.getIsFullscreen); + document.removeEventListener('mozfullscreenchange', this.getIsFullscreen); + document.removeEventListener('fullscreenchange', this.getIsFullscreen); + document.removeEventListener('MSFullscreenChange', this.getIsFullscreen); + }, + methods: { + minimizeWindow() { + ipc.minimizeWindow(); + }, + toggleMaximize() { + ipc.toggleMaximize(); + this.getIsMaximized(); + }, + closeWindow() { + ipc.closeWindow(); + }, + getIsMaximized() { + ipc + .isMaximized() + .then((result) => { + this.isMax = result; + }) + .catch((error) => { + console.error(error); + }); + }, + getIsFullscreen() { + ipc + .isFullscreen() + .then((result) => { + this.isFullscreen = result; + }) + .catch((error) => { + console.error(error); + }); + }, + }, }; diff --git a/utils/messages.ts b/utils/messages.ts index d470556d..601a94ce 100644 --- a/utils/messages.ts +++ b/utils/messages.ts @@ -5,6 +5,13 @@ export enum IPC_MESSAGES { OPEN_EXTERNAL = 'open-external', SHOW_ITEM_IN_FOLDER = 'show-item-in-folder', RELOAD_MAIN_WINDOW = 'reload-main-window', + MINIMIZE_MAIN_WINDOW = 'minimize-main-window', + MAXIMIZE_MAIN_WINDOW = 'maximize-main-window', + ISMAXIMIZED_MAIN_WINDOW = 'ismaximized-main-window', + ISMAXIMIZED_RESULT = 'ismaximized-result', + ISFULLSCREEN_MAIN_WINDOW = 'isfullscreen-main-window', + ISFULLSCREEN_RESULT = 'isfullscreen-result', + CLOSE_MAIN_WINDOW = 'close-main-window', } // ipcRenderer.invoke(...)