Managing task list, recent documents, and the dock menu

In Windows and macOS, you can see the recently opened documents by right-clicking on the taskbar icon or dock menu icon. The recent document list can be added to the application dock menu icon using Electron's app module. A recent list can be displayed by simply adding a file to the recent list in the app module:

const { app } = require('electron');
app.addRecentDocument('/User/Guest/Downloads/image.png');

You can clear the recent document list using the app.clearRecentDocuments method. Adding a file path into the recent document list is very simple, as shown above. However, handling a user click event on the recent document list more important and difficult; it varies in different platforms. On macOS, clicking an item in the recent document list will emit an open-file event on app module. This event should be handled properly to open the target file in your application. The following code snippet shows you to open the file from the recent document list in MacOS: 

const { BrowserWindow, app } = require('electron');

// prevent window being garbage collected
let mainWindow;

function onClosed() {
mainWindow = null;
}

function createMainWindow() {
const win = new electron.BrowserWindow({
width: 600,
height: 400
});

win.loadURL(`file://${__dirname}/index.html`);
win.on('closed', onClosed);

app.addRecentDocument('/Users/guest/desktop/image001.png');

// Works only on mac
app.on('open-file', (event, path) => {
if (path != null) {
let ext = path.substr(path.lastIndexOf('.'));
if (['.png', '.jpg', '.gif', '.html', '.txt'].indexOf(ext) != -1) {
let browserWindow = new electron.BrowserWindow({ width: 500, height: 500 });
browserWindow.loadURL('file://' + path);
}
}
});
return win;
}

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

app.on('activate', () => {
if (!mainWindow) {
mainWindow = createMainWindow();
}
});

app.on('ready', () => {
mainWindow = createMainWindow();
});

The preceding code will give you the following output when you right-click on the dock icon. It simply adds a static image file into the recent list. When the user clicks on the menu item, the open-file event will be triggered. The file path will be passed to the event handler as its argument. You can use this path to do whatever you need to the file. Here, we matches the file extension with a set of file extension, and if it matches, open the file in a new browser window:

In order to implement the click handler inside the Windows platform, you need to associate your application with a specific file type that you want to open when you click on the recent document menu, only then the click handler will work in your Windows machine. However, this cannot be done from the application code. This is all about adding registry keys into the Windows registry. It's not practical to ask the end user to do this manually for each install. So, it should be automated with your installer or setup script. You can find detailed instructions for registering an application with a specific file type in the Microsoft website at: https://msdn.microsoft.com/en-us/library/windows/desktop/ee872121(v=vs.85).aspx:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.147.160