Skip to content

Commit

Permalink
Merge pull request nwjs#3936 from jtg-gg/tray_fixes_for_nw13
Browse files Browse the repository at this point in the history
[Tray API] fixes for nw13
  • Loading branch information
rogerwang committed Nov 3, 2015
2 parents 2302d8e + b0bd587 commit f6323b2
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 28 deletions.
1 change: 1 addition & 0 deletions nw.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@
'src/api/menu/menu_delegate_mac.mm',
'src/api/menuitem/menuitem_delegate_mac.h',
'src/api/menuitem/menuitem_delegate_mac.mm',
'src/api/tray/tray_mac.mm',
'src/nw_content_mac.h',
'src/nw_content_mac.mm',
],
Expand Down
4 changes: 4 additions & 0 deletions src/api/_api_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
"channel": "stable",
"contexts": ["blessed_extension"]
},
"nw.Tray": {
"channel": "stable",
"contexts": ["blessed_extension"]
},
"nw.currentWindowInternal": {
"noparent": true,
"internal": true,
Expand Down
17 changes: 2 additions & 15 deletions src/api/nw_tray_api.h
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
#ifndef NW_API_MENU_API_H_
#define NW_API_MENU_API_H_
#ifndef NW_API_TRAY_API_H_
#define NW_API_TRAY_API_H_

#include <vector>

#include "extensions/browser/extension_function.h"

namespace extensions {

class NwMenuCreateItemFunction : public NWSyncExtensionFunction {
public:
NwMenuCreateItemFunction();
bool RunNWSync(base::ListValue* response, std::string* error) override;

protected:
~NwMenuCreateItemFunction() override;

DECLARE_EXTENSION_FUNCTION("nw.Menu.createItem", UNKNOWN)
private:
DISALLOW_COPY_AND_ASSIGN(NwMenuCreateItemFunction);
};

} // namespace extensions
#endif
8 changes: 4 additions & 4 deletions src/api/object_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
//#include "content/nw/src/api/screen/screen.h"
#include "content/nw/src/api/shell/shell.h"
//#include "content/nw/src/api/shortcut/shortcut.h"
//#include "content/nw/src/api/tray/tray.h"
#include "content/nw/src/api/tray/tray.h"
#include "content/nw/src/common/shell_switches.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
Expand Down Expand Up @@ -96,11 +96,11 @@ void ObjectManager::OnAllocateObject(int object_id,
} else if (type == "MenuItem") {
objects_registry_.AddWithID(
new MenuItem(object_id, weak_ptr_factory_.GetWeakPtr(), option, extension_id), object_id);
} else if (type == "Tray") {
objects_registry_.AddWithID(new Tray(object_id, weak_ptr_factory_.GetWeakPtr(), option, extension_id), object_id);
}
#if 0
else if (type == "Tray") {
objects_registry_.AddWithID(new Tray(object_id, weak_ptr_factory_.GetWeakPtr(), option), object_id);
} else if (type == "Clipboard") {
else if (type == "Clipboard") {
objects_registry_.AddWithID(
new Clipboard(object_id, weak_ptr_factory_.GetWeakPtr(), option), object_id);
} else if (type == "Window") {
Expand Down
4 changes: 3 additions & 1 deletion src/api/tray/tray_aura.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "base/files/file_path.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
#include "base/values.h"
#include "chrome/browser/status_icons/status_icon.h"
#include "chrome/browser/status_icons/status_icon_observer.h"
Expand Down Expand Up @@ -55,7 +56,7 @@ class TrayObserver : public StatusIconObserver {
data->SetInteger("x", cursor_pos.x());
data->SetInteger("y", cursor_pos.y());
args.Append(data);
tray_->object_manager()->SendEvent(tray_, "click", args);
tray_->object_manager()->SendEvent(tray_, "TrayClick", args);
}

private:
Expand Down Expand Up @@ -86,6 +87,7 @@ void Tray::SetTitle(const std::string& title) {
void Tray::SetIcon(const std::string& path) {
gfx::Image icon;
nw::Package* package = nw::InitNWPackage();
base::ThreadRestrictions::ScopedAllowIO allowIO;
nw::GetImage(package, base::FilePath::FromUTF8Unsafe(path), &icon);

if (!icon.IsEmpty())
Expand Down
14 changes: 7 additions & 7 deletions src/api/tray/tray_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@
#include "base/values.h"
#import <Cocoa/Cocoa.h>
#include "ui/gfx/screen.h"
#include "content/nw/src/api/dispatcher_host.h"
#include "content/nw/src/api/menu/menu.h"
#include "content/nw/src/api/object_manager.h"


@interface MacTrayObserver : NSObject {
@private
nwapi::Tray* tray_;
nw::Tray* tray_;
}
- (void)setBacking:(nwapi::Tray*)tray_;
- (void)setBacking:(nw::Tray*)tray_;
- (void)onClick:(id)sender;
@end

@implementation MacTrayObserver
- (void)setBacking:(nwapi::Tray*)newTray {
- (void)setBacking:(nw::Tray*)newTray {
tray_ = newTray;
}
- (void)onClick:(id)sender {
Expand All @@ -50,11 +50,11 @@ - (void)onClick:(id)sender {
data->SetInteger("x", pos.x);
data->SetInteger("y", pos.y);
args.Append(data);
tray_->dispatcher_host()->SendEvent(tray_,"click",args);
tray_->object_manager()->SendEvent(tray_,"TrayClick",args);
}
@end

namespace nwapi {
namespace nw {

void Tray::Create(const base::DictionaryValue& option) {
NSStatusBar *status_bar = [NSStatusBar systemStatusBar];
Expand Down Expand Up @@ -131,4 +131,4 @@ - (void)onClick:(id)sender {
[[NSStatusBar systemStatusBar] removeStatusItem:status_item_];
}

} // namespace nwapi
} // namespace nw
6 changes: 6 additions & 0 deletions src/nw_custom_bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ bool MakePathAbsolute(base::FilePath* file_path) {
if (!current_directory.IsAbsolute())
return false;

#ifdef OS_LINUX
//linux might gives "/" as current_directory, return false
if (current_directory.value().length() <= 1)
return false;
#endif

*file_path = current_directory.Append(*file_path);
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/resources/api_nw_menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var Event = require('event_bindings').Event;

var menuItems = { objs : {}, clickEvent: {} };

var Menu = function(id, option) {
var Menu = function Menu (id, option) {
if (option.type != 'contextmenu' && option.type != 'menubar')
throw new TypeError('Invalid menu type: ' + option.type);

Expand Down
182 changes: 182 additions & 0 deletions src/resources/api_nw_tray.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
var Binding = require('binding').Binding;
var forEach = require('utils').forEach;
var nw_binding = require('binding').Binding.create('nw.Tray');
var nwNative = requireNative('nw_natives');
var sendRequest = require('sendRequest');
var contextMenuNatives = requireNative('context_menus');
var messagingNatives = requireNative('messaging_natives');
var Event = require('event_bindings').Event;

var trayEvents = { objs: {}, clickEvent: {} };

function Tray(id, option) {
if (typeof option != 'object')
throw new TypeError('Invalid option');

if (!option.hasOwnProperty('title') && !option.hasOwnProperty('icon'))
throw new TypeError("Must set 'title' or 'icon' field in option");

if (!option.hasOwnProperty('title'))
option.title = '';
else
option.title = String(option.title);

if (option.hasOwnProperty('icon')) {
option.shadowIcon = String(option.icon);
option.icon = nwNative.getAbsolutePath(option.icon);
}

if (option.hasOwnProperty('alticon')) {
option.shadowAlticon = String(option.alticon);
option.alticon = nwNative.getAbsolutePath(option.alticon);
}

if (option.hasOwnProperty('iconsAreTemplates'))
option.iconsAreTemplates = Boolean(option.iconsAreTemplates);
else
option.iconsAreTemplates = true;

if (option.hasOwnProperty('tooltip'))
option.tooltip = String(option.tooltip);

if (option.hasOwnProperty('click')) {
if (typeof option.click != 'function') {
throw new TypeError("'click' must be a valid Function");
} else {
this.click = option.click;
}
}

if (option.hasOwnProperty('menu')) {
if (option.menu.constructor.name != 'Menu')
throw new TypeError("'menu' must be a valid Menu");

// Transfer only object id
privates(this).menu = option.menu;
option.menu = option.menu.id;
}

this.id = id;
privates(this).option = option;

// All properties must be set after initialization.
if (!option.hasOwnProperty('icon'))
option.shadowIcon = '';
if (!option.hasOwnProperty('alticon'))
option.shadowAlticon = '';
if (!option.hasOwnProperty('tooltip'))
option.tooltip = '';
}

Tray.prototype.handleGetter = function(name) {
return privates(this).option[name];
};

Tray.prototype.handleSetter = function(name, setter, type, value) {
value = type(value);
privates(this).option[name] = value;
nw.Object.callObjectMethod(this.id, 'Tray', setter, [ value ]);
};

Tray.prototype.__defineGetter__('title', function() {
return this.handleGetter('title');
});

Tray.prototype.__defineSetter__('title', function(val) {
this.handleSetter('title', 'SetTitle', String, val);
});

Tray.prototype.__defineGetter__('icon', function() {
return this.handleGetter('shadowIcon');
});

Tray.prototype.__defineGetter__('alticon', function() {
return this.handleGetter('shadowAlticon');
});

Tray.prototype.__defineSetter__('icon', function(val) {
privates(this).option.shadowIcon = String(val);
var real_path = val == '' ? '' : nwNative.getAbsolutePath(val);
this.handleSetter('icon', 'SetIcon', String, real_path);
});

Tray.prototype.__defineSetter__('alticon', function(val) {
privates(this).option.shadowAlticon = String(val);
var real_path = val == '' ? '' : nwNative.getAbsolutePath(val);
this.handleSetter('alticon', 'SetAltIcon', String, real_path);
});

Tray.prototype.__defineGetter__('iconsAreTemplates', function() {
return this.handleGetter('iconsAreTemplates');
});

Tray.prototype.__defineSetter__('iconsAreTemplates', function(val) {
this.handleSetter('iconsAreTemplates', 'SetIconsAreTemplates', Boolean, val);
});

Tray.prototype.__defineGetter__('tooltip', function() {
return this.handleGetter('tooltip');
});

Tray.prototype.__defineSetter__('tooltip', function(val) {
this.handleSetter('tooltip', 'SetTooltip', String, val);
});

Tray.prototype.__defineGetter__('menu', function() {
return privates(this).menu;
});

Tray.prototype.__defineSetter__('menu', function(val) {
if (val.constructor.name != 'Menu')
throw new TypeError("'menu' property requries a valid Menu");

privates(this).menu = val;
nw.Object.callObjectMethod(this.id, 'Tray', 'SetMenu', [ val.id ]);
});

Tray.prototype.remove = function() {
if (trayEvents.objs[this.id])
this.removeListener('click');
nw.Object.callObjectMethod(this.id, 'Tray', 'Remove', []);
}

Tray.prototype.on = function (event, callback) {
if (event == 'click') {
trayEvents.objs[this.id] = this;
this._onclick = callback;
}
}

Tray.prototype.removeListener = function (event) {
if (event == 'click') {
delete trayEvents.objs[this.id];
delete this._onclick;
}
}

nw_binding.registerCustomHook(function(bindingsAPI) {
var apiFunctions = bindingsAPI.apiFunctions;
trayEvents.clickEvent = new Event("NWObjectTrayClick");
trayEvents.clickEvent.addListener(function(id) {
if (!trayEvents.objs[id])
return;
trayEvents.objs[id]._onclick();
});
apiFunctions.setHandleRequest('destroy', function(id) {
sendRequest.sendRequestSync('nw.Object.destroy', [id], this.definition.parameters, {});
});
apiFunctions.setHandleRequest('create', function(option) {
var id = contextMenuNatives.GetNextContextMenuId();
if (typeof option != 'object' || !option)
option = { };

option.generatedId = id;
var ret = new Tray(id, option);
sendRequest.sendRequestSync('nw.Object.create', [id, 'Tray', option], this.definition.parameters, {});
messagingNatives.BindToGC(ret, nw.Tray.destroy.bind(undefined, id), -1);
return ret;
});
});

exports.binding = nw_binding.generate();

0 comments on commit f6323b2

Please sign in to comment.