Code refactoring

This commit is contained in:
YuSanka 2019-04-17 11:22:30 +02:00
parent 3b9803ba6e
commit 3e9c0c396e
3 changed files with 84 additions and 86 deletions

View file

@ -59,20 +59,41 @@ public:
{
m_scale_factor = (float)get_dpi_for_window(this) / (float)DPI_DEFAULT;
// ->-
m_prev_scale_factor = -1;
// -<-
m_prev_scale_factor = m_scale_factor;
recalc_font();
this->Bind(EVT_DPI_CHANGED, [this](const DpiChangedEvent &evt) {
// ->-
if (m_prev_scale_factor < 0)
reset_prev_scale_factor();
// -<-
m_scale_factor = (float)evt.dpi / (float)DPI_DEFAULT;
on_dpi_changed(evt.rect);
if (!m_can_rescale)
return;
if (is_new_scale_factor())
rescale(evt.rect);
});
this->Bind(wxEVT_MOVE_START, [this](wxMoveEvent& event)
{
event.Skip();
// Suppress application rescaling, when a MainFrame moving is not ended
m_can_rescale = false;
});
this->Bind(wxEVT_MOVE_END, [this](wxMoveEvent& event)
{
event.Skip();
m_can_rescale = is_new_scale_factor();
// If scale factor is different after moving of MainFrame ...
if (m_can_rescale)
// ... rescale application
rescale(event.GetRect());
else
// set value to _true_ in purpose of possibility of a display dpi changing from System Settings
m_can_rescale = true;
});
}
@ -80,23 +101,21 @@ public:
float scale_factor() const { return m_scale_factor; }
float prev_scale_factor() const { return m_prev_scale_factor; }
void reset_prev_scale_factor() { m_prev_scale_factor = m_scale_factor; }
int em_unit() const { return m_em_unit; }
int font_size() const { return m_font_size; }
protected:
virtual void on_dpi_changed(const wxRect &suggested_rect) = 0;
// ->-
// virtual void scale(wxWindow *window, const float& scale) = 0;
// -<-
private:
float m_scale_factor;
float m_prev_scale_factor;
int m_em_unit;
int m_font_size;
float m_prev_scale_factor;
bool m_can_rescale{ true };
void recalc_font()
{
wxClientDC dc(this);
@ -104,6 +123,40 @@ private:
m_font_size = metrics.height;
m_em_unit = metrics.averageWidth;
}
// check if new scale is differ from previous
bool is_new_scale_factor() const { return fabs(m_scale_factor - m_prev_scale_factor) > 0.001; }
// recursive function for scaling fonts for all controls in Window
void scale_controls_fonts(wxWindow *window, const float scale_f)
{
auto children = window->GetChildren();
for (auto child : children) {
scale_controls_fonts(child, scale_f);
child->SetFont(child->GetFont().Scaled(scale_f));
}
window->Layout();
}
void rescale(const wxRect &suggested_rect)
{
this->Freeze();
// rescale fonts of all controls
scale_controls_fonts(this, m_scale_factor / m_prev_scale_factor);
// rescale missed controls sizes and images
on_dpi_changed(suggested_rect);
this->Layout();
this->Thaw();
// reset previous scale factor from current scale factor value
m_prev_scale_factor = m_scale_factor;
}
};
typedef DPIAware<wxFrame> DPIFrame;