1
0

DefaultTheme.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 OC\AppFramework\Http\Request;
  27. use OCA\Theming\ImageManager;
  28. use OCA\Theming\ITheme;
  29. use OCA\Theming\Service\BackgroundService;
  30. use OCA\Theming\ThemingDefaults;
  31. use OCA\Theming\Util;
  32. use OCP\App\IAppManager;
  33. use OCP\IConfig;
  34. use OCP\IL10N;
  35. use OCP\IRequest;
  36. use OCP\IURLGenerator;
  37. use OCP\IUserSession;
  38. class DefaultTheme implements ITheme {
  39. use CommonThemeTrait;
  40. public string $defaultPrimaryColor;
  41. public string $primaryColor;
  42. public function __construct(
  43. public Util $util,
  44. public ThemingDefaults $themingDefaults,
  45. public IUserSession $userSession,
  46. public IURLGenerator $urlGenerator,
  47. public ImageManager $imageManager,
  48. public IConfig $config,
  49. public IL10N $l,
  50. public IAppManager $appManager,
  51. private ?IRequest $request,
  52. ) {
  53. $this->defaultPrimaryColor = $this->themingDefaults->getDefaultColorPrimary();
  54. $this->primaryColor = $this->themingDefaults->getColorPrimary();
  55. // Override primary colors (if set) to improve accessibility
  56. if ($this->primaryColor === BackgroundService::DEFAULT_COLOR) {
  57. $this->primaryColor = BackgroundService::DEFAULT_ACCESSIBLE_COLOR;
  58. }
  59. }
  60. public function getId(): string {
  61. return 'default';
  62. }
  63. public function getType(): int {
  64. return ITheme::TYPE_THEME;
  65. }
  66. public function getTitle(): string {
  67. return $this->l->t('System default theme');
  68. }
  69. public function getEnableLabel(): string {
  70. return $this->l->t('Enable the system default');
  71. }
  72. public function getDescription(): string {
  73. return $this->l->t('Using the default system appearance.');
  74. }
  75. public function getMediaQuery(): string {
  76. return '';
  77. }
  78. public function getCSSVariables(): array {
  79. $colorMainText = '#222222';
  80. $colorMainTextRgb = join(',', $this->util->hexToRGB($colorMainText));
  81. // Color that still provides enough contrast for text, so we need a ratio of 4.5:1 on main background AND hover
  82. $colorTextMaxcontrast = '#6b6b6b'; // 4.5 : 1 for hover background and background dark
  83. $colorMainBackground = '#ffffff';
  84. $colorMainBackgroundRGB = join(',', $this->util->hexToRGB($colorMainBackground));
  85. $colorBoxShadow = $this->util->darken($colorMainBackground, 70);
  86. $colorBoxShadowRGB = join(',', $this->util->hexToRGB($colorBoxShadow));
  87. $colorError = '#DB0606';
  88. $colorWarning = '#A37200';
  89. $colorSuccess = '#2d7b41';
  90. $colorInfo = '#0071ad';
  91. $user = $this->userSession->getUser();
  92. // Chromium based browsers currently (2024) have huge performance issues with blur filters
  93. $isChromium = $this->request !== null && $this->request->isUserAgent([Request::USER_AGENT_CHROME, Request::USER_AGENT_MS_EDGE]);
  94. // Ignore MacOS because they always have hardware accelartion
  95. $isChromium = $isChromium && !$this->request->isUserAgent(['/Macintosh/']);
  96. // Allow to force the blur filter
  97. $forceEnableBlur = $user === null ? false : $this->config->getUserValue(
  98. $user->getUID(),
  99. 'theming',
  100. 'force_enable_blur_filter',
  101. );
  102. $workingBlur = match($forceEnableBlur) {
  103. 'yes' => true,
  104. 'no' => false,
  105. default => !$isChromium
  106. };
  107. $variables = [
  108. '--color-main-background' => $colorMainBackground,
  109. '--color-main-background-rgb' => $colorMainBackgroundRGB,
  110. '--color-main-background-translucent' => 'rgba(var(--color-main-background-rgb), .97)',
  111. '--color-main-background-blur' => 'rgba(var(--color-main-background-rgb), .8)',
  112. '--filter-background-blur' => $workingBlur ? 'blur(25px)' : 'none',
  113. // to use like this: background-image: linear-gradient(0, var('--gradient-main-background));
  114. '--gradient-main-background' => 'var(--color-main-background) 0%, var(--color-main-background-translucent) 85%, transparent 100%',
  115. // used for different active/hover/focus/disabled states
  116. '--color-background-hover' => $this->util->darken($colorMainBackground, 4),
  117. '--color-background-dark' => $this->util->darken($colorMainBackground, 7),
  118. '--color-background-darker' => $this->util->darken($colorMainBackground, 14),
  119. '--color-placeholder-light' => $this->util->darken($colorMainBackground, 10),
  120. '--color-placeholder-dark' => $this->util->darken($colorMainBackground, 20),
  121. // max contrast for WCAG compliance
  122. '--color-main-text' => $colorMainText,
  123. '--color-text-maxcontrast' => $colorTextMaxcontrast,
  124. '--color-text-maxcontrast-default' => $colorTextMaxcontrast,
  125. '--color-text-maxcontrast-background-blur' => $this->util->darken($colorTextMaxcontrast, 7),
  126. '--color-text-light' => 'var(--color-main-text)', // deprecated
  127. '--color-text-lighter' => 'var(--color-text-maxcontrast)', // deprecated
  128. '--color-scrollbar' => 'rgba(' . $colorMainTextRgb . ', .15)',
  129. // error/warning/success/info feedback colours
  130. '--color-error' => $colorError,
  131. '--color-error-rgb' => join(',', $this->util->hexToRGB($colorError)),
  132. '--color-error-hover' => $this->util->mix($colorError, $colorMainBackground, 75),
  133. '--color-error-text' => $this->util->darken($colorError, 5),
  134. '--color-warning' => $colorWarning,
  135. '--color-warning-rgb' => join(',', $this->util->hexToRGB($colorWarning)),
  136. '--color-warning-hover' => $this->util->darken($colorWarning, 5),
  137. '--color-warning-text' => $this->util->darken($colorWarning, 7),
  138. '--color-success' => $colorSuccess,
  139. '--color-success-rgb' => join(',', $this->util->hexToRGB($colorSuccess)),
  140. '--color-success-hover' => $this->util->mix($colorSuccess, $colorMainBackground, 80),
  141. '--color-success-text' => $this->util->darken($colorSuccess, 4),
  142. '--color-info' => $colorInfo,
  143. '--color-info-rgb' => join(',', $this->util->hexToRGB($colorInfo)),
  144. '--color-info-hover' => $this->util->mix($colorInfo, $colorMainBackground, 80),
  145. '--color-info-text' => $this->util->darken($colorInfo, 4),
  146. // used for the icon loading animation
  147. '--color-loading-light' => '#cccccc',
  148. '--color-loading-dark' => '#444444',
  149. '--color-box-shadow-rgb' => $colorBoxShadowRGB,
  150. '--color-box-shadow' => "rgba(var(--color-box-shadow-rgb), 0.5)",
  151. '--color-border' => $this->util->darken($colorMainBackground, 7),
  152. '--color-border-dark' => $this->util->darken($colorMainBackground, 14),
  153. '--color-border-maxcontrast' => $this->util->darken($colorMainBackground, 51),
  154. '--font-face' => "system-ui, -apple-system, 'Segoe UI', Roboto, Oxygen-Sans, Cantarell, Ubuntu, 'Helvetica Neue', 'Noto Sans', 'Liberation Sans', Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji', 'Segoe UI Symbol', 'Noto Color Emoji'",
  155. '--default-font-size' => '15px',
  156. // TODO: support "(prefers-reduced-motion)"
  157. '--animation-quick' => '100ms',
  158. '--animation-slow' => '300ms',
  159. // Default variables --------------------------------------------
  160. '--border-radius' => '3px',
  161. '--border-radius-large' => '10px',
  162. '--border-radius-rounded' => '28px',
  163. // pill-style button, value is large so big buttons also have correct roundness
  164. '--border-radius-pill' => '100px',
  165. '--default-clickable-area' => '44px',
  166. '--default-line-height' => '24px',
  167. '--default-grid-baseline' => '4px',
  168. // various structure data
  169. '--header-height' => '50px',
  170. '--navigation-width' => '300px',
  171. '--sidebar-min-width' => '300px',
  172. '--sidebar-max-width' => '500px',
  173. '--list-min-width' => '200px',
  174. '--list-max-width' => '300px',
  175. '--header-menu-item-height' => '44px',
  176. '--header-menu-profile-item-height' => '66px',
  177. // mobile. Keep in sync with core/js/js.js
  178. '--breakpoint-mobile' => '1024px',
  179. '--background-invert-if-dark' => 'no',
  180. '--background-invert-if-bright' => 'invert(100%)',
  181. '--background-image-invert-if-bright' => 'no',
  182. '--background-image-color-text' => '#ffffff',
  183. ];
  184. // Primary variables
  185. $variables = array_merge($variables, $this->generatePrimaryVariables($colorMainBackground, $colorMainText));
  186. $variables = array_merge($variables, $this->generateGlobalBackgroundVariables());
  187. $variables = array_merge($variables, $this->generateUserBackgroundVariables());
  188. return $variables;
  189. }
  190. public function getCustomCss(): string {
  191. return '';
  192. }
  193. }