Review Request of Main & Renderer Separation with ref to Menu & Dialog modules


#1

First I’d like to say that I am aware that the separation of concerns in Electron is something that has been discussed before Electron App Database File Structure/Code Structure . I’m submitting this post out of being personally confused but also because I think clarity on the question would benefit everyone looking to begin with Electron. Also in this post I’m using main and renderer, rather than electron and browser.

My question is how to resolve control of ‘main’ modules ‘Menu’ and ‘Dialog’. This presumes creation of an app that uses the Electron desktop menu for operations such as open, save etc (not the creation of 'native menus’s Using native menus). As I’ve understood the documentation and through first hand experience these modules can only be loaded in a main process (and accessed by remote, but I’ll get to that). In many introductions to Electron I see this functionality resolved by instigating an IPC call and response between the main process and a javascript module that is loaded in the renderer’s index.html. For the example of opening a .txt file and loading it into a

on a page I have seen advice as follows (paraphrased of sorts)s:

To my mind this is a common use case which causes confusion regarding how best to separate concerns in an Electron app. As it is a common use case I am hoping it can help guide my efforts to both understand electron and eventually submit some documentation back to the community.

So my request would be some skeleton code that illustrates a best practice. I’ve made a stab of it in the above gist. I’d like to know if this is a a standard pattern for menu functions in electron: all of the code main side

AFAIK in the above instance writing a separate script for file functionality that employs the remote module doesn’t make sense as everything apart from appending text to a DOM element (MainWindow, Menu, Dialog) is handled by a main module. To my mind there also isn’t the ability to write the openFile() and saveFile() functionality as a separate file on the renderer side – if a js file requires ‘remote’ (as such a file would) it can’t be loaded via 'require ()' until a BrowserWindow exists. The docs make mention of Window.executeJavascript() as the partner functionality to remote but I don’t believe it would help in this instance.