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.
|
|
|
|
|
|
|
|
#include "libcef/browser/views/view_util.h"
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
#include "libcef/browser/views/view_adapter.h"
|
|
|
|
|
2016-05-25 01:35:43 +02:00
|
|
|
#include "ui/display/display.h"
|
|
|
|
#include "ui/display/screen.h"
|
2017-05-17 11:29:28 +02:00
|
|
|
#include "ui/gfx/geometry/point.h"
|
|
|
|
#include "ui/gfx/geometry/point_conversions.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
#include "ui/views/widget/widget.h"
|
|
|
|
#include "ui/views/widget/widget_delegate.h"
|
|
|
|
#include "ui/views/window/non_client_view.h"
|
|
|
|
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2016-05-25 01:35:43 +02:00
|
|
|
#include "ui/display/win/screen_win.h"
|
2016-01-19 21:09:01 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace view_util {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Manages the association between views::View and CefView instances.
|
|
|
|
class UserData : public base::SupportsUserData::Data {
|
|
|
|
public:
|
|
|
|
// Create the initial association between the views::View and the CefView. The
|
|
|
|
// CefView owns the views::View at this stage.
|
|
|
|
static void Register(CefRefPtr<CefView> cef_view) {
|
|
|
|
DCHECK(cef_view->IsValid());
|
|
|
|
DCHECK(!cef_view->IsAttached());
|
|
|
|
|
|
|
|
views::View* view = CefViewAdapter::GetFor(cef_view)->Get();
|
|
|
|
DCHECK(view);
|
|
|
|
|
|
|
|
// The CefView should not already be registered.
|
|
|
|
DCHECK(!view->GetUserData(UserDataKey()));
|
|
|
|
|
2017-05-31 17:33:30 +02:00
|
|
|
view->SetUserData(UserDataKey(), base::WrapUnique(new UserData(cef_view)));
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static CefRefPtr<CefView> GetFor(const views::View* view) {
|
|
|
|
DCHECK(view);
|
|
|
|
UserData* data = static_cast<UserData*>(view->GetUserData(UserDataKey()));
|
2023-01-02 23:59:03 +01:00
|
|
|
if (data) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return data->view_ref_;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Transfer ownership of the views::View to the caller. The views::View will
|
|
|
|
// gain a ref-counted reference to the CefView and the CefView will keep an
|
|
|
|
// unowned reference to the views::View. Destruction of the views::View will
|
|
|
|
// release the ref-counted reference to the CefView.
|
2022-02-21 23:23:40 +01:00
|
|
|
[[nodiscard]] static std::unique_ptr<views::View> PassOwnership(
|
|
|
|
CefRefPtr<CefView> cef_view) {
|
2016-01-19 21:09:01 +01:00
|
|
|
DCHECK(cef_view->IsValid());
|
|
|
|
DCHECK(!cef_view->IsAttached());
|
|
|
|
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<views::View> view =
|
2016-01-19 21:09:01 +01:00
|
|
|
CefViewAdapter::GetFor(cef_view)->PassOwnership();
|
|
|
|
DCHECK(view);
|
|
|
|
|
|
|
|
UserData* data = static_cast<UserData*>(view->GetUserData(UserDataKey()));
|
|
|
|
DCHECK(data);
|
|
|
|
data->TakeReference();
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The CefView resumes ownership of the views::View. The views::View no longer
|
|
|
|
// keeps a ref-counted reference to the CefView.
|
|
|
|
static void ResumeOwnership(CefRefPtr<CefView> cef_view) {
|
|
|
|
DCHECK(cef_view->IsValid());
|
|
|
|
DCHECK(cef_view->IsAttached());
|
|
|
|
|
|
|
|
CefViewAdapter* adapter = CefViewAdapter::GetFor(cef_view);
|
|
|
|
adapter->ResumeOwnership();
|
|
|
|
|
|
|
|
views::View* view = adapter->Get();
|
|
|
|
DCHECK(view);
|
|
|
|
|
|
|
|
UserData* data = static_cast<UserData*>(view->GetUserData(UserDataKey()));
|
|
|
|
DCHECK(data);
|
|
|
|
data->ReleaseReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-05-31 17:33:30 +02:00
|
|
|
friend std::default_delete<UserData>;
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
explicit UserData(CefRefPtr<CefView> cef_view) : view_ref_(cef_view.get()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
DCHECK(view_ref_);
|
|
|
|
}
|
|
|
|
|
|
|
|
~UserData() override {
|
|
|
|
if (view_) {
|
|
|
|
// The CefView does not own the views::View. Remove the CefView's
|
|
|
|
// reference to the views::View.
|
|
|
|
CefViewAdapter::GetFor(view_)->Detach();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void TakeReference() { view_ = view_ref_; }
|
2016-01-19 21:09:01 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void ReleaseReference() { view_ = nullptr; }
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
static void* UserDataKey() {
|
|
|
|
// We just need a unique constant. Use the address of a static that
|
|
|
|
// COMDAT folding won't touch in an optimizing linker.
|
|
|
|
static int data_key = 0;
|
|
|
|
return reinterpret_cast<void*>(&data_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefView> view_;
|
|
|
|
CefView* view_ref_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
const SkColor kDefaultBackgroundColor = SkColorSetARGB(255, 255, 255, 255);
|
|
|
|
const char kDefaultFontList[] = "Arial, Helvetica, 14px";
|
|
|
|
|
|
|
|
void Register(CefRefPtr<CefView> view) {
|
|
|
|
UserData::Register(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefView> GetFor(const views::View* view, bool find_known_parent) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!view) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return nullptr;
|
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 (!find_known_parent) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return UserData::GetFor(view);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
CefRefPtr<CefView> cef_view;
|
|
|
|
const views::View* current_view = view;
|
|
|
|
do {
|
|
|
|
cef_view = UserData::GetFor(current_view);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cef_view) {
|
2016-01-19 21:09:01 +01:00
|
|
|
break;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
current_view = current_view->parent();
|
|
|
|
} while (current_view);
|
|
|
|
|
|
|
|
return cef_view;
|
|
|
|
}
|
|
|
|
|
|
|
|
views::View* GetFor(CefRefPtr<CefView> view) {
|
|
|
|
return CefViewAdapter::GetFor(view)->Get();
|
|
|
|
}
|
|
|
|
|
2016-04-27 22:38:52 +02:00
|
|
|
std::unique_ptr<views::View> PassOwnership(CefRefPtr<CefView> view) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return UserData::PassOwnership(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResumeOwnership(CefRefPtr<CefView> view) {
|
|
|
|
UserData::ResumeOwnership(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefWindow> GetWindowFor(views::Widget* widget) {
|
|
|
|
CefRefPtr<CefWindow> window;
|
|
|
|
|
2023-04-14 18:58:21 +02:00
|
|
|
// If |widget| is an overlay, retrieve the host Widget.
|
2021-08-28 03:55:15 +02:00
|
|
|
if (widget) {
|
2023-04-14 18:58:21 +02:00
|
|
|
if (auto widget_view = GetHostView(widget)) {
|
2021-08-28 03:55:15 +02:00
|
|
|
widget = widget_view->GetWidget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
if (widget) {
|
|
|
|
// The views::WidgetDelegate should be a CefWindowView and |content_view|
|
|
|
|
// should be the same CefWindowView. However, just in case the views::Widget
|
|
|
|
// was created by something else let's go about this the safer way.
|
|
|
|
views::View* content_view = widget->widget_delegate()->GetContentsView();
|
|
|
|
CefRefPtr<CefView> cef_view = GetFor(content_view, false);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (cef_view && cef_view->AsPanel()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
window = cef_view->AsPanel()->AsWindow();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
// The Window should always exist if we created the views::Widget.
|
|
|
|
DCHECK(window);
|
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
return window;
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:35:43 +02:00
|
|
|
display::Display GetDisplayNearestPoint(const gfx::Point& point,
|
|
|
|
bool input_pixel_coords) {
|
2016-01-19 21:09:01 +01:00
|
|
|
gfx::Point find_point = point;
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2017-12-07 22:44:24 +01:00
|
|
|
if (input_pixel_coords) {
|
|
|
|
find_point = gfx::ToFlooredPoint(
|
|
|
|
display::win::ScreenWin::ScreenToDIPPoint(gfx::PointF(point)));
|
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
#endif
|
2016-05-25 01:35:43 +02:00
|
|
|
return display::Screen::GetScreen()->GetDisplayNearestPoint(find_point);
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
2016-05-25 01:35:43 +02:00
|
|
|
display::Display GetDisplayMatchingBounds(const gfx::Rect& bounds,
|
|
|
|
bool input_pixel_coords) {
|
2016-01-19 21:09:01 +01:00
|
|
|
gfx::Rect find_bounds = bounds;
|
2022-01-24 18:58:02 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
2016-05-25 01:35:43 +02:00
|
|
|
if (input_pixel_coords) {
|
2017-05-17 11:29:28 +02:00
|
|
|
find_bounds =
|
|
|
|
display::win::ScreenWin::ScreenToDIPRect(nullptr, find_bounds);
|
2016-05-25 01:35:43 +02:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
#endif
|
2016-06-21 00:59:23 +02:00
|
|
|
return display::Screen::GetScreen()->GetDisplayMatching(find_bounds);
|
2016-01-19 21:09:01 +01:00
|
|
|
}
|
|
|
|
|
2022-04-15 00:30:32 +02:00
|
|
|
void ConvertPointFromPixels(gfx::Point* point, float device_scale_factor) {
|
2016-01-19 21:09:01 +01:00
|
|
|
*point = gfx::ToFlooredPoint(
|
|
|
|
gfx::ScalePoint(gfx::PointF(*point), 1.0f / device_scale_factor));
|
|
|
|
}
|
|
|
|
|
2022-04-15 00:30:32 +02:00
|
|
|
void ConvertPointToPixels(gfx::Point* point, float device_scale_factor) {
|
2016-01-19 21:09:01 +01:00
|
|
|
*point = gfx::ToFlooredPoint(
|
|
|
|
gfx::ScalePoint(gfx::PointF(*point), device_scale_factor));
|
|
|
|
}
|
|
|
|
|
2022-10-13 20:43:40 +02:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
|
|
gfx::Point ConvertPointFromPixels(const gfx::Point& point) {
|
|
|
|
return gfx::ToFlooredPoint(
|
|
|
|
display::win::ScreenWin::ScreenToDIPPoint(gfx::PointF(point)));
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Point ConvertPointToPixels(const gfx::Point& point) {
|
|
|
|
return display::win::ScreenWin::DIPToScreenPoint(point);
|
|
|
|
}
|
2022-11-07 21:21:44 +01:00
|
|
|
|
|
|
|
gfx::Rect ConvertRectFromPixels(const gfx::Rect& rect) {
|
|
|
|
return display::win::ScreenWin::ScreenToDIPRect(nullptr, rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::Rect ConvertRectToPixels(const gfx::Rect& rect) {
|
|
|
|
return display::win::ScreenWin::DIPToScreenRect(nullptr, rect);
|
|
|
|
}
|
2022-10-13 20:43:40 +02:00
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
|
|
|
|
2016-01-19 21:09:01 +01:00
|
|
|
bool ConvertPointToScreen(views::View* view,
|
|
|
|
gfx::Point* point,
|
|
|
|
bool output_pixel_coords) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!view->GetWidget()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
views::View::ConvertPointToScreen(view, point);
|
|
|
|
|
|
|
|
if (output_pixel_coords) {
|
2016-05-25 01:35:43 +02:00
|
|
|
const display::Display& display = GetDisplayNearestPoint(*point, false);
|
2016-01-19 21:09:01 +01:00
|
|
|
ConvertPointToPixels(point, display.device_scale_factor());
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConvertPointFromScreen(views::View* view,
|
|
|
|
gfx::Point* point,
|
|
|
|
bool input_pixel_coords) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!view->GetWidget()) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
if (input_pixel_coords) {
|
2016-05-25 01:35:43 +02:00
|
|
|
const display::Display& display = GetDisplayNearestPoint(*point, true);
|
2016-01-19 21:09:01 +01:00
|
|
|
ConvertPointFromPixels(point, display.device_scale_factor());
|
|
|
|
}
|
|
|
|
|
|
|
|
views::View::ConvertPointFromScreen(view, point);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool ConvertPointToWindow(views::View* view, gfx::Point* point) {
|
2016-01-19 21:09:01 +01:00
|
|
|
views::Widget* widget = view->GetWidget();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!widget) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
views::View::ConvertPointToWidget(view, point);
|
|
|
|
|
|
|
|
if (widget->non_client_view()) {
|
|
|
|
views::NonClientFrameView* non_client_frame_view =
|
|
|
|
widget->non_client_view()->frame_view();
|
|
|
|
if (non_client_frame_view) {
|
|
|
|
// When using a custom drawn NonClientFrameView the native Window will not
|
|
|
|
// know the actual client bounds. Adjust the native Window bounds for the
|
|
|
|
// reported client bounds.
|
|
|
|
const gfx::Rect& client_bounds =
|
|
|
|
non_client_frame_view->GetBoundsForClientView();
|
|
|
|
*point -= client_bounds.OffsetFromOrigin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool ConvertPointFromWindow(views::View* view, gfx::Point* point) {
|
2016-01-19 21:09:01 +01:00
|
|
|
views::Widget* widget = view->GetWidget();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!widget) {
|
2016-01-19 21:09:01 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-01-19 21:09:01 +01:00
|
|
|
|
|
|
|
if (widget->non_client_view()) {
|
|
|
|
views::NonClientFrameView* non_client_frame_view =
|
|
|
|
widget->non_client_view()->frame_view();
|
|
|
|
if (non_client_frame_view) {
|
|
|
|
// When using a custom drawn NonClientFrameView the native Window will not
|
|
|
|
// know the actual client bounds. Adjust the native Window bounds for the
|
|
|
|
// reported client bounds.
|
|
|
|
const gfx::Rect& client_bounds =
|
|
|
|
non_client_frame_view->GetBoundsForClientView();
|
|
|
|
*point += client_bounds.OffsetFromOrigin();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
views::View::ConvertPointFromWidget(view, point);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace view_util
|