add window state management to store
This commit is contained in:
parent
2dee81fb36
commit
f1a5cad4db
3 changed files with 29 additions and 15 deletions
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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({
|
||||
|
|
|
|||
27
src/store.ts
27
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");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 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");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue