]> source.dussan.org Git - nextcloud-server.git/commitdiff
Write body theme selector straight in the template
authorJohn Molakvoæ <skjnldsv@protonmail.com>
Fri, 15 Apr 2022 11:54:53 +0000 (13:54 +0200)
committerJohn Molakvoæ <skjnldsv@protonmail.com>
Thu, 21 Apr 2022 07:31:07 +0000 (09:31 +0200)
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
apps/theming/lib/Service/JSDataService.php
apps/theming/lib/Service/ThemeInjectionService.php
apps/theming/lib/Service/ThemesService.php
core/css/apps.scss
core/templates/layout.user.php
lib/private/TemplateLayout.php

index 1c4d138a76468a9961140b422f7e1d614f489712..fdc85ea445aa2799838a1a0378844af00f01189b 100644 (file)
@@ -32,22 +32,21 @@ use OCA\Theming\Util;
 use OCP\IConfig;
 
 class JSDataService implements \JsonSerializable {
-
-       /** @var ThemingDefaults */
-       private $themingDefaults;
-       /** @var Util */
-       private $util;
-       /** @var IConfig */
-       private $appConfig;
+       private ThemingDefaults $themingDefaults;
+       private Util $util;
+       private IConfig $appConfig;
+       private ThemesService $themesService;
 
        public function __construct(
                ThemingDefaults $themingDefaults,
                Util $util,
-               IConfig $appConfig
+               IConfig $appConfig,
+               ThemesService $themesService
        ) {
                $this->themingDefaults = $themingDefaults;
                $this->util = $util;
                $this->appConfig = $appConfig;
+               $this->themesService = $themesService;
        }
 
        public function jsonSerialize(): array {
@@ -60,6 +59,7 @@ class JSDataService implements \JsonSerializable {
                        'privacyUrl' => $this->themingDefaults->getPrivacyUrl(),
                        'inverted' => $this->util->invertTextColor($this->themingDefaults->getColorPrimary()),
                        'cacheBuster' => $this->appConfig->getAppValue(Application::APP_ID, 'cachebuster', '0'),
+                       'enabledThemes' => $this->themesService->getEnabledThemes(),
                ];
        }
 }
index 0b4890cd08baed87ad0e4365ecad5f2549a49e10..e00839c21c5611fd4dbaa613a94dd90bf386f944 100644 (file)
@@ -22,6 +22,7 @@
  */
 namespace OCA\Theming\Service;
 
+use OCA\Theming\AppInfo\Application;
 use OCA\Theming\Themes\DefaultTheme;
 use OCP\IURLGenerator;
 use OCP\Util;
index 3092b3bcbb5acee606a5f9b1fc43e38522a383ac..832c443a2e12ced2e01b602536e03925751d5e89 100644 (file)
  */
 namespace OCA\Theming\Service;
 
+use OCA\Theming\AppInfo\Application;
 use OCA\Theming\Themes\DefaultTheme;
 use OCA\Theming\Themes\DarkTheme;
 use OCA\Theming\Themes\DarkHighContrastTheme;
 use OCA\Theming\Themes\HighContrastTheme;
 use OCA\Theming\ITheme;
