Add screen pixel to screen DIP conversion methods.

On Windows these new CefDisplay methods convert between screen pixel coordinates
and screen DIP coordinates. On macOS and Linux these methods just return a copy
of the input coordinates.
This commit is contained in:
npavlov 2022-10-13 20:43:40 +02:00 committed by Marshall Greenblatt
parent fa643b269e
commit 485f0b9caf
10 changed files with 171 additions and 8 deletions

View File

@ -33,7 +33,7 @@
// by hand. See the translator.README.txt file in the tools directory for
// more information.
//
// $hash=3ff71283b14972df89a3c4c7c698faf887cef6d0$
// $hash=6eed21d200bad5e898dfbe2701ad327cc1e4cc5c$
//
#ifndef CEF_INCLUDE_CAPI_VIEWS_CEF_DISPLAY_CAPI_H_
@ -140,6 +140,20 @@ CEF_EXPORT size_t cef_display_get_count(void);
CEF_EXPORT void cef_display_get_alls(size_t* displaysCount,
cef_display_t** displays);
///
/// Convert |point| from DIP screen coordinates to pixel screen coordinates.
/// This function is only used on Windows.
///
CEF_EXPORT cef_point_t
cef_display_convert_screen_point_to_pixels(const cef_point_t* point);
///
/// Convert |point| from pixel screen coordinates to DIP screen coordinates.
/// This function is only used on Windows.
///
CEF_EXPORT cef_point_t
cef_display_convert_screen_point_from_pixels(const cef_point_t* point);
#ifdef __cplusplus
}
#endif

View File

@ -42,13 +42,13 @@
// way that may cause binary incompatibility with other builds. The universal
// hash value will change if any platform is affected whereas the platform hash
// values will change only if that particular platform is affected.
#define CEF_API_HASH_UNIVERSAL "dfec58669a2c79108d61246f4e523570c3a21f63"
#define CEF_API_HASH_UNIVERSAL "befd42a0b7773d9e98205d8024d90af580a6d4fa"
#if defined(OS_WIN)
#define CEF_API_HASH_PLATFORM "bd5a6f54227d8bd35d33580342cd78f81d0cee2e"
#define CEF_API_HASH_PLATFORM "c9d32d56f34afe8c33b115ece110c54a8854b2c2"
#elif defined(OS_MAC)
#define CEF_API_HASH_PLATFORM "11902ab39267c1048acfa5c41053c6cfe0bd6227"
#define CEF_API_HASH_PLATFORM "11ee1bfabb19306adf55aaf71de537514557245c"
#elif defined(OS_LINUX)
#define CEF_API_HASH_PLATFORM "213fb79db129c756661e7cfbe6749c15276bbebf"
#define CEF_API_HASH_PLATFORM "c093ef6f613de44faf3d23abf6b668054d9c749d"
#endif
#ifdef __cplusplus

View File

@ -91,6 +91,20 @@ class CefDisplay : public CefBaseRefCounted {
/*--cef(count_func=displays:GetDisplayCount)--*/
static void GetAllDisplays(std::vector<CefRefPtr<CefDisplay>>& displays);
///
/// Convert |point| from DIP screen coordinates to pixel screen coordinates.
/// This method is only used on Windows.
///
/*--cef()--*/
static CefPoint ConvertScreenPointToPixels(const CefPoint& point);
///
/// Convert |point| from pixel screen coordinates to DIP screen coordinates.
/// This method is only used on Windows.
///
/*--cef()--*/
static CefPoint ConvertScreenPointFromPixels(const CefPoint& point);
///
/// Returns the unique identifier for this Display.
///

View File

@ -51,6 +51,30 @@ void CefDisplay::GetAllDisplays(std::vector<CefRefPtr<CefDisplay>>& displays) {
displays.push_back(new CefDisplayImpl(vec[i]));
}
// static
CefPoint CefDisplay::ConvertScreenPointToPixels(const CefPoint& point) {
CEF_REQUIRE_UIT_RETURN(CefPoint());
#if BUILDFLAG(IS_WIN)
const gfx::Point pix_point =
view_util::ConvertPointToPixels(gfx::Point(point.x, point.y));
return CefPoint(pix_point.x(), pix_point.y());
#else
return point;
#endif
}
// static
CefPoint CefDisplay::ConvertScreenPointFromPixels(const CefPoint& point) {
CEF_REQUIRE_UIT_RETURN(CefPoint());
#if BUILDFLAG(IS_WIN)
const gfx::Point dip_point =
view_util::ConvertPointFromPixels(gfx::Point(point.x, point.y));
return CefPoint(dip_point.x(), dip_point.y());
#else
return point;
#endif
}
CefDisplayImpl::CefDisplayImpl(const display::Display& display)
: display_(display) {
CEF_REQUIRE_UIT();

View File

@ -227,6 +227,17 @@ void ConvertPointToPixels(gfx::Point* point, float device_scale_factor) {
gfx::ScalePoint(gfx::PointF(*point), device_scale_factor));
}
#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);
}
#endif // BUILDFLAG(IS_WIN)
bool ConvertPointToScreen(views::View* view,
gfx::Point* point,
bool output_pixel_coords) {

View File

@ -91,6 +91,14 @@ void ConvertPointFromPixels(gfx::Point* point, float device_scale_factor);
// using |device_scale_factor|.
void ConvertPointToPixels(gfx::Point* point, float device_scale_factor);
#if BUILDFLAG(IS_WIN)
// Convert |point| from pixel screen coordinates to DIP screen coordinates.
gfx::Point ConvertPointFromPixels(const gfx::Point& point);
// Convert |point| from DIP screen coordinates to pixel screen coordinates.
gfx::Point ConvertPointToPixels(const gfx::Point& point);
#endif // BUILDFLAG(IS_WIN)
// Convert |point| from |view| to screen coordinates. If |output_pixel_coords|
// is true then |point| will be output in pixel coordinates instead of density
// independent pixels (DIP). Returns false if |view| does not currently belong

View File

@ -9,7 +9,7 @@
// implementations. See the translator.README.txt file in the tools directory
// for more information.
//
// $hash=546b8f890852fb4df26a85aec6b83effe1bdc6e6$
// $hash=5e68fdaae42fe008a95bcf2672debe3cf04fa2ff$
//
#include "libcef_dll/cpptoc/views/display_cpptoc.h"
@ -121,6 +121,48 @@ CEF_EXPORT void cef_display_get_alls(size_t* displaysCount,
}
}
CEF_EXPORT cef_point_t
cef_display_convert_screen_point_to_pixels(const cef_point_t* point) {
shutdown_checker::AssertNotShutdown();
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
// Verify param: point; type: simple_byref_const
DCHECK(point);
if (!point)
return CefPoint();
// Translate param: point; type: simple_byref_const
CefPoint pointVal = point ? *point : CefPoint();
// Execute
cef_point_t _retval = CefDisplay::ConvertScreenPointToPixels(pointVal);
// Return type: simple
return _retval;
}
CEF_EXPORT cef_point_t
cef_display_convert_screen_point_from_pixels(const cef_point_t* point) {
shutdown_checker::AssertNotShutdown();
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
// Verify param: point; type: simple_byref_const
DCHECK(point);
if (!point)
return CefPoint();
// Translate param: point; type: simple_byref_const
CefPoint pointVal = point ? *point : CefPoint();
// Execute
cef_point_t _retval = CefDisplay::ConvertScreenPointFromPixels(pointVal);
// Return type: simple
return _retval;
}
namespace {
// MEMBER FUNCTIONS - Body may be edited by hand.

View File

@ -9,7 +9,7 @@
// implementations. See the translator.README.txt file in the tools directory
// for more information.
//
// $hash=ba41b36a0cdd335f2a964665576aaf50d8be9c55$
// $hash=afef323719b977c74bb86d015ad1b0f5c253c3ba$
//
#include "libcef_dll/ctocpp/views/display_ctocpp.h"
@ -110,6 +110,32 @@ void CefDisplay::GetAllDisplays(std::vector<CefRefPtr<CefDisplay>>& displays) {
}
}
NO_SANITIZE("cfi-icall")
CefPoint CefDisplay::ConvertScreenPointToPixels(const CefPoint& point) {
shutdown_checker::AssertNotShutdown();
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
// Execute
cef_point_t _retval = cef_display_convert_screen_point_to_pixels(&point);
// Return type: simple
return _retval;
}
NO_SANITIZE("cfi-icall")
CefPoint CefDisplay::ConvertScreenPointFromPixels(const CefPoint& point) {
shutdown_checker::AssertNotShutdown();
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
// Execute
cef_point_t _retval = cef_display_convert_screen_point_from_pixels(&point);
// Return type: simple
return _retval;
}
// VIRTUAL METHODS - Body may be edited by hand.
NO_SANITIZE("cfi-icall") int64 CefDisplayCToCpp::GetID() {

View File

@ -9,7 +9,7 @@
// implementations. See the translator.README.txt file in the tools directory
// for more information.
//
// $hash=a753ff43760df4f3c1126d248b4128ca14a8cd68$
// $hash=bb6f61b0d69253de7bcc5506fd04562e46fa797c$
//
#include <dlfcn.h>
@ -234,6 +234,10 @@ struct libcef_pointers {
decltype(&cef_display_get_matching_bounds) cef_display_get_matching_bounds;
decltype(&cef_display_get_count) cef_display_get_count;
decltype(&cef_display_get_alls) cef_display_get_alls;
decltype(&cef_display_convert_screen_point_to_pixels)
cef_display_convert_screen_point_to_pixels;
decltype(&cef_display_convert_screen_point_from_pixels)
cef_display_convert_screen_point_from_pixels;
decltype(&cef_label_button_create) cef_label_button_create;
decltype(&cef_menu_button_create) cef_menu_button_create;
decltype(&cef_panel_create) cef_panel_create;
@ -445,6 +449,8 @@ int libcef_init_pointers(const char* path) {
INIT_ENTRY(cef_display_get_matching_bounds);
INIT_ENTRY(cef_display_get_count);
INIT_ENTRY(cef_display_get_alls);
INIT_ENTRY(cef_display_convert_screen_point_to_pixels);
INIT_ENTRY(cef_display_convert_screen_point_from_pixels);
INIT_ENTRY(cef_label_button_create);
INIT_ENTRY(cef_menu_button_create);
INIT_ENTRY(cef_panel_create);
@ -1273,6 +1279,18 @@ void cef_display_get_alls(size_t* displaysCount,
g_libcef_pointers.cef_display_get_alls(displaysCount, displays);
}
NO_SANITIZE("cfi-icall")
cef_point_t cef_display_convert_screen_point_to_pixels(
const cef_point_t* point) {
return g_libcef_pointers.cef_display_convert_screen_point_to_pixels(point);
}
NO_SANITIZE("cfi-icall")
cef_point_t cef_display_convert_screen_point_from_pixels(
const cef_point_t* point) {
return g_libcef_pointers.cef_display_convert_screen_point_from_pixels(point);
}
NO_SANITIZE("cfi-icall")
struct _cef_label_button_t* cef_label_button_create(
struct _cef_button_delegate_t* delegate,

View File

@ -200,6 +200,12 @@ void RunWindowLayoutAndCoords(CefRefPtr<CefWindow> window) {
display->ConvertPointFromPixels(point);
EXPECT_EQ(CefPoint(client_bounds_in_screen.x, client_bounds_in_screen.y),
point);
// We don't know what the pixel values will be, but they should be reversable.
point = CefPoint(client_bounds_in_screen.x, client_bounds_in_screen.y);
const auto pixels = CefDisplay::ConvertScreenPointToPixels(point);
const auto dip = CefDisplay::ConvertScreenPointFromPixels(pixels);
EXPECT_EQ(point, dip);
}
void WindowLayoutAndCoordsImpl(CefRefPtr<CefWaitableEvent> event) {