Window Tutorial
This tutorial shows you how to create and interact with Windows.
Windows are floating containers that you can use to render any content.
Usage
To create a new Window
instance:
const options = {title: 'My Window'};
// Attach to the application
const win = proc.createWindow(options)
// Outside your application
const win = core.make('osjs/window', options)
Render
Use the provided render method to put content into your Window via a DOM element.
[info] This DOM element is the root of the contents allowing you to use anything from plain HTML as strings to advanced frameworks.
win.render($content => $content.appendChild(
document.createTextNode('Hello World!')
));
See GUI tutorial on how to use the design user interfaces with the official reactive GUI component library.
Options
id
A unique identifiertitle
The titleparent
Parent window instanceposition: {top?: number, left?: nubmer} | string
Position object or a string (ex:center
,topleft
,bottomright
, etc)dimension: {width?: number, height?: number}
Dimension objectattributes
A set of attributesstate: {maximized?: boolean, minimized?: boolean}
Default state
Example:
proc.createWindow({
title: 'My Window'
})
Attributes
gravity: string
- Where to place the window (ex:center
,topleft
,bottomright
, etc)modal: boolean
- If a parent is provided it will be disabled until this window closesontop: boolean
- Place on-top of all other windowsresizable: boolean
- Set if window can be resizedfocusable: boolean
- Set if window can be focusedmaximizable: boolean
- Set if window can be minimizedminimizable: boolean
- Set if window can be maximizedsessionable: boolean
- Set if window can be saved in sessiondroppable: boolean
- Set if window can receive drop eventscloseable: boolean
- Set if window can be closedheader: boolean
- Header visibilitycontrols: boolean
- Header controls visibility (min/max/close buttons)position: {top: number, left: number}
- Default positionminDimension: {width: number, height: number}
- Minimum dimensionmaxDimension: {width: number, height: number}
- Maximum dimensionvisibility: string
- Set torestricted
to hide from the panels, etc.clamp: boolean
- Clamps window position to the viewport if it overflows (default)
Example:
proc.createWindow({
attributes: {
ontop: true
}
})
[info] You can define initial window position and dimension by percentage using a float between
0.0 - 1.0
. Example:{position: {left: 0.5, top: 0.5}}
.
Events
moved => (position, win)
- After movement completesresized => (dimension, win)
- After resize completesdestroy => (win)
- Destroyedinit => (win)
- On initrender => (win)
- On renderclose => (win)
- On closefocus => (win)
- On focusblur => (win)
- On blurminimize => (win)
- On minimizemaximize => (win)
- On maximizeraise => (win)
- On "un-minimize"restore => (win)
- On "un-maximize"dragenter => (ev, win)
- On "drag enter"dragover => (ev, win)
- On "drag over"dragleave => (ev, win)
- On "drag leave"drop => (ev, data, files, win)
- On "drop"keypress => (ev, win)
- On "keypress"keydown => (ev, win)
- On "keydown"keyup => (ev, win)
- On "keyup"
You can listen on events with:
win.on('event-name', (...args) => console.log(...args));
Methods
Common methods:
win.close(); // Close
win.destroy(); // Destroy ("force close")
win.blur(); // Un-focus
win.focus(); // Focus
win.minimize(); // Minimize
win.maximize(); // Maximize
win.raise(); // Un-minimize
win.restore(); // Un-maximize
win.gravitate(string); // Gravitate toward direction
win.resizeFit(node); // Resize to fit given DOM container
win.setIcon(string); // Sets icon
win.setTitle(string); // Sets title
win.setPosition({top?, left?}); // Sets position
win.setDimension({width?, height?}); // Sets dimension
win.setZindex(number); // Sets z-index
win.setNextZindex(); // Sets next z-index (move to top)
Media Queries
You can assign local media queries to a window using attributes. By default OS.js provides:
small:<=640px
medium:<=1024px
large:>=1024px
const options = {
title: 'My Window',
attributes: {
mediaQueries: {
custom: 'screen and (min-width: 1280px)'
}
}
};
The mediaQueries
key will be assigned to a data-media=""
attribute on your Window root DOM element so you can create responsive interfaces via CSS. You can also get the media state via win.state.media
.