メインコンテンツへスキップ

BrowserWindow でのファイルの表現

概要

macOS では、アプリケーション内の任意のウィンドウに対して、表現されるファイルを設定できます。表現されるファイルのアイコンはタイトルバーに表示され、ユーザーが Command-Click または Control-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 アプリケーションを起動した後、Command または Control キーを押しながらタイトルをクリックします。一番上に、表現されるファイルが表示されたポップアップが表示されるはずです。このガイドでは、これは現在のユーザーのホームディレクトリです。

Represented file