KiCad PCB EDA Suite
DPI_SCALING Class Reference

Class to handle configuration and automatic determination of the DPI scale to use for canvases. More...

#include <dpi_scaling.h>

Public Member Functions

 DPI_SCALING (COMMON_SETTINGS *aConfig, const wxWindow *aWindow)
 Construct a DPI scale provider. More...
 
double GetScaleFactor () const
 Get the DPI scale from all known sources in order: More...
 
double GetContentScaleFactor () const
 Get the content scale factor, which may be different from the scale factor on some platforms. More...
 
bool GetCanvasIsAutoScaled () const
 Is the current value auto scaled, or is it user-set in the config. More...
 
void SetDpiConfig (bool aAuto, double aValue)
 Set the common DPI config in a given config object. More...
 

Static Public Member Functions

static double GetMaxScaleFactor ()
 
static double GetMinScaleFactor ()
 
static double GetDefaultScaleFactor ()
 Get the "default" scaling factor to use if not other config is available. More...
 

Private Attributes

COMMON_SETTINGSm_config
 The configuration object to use to get/set user setting. More...
 
const wxWindow * m_window
 The WX window to use for WX's automatic DPI checking. More...
 

Detailed Description

Class to handle configuration and automatic determination of the DPI scale to use for canvases.

This has several sources and the availability of some of them are platform dependent.

Definition at line 36 of file dpi_scaling.h.

Constructor & Destructor Documentation

◆ DPI_SCALING()

DPI_SCALING::DPI_SCALING ( COMMON_SETTINGS aConfig,
const wxWindow *  aWindow 
)

Construct a DPI scale provider.

Parameters
aConfigthe config store to check for a user value (can be nullptr, in which case on automatically determined values are considered)
aWindowa WX window to use for automatic DPI determination
Returns
the scaling factor (1.0 = no scaling)

Definition at line 85 of file dpi_scaling.cpp.

86 : m_config( aConfig ), m_window( aWindow )
87{
88}
COMMON_SETTINGS * m_config
The configuration object to use to get/set user setting.
Definition: dpi_scaling.h:103
const wxWindow * m_window
The WX window to use for WX's automatic DPI checking.
Definition: dpi_scaling.h:108

Member Function Documentation

◆ GetCanvasIsAutoScaled()

bool DPI_SCALING::GetCanvasIsAutoScaled ( ) const

Is the current value auto scaled, or is it user-set in the config.

Definition at line 169 of file dpi_scaling.cpp.

170{
171 if( m_config == nullptr )
172 {
173 // No configuration given, so has to be automatic scaling
174 return true;
175 }
176
177 const bool automatic = getKiCadConfiguredScale( *m_config ) == std::nullopt;
178 wxLogTrace( traceHiDpi, wxS( "Scale is automatic: %d" ), automatic );
179 return automatic;
180}
static std::optional< double > getKiCadConfiguredScale(const COMMON_SETTINGS &aConfig)
Get a user-configured scale factor from KiCad config file.
Definition: dpi_scaling.cpp:50
const wxChar *const traceHiDpi
Flag to enable trace for HiDPI scaling factors.
Definition: dpi_scaling.cpp:42

References getKiCadConfiguredScale(), m_config, and traceHiDpi.

Referenced by PANEL_COMMON_SETTINGS::applySettingsToPanel().

◆ GetContentScaleFactor()

double DPI_SCALING::GetContentScaleFactor ( ) const

Get the content scale factor, which may be different from the scale factor on some platforms.

This value should be used for scaling user interface elements (fonts, icons, etc) whereas the scale factor should be used for scaling canvases.

Definition at line 130 of file dpi_scaling.cpp.

131{
132 std::optional<double> val;
133 wxString src;
134
135 if( m_config )
136 {
138 src = wxS( "config" );
139 }
140
141 if( !val )
142 {
143 val = getEnvironmentScale();
144 src = wxS( "env" );
145 }
146
147 if( !val && m_window )
148 {
149 // Use the native WX reporting.
150 // On Linux, this will not work until WX 3.2 and GTK >= 3.10
151 // Otherwise it returns 1.0
153 src = wxS( "platform" );
154 }
155
156 if( !val )
157 {
158 // Nothing else we can do, give it a default value
159 val = GetDefaultScaleFactor();
160 src = wxS( "default" );
161 }
162
163 wxLogTrace( traceHiDpi, wxS( "Content scale factor (%s): %f" ), src, *val );
164
165 return *val;
166}
static double GetDefaultScaleFactor()
Get the "default" scaling factor to use if not other config is available.
static std::optional< double > getEnvironmentScale()
Get the toolkit scale factor from a user-set environment variable (for example GDK_SCALE on GTK).
Definition: dpi_scaling.cpp:70
double GetContentScaleFactor(const wxWindow *aWindow)
Tries to determine the content scaling factor currently in use for the window.
Definition: gtk/ui.cpp:126

References KIPLATFORM::UI::GetContentScaleFactor(), GetDefaultScaleFactor(), getEnvironmentScale(), getKiCadConfiguredScale(), m_config, m_window, and traceHiDpi.

