From: Enrique H. Oliva Date: Wed, 28 Jul 2021 13:48:27 +0000 (-0500) Subject: add window state management to store X-Git-Tag: v2.0.1~9^2 X-Git-Url: https://andrewgundersen.net/repos?a=commitdiff_plain;h=f1a5cad4db91f8c2aa338ceb03b65a527e02b6ea;p=mime-chat add window state management to store --- diff --git a/src/init.ts b/src/init.ts index 9a0fdf6..637db63 100644 --- a/src/init.ts +++ b/src/init.ts @@ -9,6 +9,7 @@ import { app, protocol } from "electron"; import createWindow from "./window"; import main from "./main"; import { backgroundMitt } from '@/composables/useEmitter'; +import {setWindow, getWindow } from './store'; console.log('Starting Crimata electron app.'); @@ -19,11 +20,9 @@ protocol.registerSchemesAsPrivileged([ const isDev = require('electron-is-dev'); -let win: boolean; - // Listen for window creation. backgroundMitt.on('window-active', (state: boolean) => { - win = state; + setWindow(state) }); /* Start main process on ready */ @@ -41,7 +40,7 @@ app.on("window-all-closed", () => { // When user clicks app icon (re-open) app.on("activate", () => { - if (!win) createWindow(); + if (!getWindow()) createWindow(); }); // Exit cleanly on request from parent process in development mode. diff --git a/src/session.ts b/src/session.ts index 0bcd4e4..be6fdd3 100644 --- a/src/session.ts +++ b/src/session.ts @@ -5,13 +5,7 @@ import { config } from "@/config"; import useWebsockets from "./composables/useWebsockets"; import { backgroundMitt, ipcEmit } from "@/composables/useEmitter"; import { Notification } from 'electron'; - -let win: boolean; - -// Listen for window creation. -backgroundMitt.on('window-active', (state: boolean) => { - win = state; -}); +import { getWindow } from './store'; function showNotification (options: { title: string; @@ -47,7 +41,7 @@ const onMessageCallback = (payload: string) => { uiState.update(body); // show notification if window is not active - if (!win) { + if (!getWindow()) { if (body.name === "add") { const data = body.data as Message; showNotification({ diff --git a/src/store.ts b/src/store.ts index 0200681..8ac280e 100644 --- a/src/store.ts +++ b/src/store.ts @@ -3,7 +3,10 @@ const Store = require('electron-store'); const schema = { token: { type: 'string', - } + }, + window: { + type: 'boolean' + } }; const store = new Store({ @@ -11,9 +14,12 @@ const store = new Store({ encryptionKey: "super user test" }); +/* + * Token state management +* */ export const setToken = (token: string): void => { store.set("token", token); -} +} export const getToken = (): string | undefined => { return store.get("token"); @@ -21,4 +27,19 @@ export const getToken = (): string | undefined => { export const clearToken = (): void => { store.delete("token"); -} \ No newline at end of file +} + +/* + * Window state management +* */ +export const setWindow = (state: boolean): void => { + store.set("window", state); +} + +export const getWindow = (): boolean | undefined => { + return store.get("window"); +} + +export const clearWindow = (): void => { + store.delete("window"); +}