2018-02-21 18:29:17 +00:00
|
|
|
# Represented File for macOS BrowserWindows
|
2018-02-20 00:25:02 +00:00
|
|
|
|
2020-10-26 07:45:45 +00:00
|
|
|
## Overview
|
2018-02-20 00:25:02 +00:00
|
|
|
|
2020-10-26 07:45:45 +00:00
|
|
|
On macOS, you can set a represented file for any window in your application.
|
|
|
|
The represented file's icon will be shown in the title bar, and when users
|
|
|
|
`Command-Click` or `Control-Click`, a popup with a path to the file will be
|
|
|
|
shown.
|
2018-02-20 00:25:02 +00:00
|
|
|
|
|
|
|
![Represented File][represented-image]
|
|
|
|
|
2020-10-26 07:45:45 +00:00
|
|
|
> NOTE: The screenshot above is an example where this feature is used to indicate the currently opened file in the Atom text editor.
|
|
|
|
|
|
|
|
You can also set the edited state for a window so that the file icon can
|
|
|
|
indicate whether the document in this window has been modified.
|
|
|
|
|
2018-02-20 00:25:02 +00:00
|
|
|
To set the represented file of window, you can use the
|
|
|
|
[BrowserWindow.setRepresentedFilename][setrepresentedfilename] and
|
2020-10-26 07:45:45 +00:00
|
|
|
[BrowserWindow.setDocumentEdited][setdocumentedited] APIs.
|
|
|
|
|
|
|
|
## Example
|
|
|
|
|
2020-11-30 07:48:39 +00:00
|
|
|
```javascript fiddle='docs/fiddles/features/represented-file'
|
2020-10-26 07:45:45 +00:00
|
|
|
const { app, BrowserWindow } = require('electron')
|
2021-06-15 01:29:51 +00:00
|
|
|
const os = require('os');
|
|
|
|
|
2021-08-02 01:57:37 +00:00
|
|
|
const createWindow = () => {
|
2021-06-15 01:29:51 +00:00
|
|
|
const win = new BrowserWindow({
|
|
|
|
width: 800,
|
|
|
|
height: 600
|
|
|
|
})
|
|
|
|
}
|
2020-10-26 07:45:45 +00:00
|
|
|
|
|
|
|
app.whenReady().then(() => {
|
|
|
|
const win = new BrowserWindow()
|
2018-02-20 00:25:02 +00:00
|
|
|
|
2021-06-15 01:29:51 +00:00
|
|
|
win.setRepresentedFilename(os.homedir())
|
2020-10-26 07:45:45 +00:00
|
|
|
win.setDocumentEdited(true)
|
|
|
|
})
|
2021-06-15 01:29:51 +00:00
|
|
|
|
|
|
|
app.on('window-all-closed', () => {
|
|
|
|
if (process.platform !== 'darwin') {
|
|
|
|
app.quit()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
app.on('activate', () => {
|
|
|
|
if (BrowserWindow.getAllWindows().length === 0) {
|
|
|
|
createWindow()
|
|
|
|
}
|
|
|
|
})
|
2018-02-20 00:25:02 +00:00
|
|
|
```
|
|
|
|
|
2020-10-26 07:45:45 +00:00
|
|
|
After launching the Electron application, click on the title with `Command` or
|
2021-06-15 01:29:51 +00:00
|
|
|
`Control` key pressed. You should see a popup with the represented file at the top.
|
|
|
|
In this guide, this is the current user's home directory:
|
2020-10-26 07:45:45 +00:00
|
|
|
|
|
|
|
![Represented file](../images/represented-file.png)
|
|
|
|
|
2018-02-20 00:25:02 +00:00
|
|
|
[represented-image]: https://cloud.githubusercontent.com/assets/639601/5082061/670a949a-6f14-11e4-987a-9aaa04b23c1d.png
|
|
|
|
[setrepresentedfilename]: ../api/browser-window.md#winsetrepresentedfilenamefilename-macos
|
|
|
|
[setdocumentedited]: ../api/browser-window.md#winsetdocumenteditededited-macos
|