migrate app focus logic to events sent from node

pull/2242/head
Audric Ackermann 3 years ago
parent 1ebff6b3ae
commit b4dc18d65b
No known key found for this signature in database
GPG Key ID: 999F434D76324AD4

@ -1,36 +1,25 @@
import { app } from 'electron';
import { useCallback, useEffect } from 'react';
import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { isElectronWindowFocused } from '../session/utils/WindowUtils';
import { setIsAppFocused } from '../state/ducks/section';
import { getIsAppFocused } from '../state/selectors/section';
import { ipcRenderer } from 'electron';
/**
* This custom hook should be called on the top of the app only once.
* It sets up a listener for events from main_node.ts and update the global redux state with the focused state.
*/
export function useAppIsFocused() {
const dispatch = useDispatch();
const isFocused = useSelector(getIsAppFocused);
useEffect(() => {
dispatch(setIsAppFocused(isElectronWindowFocused()));
}, []);
const onFocusCallback = useCallback((_event, win) => {
if (win.webContents.id === 1) {
dispatch(setIsAppFocused(true));
}
}, []);
const onBlurCallback = useCallback((_event, win) => {
if (win.webContents.id === 1) {
dispatch(setIsAppFocused(false));
}
}, []);
const ipcCallback = (_event: unknown, isFocused: unknown) => {
dispatch(setIsAppFocused(Boolean(isFocused)));
};
useEffect(() => {
// app.on('browser-window-focus', onFocusCallback);
// app.on('browser-window-blur', onBlurCallback);
ipcRenderer.on('set-window-focus', ipcCallback);
return () => {
// app.removeListener('browser-window-blur', onBlurCallback);
// app.removeListener('browser-window-focus', onFocusCallback);
ipcRenderer.removeListener('set-window-focus', ipcCallback);
};
});

@ -311,6 +311,18 @@ async function createWindow() {
mainWindow = new BrowserWindow(windowOptions);
setupSpellChecker(mainWindow, locale.messages);
const setWindowFocus = () => {
if (!mainWindow) {
return;
}
mainWindow.webContents.send('set-window-focus', mainWindow.isFocused());
};
mainWindow.on('focus', setWindowFocus);
mainWindow.on('blur', setWindowFocus);
mainWindow.once('ready-to-show', setWindowFocus);
// This is a fallback in case we drop an event for some reason.
global.setInterval(setWindowFocus, 5000);
electronLocalshortcut.register(mainWindow, 'F5', () => {
if (!mainWindow) {
return;

@ -1,9 +0,0 @@
import { BrowserWindow } from 'electron';
export function isElectronWindowFocused() {
// const [yourBrowserWindow] = BrowserWindow.getAllWindows();
// const isFocused = yourBrowserWindow?.isFocused() || false;
// throw new Error('TOFIX');
return false;
return isFocused;
}
Loading…
Cancel
Save