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_IMPL_H_
|
|
|
|
#define CEF_LIBCEF_BROWSER_VIEWS_BUTTON_IMPL_H_
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
2024-04-30 17:45:07 +02:00
|
|
|
#include "cef/include/views/cef_button.h"
|
|
|
|
#include "cef/include/views/cef_label_button.h"
|
|
|
|
#include "cef/libcef/browser/views/view_impl.h"
|
2017-02-24 19:02:07 +01:00
|
|
|
#include "ui/gfx/color_utils.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_IMPL_T CEF_VIEW_IMPL_T
|
|
|
|
#define CEF_BUTTON_IMPL_A CEF_VIEW_IMPL_A
|
|
|
|
#define CEF_BUTTON_IMPL_D CefButtonImpl<CEF_BUTTON_IMPL_A>
|
|
|
|
|
|
|
|
// Template for implementing CefButton-derived classes. See comments in
|
|
|
|
// view_impl.h for a usage overview.
|
|
|
|
CEF_BUTTON_IMPL_T class CefButtonImpl : public CEF_VIEW_IMPL_D {
|
|
|
|
public:
|
2021-12-06 21:40:25 +01:00
|
|
|
using ParentClass = CEF_VIEW_IMPL_D;
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// CefButton methods. When adding new As*() methods make sure to update
|
|
|
|
// CefViewAdapter::GetFor() in view_adapter.cc.
|
|
|
|
CefRefPtr<CefLabelButton> AsLabelButton() override { return nullptr; }
|
|
|
|
void SetState(cef_button_state_t state) override;
|
|
|
|
cef_button_state_t GetState() override;
|
2017-02-24 19:02:07 +01:00
|
|
|
void SetInkDropEnabled(bool enabled) override;
|
2016-01-19 21:09:01 +01:00
|
|
|
void SetTooltipText(const CefString& tooltip_text) override;
|
|
|
|
void SetAccessibleName(const CefString& name) override;
|
|
|
|
|
|
|
|
// CefView methods:
|
|
|
|
CefRefPtr<CefButton> AsButton() override { return this; }
|
2017-02-17 00:19:43 +01:00
|
|
|
void SetFocusable(bool focusable) override;
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// Create a new implementation object.
|
|
|
|
// Always call Initialize() after creation.
|
|
|
|
// |delegate| may be nullptr.
|
|
|
|
explicit CefButtonImpl(CefRefPtr<CefViewDelegateClass> delegate)
|
2017-05-17 11:29:28 +02:00
|
|
|
: ParentClass(delegate) {}
|
2016-01-19 21:09:01 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetState(cef_button_state_t state) {
|
|
|
|
CEF_REQUIRE_VALID_RETURN_VOID();
|
2020-08-29 00:39:23 +02:00
|
|
|
views::Button::ButtonState old_state = ParentClass::root_view()->GetState();
|
2017-08-04 00:55:19 +02:00
|
|
|
views::Button::ButtonState new_state =
|
|
|
|
static_cast<views::Button::ButtonState>(state);
|
|
|
|
|
2021-07-23 18:40:13 +02:00
|
|
|
if (views::InkDrop::Get(ParentClass::root_view())->ink_drop_mode() !=
|
2021-06-04 03:34:56 +02:00
|
|
|
views::InkDropHost::InkDropMode::OFF &&
|
2017-08-04 00:55:19 +02:00
|
|
|
!ParentClass::root_view()->IsFocusable()) {
|
|
|
|
// Ink drop state does not get set properly on state change when the button
|
|
|
|
// is non-focusable.
|
|
|
|
views::InkDropState ink_state = views::InkDropState::HIDDEN;
|
|
|
|
if (new_state == views::Button::STATE_PRESSED) {
|
|
|
|
ink_state = views::InkDropState::ACTIVATED;
|
|
|
|
} else if (old_state == views::Button::STATE_PRESSED) {
|
|
|
|
ink_state = views::InkDropState::DEACTIVATED;
|
|
|
|
}
|
2021-07-23 18:40:13 +02:00
|
|
|
views::InkDrop::Get(ParentClass::root_view())
|
|
|
|
->AnimateToState(ink_state, nullptr);
|
2017-08-04 00:55:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ParentClass::root_view()->SetState(new_state);
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CEF_BUTTON_IMPL_T cef_button_state_t CEF_BUTTON_IMPL_D::GetState() {
|
|
|
|
CEF_REQUIRE_VALID_RETURN(CEF_BUTTON_STATE_NORMAL);
|
2020-08-29 00:39:23 +02:00
|
|
|
return static_cast<cef_button_state_t>(ParentClass::root_view()->GetState());
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
2017-02-24 19:02:07 +01:00
|
|
|
CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetInkDropEnabled(bool enabled) {
|
|
|
|
CEF_REQUIRE_VALID_RETURN_VOID();
|
2024-03-29 17:48:33 +01:00
|
|
|
auto* inkdrop = views::InkDrop::Get(ParentClass::root_view());
|
|
|
|
inkdrop->SetMode(enabled ? views::InkDropHost::InkDropMode::ON
|
|
|
|
: views::InkDropHost::InkDropMode::OFF);
|
2017-02-24 19:02:07 +01:00
|
|
|
if (enabled) {
|
2024-03-29 17:48:33 +01:00
|
|
|
// Never returns an empty value.
|
|
|
|
const auto& color = view_util::GetBackgroundColor(
|
|
|
|
ParentClass::root_view(), /*allow_transparent=*/false);
|
|
|
|
inkdrop->SetBaseColor(color_utils::BlendTowardMaxContrast(*color, 0x61));
|
2017-02-24 19:02:07 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetTooltipText(
|
|
|
|
const CefString& tooltip_text) {
|
|
|
|
CEF_REQUIRE_VALID_RETURN_VOID();
|
|
|
|
ParentClass::root_view()->SetTooltipText(tooltip_text);
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetAccessibleName(
|
|
|
|
const CefString& name) {
|
|
|
|
CEF_REQUIRE_VALID_RETURN_VOID();
|
|
|
|
ParentClass::root_view()->SetAccessibleName(name);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CEF_BUTTON_IMPL_T void CEF_BUTTON_IMPL_D::SetFocusable(bool focusable) {
|
2017-02-17 00:19:43 +01:00
|
|
|
CEF_REQUIRE_VALID_RETURN_VOID();
|
2020-10-08 21:54:42 +02:00
|
|
|
ParentClass::root_view()->SetRequestFocusOnPress(focusable);
|
2017-02-17 00:19:43 +01:00
|
|
|
ParentClass::SetFocusable(focusable);
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
#endif // CEF_LIBCEF_BROWSER_VIEWS_BUTTON_IMPL_H_
|