Window

The Window Aptitude provides the ability to obtain information about windows on a user's desktop (position, name, etc.) as well as listen to window events (gaining focus, losing focus, etc.).

Each call to the Window Aptitude is providing you with one of two types of information. It either returns WindowInfo or a WindowEvent. Let's take a moment to define these two things.

interface WindowInfo {
    title: string;    // The current title of the window
    path: string;     // The name of the process running the window
    pid: number;      // The process id of the window
    x: number;        // The current x position of the window on the screen
    y: number;        // The current y position
    width: number;    // The current width of a window
    height: number;   // The current height of a window
}

type WindowActionBlur = 'blur';                 // Window loses focus
type WindowActionClosed = 'close';              // Window is closed (can also be a tab closing, or losing focus)
type WindowActionFocused = 'focus';             // Window gains focus
type WindowActionMoved = 'move';                // Window position is moved (can also be triggered by resize)
type WindowActionOpened = 'open';               // Window is opened (can also be a new tab, or a window gaining focus)
type WindowActionResize = 'resize';             // Window size is changed
type WindowActionTitleChanged = 'titleChange';  // The title of the window has changed

type WindowAction =
    | WindowActionBlur
    | WindowActionClosed
    | WindowActionFocused
    | WindowActionMoved
    | WindowActionOpened
    | WindowActionResize
    | WindowActionTitleChanged;

interface WindowEvent {
    info: WindowInfo;
    action: WindowAction;
}

activeWindow

Get WindowInfo about the currently focused window when the function is called.

import { window } from '@oliveai/ldk';

// Retrieves information about the currently focused window.
window.activeWindow().then((windowInfo) => {
    console.log(JSON.stringify(windowInfo));
});

all

Get a WindowInfo[] list of all windows on a system.

import { window } from '@oliveai/ldk';

// Retrieves information about all windows, whether they are focused or not
window.all().then((allWindows) => {
    console.log(JSON.stringify(allWindows[0]));
});

listenActiveWindow

Triggers whenever focus is given to a different window. Takes a callback function as an argument, which sends a WindowInfo whenever called. A promise is triggered after setting the listener that provides access to a Cancellable stream, so that you can turn off the listener.

import { window } from '@oliveai/ldk';

let activeWindowStream: Cancellable;

window.listenActiveWindow((windowInfo) => {
    if (windowInfo) {
        console.debug(`This window become active: title=${windowInfo.title}`);
        activeWindowStream.cancel();
    }
}).then((cancellable: Cancellable) => {
    activeWindowStream = cancellable;
});

listenAll

Receive a notification whenever a window is opened, closed, focused, unfocused, moved, resized, or its title changes as a WindowEvent. A window that is opened with focus will generate an Opened event and a Focused event.

import { window } from '@oliveai/ldk';

let listenAllStream: Cancellable;

window.listenAll((windowEvent) => {
    if (windowEvent.action === 'move') {
        console.debug(`This window has moved: title=${windowEvent.info.title}`);
        listenAllStream.cancel();
    }
}).then((cancellable: Cancellable) => {
    listenAllStream = cancellable;
});

Last updated