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.

CommonThemeTrait.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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): array {
  38. $colorPrimaryLight = $this->util->mix($this->primaryColor, $colorMainBackground, -80);
  39. $colorPrimaryElement = $this->util->elementColor($this->primaryColor);
  40. $colorPrimaryElementDefault = $this->util->elementColor($this->defaultPrimaryColor);
  41. $colorPrimaryElementLight = $this->util->mix($colorPrimaryElement, $colorMainBackground, -80);
  42. // primary related colours
  43. return [
  44. // invert filter if primary is too bright
  45. // to be used for legacy reasons only. Use inline
  46. // svg with proper css variable instead or material
  47. // design icons.
  48. // ⚠️ Using 'no' as a value to make sure we specify an
  49. // invalid one with no fallback. 'unset' could here fallback to some
  50. // other theme with media queries
  51. '--primary-invert-if-bright' => $this->util->invertTextColor($this->primaryColor) ? 'invert(100%)' : 'no',
  52. '--color-primary' => $this->primaryColor,
  53. '--color-primary-default' => $this->defaultPrimaryColor,
  54. '--color-primary-text' => $this->util->invertTextColor($this->primaryColor) ? '#000000' : '#ffffff',
  55. '--color-primary-hover' => $this->util->mix($this->primaryColor, $colorMainBackground, 60),
  56. '--color-primary-light' => $colorPrimaryLight,
  57. '--color-primary-light-text' => $this->util->mix($this->primaryColor, $this->util->invertTextColor($colorPrimaryLight) ? '#000000' : '#ffffff', -20),
  58. '--color-primary-light-hover' => $this->util->mix($colorPrimaryLight, $colorMainText, 90),
  59. '--color-primary-text-dark' => $this->util->darken($this->util->invertTextColor($this->primaryColor) ? '#000000' : '#ffffff', 7),
  60. // used for buttons, inputs...
  61. '--color-primary-element' => $colorPrimaryElement,
  62. '--color-primary-element-default-hover' => $this->util->mix($colorPrimaryElementDefault, $colorMainBackground, 60),
  63. '--color-primary-element-text' => $this->util->invertTextColor($colorPrimaryElement) ? '#000000' : '#ffffff',
  64. '--color-primary-element-hover' => $this->util->mix($colorPrimaryElement, $colorMainBackground, 60),
  65. '--color-primary-element-light' => $colorPrimaryElementLight,
  66. '--color-primary-element-light-text' => $this->util->mix($colorPrimaryElement, $this->util->invertTextColor($colorPrimaryElementLight) ? '#000000' : '#ffffff', -20),
  67. '--color-primary-element-light-hover' => $this->util->mix($colorPrimaryElementLight, $colorMainText, 90),
  68. '--color-primary-element-text-dark' => $this->util->darken($this->util->invertTextColor($colorPrimaryElement) ? '#000000' : '#ffffff', 7),
  69. // to use like this: background-image: var(--gradient-primary-background);
  70. '--gradient-primary-background' => 'linear-gradient(40deg, var(--color-primary) 0%, var(--color-primary-hover) 100%)',
  71. ];
  72. }
  73. /**
  74. * Generate admin theming background-related variables
  75. */
  76. protected function generateGlobalBackgroundVariables(): array {
  77. $user = $this->userSession->getUser();
  78. $backgroundDeleted = $this->config->getAppValue(Application::APP_ID, 'backgroundMime', '') === 'backgroundColor';
  79. $hasCustomLogoHeader = $this->util->isLogoThemed();
  80. $variables = [];
  81. // Default last fallback values
  82. $variables['--image-background-default'] = $backgroundDeleted ?: "url('" . $this->themingDefaults->getBackground() . "')";
  83. $variables['--color-background-plain'] = $this->defaultPrimaryColor;
  84. // If primary as background has been request or if we have a custom primary colour
  85. // let's not define the background image
  86. if ($backgroundDeleted) {
  87. $variables['--color-background-plain'] = $this->themingDefaults->getColorPrimary();
  88. if ($this->themingDefaults->isUserThemingDisabled() || $user === null) {
  89. $variables['--image-background-plain'] = 'yes';
  90. }
  91. }
  92. // Register image variables only if custom-defined
  93. foreach (ImageManager::SUPPORTED_IMAGE_KEYS as $image) {
  94. if ($this->imageManager->hasImage($image)) {
  95. $imageUrl = $this->imageManager->getImageUrl($image);
  96. if ($image === 'background') {
  97. // If background deleted is set, ignoring variable
  98. if ($backgroundDeleted) {
  99. continue;
  100. }
  101. $variables['--image-background-size'] = 'cover';
  102. $variables['--image-background-default'] = "url('" . $imageUrl . "')";
  103. }
  104. // --image-background is overridden by user theming
  105. $variables["--image-$image"] = "url('" . $imageUrl . "')";
  106. }
  107. }
  108. if ($hasCustomLogoHeader) {
  109. $variables["--image-logoheader-custom"] = 'true';
  110. }
  111. return $variables;
  112. }
  113. /**
  114. * Generate user theming background-related variables
  115. */
  116. protected function generateUserBackgroundVariables(): array {
  117. $user = $this->userSession->getUser();
  118. if ($user !== null
  119. && !$this->themingDefaults->isUserThemingDisabled()
  120. && $this->appManager->isEnabledForUser(Application::APP_ID)) {
  121. $backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT);
  122. $currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0');
  123. // The user removed the background
  124. if ($backgroundImage === BackgroundService::BACKGROUND_DISABLED) {
  125. return [
  126. '--image-background' => 'no',
  127. '--color-background-plain' => $this->themingDefaults->getColorPrimary(),
  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->themingDefaults->getColorPrimary(),
  136. ];
  137. }
  138. // The user picked a shipped background
  139. if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) {
  140. return [
  141. '--image-background' => "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "img/background/$backgroundImage") . "')",
  142. '--color-background-plain' => $this->themingDefaults->getColorPrimary(),
  143. '--background-image-invert-if-bright' => BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage]['theming'] ?? null === BackgroundService::THEMING_MODE_DARK ? 'invert(100%)' : 'no',
  144. ];
  145. }
  146. }
  147. return [];
  148. }
  149. }