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