Referenced by APPEARANCE_CONTROLS::APPEARANCE_CONTROLS(), COLOR_SWATCH::COLOR_SWATCH(), GBR_LAYER_BOX_SELECTOR::Resync(), and PCB_LAYER_BOX_SELECTOR::Resync().

◆ GetDefaultScaleFactor()

double DPI_SCALING::GetDefaultScaleFactor ( )
static

Get the "default" scaling factor to use if not other config is available.

Definition at line 207 of file dpi_scaling.cpp.

208{
209 // no scaling => 1.0
210 return 1.0;
211}

Referenced by GetContentScaleFactor(), GetScaleFactor(), and PANEL_COMMON_SETTINGS::PANEL_COMMON_SETTINGS().

◆ GetMaxScaleFactor()

double DPI_SCALING::GetMaxScaleFactor ( )
static

Definition at line 193 of file dpi_scaling.cpp.

194{
195 // displays with higher than 4.0 DPI are not really going to be useful
196 // for KiCad (even an 8k display would be effectively only ~1080p at 4x)
197 return 6.0;
198}

Referenced by PANEL_COMMON_SETTINGS::PANEL_COMMON_SETTINGS().

◆ GetMinScaleFactor()

double DPI_SCALING::GetMinScaleFactor ( )
static

Definition at line 201 of file dpi_scaling.cpp.

202{
203 // scales under 1.0 don't make sense from a HiDPI perspective
204 return 1.0;
205}

Referenced by PANEL_COMMON_SETTINGS::PANEL_COMMON_SETTINGS().

◆ GetScaleFactor()

double DPI_SCALING::GetScaleFactor ( ) const

Get the DPI scale from all known sources in order:

  • user config, if given
  • user's environment variables, if set and according to platform
  • WX's internal determination of the DPI scaling (WX > 3.1)

Definition at line 91 of file dpi_scaling.cpp.

92{
93 std::optional<double> val;
94 wxString src;
95
96 if( m_config )
97 {
99 src = wxS( "config" );
100 }
101
102 if( !val )
103 {
104 val = getEnvironmentScale();
105 src = wxS( "env" );
106 }
107
108 if( !val && m_window )
109 {
110 // Use the native WX reporting.
111 // On Linux, this will not work until WX 3.2 and GTK >= 3.10
112 // Otherwise it returns 1.0
114 src = wxS( "platform" );
115 }
116
117 if( !val )
118 {
119 // Nothing else we can do, give it a default value
120 val = GetDefaultScaleFactor();
121 src = wxS( "default" );
122 }
123
124 wxLogTrace( traceHiDpi, wxS( "Scale factor (%s): %f" ), src, *val );
125
126 return *val;
127}
double GetPixelScaleFactor(const wxWindow *aWindow)
Tries to determine the pixel scaling factor currently in use for the window.
Definition: gtk/ui.cpp:113

References GetDefaultScaleFactor(), getEnvironmentScale(), getKiCadConfiguredScale(), KIPLATFORM::UI::GetPixelScaleFactor(), m_config, m_window, and traceHiDpi.

Referenced by PANEL_COMMON_SETTINGS::applySettingsToPanel(), APPEARANCE_CONTROLS::GetBestSize(), PANEL_COMMON_SETTINGS::OnCanvasScaleAuto(), LAYER_BOX_SELECTOR::ResyncBitmapOnly(), and KIGFX::GAL_DISPLAY_OPTIONS::UpdateScaleFactor().

◆ SetDpiConfig()

void DPI_SCALING::SetDpiConfig ( bool  aAuto,
double  aValue 
)

Set the common DPI config in a given config object.

The encoding of the automatic/manual nature of the config is handled internally.

Parameters
aAutostore a value meaning "no user-set scale"
aValuethe value to store (ignored if aAuto set)

Definition at line 183 of file dpi_scaling.cpp.

184{
185 wxCHECK_RET( m_config != nullptr, wxS( "Setting DPI config without a config store." ) );
186
187 const double value = aAuto ? 0.0 : aValue;
188
190}
APPEARANCE m_Appearance

References COMMON_SETTINGS::APPEARANCE::canvas_scale, COMMON_SETTINGS::m_Appearance, and m_config.

Referenced by PANEL_COMMON_SETTINGS::TransferDataFromWindow().

Member Data Documentation

◆ m_config

COMMON_SETTINGS* DPI_SCALING::m_config
private

The configuration object to use to get/set user setting.

nullptr if only automatic options are wanted

Definition at line 103 of file dpi_scaling.h.

Referenced by GetCanvasIsAutoScaled(), GetContentScaleFactor(), GetScaleFactor(), and SetDpiConfig().

◆ m_window

const wxWindow* DPI_SCALING::m_window
private

The WX window to use for WX's automatic DPI checking.

Definition at line 108 of file dpi_scaling.h.

Referenced by GetContentScaleFactor(), and GetScaleFactor().


The documentation for this class was generated from the following files: