Nodejs擴充,實現訊息彈窗

來源:互聯網
上載者:User

Nodejs擴充,實現訊息彈窗

 

 

模組的C++代碼 node_gtknotify.cc

#include

#include

#include

#include

#include

 

using namespace v8;

 

class GtkNotify : node::ObjectWrap{

public:

GtkNotify(){}

~GtkNotify(){}

 

std::string title;

std::string icon;

 

static Persistent persistent_function_template;

 

static void Init(Handletarget){

HandleScope scope;

Local local_function_template = FunctionTemplate::New(New);

GtkNotify::persistent_function_template = Persistent::New(local_function_template);

GtkNotify::persistent_function_template->InstanceTemplate()->SetInternalFieldCount(1);

GtkNotify::persistent_function_template->SetClassName(String::NewSymbol(Notification));

GtkNotify::persistent_function_template->InstanceTemplate()->SetAccessor(String::New(title), GetTitle, SetTitle);

GtkNotify::persistent_function_template->InstanceTemplate()->SetAccessor(String::New(icon), GetIcon, SetIcon);

NODE_SET_PROTOTYPE_METHOD(GtkNotify::persistent_function_template, send, Send);

target->Set(String::NewSymbol(notification), GtkNotify::persistent_function_template->GetFunction());

}

 

static Handle New(const Arguments& args){

HandleScope scope;

GtkNotify* instance = new GtkNotify();

instance->title = Node.js;

instance->icon = terminal;

instance->Wrap(args.This());

return args.This();

}

 

static Handle Send(const Arguments& args){

HandleScope scope;

GtkNotify* instance = node::ObjectWrap::Unwrap(args.This());

String::Utf8Value v8str(args[0]);

//彈出訊息框

Notify::init(Basic);

Notify::Notification n(instance->title.c_str(), *v8str, instance->icon.c_str());

n.show();

return Boolean::New(true);

}

 

static Handle GetTitle(Local property, const AccessorInfo& info){

GtkNotify* instance = node::ObjectWrap::Unwrap(info.Holder());

return String::New(instance->title.c_str());

}

 

static Handle GetIcon(Local property, const AccessorInfo& info){

GtkNotify* instance = node::ObjectWrap::Unwrap(info.Holder());

return String::New(instance->icon.c_str());

}

 

static void SetTitle(Local property, Local value, const AccessorInfo& info) {

GtkNotify* instance = node::ObjectWrap::Unwrap(info.Holder());

String::Utf8Value v8str(value);

instance->title = *v8str;

}

 

static void SetIcon(Local property, Local value, const AccessorInfo& info) {

GtkNotify* instance = node::ObjectWrap::Unwrap(info.Holder());

String::Utf8Value v8str(value);

instance->icon = *v8str;

}

};

 

Persistent GtkNotify::persistent_function_template;

 

extern C{

static void init(Handletarget){

GtkNotify::Init(target);

}

NODE_MODULE(node_gtknotify, init);

}

 

node-gyp設定檔 binding.gyp

{

targets: [

{

target_name: node_gtknotify,

sources: [ src/node_gtknotify.cc ]

}

]

}

 

目錄結構

 

運行命令

node-gyp configure

node-gyp build

 

如果沒有安裝相應的庫/路徑找不到,中間會出現標頭檔找不到的錯誤;

笨拙的解決方案,在build下面的Makefile中添加

CXXFLAGS += -I/usr/include/glibmm-2.4 -I/usr/lib/x86_64-linux-gnu/glibmm-2.4/include -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/sigc++-2.0 -I/usr/lib/x86_64-linux-gnu/sigc++-2.0/include -I/usr/include/giomm-2.4 -I/usr/include/gdkmm-3.0 -I/usr/lib/x86_64-linux-gnu/gdkmm-3.0/include -I/usr/include/pangomm-1.4 -I/usr/lib/x86_64-linux-gnu/pangomm-1.4/include -I/usr/include/gtk-3.0 -I/usr/include/pango-1.0 -I/usr/include/cairo -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/cairomm-1.0 -I/usr/include/freetype2 -I/usr/include/gtkmm-3.0 -I/usr/lib/x86_64-linux-gnu/gtkmm-3.0/include -I/usr/include/atkmm-1.6 -I/usr/include/atk-1.0 -I/usr/include/libnotifymm-1.0

 

上述的庫是根據錯誤提示進行安裝的

 

Javascript測試代碼

var notify = require(./build/Release/node_gtknotify);

var notification = new notify.notification();

notification.title = Notification title;

notification.icon = emblem-default; // see /usr/share/icons/gnome/16x16

notification.send(hello,world);

 

注意,運行時可能有錯誤提示:**符號找不到,這是因為沒有添加相應的共用連結庫

 

 

解決方案:在build/node_gtknotify.target.mk中添加

LIBS := -lglibmm-2.4 -lnotify -lnotifymm-1.0

 

運行效果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.