concept dimension in category electron

appears as: dimension, dimensions, dimensions
Cross-Platform Desktop Applications: Using Node, Electron, and NW.js

This is an excerpt from Manning's book Cross-Platform Desktop Applications: Using Node, Electron, and NW.js.

User interfaces come in a range of different dimensions; traditional IM apps like AIM or MSN Messenger (if you’re old enough to remember them) tended to have a long portrait window listing many contacts that users would want to chat with. Over time, apps like Slack and Gitter have approached the world of IM with a different window size, much in the style of a forum, and to accommodate longer messages and images (there’s a tendency in Slack communities to use a lot of animated GIFs). Window sizes for apps need to provide the best possible UX, so it’s important to be able to make them fit the dimension required for the UI. Figure 7.1 shows an example.

Figure 7.3. The Electron app running with a dimension of 400-pixel width and 200-pixel height. Notice that the app looks like it’s running in an extreme landscape mode.

Electron’s approach means you have fine-grained control over the dimensions of each app window. For more details on all the attributes you can pass into the creation of a Browser-Window object instance, see http://electron.atom.io/docs/api/browser-window/.

This is good when you know what window dimensions you want to constrain to when the app loads. But what if you want the app window’s width and height to be set to dynamic values, such as an app displaying an image (where the window’s width and height match the dimensions of the image)?

The good news is that there’s a way to do that with NW.js’s window API. Say you have an image on your computer with dimensions of 900 x 550 pixels, and you want to size the window to match. You can set the width and height of the window to 900 pixels wide and 550 pixels high with the code shown in the following listing.

Listing 7.2. Dynamically resizing the app window
const gui = require('nw.gui'); #1 const win = gui.Window.get(); #2 win.width = 1024; #3 win.height = 768; #1 - References GUI API in NW.js #2 - Uses GUI API to select current app window #3 - Sets width and height of window dynamically

Being able to resize the window programmatically lets you make the app window match the dimensions of the content inside it and therefore give a better experience to the user.