Skip to content

Commit

Permalink
Add option to run on a single display
Browse files Browse the repository at this point in the history
This seems like a nice option in terms of keeping CPU usage and visual
distraction to a minimum.
  • Loading branch information
muffinista committed Feb 14, 2018
1 parent c388cb5 commit 1625d78
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 25 deletions.
13 changes: 13 additions & 0 deletions src/lib/savers.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,14 @@ var getDisableOnBattery = function() {
return nconf.get("disable_on_battery") || false;
};

var setRunOnSingleDisplay = function(x) {
setConfig("run_on_single_display", x);
};

var getRunOnSingleDisplay = function() {
return nconf.get("run_on_single_display") || false;
};


/**
* set options for the specified screensaver
Expand Down Expand Up @@ -660,6 +668,7 @@ var getDefaults = function() {
lock: false,
disable_on_battery: true,
auto_start: false,
run_on_single_display: false,
localSource: ""
};
};
Expand Down Expand Up @@ -691,6 +700,10 @@ exports.setLock = setLock;
exports.getLock = getLock;
exports.setDisableOnBattery = setDisableOnBattery;
exports.getDisableOnBattery = getDisableOnBattery;

exports.getRunOnSingleDisplay = getRunOnSingleDisplay;
exports.setRunOnSingleDisplay = setRunOnSingleDisplay;

exports.getOptions = getOptions;
exports.listAll = listAll;
exports.updatePrefs = updatePrefs;
Expand Down
4 changes: 2 additions & 2 deletions src/main/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ var version = undefined;
var packageJSON;

try {
packageJSON = require('../../package.json');
packageJSON = require("../../package.json");
version = packageJSON.version;
}
catch(e) {
Expand Down Expand Up @@ -40,7 +40,7 @@ global.CONFIG_DEFAULTS = {
// this is a free sentry account and the URL will be in every copy of
// the app that gets distributed, so i'm committing it to the repo for now
if ( process.env.TEST_MODE === undefined && ! global.IS_DEV ) {
var os = require('os');
var os = require("os");

global.RAVEN_PRIVATE_URL = "https://b86f7b0ac5604b55b4fd03adedc5d205:[email protected]/172824";
global.RAVEN_URL = "https://[email protected]/172824";
Expand Down
4 changes: 2 additions & 2 deletions src/main/fullscreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ if ( platform === "darwin" ) {

$.framework("Foundation");
$.framework("Cocoa");
$.framework('CoreGraphics')
$.framework('CoreFoundation')
$.framework("CoreGraphics");
$.framework("CoreFoundation");

pool = $.NSAutoreleasePool("alloc")("init");

Expand Down
81 changes: 61 additions & 20 deletions src/main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,31 @@ var forceWindowClose = function(w) {
}
};


/**
* get the BrowserWindow options we'll use to launch
* on the given screen.
*/
var getWindowOpts = function(s) {
var opts = {
backgroundColor: "#000000",
autoHideMenuBar: true,
alwaysOnTop: true,
x: s.bounds.x,
y: s.bounds.y,
show: false
};

// osx will display window immediately if fullscreen is true
// so we default it to false there
if (process.platform !== "darwin") {
opts.fullscreen = true;
}

return opts;

};

/**
* run the specified screensaver on the specified screen
*/
Expand All @@ -311,25 +336,11 @@ var runScreenSaverOnDisplay = function(saver, s) {
platform: process.platform
};

var windowOpts = {
backgroundColor: "#000000",
autoHideMenuBar: true,
alwaysOnTop: true,
x: s.bounds.x,
y: s.bounds.y,
show: false,
// frame: false
};
var windowOpts = getWindowOpts(s);

var tickCount;
var diff;


// osx will display window immediately if fullscreen is true
// so we default it to false there
if (process.platform !== "darwin") {
windowOpts.fullscreen = true;
}

log.info("runScreenSaverOnDisplay", s.id, windowOpts);

Expand Down Expand Up @@ -380,11 +391,11 @@ var runScreenSaverOnDisplay = function(saver, s) {
w.once("ready-to-show", () => {
log.info("ready-to-show", s.id);
if ( debugMode !== true ) {
w.setFullScreen(true);
w.setFullScreen(true);
}

w.show();
// w.focus();
// w.focus();

diff = process.hrtime(tickCount);
log.info(`rendered in ${diff[0] * 1e9 + diff[1]} nanoseconds`);
Expand Down Expand Up @@ -426,15 +437,30 @@ var runScreenSaverOnDisplay = function(saver, s) {
else {
runSaver();
}
};

/**
* blank out the given screen
*/
var blankScreen = function(s) {
var windowOpts = getWindowOpts(s);
var w = new BrowserWindow(windowOpts);
w.setFullScreen(true);

log.info("blankScreen", s.id, windowOpts);

saverWindows.push(w);

w.show();
};


/**
* get a list of displays connected to the computer.
*/
var getDisplays = function() {
var displays = [];
if ( debugMode === true ) {
if ( debugMode === true || global.savers.getRunOnSingleDisplay() === true ) {
displays = [
electronScreen.getPrimaryDisplay()
];
Expand All @@ -446,6 +472,15 @@ var getDisplays = function() {
return displays;
};

/**
* get a list of the non primary displays connected to the computer
*/
var getNonPrimaryDisplays = function() {
var primary = electronScreen.getPrimaryDisplay()
return electronScreen.getAllDisplays().filter((d) => {
return d.id !== primary.id;
});
}

/**
* manually trigger screensaver by setting state to run
Expand Down Expand Up @@ -477,8 +512,6 @@ var runScreenSaver = function() {
// move cursor so far off screen, it isn't even funny
robot.moveMouse(30000, 30000);

// @todo maybe add an option to only run on a single display?

// limit to a single screen when debugging
if ( debugMode === true ) {
if ( typeof(app.dock) !== "undefined" ) {
Expand All @@ -493,6 +526,14 @@ var runScreenSaver = function() {
for ( var i in displays ) {
runScreenSaverOnDisplay(saver, displays[i]);
} // for

// if we're only running on primary display, blank out the other ones
if ( debugMode !== true && global.savers.getRunOnSingleDisplay() === true ) {
var otherDisplays = getNonPrimaryDisplays();
for ( var i in otherDisplays ) {
blankScreen(otherDisplays[i]);
}
}
}
catch (e) {
stateManager.ignoreReset(false);
Expand Down
16 changes: 15 additions & 1 deletion src/renderer/components/PrefsForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@
</small>
</div>


<div class="form-check">
<label for="lock" class="form-check-label">
<input type="checkbox" id="lock" class="form-check-input"
v-model="prefs.run_on_single_display" />
Only run on the primary display?
</label>
<small class="form-text text-muted">
If you have multiple displays, only run on the primary one.
</small>
</div>

</fieldset>
</form>

Expand All @@ -80,7 +92,9 @@
<label for="repo">Github Repo URL:</label>
<div class="input-group">
<div class="input-group-addon">github.com/</div>
<input type="text" v-model="prefs.sourceRepo" class="form-control" placeholder="muffinista/before-dawn-screensavers" />
<input type="text" v-model="prefs.sourceRepo"
class="form-control"
placeholder="muffinista/before-dawn-screensavers" />
</div>
<small class="form-text text-muted">
We will download releases from this repository instead of the default repo if specified.
Expand Down

0 comments on commit 1625d78

Please sign in to comment.