feat: add MdTextButton to View APIs (#15328)

* view: make button focusable by default

* view: add MdTextButton

* view: add common methods to LabelButton
This commit is contained in:
Cheng Zhao 2018-10-23 23:57:13 +09:00 committed by John Kleinschmidt
parent 43a8b6039e
commit 260778e0fb
10 changed files with 157 additions and 2 deletions

View file

@ -13,8 +13,10 @@ namespace atom {
namespace api {
Button::Button(views::Button* button) : View(button) {
Button::Button(views::Button* impl) : View(impl) {
view()->set_owned_by_client();
// Make the button focusable as per the platform.
button()->SetFocusForPlatform();
}
Button::~Button() {}

View file

@ -27,6 +27,8 @@ class Button : public View, public views::ButtonListener {
// views::ButtonListener:
void ButtonPressed(views::Button* sender, const ui::Event& event) override;
views::Button* button() const { return static_cast<views::Button*>(view()); }
private:
DISALLOW_COPY_AND_ASSIGN(Button);
};

View file

@ -7,7 +7,6 @@
#include "atom/common/api/constructor.h"
#include "base/strings/utf_string_conversions.h"
#include "native_mate/dictionary.h"
#include "ui/views/controls/button/label_button.h"
#include "atom/common/node_includes.h"
@ -15,11 +14,29 @@ namespace atom {
namespace api {
LabelButton::LabelButton(views::LabelButton* impl) : Button(impl) {}
LabelButton::LabelButton(const std::string& text)
: Button(new views::LabelButton(this, base::UTF8ToUTF16(text))) {}
LabelButton::~LabelButton() {}
const base::string16& LabelButton::GetText() const {
return label_button()->GetText();
}
void LabelButton::SetText(const base::string16& text) {
label_button()->SetText(text);
}
bool LabelButton::IsDefault() const {
return label_button()->is_default();
}
void LabelButton::SetIsDefault(bool is_default) {
label_button()->SetIsDefault(is_default);
}
// static
mate::WrappableBase* LabelButton::New(mate::Arguments* args,
const std::string& text) {
@ -33,6 +50,11 @@ mate::WrappableBase* LabelButton::New(mate::Arguments* args,
void LabelButton::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "LabelButton"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("getText", &LabelButton::GetText)
.SetMethod("setText", &LabelButton::SetText)
.SetMethod("isDefault", &LabelButton::IsDefault)
.SetMethod("setIsDefault", &LabelButton::SetIsDefault);
}
} // namespace api

View file

@ -8,6 +8,7 @@
#include <string>
#include "atom/browser/api/views/atom_api_button.h"
#include "ui/views/controls/button/label_button.h"
namespace atom {
@ -21,10 +22,20 @@ class LabelButton : public Button {
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
const base::string16& GetText() const;
void SetText(const base::string16& text);
bool IsDefault() const;
void SetIsDefault(bool is_default);
protected:
explicit LabelButton(views::LabelButton* impl);
explicit LabelButton(const std::string& text);
~LabelButton() override;
views::LabelButton* label_button() const {
return static_cast<views::LabelButton*>(view());
}
private:
DISALLOW_COPY_AND_ASSIGN(LabelButton);
};

View file

@ -0,0 +1,57 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/api/views/atom_api_md_text_button.h"
#include "atom/common/api/constructor.h"
#include "base/strings/utf_string_conversions.h"
#include "native_mate/dictionary.h"
#include "atom/common/node_includes.h"
namespace atom {
namespace api {
MdTextButton::MdTextButton(const std::string& text)
: LabelButton(views::MdTextButton::Create(this, base::UTF8ToUTF16(text))) {}
MdTextButton::~MdTextButton() {}
// static
mate::WrappableBase* MdTextButton::New(mate::Arguments* args,
const std::string& text) {
// Constructor call.
auto* view = new MdTextButton(text);
view->InitWith(args->isolate(), args->GetThis());
return view;
}
// static
void MdTextButton::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "MdTextButton"));
}
} // namespace api
} // namespace atom
namespace {
using atom::api::MdTextButton;
void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("MdTextButton", mate::CreateConstructor<MdTextButton>(
isolate, base::Bind(&MdTextButton::New)));
}
} // namespace
NODE_BUILTIN_MODULE_CONTEXT_AWARE(atom_browser_md_text_button, Initialize)

View file

@ -0,0 +1,41 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_API_VIEWS_ATOM_API_MD_TEXT_BUTTON_H_
#define ATOM_BROWSER_API_VIEWS_ATOM_API_MD_TEXT_BUTTON_H_
#include <string>
#include "atom/browser/api/views/atom_api_label_button.h"
#include "ui/views/controls/button/md_text_button.h"
namespace atom {
namespace api {
class MdTextButton : public LabelButton {
public:
static mate::WrappableBase* New(mate::Arguments* args,
const std::string& text);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
protected:
explicit MdTextButton(const std::string& text);
~MdTextButton() override;
views::MdTextButton* md_text_button() const {
return static_cast<views::MdTextButton*>(view());
}
private:
DISALLOW_COPY_AND_ASSIGN(MdTextButton);
};
} // namespace api
} // namespace atom
#endif // ATOM_BROWSER_API_VIEWS_ATOM_API_MD_TEXT_BUTTON_H_