Code cleanup

This commit is contained in:
Samuel Attard 2017-04-24 13:07:42 +10:00
parent 03688b9415
commit c6196810a6
No known key found for this signature in database
GPG key ID: 273DC1869D8F13EF
11 changed files with 758 additions and 614 deletions

View file

@ -4,6 +4,7 @@
#include "atom/browser/api/atom_api_notification.h"
#include <map>
#include <string>
#include "atom/browser/api/atom_api_menu.h"
@ -25,7 +26,9 @@ namespace api {
int id_counter = 1;
std::map<int, Notification*> notifications_;
Notification::Notification(v8::Isolate* isolate, v8::Local<v8::Object> wrapper, mate::Arguments* args) {
Notification::Notification(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
mate::Arguments* args) {
InitWith(isolate, wrapper);
mate::Dictionary opts;
@ -65,19 +68,46 @@ Notification* Notification::FromID(int id) {
}
// Getters
int Notification::GetID() { return id_; }
base::string16 Notification::GetTitle() { return title_; }
base::string16 Notification::GetBody() { return body_; }
bool Notification::GetSilent() { return silent_; }
base::string16 Notification::GetReplyPlaceholder() { return reply_placeholder_; }
bool Notification::GetHasReply() { return has_reply_; }
int Notification::GetID() {
return id_;
}
base::string16 Notification::GetTitle() {
return title_;
}
base::string16 Notification::GetBody() {
return body_;
}
bool Notification::GetSilent() {
return silent_;
}
base::string16 Notification::GetReplyPlaceholder() {
return reply_placeholder_;
}
bool Notification::GetHasReply() {
return has_reply_;
}
// Setters
void Notification::SetTitle(base::string16 new_title) { title_ = new_title; NotifyPropsUpdated(); }
void Notification::SetBody(base::string16 new_body) { body_ = new_body; NotifyPropsUpdated(); }
void Notification::SetSilent(bool new_silent) { silent_ = new_silent; NotifyPropsUpdated(); }
void Notification::SetReplyPlaceholder(base::string16 new_reply_placeholder) { reply_placeholder_ = new_reply_placeholder; NotifyPropsUpdated(); }
void Notification::SetHasReply(bool new_has_reply) { has_reply_ = new_has_reply; NotifyPropsUpdated(); }
void Notification::SetTitle(base::string16 new_title) {
title_ = new_title;
NotifyPropsUpdated();
}
void Notification::SetBody(base::string16 new_body) {
body_ = new_body;
NotifyPropsUpdated();
}
void Notification::SetSilent(bool new_silent) {
silent_ = new_silent;
NotifyPropsUpdated();
}
void Notification::SetReplyPlaceholder(base::string16 new_reply_placeholder) {
reply_placeholder_ = new_reply_placeholder;
NotifyPropsUpdated();
}
void Notification::SetHasReply(bool new_has_reply) {
has_reply_ = new_has_reply;
NotifyPropsUpdated();
}
void Notification::OnClicked() {
Emit("click");
@ -93,7 +123,7 @@ void Notification::OnShown() {
// static
void Notification::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "Notification"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable()
@ -102,26 +132,30 @@ void Notification::BuildPrototype(v8::Isolate* isolate,
.SetProperty("title", &Notification::GetTitle, &Notification::SetTitle)
.SetProperty("body", &Notification::GetBody, &Notification::SetBody)
.SetProperty("silent", &Notification::GetSilent, &Notification::SetSilent)
.SetProperty("replyPlaceholder", &Notification::GetReplyPlaceholder, &Notification::SetReplyPlaceholder)
.SetProperty("hasReply", &Notification::GetHasReply, &Notification::SetHasReply);
.SetProperty("replyPlaceholder", &Notification::GetReplyPlaceholder,
&Notification::SetReplyPlaceholder)
.SetProperty("hasReply", &Notification::GetHasReply,
&Notification::SetHasReply);
}
} // namespace api
} // namespace atom
namespace {
using atom::api::Notification;
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
Notification::SetConstructor(isolate, base::Bind(&Notification::New));
mate::Dictionary dict(isolate, exports);
dict.Set("Notification", Notification::GetConstructor(isolate)->GetFunction());
dict.Set("Notification",
Notification::GetConstructor(isolate)->GetFunction());
}
} // namespace

View file

@ -20,7 +20,7 @@ namespace atom {
namespace api {
class Notification : public mate::TrackableObject<Notification>,
public NotifictionObserver {
public NotifictionObserver {
public:
static mate::WrappableBase* New(mate::Arguments* args);
static bool HasID(int id);
@ -35,7 +35,9 @@ class Notification : public mate::TrackableObject<Notification>,
void OnShown() override;
protected:
Notification(v8::Isolate* isolate, v8::Local<v8::Object> wrapper, mate::Arguments* args);
Notification(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
mate::Arguments* args);
~Notification() override;
void OnInitialProps();

View file

@ -48,7 +48,8 @@ void Notification::Show() {
if (can_toast_) {
atom::AtomToastHandler* handler = new atom::AtomToastHandler(this);
WinToastLib::WinToastTemplate::WinToastTemplateType toastType = WinToastLib::WinToastTemplate::TextOneLine;
WinToastLib::WinToastTemplate::WinToastTemplateType toastType =
WinToastLib::WinToastTemplate::TextOneLine;
if (!has_icon_) {
if (body_ != L"") {
toastType = WinToastLib::WinToastTemplate::TextTwoLines;
@ -62,40 +63,41 @@ void Notification::Show() {
toastType = WinToastLib::WinToastTemplate::ImageWithOneLine;
}
}
WinToastLib::WinToastTemplate toast = WinToastLib::WinToastTemplate(toastType);
WinToastLib::WinToastTemplate toast =
WinToastLib::WinToastTemplate(toastType);
std::string filename = base::MD5String(base::UTF16ToUTF8(icon_path_)) + ".png";
base::FilePath savePath = temp_dir_.GetPath().Append(base::UTF8ToUTF16(filename));
std::string filename =
base::MD5String(base::UTF16ToUTF8(icon_path_)) + ".png";
base::FilePath savePath =
temp_dir_.GetPath().Append(base::UTF8ToUTF16(filename));
if (has_icon_ && SaveIconToPath(image, savePath)) {
toast.setImagePath(savePath.value());
}
toast.setTextField(title_, WinToastLib::WinToastTemplate::TextField::FirstLine);
toast.setTextField(body_, WinToastLib::WinToastTemplate::TextField::SecondLine);
toast.setTextField(title_,
WinToastLib::WinToastTemplate::TextField::FirstLine);
toast.setTextField(body_,
WinToastLib::WinToastTemplate::TextField::SecondLine);
toast.setSilent(silent_);
WinToastLib::WinToast::instance()->showToast(toast, handler);
OnShown();
} else {
AtomNotificationDelegateAdapter* adapter = new AtomNotificationDelegateAdapter(this);
AtomNotificationDelegateAdapter* adapter =
new AtomNotificationDelegateAdapter(this);
auto notif = presenter->CreateNotification(adapter);
GURL nullUrl = *(new GURL);
notif->Show(
title_,
body_,
"",
nullUrl,
image,
silent_
);
notif->Show(title_, body_, "", nullUrl, image, silent_);
}
}
void Notification::OnInitialProps() {
if (!initialized_) {
Browser* browser = Browser::Get();
WinToastLib::WinToast::instance()->setAppName(base::UTF8ToUTF16(browser->GetName()));
WinToastLib::WinToast::instance()->setAppUserModelId(browser->GetAppUserModelID());
WinToastLib::WinToast::instance()->setAppName(
base::UTF8ToUTF16(browser->GetName()));
WinToastLib::WinToast::instance()->setAppUserModelId(
browser->GetAppUserModelID());
can_toast_ = WinToastLib::WinToast::instance()->initialize();
temp_dir_.CreateUniqueTempDir();
}
@ -104,10 +106,6 @@ void Notification::OnInitialProps() {
}
}
void Notification::NotifyPropsUpdated() {
}
}
}
void Notification::NotifyPropsUpdated() {}
} // namespace api
} // namespace atom