feature: notify newMessage if window is blurred or closed
This commit is contained in:
parent
95e73f7c28
commit
515f67a120
7 changed files with 34 additions and 18 deletions
|
|
@ -9,7 +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';
|
||||
import {setWindowOpen, getWindowOpen } from './store';
|
||||
|
||||
console.log('Starting Crimata electron app.');
|
||||
|
||||
|
|
@ -22,7 +22,7 @@ const isDev = require('electron-is-dev');
|
|||
|
||||
// Listen for window creation.
|
||||
backgroundMitt.on('window-active', (state: boolean) => {
|
||||
setWindow(state)
|
||||
setWindowOpen(state)
|
||||
});
|
||||
|
||||
/* Start main process on ready */
|
||||
|
|
@ -40,7 +40,7 @@ app.on("window-all-closed", () => {
|
|||
|
||||
// When user clicks app icon (re-open)
|
||||
app.on("activate", () => {
|
||||
if (!getWindow()) createWindow();
|
||||
if (!getWindowOpen()) createWindow();
|
||||
});
|
||||
|
||||
// Exit cleanly on request from parent process in development mode.
|
||||
|
|
|
|||
|
|
@ -1,12 +1,13 @@
|
|||
|
||||
import { IpcListener } from "@/composables/useIpcMain"
|
||||
import { sendMessage } from '@/session';
|
||||
import { updateAppState } from "@/account";
|
||||
import { onNavBar } from "@/window";
|
||||
import { setWindowFocus } from '@/store';
|
||||
|
||||
const CLIENT_MESSAGE_CHANNEL = "post-session-send"
|
||||
const APP_MOUNT_CHANNEL = "post-app-mount";
|
||||
const NAV_BAR_CHANNEL = "post-nav-bar";
|
||||
const POST_WINDOW_FOCUS = 'post-window-focus';
|
||||
|
||||
export const messageListener = new IpcListener<Raw | Request>({
|
||||
channel: CLIENT_MESSAGE_CHANNEL,
|
||||
|
|
@ -22,3 +23,8 @@ export const navBarListener = new IpcListener({
|
|||
channel: NAV_BAR_CHANNEL,
|
||||
listenerCallback: onNavBar
|
||||
});
|
||||
|
||||
export const windowFocusListener = new IpcListener<FocusPayload>({
|
||||
channel: POST_WINDOW_FOCUS,
|
||||
listenerCallback: ({ isFocused }) => setWindowFocus(isFocused)
|
||||
});
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ import ConnectionStatus from "./connectionStatus.vue";
|
|||
import { profile } from "@/render/shared/profile";
|
||||
import { messages } from "@/render/shared/messages";
|
||||
import useScroll from "@/render/composables/useScroll";
|
||||
import { postMessage } from "@/render/ipc";
|
||||
import { postMessage, postWindowFocus } from "@/render/ipc";
|
||||
|
||||
export default defineComponent({
|
||||
name: "Messenger",
|
||||
|
|
@ -46,6 +46,7 @@ export default defineComponent({
|
|||
setup() {
|
||||
|
||||
const onFocus = () => {
|
||||
postWindowFocus({ isFocused: true });
|
||||
postMessage({
|
||||
intent: "focus",
|
||||
params: { "focus": true },
|
||||
|
|
@ -55,6 +56,7 @@ export default defineComponent({
|
|||
}
|
||||
|
||||
const onBlur = () => {
|
||||
postWindowFocus({ isFocused: false });
|
||||
postMessage({
|
||||
intent: "focus",
|
||||
params: { "focus": false },
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import { config } from "@/config";
|
|||
import useWebsockets from "./composables/useWebsockets";
|
||||
import { backgroundMitt, ipcEmit } from "@/composables/useEmitter";
|
||||
import { Notification } from 'electron';
|
||||
import { getWindow } from './store';
|
||||
import { getWindowFocus, getWindowOpen } from './store';
|
||||
|
||||
function showNotification (options: {
|
||||
title: string;
|
||||
|
|
@ -40,8 +40,10 @@ const onMessageCallback = (payload: string) => {
|
|||
const body = message.body as Update;
|
||||
uiState.update(body);
|
||||
|
||||
const isFocused = getWindowFocus();
|
||||
const isOpen = getWindowOpen();
|
||||
// show notification if window is not active
|
||||
if (!getWindow()) {
|
||||
if (!isFocused || !isOpen) {
|
||||
if (body.name === "add") {
|
||||
const data = body.data as Message;
|
||||
showNotification({
|
||||
|
|
@ -77,7 +79,7 @@ export const updateAppUI = (): void => {
|
|||
uiState.emit();
|
||||
}
|
||||
|
||||
export function sendMessage<Message>(message: Raw | Request): void {
|
||||
export function sendMessage(message: Raw | Request): void {
|
||||
send(message);
|
||||
}
|
||||
|
||||
|
|
|
|||
20
src/store.ts
20
src/store.ts
|
|
@ -4,9 +4,6 @@ const schema = {
|
|||
token: {
|
||||
type: 'string',
|
||||
},
|
||||
window: {
|
||||
type: 'boolean'
|
||||
}
|
||||
};
|
||||
|
||||
const store = new Store({
|
||||
|
|
@ -32,14 +29,19 @@ export const clearToken = (): void => {
|
|||
/*
|
||||
* Window state management
|
||||
* */
|
||||
export const setWindow = (state: boolean): void => {
|
||||
store.set("window", state);
|
||||
export const setWindowOpen = (state: boolean): void => {
|
||||
store.set("window.isOpen", state);
|
||||
}
|
||||
|
||||
export const getWindow = (): boolean | undefined => {
|
||||
return store.get("window");
|
||||
export const getWindowOpen = (): boolean | undefined => {
|
||||
return store.get("window.isOpen");
|
||||
}
|
||||
|
||||
export const clearWindow = (): void => {
|
||||
store.delete("window");
|
||||
export const setWindowFocus = (state: boolean): void => {
|
||||
store.set("window.isFocused", state);
|
||||
}
|
||||
|
||||
export const getWindowFocus = (): boolean | undefined => {
|
||||
return store.get("window.isFocused");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -125,3 +125,7 @@ interface IpcRendererEvent<T> {
|
|||
payload: T | null;
|
||||
}
|
||||
|
||||
interface FocusPayload {
|
||||
isFocused: boolean;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue