Windows

Overview

Using the Window Management API, your app can easily open and manipulate browser windows. This allows you to transform your traditional single-window web app into a multi-window native-like web app. The Window Management API enables apps to:

  • open multiple windows;
  • manipulate the position and size of opened windows;
  • pass context data upon opening new windows;
  • listen for and handle events related to opening and closing windows;

Configuration

Use the windows property of the configuration object when initializing the Glue42 Web Platform library in the Main app to specify custom settings for the Window Management library:

import GlueWebPlatform from "@glue42/web-platform";

const config = {
    windows: {
        windowResponseTimeoutMs: 10000,
        defaultWindowOpenBounds: {
            top: 0,
            left: 0,
            width: 600,
            height: 600
        }
    }
};

const { glue } = await GlueWebPlatform(config);

Configuring the Window Management library is optional. The configuration object for the windows property has the following optional properties:

Property Description
windowResponseTimeout Sets the timeout in milliseconds that the Glue42 library will wait for a valid success response from the target window after the Glue42 library has been initialized and a window operation is being executed on the window object (e.g., myWindow.moveTo(200, 200)). If no response has been received within this period, the Glue42 library will assume that either the window isn't Glue42 enabled, or Glue42 hasn't been initialized yet. Note that this timeout is valid only for operations on the window object - it doesn't affect a glue.windows.open() call, for example.
defaultWindowOpenBounds Default bounds for opening a new window or an app instance.

The Live Examples section demonstrates using the Window Management API. To see the code and experiment with it, open the embedded examples directly in CodeSandbox.

Opening Windows

The Window Management API is accessible through the glue.windows object.

To open a new Glue42 Window, use the open() method:

const name = "glue42-docs";
const url = "https://docs.glue42.com";
// Specify location for the new window.
const options = {
    top: 200,
    left: 200
};

const g42Window = await glue.windows.open(name, url, options);

The name and url parameters are required. The window name must be unique. The third parameter is an optional Settings object which specifies various settings for the new Glue42 Window - bounds, relative position and context.

For all available settings when opening a new Glue42 Window, see the Window Settings section.

Opening PDF Files

To open a PDF file in a Glue42 Window, use the open() method. Pass the URL to the PDF file and optionally specify parameters in the URL for opening the PDF file:

// This will open the PDF file with the PDF toolbar turned off.
const PDF_URL = "https://url-to-pdf.com/file-name.pdf#toolbar=0";

await glue.windows.open("PDF File", PDF_URL);

To specify parameters in the URL, use the following template:

<URL to PDF file>#<parameter>=<value>

To specify multiple parameters in the URL, use & to separate them:

<URL to PDF file>#<parameter>=<value>&<parameter>=<value>&<parameter>=<value>

Note that #, & and = are special characters which you must not use in parameter values because they can't be escaped.

The following example will display page 3 of the PDF file, hide the PDF toolbar, set the zoom factor to 150% and scroll the page vertically and horizontally by 100px (pixels are relative to the zoom factor):

const PDF_URL = "https://url-to-pdf.com/file-name.pdf#page=3&toolbar=0&zoom=150,100,100";

await glue.windows.open("PDF File", PDF_URL);

The following table lists all supported URL parameters for opening PDF files:

Parameter Description Examples
page Specifies which page to display. Accepts an integer as a value. The first page of the document has a value of 1. To open the PDF file to page 3, use page=3.
toolbar Whether to enable or disable the PDF toolbar. Accepts 0 or 1 as values. To hide the PDF toolbar, use toolbar=0.
zoom Specifies the zoom factor and also the vertical and horizontal scroll position of the page in regard to the top left corner of the window. Accepts integer or floating point values. To set the zoom factor to 150.5%, use zoom=150.5. To set the zoom factor to 120% and scroll the page 200px vertically and 100px horizontally, use zoom=120,200,100.
view Specifies the view mode of the page using values defined in the PDF language specification. See the possible values in the next table. Use the page parameter before view. To fit the page in the window, use view=Fit. To fit the page vertically, use view=FitV. To fit the page horizontally and scroll it 200px vertically, use view=FitH,200.

The following table lists the possible values for the view parameter:

Value Description Example
Fit Fits the entire page horizontally and vertically in the window. If the vertical and horizontal magnification factors are different, the smaller one will be used for fitting the page. In the other dimension the page will be centered. view=Fit
FitV Fits the page vertically in the window. view=FitV
FitH Fits the page horizontally in the window. view=FitH
FitV,<left> Fits the page vertically and scrolls it horizontally from the left edge of the window with the specified integer or floating point value. view=FitV,200
FitH,<top> Fits the page horizontally and scrolls it vertically from the top edge of the window with the specified integer or floating point value. view=FitH,200

Finding Windows

All functions for finding Glue42 Windows return a WebWindow object (or a collection of such objects).

Listing

To obtain a collection of all Glue42 Windows, use the list() method:

const allG42Windows = glue.windows.list();

Current Window

To get a reference to the current window, use the my() method:

const currentWindow = glue.windows.my();

By ID

To find a window by ID, use the findById() method:

const ID = "2506_04";
const g42Window = glue.windows.findById(ID);

Window Settings

Provide window settings per window by passing a Settings object to the open() method:

const name = "glue42-docs";
const url = "https://docs.glue42.com";
// Specify location for the new window.
const options = {
    height: 640,
    width: 560,
    left: 100,
    top: 100,
    context: { glue: 42 }
};

const g42Window = await glue.windows.open(name, url, options);

The table below shows all available window settings:

Name Type Description Default
height number Window height (in pixels). 400
left number Distance of the top left window corner from the left edge of the screen (in pixels). 0
relativeDirection string Direction ("bottom", "top", "left", "right") of positioning the window relatively to the relativeTo window. Considered only if relativeTo is supplied. "right"
relativeTo string The ID of the window that will be used to relatively position the new window. Can be combined with relativeDirection. -
top number Distance of the top left window corner from the top edge of the screen (in pixels). 0
width number Window width (in pixels). 400

Relative Position

Position a new Glue42 Window relatively to an already existing Glue42 Window by providing the ID of the existing window and the relative direction:

const clientsWindow = glue.windows.findById("3HI0hHjdSq");

const name = "clientportfolio";
const url = "http://localhost:22080/clientportfolio/index.html";
// Provide the existing window ID and the relative direction.
const options = {
    relativeTo: clientsWindow.id,
    relativeDirection: "right"
};

await glue.windows.open(name, url, options);

Window Operations

The Window Management API enables you to control a WebWindow instance programmatically. Access or change various window settings using the provided properties and methods.

Title

To get the title of a Glue42 Window, use the getTitle() method of a WebWindow instance:

const winTitle = await myWindow.getTitle();

To set the title of a window, use the setTitle() method:

await myWindow.setTitle("New Title");

URL

To get the URL of a Glue42 Window, use the getURL() method of a WebWindow instance:

const winURL = await myWindow.getURL();

Bounds

The bounds of a window describe its position (top and left coordinates) and size (width and height) on the screen.

To get the bounds of a Glue42 Window, use the getBounds() method of a WebWindow instance:

const winBounds = await myWindow.getBounds();

To move or resize a Glue42 Window, use the moveTo(), resizeTo() or moveResize() methods.

To move a window:

// Top and left coordinates (in pixels) for the top-left window corner.
await myWindow.moveTo(200, 300);

To resize a window:

// Width and height (in pixels) for the window.
await myWindow.resizeTo(300, 400);

To move and/or resize a window:

// New bounds for the window. All properties are optional.
const bounds = {
    top: 200,
    left: 300,
    width: 300,
    height: 400
};

await myWindow.moveResize(bounds);

Note that programmatically moving and resizing the window of the Main app isn't possible.

