common.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #pragma once
  2. #include <Windows.h>
  3. namespace brightray {
  4. struct NotificationData {
  5. DesktopNotificationController* controller = nullptr;
  6. std::wstring caption;
  7. std::wstring body_text;
  8. HBITMAP image = NULL;
  9. NotificationData() = default;
  10. ~NotificationData() {
  11. if (image)
  12. DeleteObject(image);
  13. }
  14. NotificationData(const NotificationData& other) = delete;
  15. NotificationData& operator=(const NotificationData& other) = delete;
  16. };
  17. template <typename T>
  18. constexpr T ScaleForDpi(T value, unsigned dpi, unsigned source_dpi = 96) {
  19. return value * dpi / source_dpi;
  20. }
  21. struct ScreenMetrics {
  22. UINT dpi_x, dpi_y;
  23. ScreenMetrics() {
  24. typedef HRESULT WINAPI GetDpiForMonitor_t(HMONITOR, int, UINT*, UINT*);
  25. auto GetDpiForMonitor = reinterpret_cast<GetDpiForMonitor_t*>(
  26. GetProcAddress(GetModuleHandle(TEXT("shcore")), "GetDpiForMonitor"));
  27. if (GetDpiForMonitor) {
  28. auto monitor = MonitorFromPoint({}, MONITOR_DEFAULTTOPRIMARY);
  29. if (GetDpiForMonitor(monitor, 0, &dpi_x, &dpi_y) == S_OK)
  30. return;
  31. }
  32. HDC hdc = GetDC(NULL);
  33. dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
  34. dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
  35. ReleaseDC(NULL, hdc);
  36. }
  37. template <class T>
  38. T X(T value) const {
  39. return ScaleForDpi(value, dpi_x);
  40. }
  41. template <class T>
  42. T Y(T value) const {
  43. return ScaleForDpi(value, dpi_y);
  44. }
  45. };
  46. } // namespace brightray