跳到主内容

在 BrowserWindow 中表示文件

概述

在 macOS 上,你可以为应用程序中的任何窗口设置一个表示文件。表示文件的图标将显示在标题栏中,当用户按下 Command-ClickControl-Click 时,将显示一个包含文件路径的弹出窗口。

Represented File

注意:上面的截图是 Atom 文本编辑器中使用此功能来指示当前打开文件的示例。

你还可以设置窗口的已编辑状态,以便文件图标可以指示此窗口中的文档是否已修改。

要设置窗口的表示文件,可以使用 BrowserWindow.setRepresentedFilenameBrowserWindow.setDocumentEdited API。

示例

const { app, BrowserWindow } = require('electron/main')
const os = require('node:os')

function createWindow () {
const win = new BrowserWindow({
width: 800,
height: 600
})

win.setRepresentedFilename(os.homedir())
win.setDocumentEdited(true)

win.loadFile('index.html')
}

app.whenReady().then(() => {
createWindow()

app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow()
}
})
})

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})

启动 Electron 应用程序后,按住 CommandControl 键点击标题。您应该会在顶部看到一个带有表示文件的弹出窗口。在本指南中,这是当前用户的主目录。

Represented file