如何确定电子应用程序是否处于前台? [英] How to find if electron app is in foreground?

查看:90
本文介绍了如何确定电子应用程序是否处于前台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,我只希望在电子应用程序处于前台时才在其中执行操作。

I have a requirement where I want to perform an action inside the electron app only when it is in foreground.

这是一个电子反应应用程序。在安装组件时,我想安排一个定期任务,该任务仅在应用程序处于焦点或被用户使用时运行。并在应用程序进入后台时暂停任务。

It is an electron-react application. On mounting of a component, I want to schedule a periodic task which only runs when the app is in focus or is being used by the user. And pause the task when the app goes in background.

我们如何检测电子应用程序处于前台状态?

How can we detect the Electron app being in foreground?

推荐答案

您可以使用 <$ c $ BrowserWindow 中的c> isFocused 方法。要获得自己的BrowserWindow,可以执行以下操作:

You can use the isFocused method from BrowserWindow. To get your own BrowserWindow, you can do this :

remote.BrowserWindow.getAllWindows();

这将返回您应用程序的所有窗口。因此,要获得第一个窗口/主窗口,您可以像下面这样解构数组:

This will return all your app's windows. So to get the first / primary window, you could deconstruct the array like this :

const [yourBrowserWindow] = remote.BrowserWindow.getAllWindows();
console.log(yourBrowserWindow.isFocused());

这篇关于如何确定电子应用程序是否处于前台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