CommonThemeTrait.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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, 75),
  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 if logged in
  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. $adminBackgroundDeleted = $this->config->getAppValue(Application::APP_ID, 'backgroundMime', '') === 'backgroundColor';
  115. $backgroundImage = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background_image', BackgroundService::BACKGROUND_DEFAULT);
  116. $currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0');
  117. $isPrimaryBright = $this->util->invertTextColor($this->themingDefaults->getColorPrimary());
  118. // The user removed the background
  119. if ($backgroundImage === BackgroundService::BACKGROUND_DISABLED) {
  120. return [
  121. // Might be defined already by admin theming, needs to be overridden
  122. '--image-background' => 'none',
  123. '--color-background-plain' => $this->themingDefaults->getColorPrimary(),
  124. // If no background image is set, we need to check against the shown primary colour
  125. '--background-image-invert-if-bright' => $isPrimaryBright ? 'invert(100%)' : 'no',
  126. ];
  127. }
  128. // The user uploaded a custom background
  129. if ($backgroundImage === BackgroundService::BACKGROUND_CUSTOM) {
  130. $cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8);
  131. return [
  132. '--image-background' => "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')",
  133. '--color-background-plain' => $this->themingDefaults->getColorPrimary(),
  134. ];
  135. }
  136. // The user is using the default background and admin removed the background image
  137. if ($backgroundImage === BackgroundService::BACKGROUND_DEFAULT && $adminBackgroundDeleted) {
  138. return [
  139. // --image-background is not defined in this case
  140. '--color-background-plain' => $this->themingDefaults->getColorPrimary(),
  141. '--background-image-invert-if-bright' => $isPrimaryBright ? 'invert(100%)' : 'no',
  142. ];
  143. }
  144. // The user picked a shipped background
  145. if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage])) {
  146. return [
  147. '--image-background' => "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "img/background/$backgroundImage") . "')",
  148. '--color-background-plain' => $this->themingDefaults->getColorPrimary(),
  149. '--background-image-invert-if-bright' => BackgroundService::SHIPPED_BACKGROUNDS[$backgroundImage]['theming'] ?? null === BackgroundService::THEMING_MODE_DARK ? 'invert(100%)' : 'no',
  150. ];
  151. }
  152. }
  153. return [];
  154. }
  155. }