feature: notify newMessage if window is blurred or closed

This commit is contained in:
Enrique H. Oliva 2021-07-30 08:52:43 -05:00
commit 515f67a120
7 changed files with 34 additions and 18 deletions

View file

@ -9,7 +9,7 @@ import { app, protocol } from "electron";
import createWindow from "./window"; import createWindow from "./window";
import main from "./main"; import main from "./main";
import { backgroundMitt } from '@/composables/useEmitter'; import { backgroundMitt } from '@/composables/useEmitter';
import {setWindow, getWindow } from './store'; import {setWindowOpen, getWindowOpen } from './store';
console.log('Starting Crimata electron app.'); console.log('Starting Crimata electron app.');
@ -22,7 +22,7 @@ const isDev = require('electron-is-dev');
// Listen for window creation. // Listen for window creation.
backgroundMitt.on('window-active', (state: boolean) => { backgroundMitt.on('window-active', (state: boolean) => {
setWindow(state) setWindowOpen(state)
}); });
/* Start main process on ready */ /* Start main process on ready */
@ -40,7 +40,7 @@ app.on("window-all-closed", () => {
// When user clicks app icon (re-open) // When user clicks app icon (re-open)
app.on("activate", () => { app.on("activate", () => {
if (!getWindow()) createWindow(); if (!getWindowOpen()) createWindow();
}); });
// Exit cleanly on request from parent process in development mode. // Exit cleanly on request from parent process in development mode.

View file

@ -1,12 +1,13 @@
import { IpcListener } from "@/composables/useIpcMain" import { IpcListener } from "@/composables/useIpcMain"
import { sendMessage } from '@/session'; import { sendMessage } from '@/session';
import { updateAppState } from "@/account"; import { updateAppState } from "@/account";
import { onNavBar } from "@/window"; import { onNavBar } from "@/window";
import { setWindowFocus } from '@/store';
const CLIENT_MESSAGE_CHANNEL = "post-session-send" const CLIENT_MESSAGE_CHANNEL = "post-session-send"
const APP_MOUNT_CHANNEL = "post-app-mount"; const APP_MOUNT_CHANNEL = "post-app-mount";
const NAV_BAR_CHANNEL = "post-nav-bar"; const NAV_BAR_CHANNEL = "post-nav-bar";
const POST_WINDOW_FOCUS = 'post-window-focus';
export const messageListener = new IpcListener<Raw | Request>({ export const messageListener = new IpcListener<Raw | Request>({
channel: CLIENT_MESSAGE_CHANNEL, channel: CLIENT_MESSAGE_CHANNEL,
@ -22,3 +23,8 @@ export const navBarListener = new IpcListener({
channel: NAV_BAR_CHANNEL, channel: NAV_BAR_CHANNEL,
listenerCallback: onNavBar listenerCallback: onNavBar
}); });
export const windowFocusListener = new IpcListener<FocusPayload>({
channel: POST_WINDOW_FOCUS,
listenerCallback: ({ isFocused }) => setWindowFocus(isFocused)
});

View file

@ -31,7 +31,7 @@ import ConnectionStatus from "./connectionStatus.vue";
import { profile } from "@/render/shared/profile"; import { profile } from "@/render/shared/profile";
import { messages } from "@/render/shared/messages"; import { messages } from "@/render/shared/messages";
import useScroll from "@/render/composables/useScroll"; import useScroll from "@/render/composables/useScroll";
import { postMessage } from "@/render/ipc"; import { postMessage, postWindowFocus } from "@/render/ipc";
export default defineComponent({ export default defineComponent({
name: "Messenger", name: "Messenger",
@ -46,6 +46,7 @@ export default defineComponent({
setup() { setup() {
const onFocus = () => { const onFocus = () => {
postWindowFocus({ isFocused: true });
postMessage({ postMessage({
intent: "focus", intent: "focus",
params: { "focus": true }, params: { "focus": true },
@ -55,6 +56,7 @@ export default defineComponent({
} }
const onBlur = () => { const onBlur = () => {
postWindowFocus({ isFocused: false });
postMessage({ postMessage({
intent: "focus", intent: "focus",
params: { "focus": false }, params: { "focus": false },

View file

@ -65,7 +65,7 @@ export const postNavBar = (payload: string): void => (
* *
*/ */
export const postWindowBlur = (payload: { isFocused: boolean }): void => ( export const postWindowFocus = (payload: { isFocused: boolean }): void => (
post('post-window-focus', payload) post('post-window-focus', payload)
); );

View file

@ -5,7 +5,7 @@ import { config } from "@/config";
import useWebsockets from "./composables/useWebsockets"; import useWebsockets from "./composables/useWebsockets";
import { backgroundMitt, ipcEmit } from "@/composables/useEmitter"; import { backgroundMitt, ipcEmit } from "@/composables/useEmitter";
import { Notification } from 'electron'; import { Notification } from 'electron';
import { getWindow } from './store'; import { getWindowFocus, getWindowOpen } from './store';
function showNotification (options: { function showNotification (options: {
title: string; title: string;
@ -40,8 +40,10 @@ const onMessageCallback = (payload: string) => {
const body = message.body as Update; const body = message.body as Update;
uiState.update(body); uiState.update(body);
const isFocused = getWindowFocus();
const isOpen = getWindowOpen();
// show notification if window is not active // show notification if window is not active
if (!getWindow()) { if (!isFocused || !isOpen) {
if (body.name === "add") { if (body.name === "add") {
const data = body.data as Message; const data = body.data as Message;
showNotification({ showNotification({
@ -77,7 +79,7 @@ export const updateAppUI = (): void => {
uiState.emit(); uiState.emit();
} }
export function sendMessage<Message>(message: Raw | Request): void { export function sendMessage(message: Raw | Request): void {
send(message); send(message);
} }

View file

@ -4,9 +4,6 @@ const schema = {
token: { token: {
type: 'string', type: 'string',
}, },
window: {
type: 'boolean'
}
}; };
const store = new Store({ const store = new Store({
@ -32,14 +29,19 @@ export const clearToken = (): void => {
/* /*
* Window state management * Window state management
* */ * */
export const setWindow = (state: boolean): void => { export const setWindowOpen = (state: boolean): void => {
store.set("window", state); store.set("window.isOpen", state);
} }
export const getWindow = (): boolean | undefined => { export const getWindowOpen = (): boolean | undefined => {
return store.get("window"); return store.get("window.isOpen");
} }
export const clearWindow = (): void => { export const setWindowFocus = (state: boolean): void => {
store.delete("window"); store.set("window.isFocused", state);
} }
export const getWindowFocus = (): boolean | undefined => {
return store.get("window.isFocused");
}

View file

@ -125,3 +125,7 @@ interface IpcRendererEvent<T> {
payload: T | null; payload: T | null;
} }
interface FocusPayload {
isFocused: boolean;
}