CommonThemeTrait.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2022 Joas Schilling <coding@schilljs.com>
  5. *
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author John Molakvoæ <skjnldsv@protonmail.com>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Theming\Themes;
  26. use OCA\Theming\AppInfo\Application;
  27. use OCA\Theming\ImageManager;
  28. use OCA\Theming\Service\BackgroundService;
  29. use OCA\Theming\Util;
  30. trait CommonThemeTrait {
  31. public Util $util;
  32. /**
  33. * Generate primary-related variables
  34. * This is shared between multiple themes because colorMainBackground and colorMainText
  35. * will change in between.
  36. */
  37. protected function generatePrimaryVariables(string $colorMainBackground, string $colorMainText, bool $highContrast = false): array {
  38. $isBrightColor = $this->util->isBrightColor($colorMainBackground);
  39. $colorPrimaryElement = $this->util->elementColor($this->primaryColor, $isBrightColor, $colorMainBackground, $highContrast);
  40. $colorPrimaryLight = $this->util->mix($colorPrimaryElement, $colorMainBackground, -80);
  41. $colorPrimaryElementLight = $this->util->mix($colorPrimaryElement, $colorMainBackground, -80);
  42. $invertPrimaryTextColor = $this->util->invertTextColor($colorPrimaryElement);
  43. // primary related colours
  44. return [
  45. // invert filter if primary is too bright
  46. // to be used for legacy reasons only. Use inline
  47. // svg with proper css variable instead or material
  48. // design icons.
  49. // ⚠️ Using 'no' as a value to make sure we specify an
  50. // invalid one with no fallback. 'unset' could here fallback to some
  51. // other theme with media queries
  52. '--primary-invert-if-bright' => $this->util->invertTextColor($colorPrimaryElement) ? 'invert(100%)' : 'no',
  53. '--primary-invert-if-dark' => $this->util->invertTextColor($colorPrimaryElement) ? 'no' : 'invert(100%)',
  54. '--color-primary' => $this->primaryColor,
  55. '--color-primary-default' => $this->defaultPrimaryColor,
  56. '--color-primary-text' => $this->util->invertTextColor($this->primaryColor) ? '#000000' : '#ffffff',
  57. '--color-primary-hover' => $this->util->mix($this->primaryColor, $colorMainBackground, 60),
  58. '--color-primary-light' => $colorPrimaryLight,
  59. '--color-primary-light-text' => $this->util->mix($this->primaryColor, $this->util->invertTextColor($colorPrimaryLight) ? '#000000' : '#ffffff', -20),
  60. '--color-primary-light-hover' => $this->util->mix($colorPrimaryLight, $colorMainText, 90),
  61. // used for buttons, inputs...
  62. '--color-primary-element' => $colorPrimaryElement,
  63. '--color-primary-element-hover' => $invertPrimaryTextColor ? $this->util->lighten($colorPrimaryElement, 4) : $this->util->darken($colorPrimaryElement, 4),
  64. '--color-primary-element-text' => $invertPrimaryTextColor ? '#000000' : '#ffffff',
  65. // mostly used for disabled states
  66. '--color-primary-element-text-dark' => $invertPrimaryTextColor ? $this->util->lighten('#000000', 4) : $this->util->darken('#ffffff', 4),
  67. // used for hover/focus states
  68. '--color-primary-element-light' => $colorPrimaryElementLight,
  69. '--color-primary-element-light-hover' => $this->util->mix($colorPrimaryElementLight, $colorMainText, 90),
  70. '--color-primary-element-light-text' => $this->util->mix($colorPrimaryElement, $this->util->invertTextColor($colorPrimaryElementLight) ? '#000000' : '#ffffff', -20),
  71. // to use like this: background-image: var(--gradient-primary-background);
  72. '--gradient-primary-background' => 'linear-gradient(40deg, var(--color-primary) 0%, var(--color-primary-hover) 100%)',
  73. ];
  74. }
  75. /**
  76. * Generate admin theming background-related variables
  77. */
  78. protected function generateGlobalBackgroundVariables(): array {
  79. $backgroundDeleted = $this->config->getAppValue(Application::APP_ID, 'backgroundMime', '') === 'backgroundColor';
  80. $hasCustomLogoHeader = $this->util->isLogoThemed();
  81. $isPrimaryBright = $this->util->invertTextColor($this->primaryColor);
  82. $variables = [];
  83. // Default last fallback values
  84. $variables['--image-background-default'] = "url('" . $this->themingDefaults->getBackground() . "')";
  85. $variables['--color-background-plain'] = $this->primaryColor;
  86. // Register image variables only if custom-defined
  87. foreach (ImageManager::SUPPORTED_IMAGE_KEYS as $image) {
  88. if ($this->imageManager->hasImage($image)) {
  89. $imageUrl = $this->imageManager->getImageUrl($image);
  90. // --image-background is overridden by user theming if logged in
  91. $variables["--image-$image"] = "url('" . $imageUrl . "')";
  92. }
  93. }
  94. // If primary as background has been request or if we have a custom primary colour
  95. // let's not define the background image
  96. if ($backgroundDeleted) {
  97. $variables['--color-background-plain'] = $this->primaryColor;
  98. $variables['--image-background-plain'] = 'yes';
  99. $variables['--image-background'] = 'no';
  100. // If no background image is set, we need to check against the shown primary colour
  101. $variables['--background-image-invert-if-bright'] = $isPrimaryBright ? 'invert(100%)' : 'no';
  102. }
  103. if ($hasCustomLogoHeader) {
  104. $variables['--image-logoheader-custom'] = 'true';
  105. }
  106. return $variables;
  107. }
  108. /**
  109. * Generate user theming background-related variables
  110. */
  111. protected function generateUserBackgroundVariables(): array {
  112. $user = $this->userSession->getUser();
  113. if ($user !== null
  114. && !$this->themingDefaults->isUserThemingDisabled()
  115. && $this->appManager->isEnabledForUser(Application::APP_ID)) {
  116. $adminBackgroundDeleted = $this->config->getAppValue(Application::APP_ID, 'backgroundMime', '') === 'backgroundColor';
  117. $backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT);
  118. $currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0');
  119. $isPrimaryBright = $this->util->invertTextColor($this->primaryColor);
  120. // The user removed the background
  121. if ($backgroundImage === BackgroundService::BACKGROUND_DISABLED) {
  122. return [
  123. // Might be defined already by admin theming, needs to be overridden
  124. '--image-background' => 'none',
  125. '--color-background-plain' => $this->primaryColor,
  126. // If no background image is set, we need to check against the shown primary colour
  127. '--background-image-invert-if-bright' => $isPrimaryBright ? 'invert(100%)' : 'no',
  128. ];
  129. }
  130. // The user uploaded a custom background
  131. if ($backgroundImage === BackgroundService::BACKGROUND_CUSTOM) {
  132. $cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8);
  133. return [
  134. '--image-background' => "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')",
  135. '--color-background-plain' => $this->primaryColor,
  136. ];
  137. }
  138. // The user is using the default background and admin removed the background image
  139. if ($backgroundImage === BackgroundService::BACKGROUND_DEFAULT && $adminBackgroundDeleted) {
  140. return [
  141. // --image-background is not defined in this case
  142. '--color-background-plain' => $this->primaryColor,
  143. '--background-image-invert-if-bright' => $isPrimaryBright ? 'invert(100%)' : 'no',
  144. ];
  145. }
  146. // The user picked a shipped background
  147. if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) {
  148. return [
  149. '--image-background' => "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "img/background/$backgroundImage") . "')",
  150. '--color-background-plain' => $this->primaryColor,
  151. '--background-image-invert-if-bright' => BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage]['theming'] ?? null === BackgroundService::THEMING_MODE_DARK ? 'invert(100%)' : 'no',
  152. ];
  153. }
  154. }
  155. return [];
  156. }
  157. }