ConsoleCommand
1 month ago
Dimension
1 month ago
API.php
6 months ago
AggregatedMetric.php
2 years ago
ArchivedMetric.php
1 year ago
Archiver.php
1 month ago
Categories.php
2 years ago
ComponentFactory.php
3 months ago
ComputedMetric.php
1 year ago
ConsoleCommand.php
1 month ago
Controller.php
1 month ago
ControllerAdmin.php
2 weeks ago
Dependency.php
1 month ago
LogTablesProvider.php
2 years ago
Manager.php
1 month ago
Menu.php
1 month ago
MetadataLoader.php
1 month ago
Metric.php
1 month ago
PluginException.php
1 year ago
ProcessedMetric.php
3 months ago
ReleaseChannels.php
3 months ago
Report.php
1 month ago
ReportsProvider.php
2 years ago
RequestProcessors.php
4 months ago
Segment.php
3 months ago
SettingsProvider.php
1 month ago
Tasks.php
1 month ago
ThemeStyles.php
2 weeks ago
ViewDataTable.php
3 months ago
Visualization.php
1 year ago
WidgetsProvider.php
3 months ago
ThemeStyles.php
293 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Matomo - free/libre analytics platform |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 8 | */ |
| 9 | namespace Piwik\Plugin; |
| 10 | |
| 11 | use Piwik\Piwik; |
| 12 | class ThemeStyles |
| 13 | { |
| 14 | public const AUTO_MODE = 'auto'; |
| 15 | public const LIGHT_MODE = 'light'; |
| 16 | public const DARK_MODE = 'dark'; |
| 17 | // to maintain BC w/ old names that were defined in LESS |
| 18 | private static $propertyNamesToLessVariableNames = ['fontFamilyBase' => 'theme-fontFamily-base', 'colorBrand' => 'theme-color-brand', 'colorBrandContrast' => 'theme-color-brand-contrast', 'colorFocusRing' => 'theme-color-focus-ring', 'colorFocusRingAlternative' => 'theme-color-focus-ring-alternative', 'colorTextHighContrast' => 'theme-color-text-highContrast', 'colorText' => 'theme-color-text', 'colorTextContrast' => 'theme-color-text-contrast', 'colorTextLight' => 'theme-color-text-light', 'colorTextLighter' => 'theme-color-text-lighter', 'colorTextOnDisabled' => 'theme-color-text-on-disabled', 'colorTextInvert' => 'theme-color-text-invert', 'colorTextInvertContrast' => 'theme-color-text-invert-contrast', 'colorTextInvertLight' => 'theme-color-text-invert-light', 'colorTextDisabled' => 'theme-color-text-disabled', 'colorLink' => 'theme-color-link', 'colorBaseSeries' => 'theme-color-base-series', 'colorHeadlineAlternative' => 'theme-color-headline-alternative', 'colorHeaderBackground' => 'theme-color-header-background', 'colorHeaderText' => 'theme-color-header-text', 'colorMenuContrastText' => 'theme-color-menu-contrast-text', 'colorMenuContrastTextSelected' => 'theme-color-menu-contrast-textSelected', 'colorMenuContrastTextActive' => 'theme-color-menu-contrast-textActive', 'colorMenuContrastBackground' => 'theme-color-menu-contrast-background', 'colorWidgetExportedBackgroundBase' => 'theme-color-widget-exported-background-base', 'colorWidgetTitleText' => 'theme-color-widget-title-text', 'colorWidgetTitleBackground' => 'theme-color-widget-title-background', 'colorBackgroundBase' => 'theme-color-background-base', 'colorBackgroundTinyContrast' => 'theme-color-background-tinyContrast', 'colorBackgroundLowContrast' => 'theme-color-background-lowContrast', 'colorBackgroundContrast' => 'theme-color-background-contrast', 'colorBackgroundHighContrast' => 'theme-color-background-highContrast', 'colorBackgroundDisabled' => 'theme-color-background-disabled', 'colorBorder' => 'theme-color-border', 'colorBorderAlternative' => 'theme-color-border-alternative', 'colorBorderLight' => 'theme-color-border-light', 'colorBoxShadow' => 'theme-color-boxShadow', 'colorCode' => 'theme-color-code', 'colorCodeBackground' => 'theme-color-code-background', 'colorWidgetBackground' => 'theme-color-widget-background', 'colorWidgetBorder' => 'theme-color-widget-border', 'filterOnIllustration' => 'theme-filter-on-illustration', 'colorMenuContrastBackgroundHover' => 'theme-color-menu-contrast-backgroundHover']; |
| 19 | /** |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $themeMode = self::AUTO_MODE; |
| 23 | /** |
| 24 | * @var string|array<string> |
| 25 | */ |
| 26 | public $fontFamilyBase = '-apple-system, BlinkMacSystemFont, \'Segoe UI\', Roboto, Oxygen-Sans, Cantarell, \'Helvetica Neue\', sans-serif'; |
| 27 | /** |
| 28 | * @var string|array<string> |
| 29 | */ |
| 30 | public $colorBrand = ['#43a047', '#778fd4']; |
| 31 | /** |
| 32 | * @var string|array<string> |
| 33 | */ |
| 34 | public $colorBrandContrast = ['#fff', '#ffffff']; |
| 35 | /** |
| 36 | * @var string|array<string> |
| 37 | */ |
| 38 | public $colorFocusRing = '#0969da'; |
| 39 | /** |
| 40 | * @var string|array<string> |
| 41 | */ |
| 42 | public $colorFocusRingAlternative = '#00b4bE'; |
| 43 | /** |
| 44 | * @var string|array<string> |
| 45 | */ |
| 46 | public $colorTextHighContrast = ['#000', '#d9d9d9']; |
| 47 | /** |
| 48 | * @var string|array<string> |
| 49 | */ |
| 50 | public $colorText = ['#212121', '#ccc']; |
| 51 | /** |
| 52 | * @var string|array<string> |
| 53 | */ |
| 54 | public $colorTextContrast = ['#37474f', '#bbb']; |
| 55 | /** |
| 56 | * @var string|array<string> |
| 57 | */ |
| 58 | public $colorTextLight = ['#444', '#aaa']; |
| 59 | /** |
| 60 | * @var string|array<string> |
| 61 | */ |
| 62 | public $colorTextLighter = ['#646464', '#999']; |
| 63 | /** |
| 64 | * @var string|array<string> |
| 65 | */ |
| 66 | public $colorTextOnDisabled = ['#666666', '#999']; |
| 67 | /** |
| 68 | * @var string|array<string> |
| 69 | */ |
| 70 | public $colorTextInvert = ['#ccc', '#555']; |
| 71 | /** |
| 72 | * @var string|array<string> |
| 73 | */ |
| 74 | public $colorTextInvertContrast = ['#fff', '#000']; |
| 75 | /** |
| 76 | * @var string|array<string> |
| 77 | */ |
| 78 | public $colorTextInvertLight = ['#b9b9b9', '#666']; |
| 79 | /** |
| 80 | * @var string|array<string> |
| 81 | */ |
| 82 | public $colorTextDisabled = ['#aaa', '#666']; |
| 83 | /** |
| 84 | * @var string|array<string> |
| 85 | */ |
| 86 | public $colorLink = ['#1976D2', '#778fd4']; |
| 87 | /** |
| 88 | * @var string|array<string> |
| 89 | */ |
| 90 | public $colorBaseSeries = '#ee3024'; |
| 91 | /** |
| 92 | * @var string|array<string> |
| 93 | */ |
| 94 | public $colorHeadlineAlternative = ['#4E4E4E', '#aaa']; |
| 95 | /** |
| 96 | * @var string|array<string> |
| 97 | */ |
| 98 | public $colorHeaderBackground; |
| 99 | /** |
| 100 | * @var string|array<string> |
| 101 | */ |
| 102 | public $colorHeaderText; |
| 103 | /** |
| 104 | * @var string|array<string> |
| 105 | */ |
| 106 | public $colorMenuContrastText; |
| 107 | /** |
| 108 | * @var string|array<string> |
| 109 | */ |
| 110 | public $colorMenuContrastTextSelected; |
| 111 | /** |
| 112 | * @var string|array<string> |
| 113 | */ |
| 114 | public $colorMenuContrastTextActive = ['#1976D2', '#fff']; |
| 115 | /** |
| 116 | * @var string|array<string> |
| 117 | */ |
| 118 | public $colorMenuContrastBackgroundHover = ['#eff0f1', '#151819']; |
| 119 | /** |
| 120 | * @var string|array<string> |
| 121 | */ |
| 122 | public $colorMenuContrastBackground; |
| 123 | /** |
| 124 | * @var string|array<string> |
| 125 | */ |
| 126 | public $colorWidgetExportedBackgroundBase; |
| 127 | /** |
| 128 | * @var string|array<string> |
| 129 | */ |
| 130 | public $colorWidgetTitleText; |
| 131 | /** |
| 132 | * @var string|array<string> |
| 133 | */ |
| 134 | public $colorWidgetTitleBackground; |
| 135 | /** |
| 136 | * @var string|array<string> |
| 137 | */ |
| 138 | public $colorBackgroundBase = ['#f5f5f5', '#151819']; |
| 139 | /** |
| 140 | * @var string|array<string> |
| 141 | */ |
| 142 | public $colorBackgroundTinyContrast = ['#f2f2f2', '#182c32']; |
| 143 | /** |
| 144 | * @var string|array<string> |
| 145 | */ |
| 146 | public $colorBackgroundLowContrast = ['#d9d9d9', '#192d33']; |
| 147 | /** |
| 148 | * @var string|array<string> |
| 149 | */ |
| 150 | public $colorBackgroundContrast = ['#fff', '#202329']; |
| 151 | /** |
| 152 | * @var string|array<string> |
| 153 | */ |
| 154 | public $colorBackgroundHighContrast = ['#202020', '#404349']; |
| 155 | /** |
| 156 | * @var string|array<string> |
| 157 | */ |
| 158 | public $colorBackgroundDisabled = ['#d9d9d9', '#303339']; |
| 159 | /** |
| 160 | * @var string|array<string> |
| 161 | */ |
| 162 | public $colorBorderLight = ['#a9a399', '#645e54']; |
| 163 | /** |
| 164 | * @var string|array<string> |
| 165 | * @deprecated Use $colorBorderAlternative instead. Retained only for legacy use and will be |
| 166 | * phased out once everything has moved to the alternative. |
| 167 | */ |
| 168 | public $colorBorder = ['#cccccc', '#555555']; |
| 169 | /** |
| 170 | * Use this as the new border color. $colorBorder only remains so that we don't need to update |
| 171 | * screenshots unless we really need to |
| 172 | * |
| 173 | * @var string|array<string> |
| 174 | */ |
| 175 | public $colorBorderAlternative = ['#E0E0E0', '#555555']; |
| 176 | /** |
| 177 | * @var string|array<string> |
| 178 | */ |
| 179 | public $colorBoxShadow = ['rgba(0, 0, 0, 0.1)', 'rgba(0, 0, 0, 0.1)']; |
| 180 | /** |
| 181 | * @var string|array<string> |
| 182 | */ |
| 183 | public $colorCode = '#f3f3f3'; |
| 184 | /** |
| 185 | * @var string|array<string> |
| 186 | */ |
| 187 | public $colorCodeBackground = '#4d4d4d'; |
| 188 | /** |
| 189 | * @var string|array<string> |
| 190 | */ |
| 191 | public $colorWidgetBackground; |
| 192 | /** |
| 193 | * @var string|array<string> |
| 194 | */ |
| 195 | public $colorWidgetBorder; |
| 196 | /** |
| 197 | * @var string|array<string> |
| 198 | */ |
| 199 | public $filterOnIllustration = ['none', 'brightness(89%) invert(100%) hue-rotate(180deg)']; |
| 200 | public function __construct(string $themeMode) |
| 201 | { |
| 202 | $this->themeMode = $themeMode; |
| 203 | $this->colorMenuContrastText = $this->colorText; |
| 204 | $this->colorMenuContrastTextSelected = $this->colorMenuContrastText; |
| 205 | $this->colorMenuContrastBackground = $this->colorBackgroundContrast; |
| 206 | $this->colorWidgetExportedBackgroundBase = $this->colorBackgroundContrast; |
| 207 | $this->colorWidgetTitleText = $this->colorText; |
| 208 | $this->colorWidgetTitleBackground = $this->colorBackgroundContrast; |
| 209 | $this->colorWidgetBackground = $this->colorBackgroundContrast; |
| 210 | $this->colorWidgetBorder = $this->colorBackgroundTinyContrast; |
| 211 | $this->colorHeaderBackground = $this->colorBackgroundContrast; |
| 212 | $this->colorHeaderText = $this->colorTextLighter; |
| 213 | } |
| 214 | /** |
| 215 | * @return ThemeStyles |
| 216 | */ |
| 217 | public static function get(string $mode = self::AUTO_MODE) |
| 218 | { |
| 219 | $result = new self($mode); |
| 220 | /** |
| 221 | * @ignore |
| 222 | */ |
| 223 | Piwik::postEvent('Theme.configureThemeVariables', [$result]); |
| 224 | return $result; |
| 225 | } |
| 226 | public function getIsLightMode() : bool |
| 227 | { |
| 228 | return $this->themeMode === self::LIGHT_MODE; |
| 229 | } |
| 230 | public function getIsDarkMode() : bool |
| 231 | { |
| 232 | return $this->themeMode === self::DARK_MODE; |
| 233 | } |
| 234 | public function getThemeMode() : string |
| 235 | { |
| 236 | return $this->themeMode; |
| 237 | } |
| 238 | public function getPropertyValue(string $name) : string |
| 239 | { |
| 240 | if (!property_exists($this, $name)) { |
| 241 | return ''; |
| 242 | } |
| 243 | return $this->resolvePropertyValue($this->{$name}, $this->getIsDarkMode() ? 1 : 0); |
| 244 | } |
| 245 | public function toLessCode() |
| 246 | { |
| 247 | $rootCssVars = []; |
| 248 | $darkCssVars = []; |
| 249 | foreach (get_object_vars($this) as $name => $value) { |
| 250 | $varName = isset(self::$propertyNamesToLessVariableNames[$name]) ? self::$propertyNamesToLessVariableNames[$name] : $this->getGenericThemeVarName($name); |
| 251 | if (is_array($value)) { |
| 252 | $rootCssVars[] = " --{$varName}: " . $this->resolvePropertyValue($value, 0) . ";\n"; |
| 253 | $darkCssVars[] = " --{$varName}: " . $this->resolvePropertyValue($value, 1) . ";\n"; |
| 254 | } else { |
| 255 | $rootCssVars[] = " --{$varName}: " . $this->resolvePropertyValue($value, 0) . ";\n"; |
| 256 | } |
| 257 | } |
| 258 | $result = ":root {\n color-scheme: light;\n" . implode('', $rootCssVars) . "}\n\n"; |
| 259 | if (!empty($darkCssVars)) { |
| 260 | $result .= "[data-theme-mode=\"dark\"] {\n color-scheme: dark;\n" . implode('', $darkCssVars) . "}\n\n"; |
| 261 | $result .= "@media (prefers-color-scheme: dark) {\n"; |
| 262 | $result .= " [data-theme-mode=\"auto\"] {\n color-scheme: dark;\n" . implode('', $darkCssVars) . " }\n"; |
| 263 | $result .= "}\n\n"; |
| 264 | } |
| 265 | foreach (get_object_vars($this) as $name => $_) { |
| 266 | $varName = isset(self::$propertyNamesToLessVariableNames[$name]) ? self::$propertyNamesToLessVariableNames[$name] : $this->getGenericThemeVarName($name); |
| 267 | $result .= "@{$varName}: ~\"var(--{$varName})\";\n"; |
| 268 | } |
| 269 | return $result; |
| 270 | } |
| 271 | private function getGenericThemeVarName($propertyName) |
| 272 | { |
| 273 | return 'theme-' . $propertyName; |
| 274 | } |
| 275 | /** |
| 276 | * @param mixed $value |
| 277 | */ |
| 278 | private function resolvePropertyValue($value, int $preferredIndex) : string |
| 279 | { |
| 280 | if (!is_array($value)) { |
| 281 | return is_string($value) ? $value : ''; |
| 282 | } |
| 283 | $fallbackIndex = $preferredIndex === 1 ? 0 : 1; |
| 284 | if (isset($value[$preferredIndex]) && is_string($value[$preferredIndex])) { |
| 285 | return $value[$preferredIndex]; |
| 286 | } |
| 287 | if (isset($value[$fallbackIndex]) && is_string($value[$fallbackIndex])) { |
| 288 | return $value[$fallbackIndex]; |
| 289 | } |
| 290 | return ''; |
| 291 | } |
| 292 | } |
| 293 |