+use OCP\IAppConfig;
+use OCP\IConfig;
+use OCP\IUser;
+use OCP\IUserSession;
 
 class ThemesService {
+       private IUserSession $session;
+       private IConfig $config;
 
        /** @var ITheme[] */
        private array $themesProviders;
 
-       public function __construct(DefaultTheme $defaultTheme,
+       public function __construct(IUserSession $userSession,
+                                                               IConfig $config,
+                                                               DefaultTheme $defaultTheme,
                                                                DarkTheme $darkTheme,
                                                                DarkHighContrastTheme $darkHighContrastTheme,
                                                                HighContrastTheme $highContrastTheme) {
+               $this->userSession = $userSession;
+               $this->config = $config;
+
                // Register themes
                $this->themesProviders = [
                        $defaultTheme->getId()                  => $defaultTheme,
@@ -46,11 +58,48 @@ class ThemesService {
                ];
        }
 
-       public function getThemes() {
+       public function getThemes(): array {
                return $this->themesProviders;
        }
 
-       public function getThemeVariables(string $id) {
+       public function getThemeVariables(string $id): array {
                return $this->themesProviders[$id]->getCSSVariables();
        }
+
+       public function enableTheme(ITheme $theme): void {
+               $themes = $this->getEnabledThemes();
+               array_push($themes, $theme->getId());
+               $this->setEnabledThemes($themes);
+       }
+
+       public function disableTheme(ITheme $theme): void {
+               // Using keys as it's faster
+               $themes = $this->getEnabledThemes();
+               // If enabled, removing it
+               if (in_array($theme->getId(), $themes)) {
+                       $this->setEnabledThemes(array_filter($themes, function($themeId) use ($theme) {
+                               return $themeId !== $theme->getId();
+                       }));
+               }
+       }
+
+       public function isEnabled(ITheme $theme): bool {
+               $user = $this->userSession->getUser();
+               if ($user instanceof IUser) {
+                       // Using keys as it's faster
+                       $themes = $this->getEnabledThemes();
+                       return in_array($theme->getId(), $themes);
+               }
+       }
+
+       public function getEnabledThemes(): array {
+               $user = $this->userSession->getUser();
+               $enabledThemes = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', '[]');
+               return json_decode($enabledThemes);
+       }
+
+       private function setEnabledThemes(array $themes): void {
+               $user = $this->userSession->getUser();
+               $this->config->setUserValue($user->getUID(), Application::APP_ID, 'enabled-themes', json_encode(array_unique($themes)));
+       }
 }
index b683ffae0ef087700932c692cfde8b8acd6d67c6..cf061303498b75070681c7ebb6a30abbedaf5ecc 100644 (file)
@@ -166,6 +166,7 @@ kbd {
                                &,
                                > a {
                                        background-color: var(--color-primary-light);
+                                       color: var(--color-primary-text);
                                }
                        }
 
index f5ac783b3402fae7b425fc4716370361dfb75f91..00e16414535a1787b5dc54472f1df73a235a4d1e 100644 (file)
@@ -40,7 +40,7 @@ $getUserAvatar = static function (int $size) use ($_): string {
                <?php emit_script_loading_tags($_); ?>
                <?php print_unescaped($_['headers']); ?>
        </head>
-       <body id="<?php p($_['bodyid']);?>">
+       <body id="<?php p($_['bodyid']);?>" <?php foreach ($_['enabledThemes'] as $themeId) { p("data-theme-$themeId "); }?>>
        <?php include 'layout.noscript.warning.php'; ?>
 
                <?php foreach ($_['initialStates'] as $app => $initialState) { ?>
index a379f23a1ef6099e1971338b20b7f36b611d4b68..b49ba49ebb8800205d670bf53b35ddafef7e4f88 100644 (file)
@@ -81,6 +81,7 @@ class TemplateLayout extends \OC_Template {
                /** @var IInitialStateService */
                $this->initialState = \OC::$server->get(IInitialStateService::class);
 
+
                // Decide which page we show
                if ($renderAs === TemplateResponse::RENDER_AS_USER) {
                        /** @var INavigationManager */
@@ -99,6 +100,13 @@ class TemplateLayout extends \OC_Template {
                        $this->initialState->provideInitialState('unified-search', 'live-search', $this->config->getAppValue('core', 'unified-search.live-search', 'yes') === 'yes');
                        Util::addScript('core', 'unified-search', 'core');
 
+                       // Set body data-theme
+                       if (\OC::$server->getAppManager()->isEnabledForUser('theming') && class_exists('\OCA\Theming\Service\ThemesService')) {
+                               /** @var \OCA\Theming\Service\ThemesService */
+                               $themesService = \OC::$server->get(\OCA\Theming\Service\ThemesService::class);
+                               $this->assign('enabledThemes', $themesService->getEnabledThemes());
+                       }
+
                        // set logo link target
                        $logoUrl = $this->config->getSystemValueString('logo_url', '');
                        $this->assign('logoUrl', $logoUrl);