aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/win/native/DpiUtil.hpp35
-rw-r--r--src/win/native/InputMethod.cpp6
-rw-r--r--src/win/native/UiApplication.cpp2
-rw-r--r--src/win/native/Window.cpp34
-rw-r--r--src/win/native/WindowRenderTarget.cpp19
5 files changed, 39 insertions, 57 deletions
diff --git a/src/win/native/DpiUtil.hpp b/src/win/native/DpiUtil.hpp
index 16ffda25..8401b03f 100644
--- a/src/win/native/DpiUtil.hpp
+++ b/src/win/native/DpiUtil.hpp
@@ -8,39 +8,4 @@ namespace cru::platform::native::win {
inline platform::native::Dpi GetDpi() {
return platform::native::Dpi{96.0f, 96.0f};
}
-
-inline int DipToPixelInternal(const float dip, const float dpi) {
- return static_cast<int>(dip * dpi / 96.0f);
-}
-
-inline int DipToPixelX(const float dip_x) {
- return DipToPixelInternal(dip_x, GetDpi().x);
-}
-
-inline int DipToPixelY(const float dip_y) {
- return DipToPixelInternal(dip_y, GetDpi().y);
-}
-
-inline float DipToPixelInternal(const int pixel, const float dpi) {
- return static_cast<float>(pixel) * 96.0f / dpi;
-}
-
-inline float PixelToDipX(const int pixel_x) {
- return DipToPixelInternal(pixel_x, GetDpi().x);
-}
-
-inline float PixelToDipY(const int pixel_y) {
- return DipToPixelInternal(pixel_y, GetDpi().y);
-}
-
-inline Point PiToDip(const POINT& pi_point) {
- return Point(PixelToDipX(pi_point.x), PixelToDipY(pi_point.y));
-}
-
-inline POINT DipToPi(const Point& dip_point) {
- POINT result;
- result.x = DipToPixelX(dip_point.x);
- result.y = DipToPixelY(dip_point.y);
- return result;
-}
} // namespace cru::platform::native::win
diff --git a/src/win/native/InputMethod.cpp b/src/win/native/InputMethod.cpp
index 21681de2..7a46bef4 100644
--- a/src/win/native/InputMethod.cpp
+++ b/src/win/native/InputMethod.cpp
@@ -218,7 +218,11 @@ void WinInputMethodContext::SetCandidateWindowPosition(const Point& point) {
::CANDIDATEFORM form;
form.dwIndex = 1;
form.dwStyle = CFS_CANDIDATEPOS;
- form.ptCurrentPos = DipToPi(point);
+
+ auto window =
+ dynamic_cast<WinNativeWindow*>(this->native_window_resolver_->Resolve());
+ form.ptCurrentPos =
+ window == nullptr ? POINT{0, 0} : window->DipToPixel(point);
if (!::ImmSetCandidateWindow(himc.Get(), &form))
log::TagDebug(log_tag,
diff --git a/src/win/native/UiApplication.cpp b/src/win/native/UiApplication.cpp
index 198f03f2..3f7a0cf5 100644
--- a/src/win/native/UiApplication.cpp
+++ b/src/win/native/UiApplication.cpp
@@ -29,6 +29,8 @@ WinUiApplication::WinUiApplication() {
if (!instance_handle_)
throw Win32Error("Failed to get module(instance) handle.");
+ ::SetThreadDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE);
+
log::Logger::GetInstance()->AddSource(
std::make_unique<::cru::platform::win::WinDebugLoggerSource>());
log::Logger::GetInstance()->AddSource(
diff --git a/src/win/native/Window.cpp b/src/win/native/Window.cpp
index 81642451..7efc6160 100644
--- a/src/win/native/Window.cpp
+++ b/src/win/native/Window.cpp
@@ -39,13 +39,19 @@ WinNativeWindow::WinNativeWindow(WinUiApplication* application,
if (hwnd_ == nullptr)
throw Win32Error(::GetLastError(), "Failed to create window.");
+ auto dpi = ::GetDpiForWindow(hwnd_);
+ if (dpi == 0)
+ throw Win32Error(::GetLastError(), "Failed to get dpi of window.");
+ dpi_ = static_cast<float>(dpi);
+ log::Debug(u"Dpi of window is {}.", dpi_);
+
window_manager->RegisterWindow(hwnd_, this);
SetCursor(application->GetCursorManager()->GetSystemCursor(
cru::platform::native::SystemCursorType::Arrow));
window_render_target_ = std::make_unique<WindowRenderTarget>(
- application->GetDirectFactory(), hwnd_);
+ application->GetDirectFactory(), this);
}
WinNativeWindow::~WinNativeWindow() {
@@ -65,7 +71,7 @@ void WinNativeWindow::SetVisible(bool is_visible) {
}
Size WinNativeWindow::GetClientSize() {
const auto pixel_rect = GetClientRectPixel();
- return Size(PixelToDipX(pixel_rect.right), PixelToDipY(pixel_rect.bottom));
+ return Size(PixelToDip(pixel_rect.right), PixelToDip(pixel_rect.bottom));
}
void WinNativeWindow::SetClientSize(const Size& size) {
@@ -77,8 +83,8 @@ void WinNativeWindow::SetClientSize(const Size& size) {
RECT rect;
rect.left = 0;
rect.top = 0;
- rect.right = DipToPixelX(size.width);
- rect.bottom = DipToPixelY(size.height);
+ rect.right = DipToPixel(size.width);
+ rect.bottom = DipToPixel(size.height);
if (!AdjustWindowRectEx(&rect, window_style, FALSE, window_ex_style))
throw Win32Error(::GetLastError(), "Failed to invoke AdjustWindowRectEx.");
@@ -92,14 +98,14 @@ Rect WinNativeWindow::GetWindowRect() {
if (!::GetWindowRect(hwnd_, &rect))
throw Win32Error(::GetLastError(), "Failed to invoke GetWindowRect.");
- return Rect::FromVertices(PixelToDipX(rect.left), PixelToDipY(rect.top),
- PixelToDipX(rect.right), PixelToDipY(rect.bottom));
+ return Rect::FromVertices(PixelToDip(rect.left), PixelToDip(rect.top),
+ PixelToDip(rect.right), PixelToDip(rect.bottom));
}
void WinNativeWindow::SetWindowRect(const Rect& rect) {
- if (!SetWindowPos(hwnd_, nullptr, DipToPixelX(rect.left),
- DipToPixelY(rect.top), DipToPixelX(rect.GetRight()),
- DipToPixelY(rect.GetBottom()), SWP_NOZORDER))
+ if (!SetWindowPos(hwnd_, nullptr, DipToPixel(rect.left), DipToPixel(rect.top),
+ DipToPixel(rect.GetRight()), DipToPixel(rect.GetBottom()),
+ SWP_NOZORDER))
throw Win32Error(::GetLastError(), "Failed to invoke SetWindowPos.");
}
@@ -109,7 +115,7 @@ Point WinNativeWindow::GetMousePosition() {
throw Win32Error(::GetLastError(), "Failed to get cursor position.");
if (!::ScreenToClient(hwnd_, &p))
throw Win32Error(::GetLastError(), "Failed to call ScreenToClient.");
- return PiToDip(p);
+ return PixelToDip(p);
}
bool WinNativeWindow::CaptureMouse() {
@@ -360,7 +366,7 @@ void WinNativeWindow::OnResizeInternal(const int new_width,
const int new_height) {
if (!(new_width == 0 && new_height == 0)) {
window_render_target_->ResizeBuffer(new_width, new_height);
- resize_event_.Raise(Size{PixelToDipX(new_width), PixelToDipY(new_height)});
+ resize_event_.Raise(Size{PixelToDip(new_width), PixelToDip(new_height)});
}
}
@@ -389,7 +395,7 @@ void WinNativeWindow::OnMouseMoveInternal(const POINT point) {
mouse_enter_leave_event_.Raise(MouseEnterLeaveType::Enter);
}
- mouse_move_event_.Raise(PiToDip(point));
+ mouse_move_event_.Raise(PixelToDip(point));
}
void WinNativeWindow::OnMouseLeaveInternal() {
@@ -399,13 +405,13 @@ void WinNativeWindow::OnMouseLeaveInternal() {
void WinNativeWindow::OnMouseDownInternal(platform::native::MouseButton button,
POINT point) {
- const auto dip_point = PiToDip(point);
+ const auto dip_point = PixelToDip(point);
mouse_down_event_.Raise({button, dip_point, RetrieveKeyMofifier()});
}
void WinNativeWindow::OnMouseUpInternal(platform::native::MouseButton button,
POINT point) {
- const auto dip_point = PiToDip(point);
+ const auto dip_point = PixelToDip(point);
mouse_up_event_.Raise({button, dip_point, RetrieveKeyMofifier()});
}
diff --git a/src/win/native/WindowRenderTarget.cpp b/src/win/native/WindowRenderTarget.cpp
index 4a114ebf..b1f867b0 100644
--- a/src/win/native/WindowRenderTarget.cpp
+++ b/src/win/native/WindowRenderTarget.cpp
@@ -1,19 +1,24 @@
#include "cru/win/native/WindowRenderTarget.hpp"
+#include "DpiUtil.hpp"
#include "cru/win/graph/direct/Exception.hpp"
#include "cru/win/graph/direct/Factory.hpp"
-#include "DpiUtil.hpp"
+#include "cru/win/native/Window.hpp"
namespace cru::platform::native::win {
using namespace cru::platform::graph::win::direct;
-WindowRenderTarget::WindowRenderTarget(DirectGraphFactory* factory, HWND hwnd)
- : factory_(factory) {
+WindowRenderTarget::WindowRenderTarget(DirectGraphFactory* factory,
+ WinNativeWindow* window)
+ : window_(window), factory_(factory) {
Expects(factory);
const auto d3d11_device = factory->GetD3D11Device();
const auto dxgi_factory = factory->GetDxgiFactory();
d2d1_device_context_ = factory->CreateD2D1DeviceContext();
+ d2d1_device_context_->SetUnitMode(D2D1_UNIT_MODE_DIPS);
+ auto dpi = window_->GetDpi();
+ d2d1_device_context_->SetDpi(dpi, dpi);
// Allocate a descriptor.
DXGI_SWAP_CHAIN_DESC1 swap_chain_desc;
@@ -34,8 +39,8 @@ WindowRenderTarget::WindowRenderTarget(DirectGraphFactory* factory, HWND hwnd)
// Get the final swap chain for this window from the DXGI factory.
ThrowIfFailed(dxgi_factory->CreateSwapChainForHwnd(
- d3d11_device, hwnd, &swap_chain_desc, nullptr, nullptr,
- &dxgi_swap_chain_));
+ d3d11_device, window->GetWindowHandle(), &swap_chain_desc, nullptr,
+ nullptr, &dxgi_swap_chain_));
CreateTargetBitmap();
}
@@ -61,12 +66,12 @@ void WindowRenderTarget::CreateTargetBitmap() {
ThrowIfFailed(
dxgi_swap_chain_->GetBuffer(0, IID_PPV_ARGS(&dxgi_back_buffer)));
- const auto dpi = GetDpi(); // TODO! DPI awareness.
+ auto dpi = window_->GetDpi();
auto bitmap_properties = D2D1::BitmapProperties1(
D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE),
- dpi.x, dpi.y);
+ dpi, dpi);
// Get a D2D surface from the DXGI back buffer to use as the D2D render
// target.