CommonThemeTrait.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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\Util;
  27. use OCA\Theming\ImageManager;
  28. use OCA\Theming\AppInfo\Application;
  29. use OCA\Theming\Service\BackgroundService;
  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. $isBrightColor = $this->util->isBrightColor($colorMainBackground);
  39. $colorPrimaryElement = $this->util->elementColor($this->primaryColor, $isBrightColor);
  40. $colorPrimaryLight = $this->util->mix($colorPrimaryElement, $colorMainBackground, -80);
  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. // used for buttons, inputs...
  60. '--color-primary-element' => $colorPrimaryElement,
  61. '--color-primary-element-hover' => $this->util->mix($colorPrimaryElement, $colorMainBackground, 60),
  62. '--color-primary-element-text' => $this->util->invertTextColor($colorPrimaryElement) ? '#000000' : '#ffffff',
  63. // used for hover/focus states
  64. '--color-primary-element-light' => $colorPrimaryElementLight,
  65. '--color-primary-element-light-hover' => $this->util->mix($colorPrimaryElementLight, $colorMainText, 90),
  66. '--color-primary-element-light-text' => $this->util->mix($colorPrimaryElement, $this->util->invertTextColor($colorPrimaryElementLight) ? '#000000' : '#ffffff', -20),
  67. // mostly used for disabled states
  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. $backgroundDeleted = $this->config->getAppValue(Application::APP_ID, 'backgroundMime', '') === 'backgroundColor';
  78. $hasCustomLogoHeader = $this->util->isLogoThemed();
  79. $isDefaultPrimaryBright = $this->util->invertTextColor($this->defaultPrimaryColor);
  80. $variables = [];
  81. // Default last fallback values
  82. $variables['--image-background-default'] = "url('" . $this->themingDefaults->getBackground() . "')";
  83. $variables['--color-background-plain'] = $this->defaultPrimaryColor;
  84. // Register image variables only if custom-defined
  85. foreach (ImageManager::SUPPORTED_IMAGE_KEYS as $image) {
  86. if ($this->imageManager->hasImage($image)) {
  87. $imageUrl = $this->imageManager->getImageUrl($image);
  88. // --image-background is overridden by user theming
  89. $variables["--image-$image"] = "url('" . $imageUrl . "')";
  90. }
  91. }
  92. // If primary as background has been request or if we have a custom primary colour
  93. // let's not define the background image
  94. if ($backgroundDeleted) {
  95. $variables['--color-background-plain'] = $this->defaultPrimaryColor;
  96. $variables['--image-background-plain'] = 'yes';
  97. $variables['--image-background'] = 'no';
  98. // If no background image is set, we need to check against the shown primary colour
  99. $variables['--background-image-invert-if-bright'] = $isDefaultPrimaryBright ? 'invert(100%)' : 'no';
  100. }
  101. if ($hasCustomLogoHeader) {
  102. $variables['--image-logoheader-custom'] = 'true';
  103. }
  104. return $variables;
  105. }
  106. /**
  107. * Generate user theming background-related variables
  108. */
  109. protected function generateUserBackgroundVariables(): array {
  110. $user = $this->userSession->getUser();
  111. if ($user !== null
  112. && !$this->themingDefaults->isUserThemingDisabled()
  113. && $this->appManager->isEnabledForUser(Application::APP_ID)) {
  114. $backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT);
  115. $currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0');
  116. $isPrimaryBright = $this->util->invertTextColor($this->primaryColor);
  117. // The user removed the background
  118. if ($backgroundImage === BackgroundService::BACKGROUND_DISABLED) {
  119. return [
  120. '--image-background' => 'no',
  121. '--color-background-plain' => $this->primaryColor,
  122. // If no background image is set, we need to check against the shown primary colour
  123. '--background-image-invert-if-bright' => $isPrimaryBright ? 'invert(100%)' : 'no',
  124. ];
  125. }
  126. // The user uploaded a custom background
  127. if ($backgroundImage === BackgroundService::BACKGROUND_CUSTOM) {
  128. $cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8);
  129. return [
  130. '--image-background' => "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')",
  131. '--color-background-plain' => $this->themingDefaults->getColorPrimary(),
  132. ];
  133. }
  134. // The user picked a shipped background
  135. if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) {
  136. return [
  137. '--image-background' => "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "img/background/$backgroundImage") . "')",
  138. '--color-background-plain' => $this->themingDefaults->getColorPrimary(),
  139. '--background-image-invert-if-bright' => BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage]['theming'] ?? null === BackgroundService::THEMING_MODE_DARK ? 'invert(100%)' : 'no',
  140. ];
  141. }
  142. }
  143. return [];
  144. }
  145. }