From 4947f735fbf035febbd8435c32a50d37af2633cb Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sun, 31 Mar 2024 19:27:56 +0200 Subject: Add isHiDPI function to NativeWindow and it's implementations. --- dggui/nativewindow.h | 5 +++++ dggui/nativewindow_cocoa.h | 31 ++++++++++++++++--------------- dggui/nativewindow_cocoa.mm | 6 ++++++ dggui/nativewindow_win32.cc | 9 +++++++++ dggui/nativewindow_win32.h | 1 + dggui/nativewindow_x11.cc | 9 +++++++++ dggui/nativewindow_x11.h | 1 + 7 files changed, 47 insertions(+), 15 deletions(-) diff --git a/dggui/nativewindow.h b/dggui/nativewindow.h index 07c3559..715d7ca 100644 --- a/dggui/nativewindow.h +++ b/dggui/nativewindow.h @@ -59,6 +59,11 @@ public: //! Query size of the native window. virtual std::pair getSize() const = 0; + //! While there isn’t a universally agreed-upon meaning for what constitutes + //! HiDPI, a common benchmark is that displays with more than double the PPI + //! of the old standard (72-96) can arguably be classified as HiDPI. + virtual bool isHiDPI() = 0; + //! Move the window to a new position. //! Note: negative value are allowed. virtual void move(int x, int y) = 0; diff --git a/dggui/nativewindow_cocoa.h b/dggui/nativewindow_cocoa.h index e17d4af..0bfa73b 100644 --- a/dggui/nativewindow_cocoa.h +++ b/dggui/nativewindow_cocoa.h @@ -42,21 +42,22 @@ public: ~NativeWindowCocoa(); // From NativeWindow: - virtual void setFixedSize(std::size_t width, std::size_t height) override; - virtual void setAlwaysOnTop(bool always_on_top) override; - virtual void resize(std::size_t width, std::size_t height) override; - virtual std::pair getSize() const override; - virtual void move(int x, int y) override; - virtual std::pair getPosition() const override; - virtual void show() override; - virtual void hide() override; - virtual bool visible() const override; - virtual void setCaption(const std::string &caption) override; - virtual void redraw(const Rect& dirty_rect) override; - virtual void grabMouse(bool grab) override; - virtual EventQueue getEvents() override; - virtual void* getNativeWindowHandle() const override; - virtual Point translateToScreen(const Point& point) override; + void setFixedSize(std::size_t width, std::size_t height) override; + void setAlwaysOnTop(bool always_on_top) override; + void resize(std::size_t width, std::size_t height) override; + std::pair getSize() const override; + bool isHiDPI() override; + void move(int x, int y) override; + std::pair getPosition() const override; + void show() override; + void hide() override; + bool visible() const override; + void setCaption(const std::string &caption) override; + void redraw(const Rect& dirty_rect) override; + void grabMouse(bool grab) override; + EventQueue getEvents() override; + void* getNativeWindowHandle() const override; + Point translateToScreen(const Point& point) override; // Expose friend members of Window to ObjC++ implementation. class Window& getWindow(); diff --git a/dggui/nativewindow_cocoa.mm b/dggui/nativewindow_cocoa.mm index 3214f57..9484af7 100644 --- a/dggui/nativewindow_cocoa.mm +++ b/dggui/nativewindow_cocoa.mm @@ -609,6 +609,12 @@ std::pair NativeWindowCocoa::getSize() const } } +bool NativeWindowCocoa::isHiDPI() +{ + float dpi = [priv->window backingScaleFactor]; + return dpi > 1.0f; +} + void NativeWindowCocoa::move(int x, int y) { NSRect screen = [[NSScreen mainScreen] frame]; diff --git a/dggui/nativewindow_win32.cc b/dggui/nativewindow_win32.cc index db785e9..45cbd30 100644 --- a/dggui/nativewindow_win32.cc +++ b/dggui/nativewindow_win32.cc @@ -489,6 +489,15 @@ std::pair NativeWindowWin32::getSize() const return std::make_pair(rect.right - rect.left, rect.bottom - rect.top); } +bool NativeWindowWin32::isHiDPI() +{ + HDC hDC = GetDC(m_hwnd); + INT xres = GetDeviceCaps(hDC, LOGPIXELSX); + INT yres = GetDeviceCaps(hDC, LOGPIXELSY); + ReleaseDC(m_hwnd, hDC); + return xres >= 2 * 96 || yres >= 2 * 96; +} + void NativeWindowWin32::move(int x, int y) { SetWindowPos(m_hwnd, always_on_top ? HWND_TOPMOST : nullptr, diff --git a/dggui/nativewindow_win32.h b/dggui/nativewindow_win32.h index f4748fe..e8adbfd 100644 --- a/dggui/nativewindow_win32.h +++ b/dggui/nativewindow_win32.h @@ -49,6 +49,7 @@ public: void setAlwaysOnTop(bool always_on_top) override; void resize(std::size_t width, std::size_t height) override; std::pair getSize() const override; + bool isHiDPI() override; void move(int x, int y) override; std::pair getPosition() const override; void show() override; diff --git a/dggui/nativewindow_x11.cc b/dggui/nativewindow_x11.cc index 04f1b35..2f94935 100644 --- a/dggui/nativewindow_x11.cc +++ b/dggui/nativewindow_x11.cc @@ -240,6 +240,15 @@ std::pair NativeWindowX11::getSize() const return {width, height}; } +bool NativeWindowX11::isHiDPI() +{ + auto xres = ((((double) DisplayWidth(display, screen)) * 25.4) / + ((double) DisplayWidthMM(display, screen))) + 0.5; + auto yres = ((((double) DisplayHeight(display, screen)) * 25.4) / + ((double) DisplayHeightMM(display, screen))) + 0.5; + return xres >= 2 * 96 || yres >= 2 * 96; +} + void NativeWindowX11::move(int x, int y) { if(display == nullptr) diff --git a/dggui/nativewindow_x11.h b/dggui/nativewindow_x11.h index bed86bd..94ac6a7 100644 --- a/dggui/nativewindow_x11.h +++ b/dggui/nativewindow_x11.h @@ -50,6 +50,7 @@ public: void setAlwaysOnTop(bool always_on_top) override; void resize(std::size_t width, std::size_t height) override; std::pair getSize() const override; + bool isHiDPI() override;; void move(int x, int y) override; std::pair getPosition() const override; void show() override; -- cgit v1.2.3