There are a number of static methods in the browserWindow class in electronjs, one of which is a static method that will create and return a collection of all browser windows currently open. This post will then be a quick example of the use of this static method of the browserWindow class. While I am at it I will of course be touching base on a bunch of additional features in electronjs in general, such as the preload script, and various events for the browser window class. Mainly the close and ready to show events of the Browser window class that I will be using to update a simple message in each browser window when a new window is opened or closed. So if you are still fairly new to electronjs as well this might prove to be a good exercise in order to gain some insight to various features that have to do with a collection of browser windows.
If you do have at least some experience with electronjs and all additional skills that are required before hand, you might get something of value from reading this. With many projects that are made with electron there might be a need to have more than one window open, and when that is the case one might need to loop over all open windows to update things that have to do with the state of each window. This example aims to be a basic starting point for this sort of thing.
The full source code for this example is on my examples electronjs repository. I might end up making some chances to the source, but not get around to editing this content just yet, or in any case I will have at least some notes as to what the plain is for any changes. This is also where I have the source code examples for all my other posts on electronjs so it might be worth checking out.
I want to require in the Menu class because the general idea that I have for this example is to just have a simple file menu that can be used to just quit the whole application, or create a new window. Each time a new window is created, or closed, this will trigger one of two events that can then be used as a way to update the state of the content in any and all reaming windows. With that said I will need a custom Menu for this example, so if you still have not got into hot to go about dong that this example might prove to be a good starting point for that sort of thing as well.
After setting up the custom menu for this example I have a helper function that I am using to create event handers for the two events that will fire when a window starts and when it closes. These two events will provide arguments that contain the relevant data that I want for each window when calling the client side callback function that I will be getting to in my preload script, and index html file later in this post. One of the arguments will be a collection of objects for each window that is currently open, and for this example this will just be the id for each window, and then the length of the array is what I can use to know how many windows are open.
The goal that I had in mind was to just have a simple starting point example for this sort of thing when it comes to working on an electronjs application. With that said I would say that I have achieved what it was that I set out to do with this, and I can not thing of much of anything that I would add to this in terms of features at least. So I am thinking that any additional changes to this example in the future will just be code readability improvements and things that address various other simple changes like using promises rather than callbacks and things to that effect.
There is the question of expanding on this when it comes to making some kind of actual project with electronjs in which I would want to have more than one window open for one reason or another, but that would all be a matter for a whole other post on electronjs.