BrowserWindow でのファイルの表現
概要
macOS では、アプリケーション内の任意のウィンドウに対して、表現されるファイルを設定できます。表現されるファイルのアイコンはタイトルバーに表示され、ユーザーが Command-Click
または Control-Click
を行うと、ファイルへのパスを示すポップアップが表示されます。
注: 上のスクリーンショットは、この機能が Atom テキストエディタで現在開いているファイルを示すために使用されている例です。
また、ウィンドウの編集状態を設定して、このウィンドウ内のドキュメントが変更されたかどうかをファイルアイコンで示すことができます。
ウィンドウの表現されるファイルを設定するには、BrowserWindow.setRepresentedFilename と BrowserWindow.setDocumentEdited API を使用できます。
例
- main.js
- index.html
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()
}
})
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
<link rel="stylesheet" type="text/css" href="./styles.css">
</head>
<body>
<h1>Hello World!</h1>
<p>
Click on the title with the <pre>Command</pre> or <pre>Control</pre> key pressed.
You should see a popup with the represented file at the top.
</p>
</body>
</body>
</html>
Electron アプリケーションを起動した後、Command
または Control
キーを押しながらタイトルをクリックします。一番上に、表現されるファイルが表示されたポップアップが表示されるはずです。このガイドでは、これは現在のユーザーのホームディレクトリです。