Skip to content

Commit

Permalink
[Linux] Implement menu and menubar
Browse files Browse the repository at this point in the history
  • Loading branch information
milani committed Oct 28, 2012
1 parent a14666e commit ff5cdaa
Show file tree
Hide file tree
Showing 22 changed files with 414 additions and 0 deletions.
3 changes: 3 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@
'src/appjs.cpp',
'src/appjs_app.cpp',
'src/appjs_window.cpp',
'src/appjs_menu.cpp',
'src/native_window/native_window.cpp',
'src/native_menu/native_menu.cpp',
'src/includes/cef_handler.cpp',
'src/includes/cef.cpp',
'src/includes/cef_loop.cpp',
Expand Down Expand Up @@ -286,6 +288,7 @@
['OS=="linux"', {
'sources': [
'src/native_window/native_window_linux.cpp',
'src/native_menu/native_menu_linux.cpp'
],
'defines': [
'__LINUX__',
Expand Down
34 changes: 34 additions & 0 deletions examples/hello-world/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,44 @@ var window = app.createWindow({
icons : __dirname + '/content/icons'
});

var menu = app.createMenu([{
label:'_File',
submenu:[
{
label:'E_xit',
action: function(){
window.close();
}
}
]
},{
label:'_Edit',
submenu:[
{
label:'Undo'
},{
label:''//separator
},
{
label:'Copy',
action:function(item){
console.log("item "+item.label+" clicked");
}
},
{
label:'Paste',
action:function(item){
console.log("item "+item.label+" clicked");
}
}
]
}]);

window.on('create', function(){
console.log("Window Created");
window.frame.show();
window.frame.center();
window.frame.setMenuBar(menu);
});

window.on('ready', function(){
Expand Down
7 changes: 7 additions & 0 deletions lib/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var EventEmitter = require('events').EventEmitter,
Window = require('./window'),
_App = require('./bindings').App,
NativeWindow = require('./bindings').NativeWindow,
NativeMenu = require('./bindings').NativeMenu,
WindowSettings = require('./settings').WindowSettings,
AppSettings = require('./settings').AppSettings;

Expand Down Expand Up @@ -35,6 +36,12 @@ function App(){
}

inherit(App, EventEmitter, [
function createMenu(options){
var self = this,
nativeMenu = new NativeMenu(options);

return nativeMenu;
},
function createWindow(url, options){
if (!this.settings) {
this.init();
Expand Down
1 change: 1 addition & 0 deletions lib/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ decorate(Frame.prototype, true, [
_(this).move((scr.w - this.width) / 2, (scr.h - this.height) / 2);
return this;
},
function setMenuBar(options){ _(this).setMenuBar(options); return this; },
function openDialog(settings,cb){
// enum NW_DIALOGTYPE
var types = [
Expand Down
2 changes: 2 additions & 0 deletions src/appjs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ Handle<Value> InitApp(const Arguments& args) {
void Init(Handle<v8::Object> target) {
App::Init();
Window::Init();
Menu::Init();

target->Set(String::NewSymbol("init"), FunctionTemplate::New(InitApp)->GetFunction());
target->Set(String::NewSymbol("App"), App::constructor);
target->Set(String::NewSymbol("NativeWindow"), Window::constructor);
target->Set(String::NewSymbol("NativeMenu"), Menu::constructor);
}

} /* appjs */
Expand Down
7 changes: 7 additions & 0 deletions src/appjs_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "appjs.h"
#include "appjs_app.h"
#include "appjs_window.h"
#include "appjs_menu.h"
#include "includes/cef.h"
#include "includes/util.h"

Expand All @@ -18,6 +19,7 @@ Persistent<Function> App::constructor;
void App::Init() {
DECLARE_CONSTRUCTOR("App");
DECLARE_PROTOTYPE_METHOD("createWindow",CreateWindow2);
DECLARE_PROTOTYPE_METHOD("createMenu",CreateMenu);
DECLARE_CLASS_FUNCTION(screenWidth, ScreenWidth);
DECLARE_CLASS_FUNCTION(screenHeight, ScreenHeight);
END_CONSTRUCTOR();
Expand Down Expand Up @@ -47,6 +49,11 @@ Handle<Value> App::CreateWindow2(const Arguments& args) {
return scope.Close(Window::NewInstance(args));
}

Handle<Value> App::CreateMenu(const Arguments& args) {
HandleScope scope;
return scope.Close(Menu::NewInstance(args));
}

Handle<Value> App::ScreenWidth(const Arguments& args) {
HandleScope scope;
Handle<Value> width = Integer::New(NativeWindow::ScreenWidth());
Expand Down
2 changes: 2 additions & 0 deletions src/appjs_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "appjs.h"
#include "appjs_window.h"
#include "appjs_menu.h"

namespace appjs {

Expand All @@ -14,6 +15,7 @@ class App : public node::ObjectWrap {
DEFINE_CLASS_FUNCTION(ScreenWidth);
DEFINE_CLASS_FUNCTION(ScreenHeight);
DEFINE_PROTOTYPE_METHOD(CreateWindow2);
DEFINE_PROTOTYPE_METHOD(CreateMenu);

static bool initialized_;
};
Expand Down
43 changes: 43 additions & 0 deletions src/appjs_menu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <node.h>
#include "appjs.h"
#include "appjs_menu.h"
#include "includes/cef_handler.h"
#include "includes/util.h"

extern CefRefPtr<ClientHandler> g_handler;

namespace appjs {

using namespace v8;

Menu::Menu(){}
Menu::~Menu(){}

Persistent<Function> Menu::constructor;

void Menu::Init() {
DECLARE_CONSTRUCTOR("NativeMenu");
END_CONSTRUCTOR();
}

Handle<Value> Menu::New(const Arguments& args) {
HandleScope scope;

Persistent<Object> options = Persistent<Object>::New(args[0]->ToObject());
Settings* settings = new Settings(options);
NativeMenu* menu = new NativeMenu(settings);

Persistent<Object> self = Persistent<Object>::New(args.This());
menu->SetV8Handle(self);
self->SetPointerInInternalField(0, menu);

return scope.Close(args.This());
}

Handle<Value> Menu::NewInstance(const Arguments& args) {
HandleScope scope;
Handle<Value> argv[1] = { args[0] };
return scope.Close(constructor->NewInstance(1, argv));
}

} /* appjs */
17 changes: 17 additions & 0 deletions src/appjs_menu.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef APPJS_MENU_H
#define APPJS_MENU_H
#pragma once

#include "appjs.h"
#include "native_menu/native_menu.h"

namespace appjs {

using namespace v8;

class Menu : public node::ObjectWrap {
DEFINE_OBJECT_FACTORY(Menu);
};

} /* appjs */
#endif /* end of APPJS_MENU_H */
43 changes: 43 additions & 0 deletions src/appjs_tray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <node.h>
#include "appjs.h"
#include "appjs_tray.h"
#include "includes/cef_handler.h"
#include "includes/util.h"

extern CefRefPtr<ClientHandler> g_handler;

namespace appjs {

using namespace v8;

Tray::Tray(){}
Tray::~Tray(){}

Persistent<Function> Tray::constructor;

void Tray::Init() {
DECLARE_CONSTRUCTOR("NativeTray");
END_CONSTRUCTOR();
}

Handle<Value> Tray::New(const Arguments& args) {
HandleScope scope;

Persistent<Object> options = Persistent<Object>::New(args[0]->ToObject());
Settings* settings = new Settings(options);
NativeTray* window = new NativeTray(settings);

Persistent<Object> self = Persistent<Object>::New(args.This());
tray->SetV8Handle(self);
self->SetPointerInInternalField(0, tray);

return scope.Close(args.This());
}

Handle<Value> Tray::NewInstance(const Arguments& args) {
HandleScope scope;
Handle<Value> argv[1] = { args[0] };
return scope.Close(constructor->NewInstance(1, argv));
}

} /* appjs */
17 changes: 17 additions & 0 deletions src/appjs_tray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef APPJS_TRAY_H
#define APPJS_TRAY_H
#pragma once

#include "appjs.h"
#include "native_tray/native_tray.h"

namespace appjs {

using namespace v8;

class Tray : public node::ObjectWrap {
DEFINE_OBJECT_FACTORY(Tray);
};

} /* appjs */
#endif /* end of APPJS_TRAY_H */
13 changes: 13 additions & 0 deletions src/appjs_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Persistent<Function> Window::constructor;

void Window::Init() {
DECLARE_CONSTRUCTOR("NativeWindow");
DECLARE_PROTOTYPE_METHOD("setMenuBar", SetMenuBar);
DECLARE_PROTOTYPE_METHOD("openDialog", OpenDialog);
DECLARE_PROTOTYPE_METHOD("openDevTools", OpenDevTools);
DECLARE_PROTOTYPE_METHOD("closeDevTools", CloseDevTools);
Expand Down Expand Up @@ -100,6 +101,18 @@ CREATE_PROTOTYPE_INVOKER(Window, Focus)
CREATE_PROTOTYPE_INVOKER(Window, Hide)
CREATE_PROTOTYPE_INVOKER(Window, Destroy)


Handle<Value> Window::SetMenuBar(const Arguments& args) {
HandleScope scope;

NativeMenu *menu = ObjectWrap::Unwrap<NativeMenu>(args[0]->ToObject());//(NativeMenu*)args[0]->ToObject()->GetPointerFromInternalField(0);
NativeWindow *window = ObjectWrap::Unwrap<NativeWindow>(args.This());

window->SetMenuBar(menu);

return scope.Close(args.This());
}

Handle<Value> Window::OpenDialog(const Arguments& args) {
HandleScope scope;

Expand Down
1 change: 1 addition & 0 deletions src/appjs_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ using namespace v8;

class Window : public node::ObjectWrap {
DEFINE_OBJECT_FACTORY(Window);
DEFINE_PROTOTYPE_METHOD(SetMenuBar);
DEFINE_PROTOTYPE_METHOD(OpenDialog);
DEFINE_PROTOTYPE_METHOD(OpenDevTools);
DEFINE_PROTOTYPE_METHOD(CloseDevTools);
Expand Down
18 changes: 18 additions & 0 deletions src/includes/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,26 @@ Local<Object> Settings::getObject(const char* property) {
return tmp->IsObject() ? tmp->ToObject() : Object::New();
}

Persistent<Object> Settings::getSettings() {
return settings_;
}

Local<Value> Settings::get(const char* property) {
return settings_->Get(String::New(property));
}

Local<Object> Settings::getObject(const int index,Local<Object> defaultValue) {
Local<Value> tmp = get(index);
return tmp->IsObject() ? tmp->ToObject() : defaultValue;
}

Local<Object> Settings::getObject(const int index) {
Local<Value> tmp = get(index);
return tmp->IsObject() ? tmp->ToObject() : Object::New();
}

Local<Value> Settings::get(const int index) {
return settings_->Get(index);
}

}
4 changes: 4 additions & 0 deletions src/includes/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ class Settings {

v8::Local<v8::Object> getObject(const char*,v8::Local<v8::Object>);
v8::Local<v8::Object> getObject(const char*);
v8::Local<v8::Object> getObject(const int,v8::Local<v8::Object>);
v8::Local<v8::Object> getObject(const int);
v8::Persistent<v8::Object> getSettings();

private:
v8::Persistent<v8::Object> settings_;
v8::Local<v8::Value> get(const int);
v8::Local<v8::Value> get(const char*);
};

Expand Down
25 changes: 25 additions & 0 deletions src/native_menu/native_menu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "includes/cef.h"
#include "includes/cef_handler.h"
#include "native_menu/native_menu.h"

extern CefRefPtr<ClientHandler> g_handler;

namespace appjs {

using namespace v8;

NativeMenu::NativeMenu(Settings* settings){
Init(settings);
}

NativeMenu::~NativeMenu(){
// destroy all v8 persistent objects
}


void NativeMenu::SetV8Handle(Handle<Object> v8handle) {
v8handle_ = v8handle;
}


} /* appjs */
Loading

0 comments on commit ff5cdaa

Please sign in to comment.