- app·续2
- app.getApplicationNameForProtocol(url)
- app.setUserTasks(tasks) Windows
- app.getJumpListSettings() Windows
- app.setJumpList(categories) Windows
- app.requestSingleInstanceLock()
- app.hasSingleInstanceLock()
- app.releaseSingleInstanceLock()
- app.setUserActivity(type, userInfo[, webpageURL]) macOS
- app.getCurrentActivityType() macOS
- app.invalidateCurrentActivity() macOS
- app.resignCurrentActivity() macOS
- app.updateCurrentActivity(type, userInfo) macOS
- app.setAppUserModelId(id) Windows
- app.importCertificate(options, callback) Linux
- app.disableHardwareAcceleration()
- app.disableDomainBlockingFor3DAPIs()
- app.getAppMetrics()
- app.getGPUFeatureStatus()
- app.getGPUInfo(infoType)
- app.setBadgeCount(count) Linux macOS
app·续2
注意: 在macOS上, 您可以使用此方法检查应用程序是否已注册为协议的默认协议处理程序。 同时可以通过查看 ~/Library/Preferences/com.apple.LaunchServices.plist 来确认。 有关详细信息,请参阅 Apple’s documentation
The API uses the Windows Registry and LSCopyDefaultHandlerForURLScheme internally.
app.getApplicationNameForProtocol(url)
- url String - a URL with the protocol name to check. Unlike the other methods in this family, this accepts an entire URL, including :// at a minimum (e.g. https://).
Returns String - Name of the application handling the protocol, or an empty string if there is no handler. For instance, if Electron is the default handler of the URL, this could be Electron on Windows and Mac. However, don’t rely on the precise format which is not guaranteed to remain unchanged. Expect a different format on Linux, possibly with a .desktop suffix.
This method returns the application name of the default handler for the protocol (aka URI scheme) of a URL.
app.setUserTasks(tasks) Windows
- tasks Task[] - 由 Task 对象组成的数组
Adds tasks to the Tasks.aspx#tasks) category of the Jump List on Windows.
tasks 是 Task 对象组成的数组
返回 Boolean-是否成功调用。
注意: 如果您想自定义跳转列表, 请使用 aapp.setJumpList(categories) 来代替。
app.getJumpListSettings() Windows
返回 Object:
-
minItems Integer - 将在跳转列表中显示项目的最小数量(有关此值的更详细描述,请参阅 MSDN docs.aspx)).
-
removedItems JumpListItem[] - Array of JumpListItem objects that correspond to items that the user has explicitly removed from custom categories in the Jump List. 这些项目不能在 next 调用 app.setJumpList() 时重新添加到跳转列表中, Windows不会显示任何包含已删除项目的自定义类别.
app.setJumpList(categories) Windows
- categories JumpListCategory[] | null - Array of JumpListCategory objects.
设置或删除应用程序的自定义跳转列表,并返回以下字符串之一:
-
ok - 没有出现错误
-
error - 发生一个或多个错误,启用运行日志记录找出可能的原因。
-
invalidSeparatorError - An attempt was made to add a separator to a custom category in the Jump List. Separators are only allowed in the standard Tasks category.
-
fileTypeRegistrationError -尝试向自定义跳转列表添加一个文件链接,但是该应用未注册处理该应用类型
-
customCategoryAccessDeniedError - 由于用户隐私或策略组设置,自定义类别无法添加到跳转列表。
如果 categories 的值为 null, 之前设定的自定义跳转列表(如果存在) 将被替换为标准的应用跳转列表(由windows生成)
注意: 如果 JumpListCategory 对象既没有 type, 也没有 name 属性设置, 则其 type 被假定为 tasks。 如果设置了 name 属性, 但省略了 type 属性, 则假定 type 为 custom。
Note: Users can remove items from custom categories, and Windows will not allow a removed item to be added back into a custom category until after the next successful call to app.setJumpList(categories). 尝试提前将删除的项目重新添加 到自定义类别中,将导致整个自定义类别被隐藏。 删除的项目可以使用 app.getJumpListSettings() 获取。
下面是创建自定义跳转列表的一个非常简单的示例:
const { app } = require('electron')app.setJumpList([ { type: 'custom', name: 'Recent Projects', items: [ { type: 'file', path: 'C:\\Projects\\project1.proj' }, { type: 'file', path: 'C:\\Projects\\project2.proj' } ] }, { // 已经有一个名字所以 `type` 被认为是 "custom" name: 'Tools', items: [ { type: 'task', title: 'Tool A', program: process.execPath, args: '--run-tool-a', icon: process.execPath, iconIndex: 0, description: 'Runs Tool A' }, { type: 'task', title: 'Tool B', program: process.execPath, args: '--run-tool-b', icon: process.execPath, iconIndex: 0, description: 'Runs Tool B' } ] }, { type: 'frequent' }, { //这里没有设置名字 所以 `type` 被认为是 "tasks" items: [ { type: 'task', title: 'New Project', program: process.execPath, args: '--new-project', description: 'Create a new project.' }, { type: 'separator' }, { type: 'task', title: 'Recover Project', program: process.execPath, args: '--recover-project', description: 'Recover Project' } ] }])
app.requestSingleInstanceLock()
返回 Boolean
此方法的返回值表示你的应用程序实例是否成功取得了锁。 如果它取得锁失败,你可以假设另一个应用实例已经取得了锁并且仍旧在运行,并立即退出。
例如:如果你的程序是应用的主要实例并且当这个方法返回 true时,你应该继续让你的程序运行。 如果当它返回 false如果你的程序没有取得锁,它应该立刻退出,并且将参数发送给那个已经取到锁的进程。
在 macOS 上, 当用户尝试在 Finder 中打开您的应用程序的第二个实例时, 系统会通过发出 open-file 和 open-url 事件来自动强制执行单个实例,。 但是当用户在命令行中启动应用程序时, 系统的单实例机制将被绕过, 您必须手动调用此方法来确保单实例。
在第二个实例启动时激活主实例窗口的示例:
const { app } = require('electron')let myWindow = nullconst gotTheLock = app.requestSingleInstanceLock()if (!gotTheLock) { app.quit()} else { app.on('second-instance', (event, commandLine, workingDirectory) => { // 当运行第二个实例时,将会聚焦到myWindow这个窗口 if (myWindow) { if (myWindow.isMinimized()) myWindow.restore() myWindow.focus() } }) // 创建 myWindow, 加载应用的其余部分, etc... app.on('ready', () => { })}
app.hasSingleInstanceLock()
返回 Boolean
此方法返回你的应用实例当前是否持有单例锁。 你可以通过 app.requestSingleInstanceLock()请求锁,并且通过 app.releaseSingleInstanceLock() 释放锁。
app.releaseSingleInstanceLock()
Releases all locks that were created by requestSingleInstanceLock. This will allow multiple instances of the application to once again run side by side.
app.setUserActivity(type, userInfo[, webpageURL]) macOS
-
type String - 活动的唯一标识。 映射到 NSUserActivity. activityType。
-
userInfo any - App-specific state to store for use by another device.
-
webpageURL String (optional) - The webpage to load in a browser if no suitable app is installed on the resuming device. The scheme must be http or https.
创建一个 NSUserActivity 并将其设置为当前活动。 该活动之后可以Handoff到另一个设备。
app.getCurrentActivityType() macOS
返回 String - 正在运行的 activity 的类型
app.invalidateCurrentActivity() macOS
使当前的Handoff用户活动无效。
app.resignCurrentActivity() macOS
Marks the current Handoff user activity as inactive without invalidating it.
app.updateCurrentActivity(type, userInfo) macOS
-
type String - 活动的唯一标识。 映射到 NSUserActivity. activityType。
-
userInfo any - App-specific state to store for use by another device.
当其类型与 type 匹配时更新当前活动, 将项目从 用户信息 合并到其当前 用户信息 字典中。
app.setAppUserModelId(id) Windows
- id String
改变当前应用的 Application User Model ID.aspx) 为 id.
app.importCertificate(options, callback) Linux
-
options Object
-
certificate String - pkcs12 文件的路径
-
password String - 证书的密码
-
callback Function
-
result Integer - 导入结果
将 pkcs12 格式的证书导入到平台证书库。 使用导入操作的 callback 调用返回 result ,值 0 表示成功,而任何其他值表示失败,根据Chromium net_error_list 。
app.disableHardwareAcceleration()
禁用当前应用程序的硬件加速。
这个方法只能在应用程序准备就绪(ready)之前调用。
app.disableDomainBlockingFor3DAPIs()
By default, Chromium disables 3D APIs (e.g. WebGL) until restart on a per domain basis if the GPU processes crashes too frequently. This function disables that behaviour.
这个方法只能在应用程序准备就绪(ready)之前调用。
app.getAppMetrics()
Returns ProcessMetric[]: Array of ProcessMetric objects that correspond to memory and CPU usage statistics of all the processes associated with the app.
app.getGPUFeatureStatus()
返回 GPUFeatureStatus-来自 chrome://gpu/ 的图形功能状态。
Note: This information is only usable after the gpu-info-update event is emitted.
app.getGPUInfo(infoType)
- infoType String - Can be basic or complete.
Returns Promise<unknown>
For infoType equal to complete: Promise is fulfilled with Object containing all the GPU Information as in chromium’s GPUInfo object. 这包括 chrome://gpu 页面上显示的版本和驱动程序信息。
对于infoType等于basic: Promise 至少包含当请求complete时的属性Object。 下面是一个基础响应示例:
{ auxAttributes: { amdSwitchable: true, canSupportThreadedTextureMailbox: false, directComposition: false, directRendering: true, glResetNotificationStrategy: 0, inProcessGpu: true, initializationTime: 0, jpegDecodeAcceleratorSupported: false, optimus: false, passthroughCmdDecoder: false, sandboxed: false, softwareRendering: false, supportsOverlays: false, videoDecodeAcceleratorFlags: 0 },gpuDevice: [ { active: true, deviceId: 26657, vendorId: 4098 }, { active: false, deviceId: 3366, vendorId: 32902 } ],machineModelName: 'MacBookPro',machineModelVersion: '11.5' }
如果只需要基本信息,如vendorId或driverId,则应优先使用basic。
app.setBadgeCount(count) Linux macOS
- count Integer
返回 Boolean-是否成功调用。
设置当前应用程序的计数器标记. 将计数设置为 0 将隐藏该标记。
