1
0

CommonThemeTrait.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. $variables['--background-image-color-text'] = $isPrimaryBright ? '#000000' : '#ffffff';
  103. }
  104. if ($hasCustomLogoHeader) {
  105. $variables['--image-logoheader-custom'] = 'true';
  106. }
  107. return $variables;
  108. }
  109. /**
  110. * Generate user theming background-related variables
  111. */
  112. protected function generateUserBackgroundVariables(): array {
  113. $user = $this->userSession->getUser();
  114. if ($user !== null
  115. && !$this->themingDefaults->isUserThemingDisabled()
  116. && $this->appManager->isEnabledForUser(Application::APP_ID)) {
  117. $adminBackgroundDeleted = $this->config->getAppValue(Application::APP_ID, 'backgroundMime', '') === 'backgroundColor';
  118. $backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT);
  119. $currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0');
  120. $isPrimaryBright = $this->util->invertTextColor($this->primaryColor);
  121. // The user removed the background
  122. if ($backgroundImage === BackgroundService::BACKGROUND_DISABLED) {
  123. return [
  124. // Might be defined already by admin theming, needs to be overridden
  125. '--image-background' => 'none',
  126. '--color-background-plain' => $this->primaryColor,
  127. // If no background image is set, we need to check against the shown primary colour
  128. '--background-image-invert-if-bright' => $isPrimaryBright ? 'invert(100%)' : 'no',
  129. '--background-image-color-text' => $isPrimaryBright ? '#000000' : '#ffffff',
  130. ];
  131. }
  132. // The user uploaded a custom background
  133. if ($backgroundImage === BackgroundService::BACKGROUND_CUSTOM) {
  134. $cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8);
  135. return [
  136. '--image-background' => "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')",
  137. '--color-background-plain' => $this->primaryColor,
  138. ];
  139. }
  140. // The user is using the default background and admin removed the background image
  141. if ($backgroundImage === BackgroundService::BACKGROUND_DEFAULT && $adminBackgroundDeleted) {
  142. return [
  143. // --image-background is not defined in this case
  144. '--color-background-plain' => $this->primaryColor,
  145. '--background-image-invert-if-bright' => $isPrimaryBright ? 'invert(100%)' : 'no',
  146. '--background-image-color-text' => $isPrimaryBright ? '#000000' : '#ffffff',
  147. ];
  148. }
  149. // The user picked a shipped background
  150. if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) {
  151. return [
  152. '--image-background' => "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "img/background/$backgroundImage") . "')",
  153. '--color-background-plain' => $this->primaryColor,
  154. '--background-image-invert-if-bright' => BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage]['theming'] ?? null === BackgroundService::THEMING_MODE_DARK ? 'invert(100%)' : 'no',
  155. '--background-image-color-text' => BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage]['theming'] ?? null === BackgroundService::THEMING_MODE_DARK ? '#000000' : '#ffffff',
  156. ];
  157. }
  158. }
  159. return [];
  160. }
  161. }