• 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_VIEW_H_
6 #define CEF_LIBCEF_BROWSER_VIEWS_BUTTON_VIEW_H_
7 #pragma once
8 
9 #include "include/views/cef_button_delegate.h"
10 
11 #include "libcef/browser/thread_util.h"
12 #include "libcef/browser/views/view_view.h"
13 
14 #include "base/logging.h"
15 #include "ui/views/controls/button/button.h"
16 
17 // Helpers for template boiler-plate.
18 #define CEF_BUTTON_VIEW_T CEF_VIEW_VIEW_T
19 #define CEF_BUTTON_VIEW_A CEF_VIEW_VIEW_A
20 #define CEF_BUTTON_VIEW_D CefButtonView<CEF_BUTTON_VIEW_A>
21 
22 // Template for implementing views::Button-derived classes. The
23 // views::Button-derived type passed to this template must extend
24 // views::ButtonListener (for example, see LabelButtonEx from
25 // basic_label_button_view.h). See comments in view_impl.h for a usage overview.
26 CEF_BUTTON_VIEW_T class CefButtonView : public CEF_VIEW_VIEW_D {
27  public:
28   typedef CEF_VIEW_VIEW_D ParentClass;
29 
30   // |cef_delegate| may be nullptr.
31   template <typename... Args>
CefButtonView(CefViewDelegateClass * cef_delegate,Args...args)32   explicit CefButtonView(CefViewDelegateClass* cef_delegate, Args... args)
33       : ParentClass(cef_delegate, args...) {}
34 
35   // Returns the CefButton associated with this view. See comments on
36   // CefViewView::GetCefView.
GetCefButton()37   CefRefPtr<CefButton> GetCefButton() const {
38     CefRefPtr<CefButton> button = ParentClass::GetCefView()->AsButton();
39     DCHECK(button);
40     return button;
41   }
42 
43   // views::Button methods:
44   void StateChanged(views::Button::ButtonState old_state) override;
45 
46   // LabelButtonEx methods:
47   void ButtonPressed(const ui::Event& event) override;
48 };
49 
StateChanged(views::Button::ButtonState old_state)50 CEF_BUTTON_VIEW_T void CEF_BUTTON_VIEW_D::StateChanged(
51     views::Button::ButtonState old_state) {
52   ParentClass::StateChanged(old_state);
53   if (ParentClass::cef_delegate())
54     ParentClass::cef_delegate()->OnButtonStateChanged(GetCefButton());
55 }
56 
ButtonPressed(const ui::Event & event)57 CEF_BUTTON_VIEW_T void CEF_BUTTON_VIEW_D::ButtonPressed(
58     const ui::Event& event) {
59   // Callback may trigger new animation state.
60   if (ParentClass::cef_delegate())
61     ParentClass::cef_delegate()->OnButtonPressed(GetCefButton());
62   if (ParentClass::ink_drop_mode() != views::Button::InkDropMode::OFF &&
63       !ParentClass::IsFocusable() &&
64       ParentClass::GetState() != views::Button::STATE_PRESSED) {
65     // Ink drop state does not get reset properly on click when the button is
66     // non-focusable. Reset the ink drop state here if the state has not been
67     // explicitly set to pressed by the OnButtonPressed callback calling
68     // SetState (which also sets the ink drop state).
69     ParentClass::AnimateInkDrop(views::InkDropState::HIDDEN,
70                                 ui::LocatedEvent::FromIfValid(&event));
71   }
72 }
73 
74 #endif  // CEF_LIBCEF_BROWSER_VIEWS_BUTTON_VIEW_H_
75