Focus

To bring a window on focus, use the focus() method:

await myWindow.focus();

Note that programmatically focusing the window of the Main app isn't possible.

Close

To close a Glue42 Window, use the close() method of a WebWindow instance:

await myWindow.close();

Note that programmatically closing the window of the Main app isn't possible.

Context

Each Glue42 Window has a dedicated context. The window context is a JavaScript object which may contain any information regarding the window instance in the form of key/value pairs.

Contexts can be set/passed initially on window creation and updated at runtime. Context changes can be tracked by subscribing to an event which fires when the window context has been updated (see Window Events).

Note that saving large volumes of custom data as window context (e.g., thousands of lines of table data) can lead to significant delays. A user usually has several (in some cases - many) running apps and/or Workspaces (which can also contain many apps) and if one or more of the apps saves large amounts of context data, this will significantly slow down the saving process (e.g., on shutdown or when saving a layout). Saving custom context works best with smaller amounts of data. If your app needs to save large amounts of data, you have to think about how to design this process better - for instance, you may store IDs, indices, etc., as context data, save the actual data to a database and when you restore the app, fetch the data using the data IDs saved as window context.

Get

To get the context of a Glue42 Window, use the getContext() method of a WebWindow instance:

const winContext = await myWindow.getContext();

Update

To update the context of a Glue42 Window, use the updateContext() method of a WebWindow instance:

const newContext = { glue: 42 };

await myWindow.udpateContext(newContext);

This method will update the current context object with the provided properties and values, adding any new properties and updating the values of existing ones.

Set

To open a Glue42 Window with initially set context, use the context property of the Settings object:

const name = "Glue42 Docs";
const url = "https://docs.glue42.com";
// Specify window context.
const options = {
    context: { glue: 42 }
};

const g42Window = await glue.windows.open(name, url, options);

To replace the current window context, use the setContext() method of a WebWindow instance:

const newContext = { tick: 42 };
const winContext = await myWindow.setContext();

This method will completely overwrite the existing context object, replacing its current value with the specified one.

Window Events

Methods for tracking Glue42 Window events are available at top-level of the Window Management API and on the WebWindow instance. Below are described the available window events with examples of how to handle them.

The window event methods return an unsubscribe function which you can use to stop tracking the respective event.

Window Added or Removed

To track the opening and closing of Glue42 Windows, use the onWindowAdded() and onWindowRemoved() methods of the Window Management API and pass handlers for the respective events:

const handlers = {
    onAdded: (g42window) => {
        console.log(`Window added: ${g42window.name}`);
    },

    onRemoved: (g42window) => {
        console.log(`Window removed: ${g42window.name}`);
    }
};

glue.windows.onWindowAdded(handlers.onAdded);
glue.windows.onWindowRemoved(handlers.onRemoved);

Context Update

To track updates of the context of a Glue42 Window, use the onContextUpdated() method of a WebWindow instance and pass an event handler:

const contextUpdatedHandler = (context, g42window) => {
    console.log(`The context of "${g42window.name}" has been updated: ${JSON.stringify(context)}`);
};

myWindow.onContextUpdated(contextUpdatedHandler);

Live Examples

Opening Windows

The app below demonstrates opening a new window with basic configuration (context and size) by using the open() method of the Window Management API.

Use the input fields in App A to assign a name (required) to the new window and set the window context and size. Click the "Open Window" button to open a new window.

Open in CodeSandbox

Window Events

The apps below demonstrate handling window events - opening and closing windows.

On load, App A and App B subscribe for the onWindowAdded() and the onWindowRemoved() events of the Window Management API and will print to the page every time a new window is opened or an existing window is closed.

Open several new windows by using the input fields in App A to assign a name (required) to the new window and set the window context and size. Click the "Open Window" button to open a new window.

Open in CodeSandbox

Reference

For a complete list of the available Window Management API methods and properties, see the Window Management API Reference Documentation.