You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
5.1 KiB

3 years ago
3 years ago
3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2019 KiCad Developers, see AUTHORS.TXT for contributors.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, you may find one here:
  18. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  19. * or you may search the http://www.gnu.org website for the version 2 license,
  20. * or you may write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <gal/dpi_scaling.h>
  24. #include <optional>
  25. #include <env_vars.h>
  26. #include <settings/common_settings.h>
  27. #include <kiplatform/ui.h>
  28. #include <wx/log.h>
  29. /**
  30. * Flag to enable trace for HiDPI scaling factors
  31. *
  32. * Use "KICAD_TRACE_HIGH_DPI" to enable.
  33. *
  34. * @ingroup trace_env_vars
  35. */
  36. const wxChar* const traceHiDpi = wxT( "KICAD_TRACE_HIGH_DPI" );
  37. /**
  38. * Get a user-configured scale factor from KiCad config file
  39. *
  40. * @return the scale factor, if set
  41. */
  42. static std::optional<double> getKiCadConfiguredScale( const COMMON_SETTINGS& aConfig )
  43. {
  44. std::optional<double> scale;
  45. double canvas_scale = aConfig.m_Appearance.canvas_scale;
  46. if( canvas_scale > 0.0 )
  47. {
  48. scale = canvas_scale;
  49. }
  50. return scale;
  51. }
  52. /**
  53. * Get the toolkit scale factor from a user-set environment variable
  54. * (for example GDK_SCALE on GTK).
  55. *
  56. * @return the scale factor, if set
  57. */
  58. static std::optional<double> getEnvironmentScale()
  59. {
  60. const wxPortId port_id = wxPlatformInfo::Get().GetPortId();
  61. std::optional<double> scale;
  62. if( port_id == wxPORT_GTK )
  63. {
  64. // Under GTK, the user can use GDK_SCALE to force the scaling
  65. scale = ENV_VAR::GetEnvVar<double>( wxS( "GDK_SCALE" ) );
  66. }
  67. return scale;
  68. }
  69. DPI_SCALING::DPI_SCALING( COMMON_SETTINGS* aConfig, const wxWindow* aWindow )
  70. : m_config( aConfig ), m_window( aWindow )
  71. {
  72. }
  73. double DPI_SCALING::GetScaleFactor() const
  74. {
  75. std::optional<double> val;
  76. wxString src;
  77. if( m_config )
  78. {
  79. val = getKiCadConfiguredScale( *m_config );
  80. src = wxS( "config" );
  81. }
  82. if( !val )
  83. {
  84. val = getEnvironmentScale();
  85. src = wxS( "env" );
  86. }
  87. if( !val && m_window )
  88. {
  89. // Use the native WX reporting.
  90. // On Linux, this will not work until WX 3.2 and GTK >= 3.10
  91. // Otherwise it returns 1.0
  92. val = KIPLATFORM::UI::GetPixelScaleFactor( m_window );
  93. src = wxS( "platform" );
  94. }
  95. if( !val )
  96. {
  97. // Nothing else we can do, give it a default value
  98. val = GetDefaultScaleFactor();
  99. src = wxS( "default" );
  100. }
  101. wxLogTrace( traceHiDpi, wxS( "Scale factor (%s): %f" ), src, *val );
  102. return *val;
  103. }
  104. double DPI_SCALING::GetContentScaleFactor() const
  105. {
  106. std::optional<double> val;
  107. wxString src;
  108. if( m_config )
  109. {
  110. val = getKiCadConfiguredScale( *m_config );
  111. src = wxS( "config" );
  112. }
  113. if( !val )
  114. {
  115. val = getEnvironmentScale();
  116. src = wxS( "env" );
  117. }
  118. if( !val && m_window )
  119. {
  120. // Use the native WX reporting.
  121. // On Linux, this will not work until WX 3.2 and GTK >= 3.10
  122. // Otherwise it returns 1.0
  123. val = KIPLATFORM::UI::GetContentScaleFactor( m_window );
  124. src = wxS( "platform" );
  125. }
  126. if( !val )
  127. {
  128. // Nothing else we can do, give it a default value
  129. val = GetDefaultScaleFactor();
  130. src = wxS( "default" );
  131. }
  132. wxLogTrace( traceHiDpi, wxS( "Content scale factor (%s): %f" ), src, *val );
  133. return *val;
  134. }
  135. bool DPI_SCALING::GetCanvasIsAutoScaled() const
  136. {
  137. if( m_config == nullptr )
  138. {
  139. // No configuration given, so has to be automatic scaling
  140. return true;
  141. }
  142. const bool automatic = getKiCadConfiguredScale( *m_config ) == std::nullopt;
  143. wxLogTrace( traceHiDpi, wxS( "Scale is automatic: %d" ), automatic );
  144. return automatic;
  145. }
  146. void DPI_SCALING::SetDpiConfig( bool aAuto, double aValue )
  147. {
  148. wxCHECK_RET( m_config != nullptr, wxS( "Setting DPI config without a config store." ) );
  149. const double value = aAuto ? 0.0 : aValue;
  150. m_config->m_Appearance.canvas_scale = value;
  151. }
  152. double DPI_SCALING::GetMaxScaleFactor()
  153. {
  154. // displays with higher than 4.0 DPI are not really going to be useful
  155. // for KiCad (even an 8k display would be effectively only ~1080p at 4x)
  156. return 6.0;
  157. }
  158. double DPI_SCALING::GetMinScaleFactor()
  159. {
  160. // scales under 1.0 don't make sense from a HiDPI perspective
  161. return 1.0;
  162. }
  163. double DPI_SCALING::GetDefaultScaleFactor()
  164. {
  165. // no scaling => 1.0
  166. return 1.0;
  167. }