Created
May 18, 2015 02:55
-
-
Save rdtsc/68479f267693d536fc38 to your computer and use it in GitHub Desktop.
NW.js Launcher Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"> | |
<title>NW.js Launcher Demo</title> | |
<style> | |
body { | |
font-family: sans-serif; | |
} | |
#app-settings button[type="submit"] { | |
float: right; | |
} | |
.debug { | |
bottom: 1em; | |
position: fixed; | |
font-size: 1.5em; | |
text-align: center; | |
width: 100%; | |
} | |
</style> | |
</head> | |
<body> | |
<form id="app-settings"> | |
<fieldset> | |
<legend>App Settings:</legend> | |
GPU Acceleration: | |
<select id="gpu-acceleration"> | |
<option value="1">Enabled</option> | |
<option value="0">Disabled</option> | |
</select> | |
<button type="submit">Apply Settings & Reload App</button> | |
</fieldset> | |
</form> | |
<div class="debug"> | |
<a href="chrome://gpu">chrome://gpu</a> | |
</div> | |
<script> | |
document.addEventListener('DOMContentLoaded', function() { | |
var fs = require('fs'); | |
var win = require('nw.gui').Window.get(); | |
var $ = document.querySelector.bind(document); | |
// You can also write to localStorage, or whatever. Your launcher app | |
// would have to load and read the SQLite3 DB from NW's data path in | |
// that case. | |
// Display current config value in dropdown. | |
fs.readFile('config.json', function(error, data) { | |
if(!error) { | |
var config = JSON.parse(data); | |
if(config.hasOwnProperty('gpuEnabled')) { | |
var target = 2 - (config.gpuEnabled === true); | |
$('#gpu-acceleration :nth-child(' + target +')').selected = true; | |
} | |
} | |
}); | |
// Save config selection to a local file and restart the app. | |
$('#app-settings').onsubmit = function(event) { | |
event.preventDefault(); | |
var config = { | |
gpuEnabled: !!+$('#gpu-acceleration').value | |
}; | |
// Incomplete. Should use a lock or multiple instances. | |
fs.writeFile('config.json', JSON.stringify(config), function(error) { | |
win.close(); | |
if(!error) require('child_process').exec('launcher --restart'); | |
}); | |
return false; | |
}; | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If on Windows, build with: -ldflags -H=windowsgui | |
package main | |
import ( | |
"encoding/json" | |
"io/ioutil" | |
"os/exec" | |
"flag" | |
"time" | |
"os" | |
) | |
type Config struct { | |
GpuEnabled bool | |
} | |
func spawnApp(arguments ...string) { | |
args := "--ignore-gpu-blacklist" | |
if len(arguments) > 0 { | |
args = arguments[0] | |
} | |
exec.Command("./nw", args).Start() | |
os.Exit(0) | |
} | |
func main() { | |
isRestart := flag.Bool("restart", false, "") | |
flag.Parse() | |
// Should use a proper locking mechanism, instead. | |
if *isRestart { | |
time.Sleep(1 * time.Second) | |
} | |
data, error := ioutil.ReadFile("./config.json") | |
if error != nil { | |
spawnApp() | |
} | |
var config Config | |
error = json.Unmarshal(data, &config) | |
if error != nil { | |
spawnApp() | |
} | |
if !config.GpuEnabled { | |
spawnApp("--disable-gpu") | |
} | |
spawnApp() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "nw.js-launcher-demo", | |
"main": "index.html" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment