app
控制应用程序的事件生命周期。
进程:主进程
下面的这个例子将会展示如何在最后一个窗口被关闭时退出应用:
const { app } = require('electron')
app.on('window-all-closed', () => {
app.quit()
})
事件
app
对象会发出以下事件:
事件: 'will-finish-launching'
当应用程序完成基础的启动的时候被触发。 在 Windows 和 Linux 中, will-finish-launching
事件与 ready
事件是相同的; 在 macOS 中,这个事件相当于 NSApplication
中的 applicationWillFinishLaunching
提示。
绝大部分情况下,你必须在ready
事件句柄中处理所有事务。
事件: 'ready'
返回:
event
EventlaunchInfo
Record<string, any> | NotificationResponse macOS
当 Electron 完成初始化时,发出一次。 在 macOS 上,如果通过通知中心启动应用程序,launchInfo
保存 NSUserNotification
的 userInfo
或 UNNotificationResponse
的信息。 你也可以通过调用 app.isReady()
来检查该事件是否已被触发,以及通过 app.whenReady()
得到一个当Electron已初始化后fulfill 的 Promise。
Note: The ready
event is only fired after the main process has finished running the first tick of the event loop. If an Electron API needs to be called before the ready
event, ensure that it is called synchronously in the top-level context of the main process.
事件: 'window-all-closed'
当所有的窗口都被关闭时触发。
如果你没有监听此事件并且所有窗口都关闭了,默认的行为是控制退出程序;但如果你监听了此事件,你可以控制是否退出程序。 如果用户按下了 Cmd + Q
,或者开发者调用了 app.quit()
,Electron 会首先关闭所有的窗口然后触发 will-quit
事件,在这种情况下 window-all-closed
事件不会被触发。
事件:'before-quit'
返回:
event
Event
在程序关闭窗口前发信号。 调用 event.preventDefault()
将阻止终止应用程序的默认行为。
注意: 如果由 autoUpdater.quitAndInstal()
退出应用程序 ,那么在所有窗口触发 close
之后 才会触发 before-quit
并关闭所有窗口。
**注:**在 Windows 系统中,如果应用程序因系统关机/重启或用户注销而关闭,那么这个事件不会被触发。
事件: 'will-quit'
返回:
event
Event
当所有窗口被关闭后触发,同时应用程序将退出。 调用 event.preventDefault()
将阻止终止应用程序的默认行为。
关于 window-all-closed
和 will-quit
事件之间的差异, 请参见 window-all-closed
事件的说明。
**注:**在 Windows 系统中,如果应用程序因系统关机/重启或用户注销而关闭,那么这个事件不会被触发。
事件: 'quit'
返回:
event
EventexitCode
Integer
在应用程序退出时发出。
**注:**在 Windows 系统中,如果应用程序因系统关机/重启或用户注销而关闭,那么这个事件不会被触发。
事件: 'open-file' macOS
返回:
event
Eventpath
string
当用户想要在应用中打开一个文件时发出。 open-file
事件通常在应用已经打开,并且系统要再次使用该应用打开文件时发出。 open-file
也会在一个文件被拖到 dock 并且还没有运行的时候发出。 请确认在应用启动的时候(甚至在 ready
事件发出前) 就对 open-file
事件进行监听。
如果你想处理这个事件,你应该调用 event.preventDefault()
。
在 Windows 系统中,你需要解析 process.argv
(在主进程中) 来获取文件路径
事件: 'open-url' macOS
返回:
event
Eventurl
string
当用户想要在应用中打开一个 URL 时发出。 你的应用程序的 Info. plist
文件必须在 CFBundleURLTypes
这个key中定义 url scheme, 并将 NSPrincipalClass
设置为 AtomApplication
。
As with the open-file
event, be sure to register a listener for the open-url
event early in your application startup to detect if the application is being opened to handle a URL. If you register the listener in response to a ready
event, you'll miss URLs that trigger the launch of your application.
事件: 'activate' macOS
返回:
event
EventhasVisibleWindows
boolean
当应用被激活时发出。 各种操作都可以触发此事件, 例如首次启动应用程序、尝试在应用程序已运行时或单击应用程序的坞站或任务栏图标时重新激活它。
事件: 'did-become-active' macOS
返回:
event
Event
Emitted when the application becomes active. This differs from the activate
event in that did-become-active
is emitted every time the app becomes active, not only when Dock icon is clicked or application is re-launched. It is also emitted when a user switches to the app via the macOS App Switcher.
事件: 'did-rele-active' macOS
返回:
event
Event
Emitted when the app is no longer active and doesn’t have focus. This can be triggered, for example, by clicking on another application or by using the macOS App Switcher to switch to another application.
Event: 'continue-activity' macOS
返回:
event
Eventtype
string-标识活动的字符串。 Maps toNSUserActivity.activityType
.userInfo
unknown - 包含另一个设备上activity存储的应用特定状态。details
ObjectwebpageURL
string (可选) - 如果可用,可识别另一设备上活动访问的网页的 URL 的字符串。
Emitted during Handoff when an activity from a different device wants to be resumed. 如果你想处理这个事件,你应该调用 event.preventDefault()
。
只有具有支持相应的活动类型并且相同的开发团队 ID 作为启动程序时,用户行为才会进行。 所支持活动类型已在应用的 Info.plist
中的 NSUserActivityTypes
里明确定义。