2016-01-19 21:09:01 +01:00
|
|
|
// Copyright 2016 The Chromium Embedded Framework Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found
|
|
|
|
// in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef CEF_LIBCEF_BROWSER_VIEWS_BUTTON_VIEW_H_
|
|
|
|
#define CEF_LIBCEF_BROWSER_VIEWS_BUTTON_VIEW_H_
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "include/views/cef_button_delegate.h"
|
|
|
|
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
#include "libcef/browser/views/view_view.h"
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
2021-07-23 18:40:13 +02:00
|
|
|
#include "ui/views/animation/ink_drop.h"
|
2017-09-06 23:40:58 +02:00
|
|
|
#include "ui/views/controls/button/button.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// Helpers for template boiler-plate.
|
|
|
|
#define CEF_BUTTON_VIEW_T CEF_VIEW_VIEW_T
|
|
|
|
#define CEF_BUTTON_VIEW_A CEF_VIEW_VIEW_A
|
|
|
|
#define CEF_BUTTON_VIEW_D CefButtonView<CEF_BUTTON_VIEW_A>
|
|
|
|
|
2017-09-06 23:40:58 +02:00
|
|
|
// Template for implementing views::Button-derived classes. The
|
|
|
|
// views::Button-derived type passed to this template must extend
|
2016-01-19 21:09:01 +01:00
|
|
|
// views::ButtonListener (for example, see LabelButtonEx from
|
|
|
|
// basic_label_button_view.h). See comments in view_impl.h for a usage overview.
|
|
|
|
CEF_BUTTON_VIEW_T class CefButtonView : public CEF_VIEW_VIEW_D {
|
|
|
|
public:
|
2021-12-06 21:40:25 +01:00
|
|
|
using ParentClass = CEF_VIEW_VIEW_D;
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// |cef_delegate| may be nullptr.
|
2021-04-11 22:10:11 +02:00
|
|
|
template <typename... Args>
|
|
|
|
explicit CefButtonView(CefViewDelegateClass* cef_delegate, Args... args)
|
|
|
|
: ParentClass(cef_delegate, args...) {}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// Returns the CefButton associated with this view. See comments on
|
|
|
|
// CefViewView::GetCefView.
|
|
|
|
CefRefPtr<CefButton> GetCefButton() const {
|
|
|
|
CefRefPtr<CefButton> button = ParentClass::GetCefView()->AsButton();
|
|
|
|
DCHECK(button);
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
2017-09-06 23:40:58 +02:00
|
|
|
// views::Button methods:
|
|
|
|
void StateChanged(views::Button::ButtonState old_state) override;
|
2017-02-22 19:05:27 +01:00
|
|
|
|
2020-12-02 23:31:49 +01:00
|
|
|
// LabelButtonEx methods:
|
|
|
|
void ButtonPressed(const ui::Event& event) override;
|
2016-01-19 21:09:01 +01:00
|
|
|
};
|
|
|
|
|
2017-03-03 23:37:23 +01:00
|
|
|
CEF_BUTTON_VIEW_T void CEF_BUTTON_VIEW_D::StateChanged(
|
2017-09-06 23:40:58 +02:00
|
|
|
views::Button::ButtonState old_state) {
|
2017-03-03 23:37:23 +01:00
|
|
|
ParentClass::StateChanged(old_state);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (ParentClass::cef_delegate()) {
|
2017-02-22 19:05:27 +01:00
|
|
|
ParentClass::cef_delegate()->OnButtonStateChanged(GetCefButton());
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-02-22 19:05:27 +01:00
|
|
|
}
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
CEF_BUTTON_VIEW_T void CEF_BUTTON_VIEW_D::ButtonPressed(
|
2017-05-17 11:29:28 +02:00
|
|
|
const ui::Event& event) {
|
2017-08-04 00:55:19 +02:00
|
|
|
// Callback may trigger new animation state.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (ParentClass::cef_delegate()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
ParentClass::cef_delegate()->OnButtonPressed(GetCefButton());
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-07-23 18:40:13 +02:00
|
|
|
if (views::InkDrop::Get(this)->ink_drop_mode() !=
|
2021-06-04 03:34:56 +02:00
|
|
|
views::InkDropHost::InkDropMode::OFF &&
|
2017-08-04 00:55:19 +02:00
|
|
|
!ParentClass::IsFocusable() &&
|
2020-08-29 00:39:23 +02:00
|
|
|
ParentClass::GetState() != views::Button::STATE_PRESSED) {
|
2017-08-04 00:55:19 +02:00
|
|
|
// Ink drop state does not get reset properly on click when the button is
|
|
|
|
// non-focusable. Reset the ink drop state here if the state has not been
|
|
|
|
// explicitly set to pressed by the OnButtonPressed callback calling
|
|
|
|
// SetState (which also sets the ink drop state).
|
2021-07-23 18:40:13 +02:00
|
|
|
views::InkDrop::Get(this)->AnimateToState(
|
2021-06-04 03:34:56 +02:00
|
|
|
views::InkDropState::HIDDEN, ui::LocatedEvent::FromIfValid(&event));
|
2017-02-25 06:03:31 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CEF_LIBCEF_BROWSER_VIEWS_BUTTON_VIEW_H_
|