diff --git a/include/mostly_harmless/gui/mostlyharmless_DisplayInfo.h b/include/mostly_harmless/gui/mostlyharmless_DisplayInfo.h new file mode 100644 index 0000000..eba8784 --- /dev/null +++ b/include/mostly_harmless/gui/mostlyharmless_DisplayInfo.h @@ -0,0 +1,12 @@ +// +// Created by Syl Morrison on 06/02/2026. +// + +#ifndef GLEO_MOSTLYHARMLESS_DISPLAYINFO_H +#define GLEO_MOSTLYHARMLESS_DISPLAYINFO_H +#include +namespace mostly_harmless::gui::display_info { + void getScreenDimensions(std::uint32_t* width, std::uint32_t* height); + double getDevicePixelRatio(); +} // namespace mostly_harmless::gui::display_info +#endif // GLEO_MOSTLYHARMLESS_DISPLAYINFO_H diff --git a/include/mostly_harmless/gui/platform/mostlyharmless_GuiHelpersMacOS.h b/include/mostly_harmless/gui/platform/mostlyharmless_GuiHelpersMacOS.h index ecc153c..3bdea53 100644 --- a/include/mostly_harmless/gui/platform/mostlyharmless_GuiHelpersMacOS.h +++ b/include/mostly_harmless/gui/platform/mostlyharmless_GuiHelpersMacOS.h @@ -28,6 +28,10 @@ namespace mostly_harmless::gui::helpers::macos { * \param height A pointer to the height variable - the result will be written here. */ void getViewSize(void* viewHandle, std::uint32_t* width, std::uint32_t* height); + + void getScreenSize(std::uint32_t* width, std::uint32_t* height); + + double getDevicePixelRatio(); /** * Adds an NSView as a subview of another NSView. * \param parentViewHandle A void* to the NSView to add the child view to. diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 77f2d78..f47ce61 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -28,6 +28,7 @@ set(MOSTLYHARMLESS_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/gui/mostlyharmless_WebviewEditor.cpp ${CMAKE_CURRENT_SOURCE_DIR}/gui/mostlyharmless_Cursor.cpp ${CMAKE_CURRENT_SOURCE_DIR}/gui/mostlyharmless_Colour.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/gui/mostlyharmless_DisplayInfo.cpp ${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_TaskThread.cpp ${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_Timer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/utils/mostlyharmless_OnScopeExit.cpp diff --git a/source/gui/mostlyharmless_DisplayInfo.cpp b/source/gui/mostlyharmless_DisplayInfo.cpp new file mode 100644 index 0000000..bdfa342 --- /dev/null +++ b/source/gui/mostlyharmless_DisplayInfo.cpp @@ -0,0 +1,24 @@ +// +// Created by Syl Morrison on 06/02/2026. +// +#include "mostly_harmless/gui/platform/mostlyharmless_GuiHelpersMacOS.h" + + +#include +namespace mostly_harmless::gui::display_info { + +#if defined(MOSTLY_HARMLESS_MACOS) +#include + void getScreenDimensions(std::uint32_t* width, std::uint32_t* height) { + helpers::macos::getScreenSize(width, height); + } + + double getDevicePixelRatio() { + return helpers::macos::getDevicePixelRatio(); + } + +#else + +#endif + +} // namespace mostly_harmless::gui::display_info \ No newline at end of file diff --git a/source/gui/platform/mostlyharmless_GuiHelpersMacOS.mm b/source/gui/platform/mostlyharmless_GuiHelpersMacOS.mm index 23c192d..07cc4be 100644 --- a/source/gui/platform/mostlyharmless_GuiHelpersMacOS.mm +++ b/source/gui/platform/mostlyharmless_GuiHelpersMacOS.mm @@ -32,6 +32,27 @@ void getViewSize(void* viewHandle, std::uint32_t* width, std::uint32_t* height) *height = static_cast(bounds.size.height); } + void getScreenSize(std::uint32_t* width, std::uint32_t* height) { + auto frame = [[NSScreen mainScreen] frame]; + *width = static_cast(frame.size.width); + *height = static_cast(frame.size.height); + } + + double getDevicePixelRatio() { + auto mainDisplayId = CGMainDisplayID(); + auto screenSizeMm = CGDisplayScreenSize(mainDisplayId); + const auto widthMm = screenSizeMm.width; + const auto heightMm = screenSizeMm.height; + const auto hypMm = std::sqrt((widthMm * widthMm) + (heightMm * heightMm)); + std::uint32_t widthPx, heightPx; + getScreenSize(&widthPx, &heightPx); + const auto pxHyp = std::sqrt((widthPx * widthPx) + (heightPx * heightPx)); + constexpr static auto mmPerInch = 25.4; + const auto ratio = pxHyp / hypMm; + const auto dpi = ratio * mmPerInch; + return 1.0; + } + void reparentView(void* hostViewHandle, void* clientViewHandle, void* childViewHandle, Colour backgroundColour) { auto* host = static_cast(hostViewHandle); auto* client = static_cast(clientViewHandle);