Skip to content
Browse files

AppSettings and WindowSettings, change Window to NativeWindow in js, …

…change NativeWindow constructor to just take an options object, cleanup
  • Loading branch information...
1 parent 6b71646 commit 46adf695bcf015eec2e8d2d16c8b5808e4ab0ae9 @Benvie Benvie committed Jul 31, 2012
Showing with 240 additions and 202 deletions.
  1. +19 −23 lib/index.js
  2. +160 −25 lib/settings.js
  3. +10 −86 lib/utils.js
  4. +1 −1 lib/window.js
  5. +1 −1 src/appjs.cpp
  6. +12 −26 src/appjs_app.cpp
  7. +9 −16 src/appjs_window.cpp
  8. +17 −17 src/base/native_window.cpp
  9. +1 −1 src/base/native_window.h
  10. +5 −0 src/includes/util.cpp
  11. +1 −0 src/includes/util.h
  12. +4 −6 src/windows/native_window_win.cpp
View
42 lib/index.js
@@ -1,19 +1,17 @@
var bindings = require('./bindings'),
- config = require('./settings')(bindings.initConfig),
+ settings = require('./settings'),
router = require('./router'),
decorate = require('./utils').decorate,
isObject = require('./utils').isObject;
var App = bindings.App,
- NativeWindow = bindings.Window,
- Window = require('./window'),
- Icons = require('./utils').Icons;
+ NativeWindow = bindings.NativeWindow,
+ Window = require('./window');
var concat = Array.prototype.concat,
_createWindow = App.prototype.createWindow;
-var names = require('./utils').names,
- events = ['minimize', 'maximize', 'fullscreen', 'restore', 'move', 'resize'];
+var events = ['minimize', 'maximize', 'fullscreen', 'restore', 'move', 'resize'];
@@ -39,31 +37,29 @@ decorate(App.prototype, [
}, this);
mod.call(this);
},
- function createWindow(url, settings){
+ function createWindow(options){
var self = this;
- if (url && typeof url === 'object') {
- settings = url;
- url = settings.url;
- }
- settings = decorate({}, settings);
- settings.icons = new Icons(settings.icons);
- if (!settings.name) {
- settings.name = names[names.index++] + names.suffix;
- if (names.index === names.length) {
- names.index = 0;
- names.suffix = (names.suffix || 0) + 1;
+ if (typeof options === 'string') {
+ if (isObject(arguments[1])) {
+ arguments[1].url = options;
+ options = arguments[1];
+ } else {
+ options = { url: options };
}
}
+ if (!(options instanceof settings.WindowSettings)) {
+ options = new settings.WindowSettings(options);
+ }
- var nativeWindow = _createWindow.call(this, url || 'http://appjs/', settings),
+ var nativeWindow = _createWindow.call(this, options),
window = new Window(nativeWindow);
- nativeWindow.name = settings.name;
- this.windows[settings.name] = window;
+ nativeWindow.name = options.name;
+ this.windows[options.name] = window;
nativeWindow.on('close', function(){
window.emit('close');
- delete self.windows[settings.name];
+ delete self.windows[options.name];
});
events.forEach(function(event){
@@ -84,7 +80,7 @@ decorate(App.prototype, [
-var app = module.exports = bindings.init(config);
+var app = module.exports = bindings.init(new settings.AppSettings(bindings.initConfig));
app.screenWidth = App.screenWidth;
app.screenHeight = App.screenHeight;
app.windows = {};
View
185 lib/settings.js
@@ -1,41 +1,46 @@
-module.exports = function createSettings(o){
- return new Settings(o);
-};
+var decorate = require('./utils').decorate,
+ constants = require('./utils').constants;
-module.exports.Settings = Settings;
+module.exports = {
+ AppSettings: AppSettings,
+ WindowSettings: WindowSettings,
+ IconSettings: IconSettings
+};
-function Settings(o){
- for (var k in o) {
+function AppSettings(o){
+ for (var k in Object(o)) {
this[k] = o[k];
}
}
-var LOGSEVERITY = Settings.LOGSEVERITY = {
- VERBOSE : -1,
- INFO : 0,
- WARNING : 1,
- ERROR : 2,
- ERROR_REPORT : 3,
- DISABLE : 99
+AppSettings.create = function createAppSettings(o){
+ return new AppSettings(o);
};
-var DEFAULT = '';
+constants(AppSettings, {
+ LOGLEVEL_VERBOSE : -1,
+ LOGLEVEL_INFO : 0,
+ LOGLEVEL_WARNING : 1,
+ LOGLEVEL_ERROR : 2,
+ LOGLEVEL_ERROR_REPORT: 3,
+ LOGLEVEL_DISABLED : 99
+});
-Settings.prototype = {
+decorate(AppSettings.prototype, {
JSFlags : ' --harmony_proxies --harmony_collections --harmony_scoping',
- LogLevel : LOGSEVERITY.DISABLE,
+ LogLevel : AppSettings.LOGLEVEL_DISABLED,
PakLoading : true,
- LocalesPakPath : DEFAULT,
- ChromePakPath : DEFAULT,
- LogFilePath : DEFAULT,
- CachePath : DEFAULT,
- UserStylesheetPath : DEFAULT,
+ LocalesPakPath : '',
+ ChromePakPath : '',
+ LogFilePath : '',
+ CachePath : '',
+ UserStylesheetPath : '',
- UserAgent : DEFAULT,
- ProductVersion : DEFAULT,
- Locale : DEFAULT,
+ UserAgent : '',
+ ProductVersion : '',
+ Locale : '',
StandardFont : 'Times New Roman',
FixedFont : 'Courier New',
@@ -57,7 +62,6 @@ Settings.prototype = {
XSSAuditer : true,
HyperlinkAuditing : true,
- WebSecurity : true,
Plugins : true,
Java : true,
PageCache : true,
@@ -93,4 +97,135 @@ Settings.prototype = {
FullscreenAPI : true,
LocalStorageAPI : true,
WebGLAPI : true,
+});
+
+
+var names = module.exports.names = [
+ 'alpha', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot', 'golf',
+ 'hotel', 'india', 'juliet', 'kilo', 'lima', 'mike', 'november',
+ 'oscar', 'papa', 'quebec', 'romeo', 'sierra', 'tango', 'uniform',
+ 'victor', 'whiskey', 'x-ray', 'yankee', 'zulu'
+];
+
+names.index = 0;
+names.suffix = '';
+
+
+function WindowSettings(o){
+ for (var k in Object(o)) {
+ this[k] = o[k];
+ }
+ this.icons = new IconSettings(this.icons);
+ if (!this.name || typeof this.name !== 'string') {
+ this.name = names[names.index++] + names.suffix;
+ if (names.index === names.length) {
+ names.index = 0;
+ names.suffix = (names.suffix || 0) + 1;
+ }
+ }
+}
+
+WindowSettings.create = function createWindowSettings(o){
+ return new WindowSettings(o);
+};
+
+decorate(WindowSettings.prototype, {
+ url :'http://appjs/',
+ name : null,
+ icons : null,
+ width : 800,
+ height : 600,
+ left : -1,
+ top : -1,
+ opacity : 1,
+ alpha : false,
+ showChrome : true,
+ autoResize : false,
+ resizable : true,
+ showResizeGrip: false,
+ fullscreen : false,
+ topmost : false,
+});
+
+
+
+
+function IconSettings(o){
+ if (o instanceof IconSettings) {
+ Object.keys(o).forEach(function(key){
+ this[key] = o[key];
+ }, this);
+ } else if (typeof o === 'string') {
+ this.setRoot(o);
+ } else if (isObject(o)) {
+ this.setObject(o);
+ } else if (Array.isArray(o)) {
+ this.setArray(o);
+ }
+}
+
+IconSettings.create = function createIconSettings(o){
+ return new IconSettings(o);
};
+
+
+var namedSizes = ['smaller', 'small', 'largel', 'larger'],
+ numberSizes = [16, 32, 64, 128];
+
+var sizeMap = {
+ 16: 'smaller',
+ 32: 'small',
+ 64: 'large',
+ 128: 'larger',
+ smaller: 'smaller',
+ small: 'small',
+ large: 'large',
+ larger: 'larger'
+ };
+
+decorate(IconSettings.prototype, [
+ function setRoot(root){
+ if (fs.existsSync(root + '/small.png')) {
+ var sizes = namedSizes;
+ } else if (fs.existsSync(root + '/16.png')) {
+ var sizes = numberSizes;
+ } else {
+ return this;
+ }
+ sizes.forEach(function(size){
+ this[sizeMap[size]] = path.resolve(root, size+'.png');
+ }, this);
+ this.resolve();
+ return this;
+ },
+ function setObject(obj){
+ for (var k in obj) {
+ if (sizeMap.hasOwnProperty(k)) {
+ this[sizeMap[k]] = obj[k];
+ }
+ }
+ this.resolve();
+ return this;
+ },
+ function setArray(array){
+ array.forEach(function(item){
+ if (typeof item === 'string' && /\.png$/.test(item)) {
+ var name = path.basename(item).slice(0, -4);
+ if (sizeMap.hasOwnProperty(name)) {
+ this[sizeMap[name]] = item;
+ }
+ }
+ }, this);
+ this.resolve();
+ return this;
+ },
+ function resolve(){
+ Object.keys(this).forEach(function(key){
+ this[key] = path.resolve(this[key]);
+ if (!fs.existsSync(this[key])) {
+ delete this[key];
+ }
+ }, this);
+ return this;
+ }
+]);
View
96 lib/utils.js
@@ -6,6 +6,7 @@ module.exports = {
decorate: decorate,
isObject: isObject,
inherit: inherit,
+ constants: constants,
log: log
};
@@ -95,6 +96,15 @@ function decorate(o){
return o;
}
+function constants(o, p){
+ Object.keys(p).forEach(function(key){
+ Object.defineProperty(o, key, {
+ enumerable: true,
+ value: p[key]
+ });
+ });
+}
+
function inherit(Ctor, Super, props){
Ctor.prototype = Object.create(Super.prototype);
@@ -107,81 +117,6 @@ function inherit(Ctor, Super, props){
-var Icons = module.exports.Icons = function(){
- function Icons(o){
- if (typeof o === 'string') {
- this.setRoot(o);
- } else if (isObject(o)) {
- this.setObject(o);
- } else if (Array.isArray(o)) {
- this.setArray(o);
- }
- }
-
- var namedSizes = ['smaller', 'small', 'largel', 'larger'],
- numberSizes = [16, 32, 64, 128],
- sizeMap = {
- 16: 'smaller',
- 32: 'small',
- 64: 'large',
- 128: 'larger',
- smaller: 'smaller',
- small: 'small',
- large: 'large',
- larger: 'larger'
- };
-
- decorate(Icons.prototype, [
- function setRoot(root){
- if (fs.existsSync(root + '/small.png')) {
- var sizes = namedSizes;
- } else if (fs.existsSync(root + '/16.png')) {
- var sizes = numberSizes;
- } else {
- return this;
- }
- sizes.forEach(function(size){
- this[sizeMap[size]] = path.resolve(root, size+'.png');
- }, this);
- this.resolve();
- return this;
- },
- function setObject(obj){
- for (var k in obj) {
- if (sizeMap.hasOwnProperty(k)) {
- this[sizeMap[k]] = obj[k];
- }
- }
- this.resolve();
- return this;
- },
- function setArray(array){
- array.forEach(function(item){
- if (typeof item === 'string' && /\.png$/.test(item)) {
- var name = path.basename(item).slice(0, -4);
- if (sizeMap.hasOwnProperty(name)) {
- this[sizeMap[name]] = item;
- }
- }
- }, this);
- this.resolve();
- return this;
- },
- function resolve(){
- Object.keys(this).forEach(function(key){
- this[key] = path.resolve(this[key]);
- if (!fs.existsSync(this[key])) {
- delete this[key];
- }
- }, this);
- return this;
- }
- ]);
-
- return Icons;
-}();
-
-
var logPath = path.resolve(module.parent && module.parent.parent && path.dirname(module.parent.parent.filename) || __dirname);
@@ -224,14 +159,3 @@ function log(path, type, context, msg){
catch (e) { console.log(e, out) }
}
}
-
-
-var names = module.exports.names = [
- 'alpha', 'bravo', 'charlie', 'delta', 'echo', 'foxtrot', 'golf',
- 'hotel', 'india', 'juliet', 'kilo', 'lima', 'mike', 'november',
- 'oscar', 'papa', 'quebec', 'romeo', 'sierra', 'tango', 'uniform',
- 'victor', 'whiskey', 'x-ray', 'yankee', 'zulu'
-];
-
-names.index = 0;
-names.suffix = '';
View
2 lib/window.js
@@ -1,4 +1,4 @@
-var NativeWindow = require('./bindings').Window,
+var NativeWindow = require('./bindings').NativeWindow,
App = require('./bindings').App,
Ephemeral = require('./handlers').Ephemeral,
ReferenceType = require('./handlers').ReferenceType,
View
2 src/appjs.cpp
@@ -17,7 +17,7 @@ void Init(Handle<v8::Object> target) {
target->Set(String::NewSymbol("init"), FunctionTemplate::New(InitApp)->GetFunction());
target->Set(String::NewSymbol("App"), App::constructor);
- target->Set(String::NewSymbol("Window"), Window::constructor);
+ target->Set(String::NewSymbol("NativeWindow"), Window::constructor);
}
} /* appjs */
View
38 src/appjs_app.cpp
@@ -15,69 +15,57 @@ App::~App() {};
bool App::initialized_ = false;
Persistent<Function> App::constructor;
-void App::Init () {
+void App::Init() {
DECLARE_CONSTRUCTOR("App");
DECLARE_PROTOTYPE_METHOD("on",On);
DECLARE_PROTOTYPE_METHOD("createWindow",CreateWindow2);
- DECLARE_CLASS_FUNCTION(screenWidth,ScreenWidth);
- DECLARE_CLASS_FUNCTION(screenHeight,ScreenHeight);
+ DECLARE_CLASS_FUNCTION(screenWidth, ScreenWidth);
+ DECLARE_CLASS_FUNCTION(screenHeight, ScreenHeight);
END_CONSTRUCTOR();
}
Handle<Value> App::New(const Arguments& args) {
HandleScope scope;
-
App* obj = new App();
-
obj->Wrap(args.This());
-
return scope.Close(args.This());
}
Handle<Value> App::NewInstance(const Arguments& args) {
HandleScope scope;
- Persistent<Object> initSettings = Persistent<Object>::New((args[0]->IsObject()) ? args[0]->ToObject() : Object::New());
- Cef::Init(new Settings(initSettings));
+ Persistent<Object> options = Persistent<Object>::New(args[0]->ToObject());
+ Cef::Init(new Settings(options));
- const unsigned argc = 1;
- Handle<Value> argv[argc] = {args[0]};
- Local<Object> instance = constructor->NewInstance(argc,argv);
+ Handle<Value> argv[1] = { args[0] };
+ Local<Object> instance = constructor->NewInstance(1, argv);
// get the events.EventEmitter constructor
Local<Object> global = Context::GetCurrent()->Global();
Handle<Object> process = global->Get(String::NewSymbol("process"))->ToObject();
- Local<Function> emitterConstructor = Local<Function>::Cast(process->Get(String::NewSymbol("EventEmitter")));
+ Local<Function> EventEmitter = Local<Function>::Cast(process->Get(String::NewSymbol("EventEmitter")));
// create process.AppjsEmitter object
- Handle<Object> AppjsEmitter = emitterConstructor->NewInstance();
- process->Set(String::NewSymbol("AppjsEmitter"),AppjsEmitter);
+ Handle<Object> AppjsEmitter = EventEmitter->NewInstance();
+ process->Set(String::NewSymbol("AppjsEmitter"), AppjsEmitter);
return scope.Close(instance);
}
Handle<Value> App::CreateWindow2(const Arguments& args) {
-
HandleScope scope;
-
return scope.Close(Window::NewInstance(args));
}
Handle<Value> App::ScreenWidth(const Arguments& args) {
-
HandleScope scope;
-
Handle<Value> width = Integer::New(NativeWindow::ScreenWidth());
-
return scope.Close(width);
}
Handle<Value> App::ScreenHeight(const Arguments& args) {
-
HandleScope scope;
-
Handle<Value> height = Integer::New(NativeWindow::ScreenHeight());
-
return scope.Close(height);
}
@@ -89,10 +77,8 @@ Handle<Value> App::On(const Arguments& args) {
Local<Object> Emitter = Local<Object>::Cast(process->Get(String::NewSymbol("AppjsEmitter")));
Local<Function> On = Local<Function>::Cast(Emitter->Get(String::NewSymbol("on")));
- const unsigned argc = 2;
- Handle<Value> argv[argc] = { args[0] , args[1] };
-
- On->Call(Emitter,argc,argv);
+ Handle<Value> argv[2] = { args[0] , args[1] };
+ On->Call(Emitter, 2, argv);
return scope.Close(args.This());
}
View
25 src/appjs_window.cpp
@@ -15,8 +15,8 @@ Window::~Window() {};
Persistent<Function> Window::constructor;
-void Window::Init () {
- DECLARE_CONSTRUCTOR("Window");
+void Window::Init() {
+ DECLARE_CONSTRUCTOR("NativeWindow");
DECLARE_PROTOTYPE_METHOD("openDevTools", OpenDevTools);
DECLARE_PROTOTYPE_METHOD("closeDevTools", CloseDevTools);
DECLARE_PROTOTYPE_METHOD("restore", Restore);
@@ -52,28 +52,21 @@ void Window::Init () {
Handle<Value> Window::New(const Arguments& args) {
HandleScope scope;
- Handle<Object> self = Persistent<Object>::New(args.This());
+ Persistent<Object> options = Persistent<Object>::New(args[0]->ToObject());
+ Settings* settings = new Settings(options);
+ NativeWindow* window = new NativeWindow(settings);
- char* url = (args[0]->IsString()) ? V8StringToChar(args[0]->ToString()) : (char*) "/";
- Persistent<Object> windowSettings = Persistent<Object>::New((args[1]->IsObject()) ? args[1]->ToObject() : Object::New());
-
- Settings* settings = new Settings(windowSettings);
- NativeWindow* window = new NativeWindow(url, settings);
+ Persistent<Object> self = Persistent<Object>::New(args.This());
window->SetV8Handle(self);
-
- self->SetPointerInInternalField (0, window);
+ self->SetPointerInInternalField(0, window);
return scope.Close(args.This());
}
Handle<Value> Window::NewInstance(const Arguments& args) {
HandleScope scope;
-
- const unsigned argc = 2;
- Handle<Value> argv[argc] = { args[0],args[1] };
- Local<Object> instance = constructor->NewInstance(argc, argv);
-
- return scope.Close(instance);
+ Handle<Value> argv[1] = { args[0] };
+ return scope.Close(constructor->NewInstance(1, argv));
}
CREATE_INSTANCE_ACCESSOR(Window, Left, Integer, MAKE_INT32)
View
34 src/base/native_window.cpp
@@ -11,27 +11,27 @@ namespace appjs {
using namespace v8;
-NativeWindow::NativeWindow(char* url, Settings* settings){
- rect_.width = settings->getNumber("width",800);
- rect_.height = settings->getNumber("height",600);
- rect_.left = settings->getNumber("left",-1);
- rect_.top = settings->getNumber("top",-1);
- opacity_ = settings->getNumber("opacity",1);
- alpha_ = settings->getBoolean("alpha",false);
- show_chrome_ = settings->getBoolean("showChrome",true);
- auto_resize_ = settings->getBoolean("autoResize",false);
- resizable_ = settings->getBoolean("resizable",true);
- show_resize_grip = settings->getBoolean("showResizeGrip",false);
- fullscreen_ = settings->getBoolean("fullscreen",false);
- icons = new Settings(settings->getObject("icons", Object::New()));
-
+NativeWindow::NativeWindow(Settings* settings){
is_main_window_ = !initialized;
initialized = true;
closed_ = false;
- Init(url, settings);
-
- if (settings->getBoolean("topmost",false)) {
+ rect_.width = settings->getInteger("width", 800);
+ rect_.height = settings->getInteger("height", 600);
+ rect_.left = settings->getInteger("left", -1);
+ rect_.top = settings->getInteger("top", -1);
+ opacity_ = settings->getInteger("opacity", 1);
+ alpha_ = settings->getBoolean("alpha", false);
+ show_chrome_ = settings->getBoolean("showChrome", true);
+ auto_resize_ = settings->getBoolean("autoResize", false);
+ resizable_ = settings->getBoolean("resizable", true);
+ show_resize_grip = settings->getBoolean("showResizeGrip", false);
+ fullscreen_ = settings->getBoolean("fullscreen", false);
+ icons = new Settings(settings->getObject("icons"));
+
+ Init(settings->getString("url", ""), settings);
+
+ if (settings->getBoolean("topmost", false)) {
SetTopmost(true);
}
View
2 src/base/native_window.h
@@ -26,7 +26,7 @@ enum NW_STATE {
class NativeWindow {
public:
- NativeWindow(char* url, Settings* settings);
+ NativeWindow(Settings* settings);
~NativeWindow();
void Init(char* url, Settings* settings);
View
5 src/includes/util.cpp
@@ -98,6 +98,11 @@ Local<Object> Settings::getObject(const char* property, Local<Object> defaultVal
return (tmp->IsObject())? tmp->ToObject() : defaultValue;
}
+Local<Object> Settings::getObject(const char* property) {
+ Local<Value> tmp = get(property);
+ return tmp->IsObject() ? tmp->ToObject() : Object::New();
+}
+
Local<Value> Settings::get(const char* property) {
return settings_->Get(String::New(property));
}
View
1 src/includes/util.h
@@ -39,6 +39,7 @@ class Settings {
#endif
v8::Local<v8::Object> getObject(const char*,v8::Local<v8::Object>);
+ v8::Local<v8::Object> getObject(const char*);
private:
v8::Persistent<v8::Object> settings_;
View
10 src/windows/native_window_win.cpp
@@ -134,11 +134,6 @@ NativeWindow* NativeWindow::GetWindow(CefRefPtr<CefBrowser> browser){
return GetWindow(GetParent(browser->GetWindowHandle()));
}
-
-void NativeWindow::SetTitle(const char* title) {
- SetWindowText(handle_, title);
-}
-
// ############################
// ### NativeWindow methods ###
// ############################
@@ -277,6 +272,9 @@ const char* NativeWindow::GetTitle() {
return title;
}
+void NativeWindow::SetTitle(const char* title) {
+ SetWindowText(handle_, title);
+}
void NativeWindow::Move(int left, int top, int width, int height) {
UpdatePosition(left, top, width, height);
@@ -336,7 +334,7 @@ void NativeWindow::SetResizable(bool resizable) {
}
bool NativeWindow::GetResizable() {
- return GetWindowLongPtr(handle_, GWL_STYLE) & WS_SIZEBOX;
+ return GetWindowLongPtr(handle_, GWL_STYLE) & WS_SIZEBOX > 0;
}
void NativeWindow::SetShowChrome(bool showChrome) {

0 comments on commit 46adf69

Please sign in to comment.
Something went wrong with that request. Please try again.