feat: DIP <-> screen coordinate conversions (#12879)

This commit is contained in:
Milan Burda 2018-05-16 11:34:09 +02:00 committed by Cheng Zhao
parent 73eb5af2cc
commit 211d7825d3
2 changed files with 67 additions and 0 deletions

View file

@ -7,6 +7,7 @@
#include <algorithm>
#include <string>
#include "atom/browser/api/atom_api_browser_window.h"
#include "atom/browser/browser.h"
#include "atom/common/native_mate_converters/gfx_converter.h"
#include "base/bind.h"
@ -16,6 +17,10 @@
#include "ui/display/screen.h"
#include "ui/gfx/geometry/point.h"
#if defined(OS_WIN)
#include "ui/display/win/screen_win.h"
#endif
#include "atom/common/node_includes.h"
namespace atom {
@ -79,6 +84,22 @@ display::Display Screen::GetDisplayMatching(const gfx::Rect& match_rect) {
return screen_->GetDisplayMatching(match_rect);
}
#if defined(OS_WIN)
static gfx::Rect ScreenToDIPRect(atom::NativeWindow* window,
const gfx::Rect& rect) {
HWND hwnd = window ? window->GetAcceleratedWidget() : nullptr;
return display::win::ScreenWin::ScreenToDIPRect(hwnd, rect);
}
static gfx::Rect DIPToScreenRect(atom::NativeWindow* window,
const gfx::Rect& rect) {
HWND hwnd = window ? window->GetAcceleratedWidget() : nullptr;
return display::win::ScreenWin::DIPToScreenRect(hwnd, rect);
}
#endif
void Screen::OnDisplayAdded(const display::Display& new_display) {
Emit("display-added", new_display);
}
@ -119,6 +140,12 @@ void Screen::BuildPrototype(v8::Isolate* isolate,
.SetMethod("getPrimaryDisplay", &Screen::GetPrimaryDisplay)
.SetMethod("getAllDisplays", &Screen::GetAllDisplays)
.SetMethod("getDisplayNearestPoint", &Screen::GetDisplayNearestPoint)
#if defined(OS_WIN)
.SetMethod("screenToDipPoint", &display::win::ScreenWin::ScreenToDIPPoint)
.SetMethod("dipToScreenPoint", &display::win::ScreenWin::DIPToScreenPoint)
.SetMethod("screenToDipRect", &ScreenToDIPRect)
.SetMethod("dipToScreenRect", &DIPToScreenRect)
#endif
#if defined(OS_MACOSX)
.SetMethod("getMenuBarHeight", &Screen::getMenuBarHeight)
#endif

View file

@ -119,3 +119,43 @@ Returns [`Display`](structures/display.md) - The display nearest the specified p
Returns [`Display`](structures/display.md) - The display that most closely
intersects the provided bounds.
### `screen.screenToDipPoint(point)` _Windows_
* `point` [Point](structures/point.md)
Returns [`Point`](structures/point.md)
Converts a screen physical point to a screen DIP point.
The DPI scale is performed relative to the display containing the physical point.
### `screen.dipToScreenPoint(point)` _Windows_
* `point` [Point](structures/point.md)
Returns [`Point`](structures/point.md)
Converts a screen DIP point to a screen physical point.
The DPI scale is performed relative to the display containing the DIP point.
### `screen.screenToDipRect(window, rect)` _Windows_
* `window` [BrowserWindow](browser-window.md)
* `rect` [Rectangle](structures/rectangle.md)
Returns [`Rectangle`](structures/rectangle.md)
Converts a screen physical rect to a screen DIP rect.
The DPI scale is performed relative to the display nearest to `window`.
If `window` is null, scaling will be performed to the display nearest to `rect`.
### `screen.dipToScreenRect(window, rect)` _Windows_
* `window` [BrowserWindow](browser-window.md)
* `rect` [Rectangle](structures/rectangle.md)
Returns [`Rectangle`](structures/rectangle.md)
Converts a screen DIP rect to a screen physical rect.
The DPI scale is performed relative to the display nearest to `window`.
If `window` is null, scaling will be performed to the display nearest to `rect`.