1
0

CommonThemeTrait.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Theming\Themes;
  8. use OCA\Theming\AppInfo\Application;
  9. use OCA\Theming\ImageManager;
  10. use OCA\Theming\Service\BackgroundService;
  11. use OCA\Theming\ThemingDefaults;
  12. use OCA\Theming\Util;
  13. trait CommonThemeTrait {
  14. public Util $util;
  15. public ThemingDefaults $themingDefaults;
  16. protected bool $isDarkVariant = false;
  17. /**
  18. * Generate primary-related variables
  19. * This is shared between multiple themes because colorMainBackground and colorMainText
  20. * will change in between.
  21. */
  22. protected function generatePrimaryVariables(string $colorMainBackground, string $colorMainText, bool $highContrast = false): array {
  23. $isBrightColor = $this->util->isBrightColor($colorMainBackground);
  24. $colorPrimaryElement = $this->util->elementColor($this->primaryColor, $isBrightColor, $colorMainBackground, $highContrast);
  25. $colorPrimaryLight = $this->util->mix($colorPrimaryElement, $colorMainBackground, -80);
  26. $colorPrimaryElementLight = $this->util->mix($colorPrimaryElement, $colorMainBackground, -80);
  27. $invertPrimaryTextColor = $this->util->invertTextColor($colorPrimaryElement);
  28. // primary related colours
  29. return [
  30. // invert filter if primary is too bright
  31. // to be used for legacy reasons only. Use inline
  32. // svg with proper css variable instead or material
  33. // design icons.
  34. // ⚠️ Using 'no' as a value to make sure we specify an
  35. // invalid one with no fallback. 'unset' could here fallback to some
  36. // other theme with media queries
  37. '--primary-invert-if-bright' => $this->util->invertTextColor($colorPrimaryElement) ? 'invert(100%)' : 'no',
  38. '--primary-invert-if-dark' => $this->util->invertTextColor($colorPrimaryElement) ? 'no' : 'invert(100%)',
  39. '--color-primary' => $this->primaryColor,
  40. '--color-primary-text' => $this->util->invertTextColor($this->primaryColor) ? '#000000' : '#ffffff',
  41. '--color-primary-hover' => $this->util->mix($this->primaryColor, $colorMainBackground, 60),
  42. '--color-primary-light' => $colorPrimaryLight,
  43. '--color-primary-light-text' => $this->util->mix($this->primaryColor, $this->util->invertTextColor($colorPrimaryLight) ? '#000000' : '#ffffff', -20),
  44. '--color-primary-light-hover' => $this->util->mix($colorPrimaryLight, $colorMainText, 90),
  45. // used for buttons, inputs...
  46. '--color-primary-element' => $colorPrimaryElement,
  47. '--color-primary-element-hover' => $invertPrimaryTextColor ? $this->util->lighten($colorPrimaryElement, 4) : $this->util->darken($colorPrimaryElement, 4),
  48. '--color-primary-element-text' => $invertPrimaryTextColor ? '#000000' : '#ffffff',
  49. // mostly used for disabled states
  50. '--color-primary-element-text-dark' => $invertPrimaryTextColor ? $this->util->lighten('#000000', 4) : $this->util->darken('#ffffff', 4),
  51. // used for hover/focus states
  52. '--color-primary-element-light' => $colorPrimaryElementLight,
  53. '--color-primary-element-light-hover' => $this->util->mix($colorPrimaryElementLight, $colorMainText, 90),
  54. '--color-primary-element-light-text' => $this->util->mix($colorPrimaryElement, $this->util->invertTextColor($colorPrimaryElementLight) ? '#000000' : '#ffffff', -20),
  55. // to use like this: background-image: var(--gradient-primary-background);
  56. '--gradient-primary-background' => 'linear-gradient(40deg, var(--color-primary) 0%, var(--color-primary-hover) 100%)',
  57. ];
  58. }
  59. /**
  60. * Generate admin theming background-related variables
  61. */
  62. protected function generateGlobalBackgroundVariables(): array {
  63. $backgroundDeleted = $this->config->getAppValue(Application::APP_ID, 'backgroundMime', '') === 'backgroundColor';
  64. $hasCustomLogoHeader = $this->util->isLogoThemed();
  65. $backgroundColor = $this->themingDefaults->getColorBackground();
  66. // Default last fallback values
  67. $variables = [
  68. '--color-background-plain' => $backgroundColor,
  69. '--color-background-plain-text' => $this->util->invertTextColor($backgroundColor) ? '#000000' : '#ffffff',
  70. '--background-image-invert-if-bright' => $this->util->invertTextColor($backgroundColor) ? 'invert(100%)' : 'no',
  71. ];
  72. // Register image variables only if custom-defined
  73. foreach (ImageManager::SUPPORTED_IMAGE_KEYS as $image) {
  74. if ($this->imageManager->hasImage($image)) {
  75. $imageUrl = $this->imageManager->getImageUrl($image);
  76. $variables["--image-$image"] = "url('" . $imageUrl . "')";
  77. } elseif ($image === 'background') {
  78. // Apply default background if nothing is configured
  79. $variables['--image-background'] = "url('" . $this->themingDefaults->getBackground($this->isDarkVariant) . "')";
  80. }
  81. }
  82. // If a background has been requested let's not define the background image
  83. if ($backgroundDeleted) {
  84. $variables['--image-background'] = 'none';
  85. }
  86. if ($hasCustomLogoHeader) {
  87. // prevent inverting the logo on bright colors if customized
  88. $variables['--image-logoheader-custom'] = 'true';
  89. }
  90. return $variables;
  91. }
  92. /**
  93. * Generate user theming background-related variables
  94. */
  95. protected function generateUserBackgroundVariables(): array {
  96. $user = $this->userSession->getUser();
  97. if ($user !== null
  98. && !$this->themingDefaults->isUserThemingDisabled()
  99. && $this->appManager->isEnabledForUser(Application::APP_ID)) {
  100. $backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT);
  101. $backgroundColor = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_color', $this->themingDefaults->getColorBackground());
  102. $currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0');
  103. $isBackgroundBright = $this->util->invertTextColor($backgroundColor);
  104. $backgroundTextColor = $this->util->invertTextColor($backgroundColor) ? '#000000' : '#ffffff';
  105. $variables = [
  106. '--color-background-plain' => $backgroundColor,
  107. '--color-background-plain-text' => $backgroundTextColor,
  108. '--background-image-invert-if-bright' => $isBackgroundBright ? 'invert(100%)' : 'no',
  109. ];
  110. // Only use a background color without an image
  111. if ($backgroundImage === BackgroundService::BACKGROUND_COLOR) {
  112. // Might be defined already by admin theming, needs to be overridden
  113. $variables['--image-background'] = 'none';
  114. }
  115. // The user uploaded a custom background
  116. if ($backgroundImage === BackgroundService::BACKGROUND_CUSTOM) {
  117. $cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8);
  118. $variables['--image-background'] = "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')";
  119. }
  120. // The user picked a shipped background
  121. if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) {
  122. $shippedBackground = BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage];
  123. if ($this->isDarkVariant && isset($shippedBackground['dark_variant'])) {
  124. $backgroundImage = $shippedBackground['dark_variant'];
  125. }
  126. $variables['--image-background'] = "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "img/background/$backgroundImage") . "')";
  127. }
  128. return $variables;
  129. }
  130. return [];
  131. }
  132. }