DefaultTheme.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Theming\Themes;
  8. use OC\AppFramework\Http\Request;
  9. use OCA\Theming\ImageManager;
  10. use OCA\Theming\ITheme;
  11. use OCA\Theming\ThemingDefaults;
  12. use OCA\Theming\Util;
  13. use OCP\App\IAppManager;
  14. use OCP\IConfig;
  15. use OCP\IL10N;
  16. use OCP\IRequest;
  17. use OCP\IURLGenerator;
  18. use OCP\IUserSession;
  19. class DefaultTheme implements ITheme {
  20. use CommonThemeTrait;
  21. public string $defaultPrimaryColor;
  22. public string $primaryColor;
  23. public function __construct(
  24. public Util $util,
  25. public ThemingDefaults $themingDefaults,
  26. public IUserSession $userSession,
  27. public IURLGenerator $urlGenerator,
  28. public ImageManager $imageManager,
  29. public IConfig $config,
  30. public IL10N $l,
  31. public IAppManager $appManager,
  32. private ?IRequest $request,
  33. ) {
  34. $this->defaultPrimaryColor = $this->themingDefaults->getDefaultColorPrimary();
  35. $this->primaryColor = $this->themingDefaults->getColorPrimary();
  36. }
  37. public function getId(): string {
  38. return 'default';
  39. }
  40. public function getType(): int {
  41. return ITheme::TYPE_THEME;
  42. }
  43. public function getTitle(): string {
  44. return $this->l->t('System default theme');
  45. }
  46. public function getEnableLabel(): string {
  47. return $this->l->t('Enable the system default');
  48. }
  49. public function getDescription(): string {
  50. return $this->l->t('Using the default system appearance.');
  51. }
  52. public function getMediaQuery(): string {
  53. return '';
  54. }
  55. public function getMeta(): array {
  56. return [];
  57. }
  58. public function getCSSVariables(): array {
  59. $colorMainText = '#222222';
  60. $colorMainTextRgb = join(',', $this->util->hexToRGB($colorMainText));
  61. // Color that still provides enough contrast for text, so we need a ratio of 4.5:1 on main background AND hover
  62. $colorTextMaxcontrast = '#6b6b6b'; // 4.5 : 1 for hover background and background dark
  63. $colorMainBackground = '#ffffff';
  64. $colorMainBackgroundRGB = join(',', $this->util->hexToRGB($colorMainBackground));
  65. $colorBoxShadow = $this->util->darken($colorMainBackground, 70);
  66. $colorBoxShadowRGB = join(',', $this->util->hexToRGB($colorBoxShadow));
  67. $colorError = '#DB0606';
  68. $colorWarning = '#A37200';
  69. $colorSuccess = '#2d7b41';
  70. $colorInfo = '#0071ad';
  71. $user = $this->userSession->getUser();
  72. // Chromium based browsers currently (2024) have huge performance issues with blur filters
  73. $isChromium = $this->request !== null && $this->request->isUserAgent([Request::USER_AGENT_CHROME, Request::USER_AGENT_MS_EDGE]);
  74. // Ignore MacOS because they always have hardware accelartion
  75. $isChromium = $isChromium && !$this->request->isUserAgent(['/Macintosh/']);
  76. // Allow to force the blur filter
  77. $forceEnableBlur = $user === null ? false : $this->config->getUserValue(
  78. $user->getUID(),
  79. 'theming',
  80. 'force_enable_blur_filter',
  81. );
  82. $workingBlur = match($forceEnableBlur) {
  83. 'yes' => true,
  84. 'no' => false,
  85. default => !$isChromium
  86. };
  87. $variables = [
  88. '--color-main-background' => $colorMainBackground,
  89. '--color-main-background-rgb' => $colorMainBackgroundRGB,
  90. '--color-main-background-translucent' => 'rgba(var(--color-main-background-rgb), .97)',
  91. '--color-main-background-blur' => 'rgba(var(--color-main-background-rgb), .8)',
  92. '--filter-background-blur' => $workingBlur ? 'blur(25px)' : 'none',
  93. // to use like this: background-image: linear-gradient(0, var('--gradient-main-background));
  94. '--gradient-main-background' => 'var(--color-main-background) 0%, var(--color-main-background-translucent) 85%, transparent 100%',
  95. // used for different active/hover/focus/disabled states
  96. '--color-background-hover' => $this->util->darken($colorMainBackground, 4),
  97. '--color-background-dark' => $this->util->darken($colorMainBackground, 7),
  98. '--color-background-darker' => $this->util->darken($colorMainBackground, 14),
  99. '--color-placeholder-light' => $this->util->darken($colorMainBackground, 10),
  100. '--color-placeholder-dark' => $this->util->darken($colorMainBackground, 20),
  101. // max contrast for WCAG compliance
  102. '--color-main-text' => $colorMainText,
  103. '--color-text-maxcontrast' => $colorTextMaxcontrast,
  104. '--color-text-maxcontrast-default' => $colorTextMaxcontrast,
  105. '--color-text-maxcontrast-background-blur' => $this->util->darken($colorTextMaxcontrast, 7),
  106. '--color-text-light' => 'var(--color-main-text)', // deprecated
  107. '--color-text-lighter' => 'var(--color-text-maxcontrast)', // deprecated
  108. '--color-scrollbar' => 'var(--color-border-maxcontrast) transparent',
  109. // error/warning/success/info feedback colours
  110. '--color-error' => $colorError,
  111. '--color-error-rgb' => join(',', $this->util->hexToRGB($colorError)),
  112. '--color-error-hover' => $this->util->mix($colorError, $colorMainBackground, 75),
  113. '--color-error-text' => $this->util->darken($colorError, 5),
  114. '--color-warning' => $colorWarning,
  115. '--color-warning-rgb' => join(',', $this->util->hexToRGB($colorWarning)),
  116. '--color-warning-hover' => $this->util->darken($colorWarning, 5),
  117. '--color-warning-text' => $this->util->darken($colorWarning, 7),
  118. '--color-success' => $colorSuccess,
  119. '--color-success-rgb' => join(',', $this->util->hexToRGB($colorSuccess)),
  120. '--color-success-hover' => $this->util->mix($colorSuccess, $colorMainBackground, 80),
  121. '--color-success-text' => $this->util->darken($colorSuccess, 4),
  122. '--color-info' => $colorInfo,
  123. '--color-info-rgb' => join(',', $this->util->hexToRGB($colorInfo)),
  124. '--color-info-hover' => $this->util->mix($colorInfo, $colorMainBackground, 80),
  125. '--color-info-text' => $this->util->darken($colorInfo, 4),
  126. '--color-favorite' => '#A37200',
  127. // used for the icon loading animation
  128. '--color-loading-light' => '#cccccc',
  129. '--color-loading-dark' => '#444444',
  130. '--color-box-shadow-rgb' => $colorBoxShadowRGB,
  131. '--color-box-shadow' => 'rgba(var(--color-box-shadow-rgb), 0.5)',
  132. '--color-border' => $this->util->darken($colorMainBackground, 7),
  133. '--color-border-dark' => $this->util->darken($colorMainBackground, 14),
  134. '--color-border-maxcontrast' => $this->util->darken($colorMainBackground, 51),
  135. '--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'",
  136. '--default-font-size' => '15px',
  137. '--font-size-small' => '13px',
  138. // 1.5 * font-size for accessibility
  139. '--default-line-height' => '1.5',
  140. // TODO: support "(prefers-reduced-motion)"
  141. '--animation-quick' => '100ms',
  142. '--animation-slow' => '300ms',
  143. // Default variables --------------------------------------------
  144. // Border width for input elements such as text fields and selects
  145. '--border-width-input' => '1px',
  146. '--border-width-input-focused' => '2px',
  147. // Border radii (new values)
  148. '--border-radius-small' => '4px', // For smaller elements
  149. '--border-radius-element' => '8px', // For interactive elements such as buttons, input, navigation and list items
  150. '--border-radius-container' => '12px', // For smaller containers like action menus
  151. '--border-radius-container-large' => '16px', // For bigger containers like body or modals
  152. // Border radii (deprecated)
  153. '--border-radius' => 'var(--border-radius-small)',
  154. '--border-radius-large' => 'var(--border-radius-element)',
  155. '--border-radius-rounded' => '28px',
  156. '--border-radius-pill' => '100px',
  157. '--default-clickable-area' => '34px',
  158. '--clickable-area-large' => '48px',
  159. '--clickable-area-small' => '24px',
  160. '--default-grid-baseline' => '4px',
  161. // various structure data
  162. '--header-height' => '50px',
  163. '--header-menu-item-height' => '44px',
  164. '--navigation-width' => '300px',
  165. '--sidebar-min-width' => '300px',
  166. '--sidebar-max-width' => '500px',
  167. // Border radius of the body container
  168. '--body-container-radius' => 'var(--border-radius-container-large)',
  169. // Margin of the body container
  170. '--body-container-margin' => 'calc(var(--default-grid-baseline) * 2)',
  171. // Height of the body container to fully fill the view port
  172. '--body-height' => 'calc(100% - env(safe-area-inset-bottom) - var(--header-height) - var(--body-container-margin))',
  173. // mobile. Keep in sync with core/src/init.js
  174. '--breakpoint-mobile' => '1024px',
  175. '--background-invert-if-dark' => 'no',
  176. '--background-invert-if-bright' => 'invert(100%)',
  177. '--background-image-invert-if-bright' => 'no',
  178. ];
  179. // Primary variables
  180. $variables = array_merge($variables, $this->generatePrimaryVariables($colorMainBackground, $colorMainText));
  181. $variables = array_merge($variables, $this->generateGlobalBackgroundVariables());
  182. $variables = array_merge($variables, $this->generateUserBackgroundVariables());
  183. return $variables;
  184. }
  185. public function getCustomCss(): string {
  186. return '';
  187. }
  188. }