FIX: scale bitmap of SwitchButton in MacOS

Change-Id: I5c2329d5b8fd409f87276851068e6594bbae5b3d
This commit is contained in:
chunmao.guo 2022-07-27 10:18:02 +08:00 committed by Lane.Wei
parent 9fc010512c
commit 444ab79087
1 changed files with 22 additions and 7 deletions

View File

@ -3,6 +3,7 @@
#include "StaticBox.hpp" #include "StaticBox.hpp"
#include "../wxExtensions.hpp" #include "../wxExtensions.hpp"
#include "../Utils/MacDarkMode.hpp"
#include <wx/dcgraph.h> #include <wx/dcgraph.h>
@ -56,11 +57,19 @@ void SwitchButton::Rescale()
m_off.msw_rescale(); m_off.msw_rescale();
} }
else { else {
constexpr int BS = 1; #ifdef __WXOSX__
auto scale = Slic3r::GUI::mac_max_scaling_factor();
int BS = (int) scale;
#else
constexpr int BS = 1;
#endif
wxSize thumbSize; wxSize thumbSize;
wxSize trackSize; wxSize trackSize;
wxClientDC dc(this); wxClientDC dc(this);
wxSize textSize[2]; #ifdef __WXOSX__
dc.SetFont(dc.GetFont().Scaled(scale));
#endif
wxSize textSize[2];
{ {
textSize[0] = dc.GetTextExtent(labels[0]); textSize[0] = dc.GetTextExtent(labels[0]);
textSize[1] = dc.GetTextExtent(labels[1]); textSize[1] = dc.GetTextExtent(labels[1]);
@ -70,11 +79,14 @@ void SwitchButton::Rescale()
auto size = textSize[1]; auto size = textSize[1];
if (size.x > thumbSize.x) thumbSize.x = size.x; if (size.x > thumbSize.x) thumbSize.x = size.x;
else size.x = thumbSize.x; else size.x = thumbSize.x;
thumbSize.x += 12; thumbSize.x += BS * 12;
thumbSize.y += 2; thumbSize.y += BS * 2;
trackSize.x = thumbSize.x + size.x + 10; trackSize.x = thumbSize.x + size.x + BS * 10;
trackSize.y = thumbSize.y + BS * 2; trackSize.y = thumbSize.y + BS * 2;
auto maxWidth = GetMaxWidth(); auto maxWidth = GetMaxWidth();
#ifdef __WXOSX__
maxWidth *= scale;
#endif
if (trackSize.x > maxWidth) { if (trackSize.x > maxWidth) {
thumbSize.x -= (trackSize.x - maxWidth) / 2; thumbSize.x -= (trackSize.x - maxWidth) / 2;
trackSize.x = maxWidth; trackSize.x = maxWidth;
@ -86,7 +98,7 @@ void SwitchButton::Rescale()
memdc.SelectObject(bmp); memdc.SelectObject(bmp);
memdc.SetBackground(wxBrush(GetBackgroundColour())); memdc.SetBackground(wxBrush(GetBackgroundColour()));
memdc.Clear(); memdc.Clear();
memdc.SetFont(GetFont()); memdc.SetFont(dc.GetFont());
auto state = i == 0 ? StateColor::Enabled : (StateColor::Checked | StateColor::Enabled); auto state = i == 0 ? StateColor::Enabled : (StateColor::Checked | StateColor::Enabled);
{ {
#ifdef __WXMSW__ #ifdef __WXMSW__
@ -106,6 +118,9 @@ void SwitchButton::Rescale()
memdc.SetTextForeground(text_color.colorForStates(state)); memdc.SetTextForeground(text_color.colorForStates(state));
memdc.DrawText(labels[1], {trackSize.x - thumbSize.x - BS + (thumbSize.x - textSize[1].x) / 2, BS + (thumbSize.y - textSize[1].y) / 2}); memdc.DrawText(labels[1], {trackSize.x - thumbSize.x - BS + (thumbSize.x - textSize[1].x) / 2, BS + (thumbSize.y - textSize[1].y) / 2});
memdc.SelectObject(wxNullBitmap); memdc.SelectObject(wxNullBitmap);
#ifdef __WXOSX__
bmp = wxBitmap(bmp.ConvertToImage(), -1, scale);
#endif
(i == 0 ? m_off : m_on).bmp() = bmp; (i == 0 ? m_off : m_on).bmp() = bmp;
} }
} }
@ -116,4 +131,4 @@ void SwitchButton::Rescale()
void SwitchButton::update() void SwitchButton::update()
{ {
SetBitmap((GetValue() ? m_on : m_off).bmp()); SetBitmap((GetValue() ? m_on : m_off).bmp());
} }