Skip to content

Instantly share code, notes, and snippets.

@S-unya
Created November 25, 2015 10:29
Show Gist options
  • Save S-unya/898e9e66bd0cde9d4621 to your computer and use it in GitHub Desktop.
Save S-unya/898e9e66bd0cde9d4621 to your computer and use it in GitHub Desktop.
Create NWjs kiosk that spans multiple displays in OSX
/**
* There may be a better way to do this and I'd love to hear of them. This technique revolves around 2 specifics:
* 1. In OSX Mavericks+ you need to set the displays to not have their own "space"; I have bruted this by adding a cmd
* to actually write this preference on each app start. Needless to say this should NOT be done to an unsuspecting user...
* 2. Kiosk mode should be set BEFORE setting the width and height of your window. Below I include a function that will calculate
* all available space and position the window relatively to the "main" screen.
*/
// require the GUI object so that we can access Window and Screen
var gui = require("nw.gui");
// Shelljs is a package that runs bash commands from node
var shell = require('shelljs');
// Execute a cmd line to set Mission Control displays to be treated as single space
shell.exec("defaults write com.apple.spaces spans-displays -bool true", function (code, output) {
// ... Run code upon completion here ...
});
var win = gui.Window.get();
win.height = 1080;
win.width = 1920;
// init must be called once during startup, before any function to gui.Screen can be called
gui.Screen.Init();
var screens = gui.Screen.screens;
var len = screens.length;
var height = 0;
var width = 0;
var screenOffsetX = 0;
var screenOffsetY = 0;
/**
* Function to calculate available screens and offsets and automatically
* resize and place the kiosk window
* @return {void}
*/
function calculateScreens() {
var i = 0;
// store all the screen information into string
for (; i < len; i++) {
var bounds = screens[i].bounds;
// Always treats the 'main' screen as first in array, which has x and y of 0
if (bounds.x !== 0) {
if (bounds.x < 0) {
screenOffsetX = Math.min(screenOffsetX, bounds.x);
}
}
if (bounds.y !== 0) {
if (bounds.y < 0) {
screenOffsetY = Math.min(screenOffsetY, bounds.y);
}
}
width += bounds.width;
height += bounds.height;
// Here enter kioskMode before setting the window width, height and offsets
win.enterKioskMode();
win.width = width;
win.height = height;
win.moveTo(screenOffsetX, screenOffsetY);
}
}
/**
* Utitlity function to allow toggling kioskMode - connect to some trigger like keypress
* @return {void}
*/
function toggleKiosk() {
alert("kiosk")
win.toggleKioskMode();
}
/**
* Utitlity function to allow close kiosk - connect to some trigger like keypress
* @return {void}
*/
function closeWindow() {
alert("close")
win.close();
}
// EVENTS
win.on("loaded", function (){
calculateScreens();
});
win.on("close", function () {
// Pretend to be closed already
this.hide();
this.close(true);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment