• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/animation/ink_drop.h"
17 #include "ui/views/controls/button/button.h"
18 
19 // Helpers for template boiler-plate.
20 #define CEF_BUTTON_IMPL_T CEF_VIEW_IMPL_T
21 #define CEF_BUTTON_IMPL_A CEF_VIEW_IMPL_A
22 #define CEF_BUTTON_IMPL_D CefButtonImpl<CEF_BUTTON_IMPL_A>
23 
24 // Template for implementing CefButton-derived classes. See comments in
25 // view_impl.h for a usage overview.
26 CEF_BUTTON_IMPL_T class CefButtonImpl : public CEF_VIEW_IMPL_D {
27  public:
28   using ParentClass = CEF_VIEW_IMPL_D;
29 
30   // CefButton methods. When adding new As*() methods make sure to update
31   // CefViewAdapter::GetFor() in view_adapter.cc.
AsLabelButton()32   CefRefPtr<CefLabelButton> AsLabelButton() override { return nullptr; }
33   void SetState(cef_button_state_t state) override;
34   cef_button_state_t GetState() override;
35   void SetInkDropEnabled(bool enabled) override;
36   void SetTooltipText(const CefString& tooltip_text) override;
37   void SetAccessibleName(const CefString& name) override;
38 
39   // CefView methods:
AsButton()40   CefRefPtr<CefButton> AsButton() override { return this; }
41   void SetFocusable(bool focusable) override;
42 
43  protected:
44   // Create a new implementation object.
45   // Always call Initialize() after creation.
46   // |delegate| may be nullptr.
CefButtonImpl(CefRefPtr<CefViewDelegateClass> delegate)47   explicit CefButtonImpl(CefRefPtr<CefViewDelegateClass> delegate)
48       : ParentClass(delegate) {}
49 };
50 
SetState(cef_button_state_t state)51 CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetState(cef_button_state_t state) {
52   CEF_REQUIRE_VALID_RETURN_VOID();
53   views::Button::ButtonState old_state = ParentClass::root_view()->GetState();
54   views::Button::ButtonState new_state =
55       static_cast<views::Button::ButtonState>(state);
56 
57   if (views::InkDrop::Get(ParentClass::root_view())->ink_drop_mode() !=
58           views::InkDropHost::InkDropMode::OFF &&
59       !ParentClass::root_view()->IsFocusable()) {
60     // Ink drop state does not get set properly on state change when the button
61     // is non-focusable.
62     views::InkDropState ink_state = views::InkDropState::HIDDEN;
63     if (new_state == views::Button::STATE_PRESSED) {
64       ink_state = views::InkDropState::ACTIVATED;
65     } else if (old_state == views::Button::STATE_PRESSED) {
66       ink_state = views::InkDropState::DEACTIVATED;
67     }
68     views::InkDrop::Get(ParentClass::root_view())
69         ->AnimateToState(ink_state, nullptr);
70   }
71 
72   ParentClass::root_view()->SetState(new_state);
73 }
74 
GetState()75 CEF_BUTTON_IMPL_T cef_button_state_t CEF_BUTTON_IMPL_D::GetState() {
76   CEF_REQUIRE_VALID_RETURN(CEF_BUTTON_STATE_NORMAL);
77   return static_cast<cef_button_state_t>(ParentClass::root_view()->GetState());
78 }
79 
SetInkDropEnabled(bool enabled)80 CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetInkDropEnabled(bool enabled) {
81   CEF_REQUIRE_VALID_RETURN_VOID();
82   views::InkDrop::Get(ParentClass::root_view())
83       ->SetMode(enabled ? views::InkDropHost::InkDropMode::ON
84                         : views::InkDropHost::InkDropMode::OFF);
85   if (enabled) {
86     views::InkDrop::Get(ParentClass::root_view())
87         ->SetBaseColor(color_utils::BlendTowardMaxContrast(
88             ParentClass::root_view()->background()->get_color(), 0x61));
89   }
90 }
91 
SetTooltipText(const CefString & tooltip_text)92 CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetTooltipText(
93     const CefString& tooltip_text) {
94   CEF_REQUIRE_VALID_RETURN_VOID();
95   ParentClass::root_view()->SetTooltipText(tooltip_text);
96 }
97 
SetAccessibleName(const CefString & name)98 CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetAccessibleName(
99     const CefString& name) {
100   CEF_REQUIRE_VALID_RETURN_VOID();
101   ParentClass::root_view()->SetAccessibleName(name);
102 }
103 
SetFocusable(bool focusable)104 CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetFocusable(bool focusable) {
105   CEF_REQUIRE_VALID_RETURN_VOID();
106   ParentClass::root_view()->SetRequestFocusOnPress(focusable);
107   ParentClass::SetFocusable(focusable);
108 }
109 
110 #endif  // CEF_LIBCEF_BROWSER_VIEWS_BUTTON_IMPL_H_
111