CommonThemeTrait.php 6.8 KB

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