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_VIEW_VIEW_H_
|
|
|
|
#define CEF_LIBCEF_BROWSER_VIEWS_VIEW_VIEW_H_
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "include/views/cef_view.h"
|
|
|
|
#include "include/views/cef_view_delegate.h"
|
|
|
|
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
|
|
#include "libcef/browser/views/view_util.h"
|
|
|
|
|
|
|
|
#include "base/logging.h"
|
2021-10-19 00:17:16 +02:00
|
|
|
#include "ui/views/accessibility/accessibility_paint_checks.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
#include "ui/views/background.h"
|
|
|
|
#include "ui/views/view.h"
|
|
|
|
|
|
|
|
// Helpers for template boiler-plate.
|
|
|
|
#define CEF_VIEW_VIEW_T \
|
2017-05-17 11:29:28 +02:00
|
|
|
template <class ViewsViewClass, class CefViewDelegateClass>
|
|
|
|
#define CEF_VIEW_VIEW_A ViewsViewClass, CefViewDelegateClass
|
2016-01-19 21:09:01 +01:00
|
|
|
#define CEF_VIEW_VIEW_D CefViewView<CEF_VIEW_VIEW_A>
|
|
|
|
|
|
|
|
// Base template for implementing views::View-derived classes. The views::View-
|
|
|
|
// derived type passed to this template must provide a no-argument constructor
|
|
|
|
// (for example, see LabelButtonEx from basic_label_button_view.h). See comments
|
|
|
|
// in view_impl.h for a usage overview.
|
|
|
|
CEF_VIEW_VIEW_T class CefViewView : public ViewsViewClass {
|
|
|
|
public:
|
2021-12-06 21:40:25 +01:00
|
|
|
using ParentClass = ViewsViewClass;
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2016-07-21 23:21:32 +02:00
|
|
|
// Should be created from CreateRootView() in a CefViewImpl-derived class.
|
|
|
|
// Do not call complex views::View-derived methods from a CefViewView-derived
|
|
|
|
// constructor as they may attempt to call back into CefViewImpl before
|
|
|
|
// registration has been performed. |cef_delegate| may be nullptr.
|
2021-04-11 22:10:11 +02:00
|
|
|
template <typename... Args>
|
|
|
|
explicit CefViewView(CefViewDelegateClass* cef_delegate, Args... args)
|
|
|
|
: ParentClass(args...), cef_delegate_(cef_delegate) {}
|
2016-07-21 23:21:32 +02:00
|
|
|
|
|
|
|
// Should be called from InitializeRootView() in the CefViewImpl-derived
|
|
|
|
// class that created this object. This method will be called after
|
|
|
|
// CefViewImpl registration has completed so it is safe to call complex
|
|
|
|
// views::View-derived methods here.
|
|
|
|
virtual void Initialize() {
|
2016-01-19 21:09:01 +01:00
|
|
|
// Use our defaults instead of the Views framework defaults.
|
2017-07-27 01:19:27 +02:00
|
|
|
ParentClass::SetBackground(
|
|
|
|
views::CreateSolidBackground(view_util::kDefaultBackgroundColor));
|
2021-10-19 00:17:16 +02:00
|
|
|
|
|
|
|
// TODO(crbug.com/1218186): Remove this, if this view is focusable then it
|
|
|
|
// needs to add a name so that the screen reader knows what to announce.
|
|
|
|
ParentClass::SetProperty(views::kSkipAccessibilityPaintChecks, true);
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the CefViewDelegate-derived delegate associated with this view.
|
|
|
|
// May return nullptr.
|
|
|
|
CefViewDelegateClass* cef_delegate() const { return cef_delegate_; }
|
|
|
|
|
|
|
|
// Returns the CefView associated with this view. May return nullptr during
|
|
|
|
// CefViewImpl initialization. If callbacks to the CefViewImpl-derived class
|
|
|
|
// are required define an interface that the CefViewImpl-derived class can
|
|
|
|
// implement and pass as an unowned instance to this object's constructor (see
|
|
|
|
// for example CefWindowView).
|
|
|
|
CefRefPtr<CefView> GetCefView() const {
|
|
|
|
CefRefPtr<CefView> view = view_util::GetFor(this, false);
|
|
|
|
DCHECK(view);
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
// views::View methods:
|
2017-07-27 01:19:27 +02:00
|
|
|
gfx::Size CalculatePreferredSize() const override;
|
2016-01-19 21:09:01 +01:00
|
|
|
gfx::Size GetMinimumSize() const override;
|
|
|
|
gfx::Size GetMaximumSize() const override;
|
|
|
|
int GetHeightForWidth(int w) const override;
|
|
|
|
void Layout() override;
|
|
|
|
void ViewHierarchyChanged(
|
2019-04-16 16:38:48 +02:00
|
|
|
const views::ViewHierarchyChangedDetails& details) override;
|
2021-04-11 22:10:11 +02:00
|
|
|
void AddedToWidget() override;
|
|
|
|
void RemovedFromWidget() override;
|
2017-02-18 03:08:51 +01:00
|
|
|
void OnFocus() override;
|
|
|
|
void OnBlur() override;
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// Return true if this View is expected to have a minimum size (for example,
|
|
|
|
// a button where the minimum size is based on the label).
|
|
|
|
virtual bool HasMinimumSize() const { return false; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
void NotifyChildViewChanged(
|
2019-04-16 16:38:48 +02:00
|
|
|
const views::ViewHierarchyChangedDetails& details);
|
2016-01-19 21:09:01 +01:00
|
|
|
void NotifyParentViewChanged(
|
2019-04-16 16:38:48 +02:00
|
|
|
const views::ViewHierarchyChangedDetails& details);
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// Not owned by this object.
|
2021-04-11 22:10:11 +02:00
|
|
|
CefViewDelegateClass* const cef_delegate_;
|
2016-01-19 21:09:01 +01:00
|
|
|
};
|
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
CEF_VIEW_VIEW_T gfx::Size CEF_VIEW_VIEW_D::CalculatePreferredSize() const {
|
2016-01-19 21:09:01 +01:00
|
|
|
gfx::Size result;
|
|
|
|
if (cef_delegate()) {
|
|
|
|
CefSize cef_size = cef_delegate()->GetPreferredSize(GetCefView());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_size.IsEmpty()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
result = gfx::Size(cef_size.width, cef_size.height);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
2023-01-02 23:59:03 +01:00
|
|
|
if (result.IsEmpty()) {
|
2017-07-27 01:19:27 +02:00
|
|
|
result = ParentClass::CalculatePreferredSize();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
if (result.IsEmpty()) {
|
|
|
|
// Some layouts like BoxLayout expect the preferred size to be non-empty.
|
|
|
|
// The user may have set the size explicitly. Therefore return the current
|
|
|
|
// size as the preferred size.
|
|
|
|
result = ParentClass::size();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_VIEW_VIEW_T gfx::Size CEF_VIEW_VIEW_D::GetMinimumSize() const {
|
|
|
|
gfx::Size result;
|
|
|
|
if (cef_delegate()) {
|
|
|
|
CefSize cef_size = cef_delegate()->GetMinimumSize(GetCefView());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_size.IsEmpty()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
result = gfx::Size(cef_size.width, cef_size.height);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
// We don't want to call ParentClass::GetMinimumSize() in all cases because
|
|
|
|
// the default views::View implementation will call GetPreferredSize(). That
|
|
|
|
// may result in size() being returned which keeps the View from shrinking.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (result.IsEmpty() && HasMinimumSize()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
result = ParentClass::GetMinimumSize();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_VIEW_VIEW_T gfx::Size CEF_VIEW_VIEW_D::GetMaximumSize() const {
|
|
|
|
gfx::Size result;
|
|
|
|
if (cef_delegate()) {
|
|
|
|
CefSize cef_size = cef_delegate()->GetMaximumSize(GetCefView());
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_size.IsEmpty()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
result = gfx::Size(cef_size.width, cef_size.height);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
2023-01-02 23:59:03 +01:00
|
|
|
if (result.IsEmpty()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
result = ParentClass::GetMaximumSize();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_VIEW_VIEW_T int CEF_VIEW_VIEW_D::GetHeightForWidth(int w) const {
|
|
|
|
int result = 0;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cef_delegate()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
result = cef_delegate()->GetHeightForWidth(GetCefView(), w);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (result == 0) {
|
2016-01-19 21:09:01 +01:00
|
|
|
result = ParentClass::GetHeightForWidth(w);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
if (result == 0) {
|
|
|
|
// Some layouts like FillLayout will ignore the preferred size if this view
|
|
|
|
// has no children. We want to use the preferred size if not otherwise
|
|
|
|
// specified.
|
2017-07-27 01:19:27 +02:00
|
|
|
result = ParentClass::GetPreferredSize().height();
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_VIEW_VIEW_T void CEF_VIEW_VIEW_D::Layout() {
|
|
|
|
ParentClass::Layout();
|
|
|
|
|
|
|
|
// If Layout() did not provide a size then use the preferred size.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (ParentClass::size().IsEmpty()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
ParentClass::SizeToPreferredSize();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-08-28 03:55:15 +02:00
|
|
|
|
|
|
|
if (cef_delegate()) {
|
|
|
|
const auto new_bounds = ParentClass::bounds();
|
|
|
|
CefRect new_rect(new_bounds.x(), new_bounds.y(), new_bounds.width(),
|
|
|
|
new_bounds.height());
|
|
|
|
cef_delegate()->OnLayoutChanged(GetCefView(), new_rect);
|
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CEF_VIEW_VIEW_T void CEF_VIEW_VIEW_D::ViewHierarchyChanged(
|
2019-04-16 16:38:48 +02:00
|
|
|
const views::ViewHierarchyChangedDetails& details) {
|
2016-01-19 21:09:01 +01:00
|
|
|
NotifyChildViewChanged(details);
|
|
|
|
NotifyParentViewChanged(details);
|
|
|
|
ParentClass::ViewHierarchyChanged(details);
|
|
|
|
}
|
|
|
|
|
2021-04-11 22:10:11 +02:00
|
|
|
CEF_VIEW_VIEW_T void CEF_VIEW_VIEW_D::AddedToWidget() {
|
|
|
|
ParentClass::AddedToWidget();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cef_delegate()) {
|
2021-04-11 22:10:11 +02:00
|
|
|
cef_delegate()->OnWindowChanged(GetCefView(), /*added=*/true);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-04-11 22:10:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CEF_VIEW_VIEW_T void CEF_VIEW_VIEW_D::RemovedFromWidget() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cef_delegate()) {
|
2021-04-11 22:10:11 +02:00
|
|
|
cef_delegate()->OnWindowChanged(GetCefView(), /*added=*/false);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2021-04-11 22:10:11 +02:00
|
|
|
ParentClass::RemovedFromWidget();
|
|
|
|
}
|
|
|
|
|
2017-02-18 03:08:51 +01:00
|
|
|
CEF_VIEW_VIEW_T void CEF_VIEW_VIEW_D::OnFocus() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cef_delegate()) {
|
2017-02-18 03:08:51 +01:00
|
|
|
cef_delegate()->OnFocus(GetCefView());
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-02-18 03:08:51 +01:00
|
|
|
ParentClass::OnFocus();
|
|
|
|
}
|
|
|
|
|
|
|
|
CEF_VIEW_VIEW_T void CEF_VIEW_VIEW_D::OnBlur() {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cef_delegate()) {
|
2017-02-18 03:08:51 +01:00
|
|
|
cef_delegate()->OnBlur(GetCefView());
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-02-18 03:08:51 +01:00
|
|
|
ParentClass::OnBlur();
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
CEF_VIEW_VIEW_T void CEF_VIEW_VIEW_D::NotifyChildViewChanged(
|
2019-04-16 16:38:48 +02:00
|
|
|
const views::ViewHierarchyChangedDetails& details) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_delegate()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// Only interested with the parent is |this| object and the notification is
|
|
|
|
// about an immediate child (notifications are also sent for grandchildren).
|
2023-01-02 23:59:03 +01:00
|
|
|
if (details.parent != this || details.child->parent() != this) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// Only notify for children that have a known CEF root view. For example,
|
|
|
|
// don't notify when ScrollView adds child scroll bars.
|
|
|
|
CefRefPtr<CefView> child = view_util::GetFor(details.child, false);
|
2021-08-28 03:55:15 +02:00
|
|
|
if (child) {
|
2016-01-19 21:09:01 +01:00
|
|
|
cef_delegate()->OnChildViewChanged(GetCefView(), details.is_add, child);
|
2021-08-28 03:55:15 +02:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CEF_VIEW_VIEW_T void CEF_VIEW_VIEW_D::NotifyParentViewChanged(
|
2019-04-16 16:38:48 +02:00
|
|
|
const views::ViewHierarchyChangedDetails& details) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!cef_delegate()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// Only interested when the child is |this| object and notification is about
|
|
|
|
// the immediate parent (notifications are sent for all parents).
|
2023-01-02 23:59:03 +01:00
|
|
|
if (details.child != this || details.parent != ParentClass::parent()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// The immediate parent might be an intermediate view so find the closest
|
2021-08-28 03:55:15 +02:00
|
|
|
// known CEF root view. |parent| might be nullptr for overlays.
|
2016-01-19 21:09:01 +01:00
|
|
|
CefRefPtr<CefView> parent = view_util::GetFor(details.parent, true);
|
2021-08-28 03:55:15 +02:00
|
|
|
if (parent) {
|
|
|
|
cef_delegate()->OnParentViewChanged(GetCefView(), details.is_add, parent);
|
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // CEF_LIBCEF_BROWSER_VIEWS_VIEW_VIEW_H_
|