1 // Copyright 2016 The Chromium Embedded Framework Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be found 3 // in the LICENSE file. 4 5 #ifndef CEF_LIBCEF_BROWSER_VIEWS_BUTTON_IMPL_H_ 6 #define CEF_LIBCEF_BROWSER_VIEWS_BUTTON_IMPL_H_ 7 #pragma once 8 9 #include "include/views/cef_button.h" 10 #include "include/views/cef_label_button.h" 11 12 #include "libcef/browser/views/view_impl.h" 13 14 #include "base/logging.h" 15 #include "ui/gfx/color_utils.h" 16 #include "ui/views/controls/button/button.h" 17 18 // Helpers for template boiler-plate. 19 #define CEF_BUTTON_IMPL_T CEF_VIEW_IMPL_T 20 #define CEF_BUTTON_IMPL_A CEF_VIEW_IMPL_A 21 #define CEF_BUTTON_IMPL_D CefButtonImpl<CEF_BUTTON_IMPL_A> 22 23 // Template for implementing CefButton-derived classes. See comments in 24 // view_impl.h for a usage overview. 25 CEF_BUTTON_IMPL_T class CefButtonImpl : public CEF_VIEW_IMPL_D { 26 public: 27 typedef CEF_VIEW_IMPL_D ParentClass; 28 29 // CefButton methods. When adding new As*() methods make sure to update 30 // CefViewAdapter::GetFor() in view_adapter.cc. AsLabelButton()31 CefRefPtr<CefLabelButton> AsLabelButton() override { return nullptr; } 32 void SetState(cef_button_state_t state) override; 33 cef_button_state_t GetState() override; 34 void SetInkDropEnabled(bool enabled) override; 35 void SetTooltipText(const CefString& tooltip_text) override; 36 void SetAccessibleName(const CefString& name) override; 37 38 // CefView methods: AsButton()39 CefRefPtr<CefButton> AsButton() override { return this; } 40 void SetFocusable(bool focusable) override; 41 42 protected: 43 // Create a new implementation object. 44 // Always call Initialize() after creation. 45 // |delegate| may be nullptr. CefButtonImpl(CefRefPtr<CefViewDelegateClass> delegate)46 explicit CefButtonImpl(CefRefPtr<CefViewDelegateClass> delegate) 47 : ParentClass(delegate) {} 48 }; 49 SetState(cef_button_state_t state)50CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetState(cef_button_state_t state) { 51 CEF_REQUIRE_VALID_RETURN_VOID(); 52 views::Button::ButtonState old_state = ParentClass::root_view()->GetState(); 53 views::Button::ButtonState new_state = 54 static_cast<views::Button::ButtonState>(state); 55 56 if (ParentClass::root_view()->ink_drop_mode() != 57 views::Button::InkDropMode::OFF && 58 !ParentClass::root_view()->IsFocusable()) { 59 // Ink drop state does not get set properly on state change when the button 60 // is non-focusable. 61 views::InkDropState ink_state = views::InkDropState::HIDDEN; 62 if (new_state == views::Button::STATE_PRESSED) { 63 ink_state = views::InkDropState::ACTIVATED; 64 } else if (old_state == views::Button::STATE_PRESSED) { 65 ink_state = views::InkDropState::DEACTIVATED; 66 } 67 ParentClass::root_view()->AnimateInkDrop(ink_state, nullptr); 68 } 69 70 ParentClass::root_view()->SetState(new_state); 71 } 72 GetState()73CEF_BUTTON_IMPL_T cef_button_state_t CEF_BUTTON_IMPL_D::GetState() { 74 CEF_REQUIRE_VALID_RETURN(CEF_BUTTON_STATE_NORMAL); 75 return static_cast<cef_button_state_t>(ParentClass::root_view()->GetState()); 76 } 77 SetInkDropEnabled(bool enabled)78CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetInkDropEnabled(bool enabled) { 79 CEF_REQUIRE_VALID_RETURN_VOID(); 80 ParentClass::root_view()->SetInkDropMode( 81 enabled ? views::InkDropHostView::InkDropMode::ON 82 : views::InkDropHostView::InkDropMode::OFF); 83 if (enabled) { 84 ParentClass::root_view()->SetInkDropBaseColor( 85 color_utils::BlendTowardMaxContrast( 86 ParentClass::root_view()->background()->get_color(), 0x61)); 87 } 88 } 89 SetTooltipText(const CefString & tooltip_text)90CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetTooltipText( 91 const CefString& tooltip_text) { 92 CEF_REQUIRE_VALID_RETURN_VOID(); 93 ParentClass::root_view()->SetTooltipText(tooltip_text); 94 } 95 SetAccessibleName(const CefString & name)96CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetAccessibleName( 97 const CefString& name) { 98 CEF_REQUIRE_VALID_RETURN_VOID(); 99 ParentClass::root_view()->SetAccessibleName(name); 100 } 101 SetFocusable(bool focusable)102CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetFocusable(bool focusable) { 103 CEF_REQUIRE_VALID_RETURN_VOID(); 104 ParentClass::root_view()->SetRequestFocusOnPress(focusable); 105 ParentClass::SetFocusable(focusable); 106 } 107 108 #endif // CEF_LIBCEF_BROWSER_VIEWS_BUTTON_IMPL_H_ 109