From ba59d40760e2a5a11bfbe2f7cf6b0e2a71b590d7 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Fri, 7 Mar 2025 14:33:11 -0500 Subject: Better high-DPI handling for rest of app --- src/report_popup.cpp | 52 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 17 deletions(-) (limited to 'src/report_popup.cpp') diff --git a/src/report_popup.cpp b/src/report_popup.cpp index d772b32..74216c3 100644 --- a/src/report_popup.cpp +++ b/src/report_popup.cpp @@ -12,20 +12,16 @@ ReportPopup::ReportPopup(wxWindow* parent) : wxScrolledCanvas(parent, wxID_ANY) { SetBackgroundStyle(wxBG_STYLE_PAINT); - unchecked_eye_ = - wxBitmap(wxImage(GetAbsolutePath("assets/unchecked.png").c_str(), - wxBITMAP_TYPE_PNG) - .Scale(32, 32)); - checked_eye_ = wxBitmap( - wxImage(GetAbsolutePath("assets/checked.png").c_str(), wxBITMAP_TYPE_PNG) - .Scale(32, 32)); + LoadIcons(); + // TODO: This is slow on high-DPI screens. SetScrollRate(5, 5); SetBackgroundColour(*wxBLACK); Hide(); Bind(wxEVT_PAINT, &ReportPopup::OnPaint, this); + Bind(wxEVT_DPI_CHANGED, &ReportPopup::OnDPIChanged, this); } void ReportPopup::SetDoorId(int door_id) { @@ -41,14 +37,18 @@ void ReportPopup::Reset() { void ReportPopup::UpdateIndicators() { wxMemoryDC mem_dc; + wxFont the_font = GetFont().Scale(GetDPIScaleFactor()); + mem_dc.SetFont(the_font); + const std::map& report = GetDoorRequirements(door_id_); - int acc_height = 10; + int acc_height = FromDIP(10); int col_width = 0; for (const auto& [text, obtained] : report) { wxSize item_extent = mem_dc.GetTextExtent(text); - int item_height = std::max(32, item_extent.GetHeight()) + 10; + int item_height = + std::max(FromDIP(32), item_extent.GetHeight()) + FromDIP(10); acc_height += item_height; if (item_extent.GetWidth() > col_width) { @@ -56,8 +56,8 @@ void ReportPopup::UpdateIndicators() { } } - int item_width = col_width + 10 + 32; - int full_width = item_width + 20; + int item_width = col_width + FromDIP(10 + 32); + int full_width = item_width + FromDIP(20); Fit(); SetVirtualSize(full_width, acc_height); @@ -68,22 +68,23 @@ void ReportPopup::UpdateIndicators() { mem_dc.SetBrush(*wxBLACK_BRUSH); mem_dc.DrawRectangle({0, 0}, {full_width, acc_height}); - mem_dc.SetFont(GetFont()); + mem_dc.SetFont(the_font); - int cur_height = 10; + int cur_height = FromDIP(10); for (const auto& [text, obtained] : report) { wxBitmap* eye_ptr = obtained ? &checked_eye_ : &unchecked_eye_; - mem_dc.DrawBitmap(*eye_ptr, wxPoint{10, cur_height}); + mem_dc.DrawBitmap(*eye_ptr, wxPoint{FromDIP(10), cur_height}); mem_dc.SetTextForeground(obtained ? *wxWHITE : *wxRED); wxSize item_extent = mem_dc.GetTextExtent(text); mem_dc.DrawText( - text, wxPoint{10 + 32 + 10, - cur_height + (32 - mem_dc.GetFontMetrics().height) / 2}); + text, wxPoint{FromDIP(10 + 32 + 10), + cur_height + + (FromDIP(32) - mem_dc.GetFontMetrics().height) / 2}); - cur_height += 10 + 32; + cur_height += FromDIP(10 + 32); } } @@ -96,3 +97,20 @@ void ReportPopup::OnPaint(wxPaintEvent& event) { event.Skip(); } + +void ReportPopup::OnDPIChanged(wxDPIChangedEvent& event) { + LoadIcons(); + UpdateIndicators(); +} + +void ReportPopup::LoadIcons() { + // TODO: We do not have to read these in and scale them for every single + // popup. + unchecked_eye_ = + wxBitmap(wxImage(GetAbsolutePath("assets/unchecked.png").c_str(), + wxBITMAP_TYPE_PNG) + .Scale(FromDIP(32), FromDIP(32))); + checked_eye_ = wxBitmap( + wxImage(GetAbsolutePath("assets/checked.png").c_str(), wxBITMAP_TYPE_PNG) + .Scale(FromDIP(32), FromDIP(32))); +} -- cgit 1.4.1