From 69d1d1a84e5e8937046d30714f11036b680cc04a Mon Sep 17 00:00:00 2001 From: =?utf8?q?John=20Molakvo=C3=A6?= Date: Fri, 15 Apr 2022 13:54:53 +0200 Subject: [PATCH] Write body theme selector straight in the template MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ --- apps/theming/lib/Service/JSDataService.php | 16 +++--- .../lib/Service/ThemeInjectionService.php | 1 + apps/theming/lib/Service/ThemesService.php | 55 ++++++++++++++++++- core/css/apps.scss | 1 + core/templates/layout.user.php | 2 +- lib/private/TemplateLayout.php | 8 +++ 6 files changed, 71 insertions(+), 12 deletions(-) diff --git a/apps/theming/lib/Service/JSDataService.php b/apps/theming/lib/Service/JSDataService.php index 1c4d138a764..fdc85ea445a 100644 --- a/apps/theming/lib/Service/JSDataService.php +++ b/apps/theming/lib/Service/JSDataService.php @@ -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(), ]; } } diff --git a/apps/theming/lib/Service/ThemeInjectionService.php b/apps/theming/lib/Service/ThemeInjectionService.php index 0b4890cd08b..e00839c21c5 100644 --- a/apps/theming/lib/Service/ThemeInjectionService.php +++ b/apps/theming/lib/Service/ThemeInjectionService.php @@ -22,6 +22,7 @@ */ namespace OCA\Theming\Service; +use OCA\Theming\AppInfo\Application; use OCA\Theming\Themes\DefaultTheme; use OCP\IURLGenerator; use OCP\Util; diff --git a/apps/theming/lib/Service/ThemesService.php b/apps/theming/lib/Service/ThemesService.php index 3092b3bcbb5..832c443a2e1 100644 --- a/apps/theming/lib/Service/ThemesService.php +++ b/apps/theming/lib/Service/ThemesService.php @@ -22,21 +22,33 @@ */ 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))); + } } diff --git a/core/css/apps.scss b/core/css/apps.scss index b683ffae0ef..cf061303498 100644 --- a/core/css/apps.scss +++ b/core/css/apps.scss @@ -166,6 +166,7 @@ kbd { &, > a { background-color: var(--color-primary-light); + color: var(--color-primary-text); } } diff --git a/core/templates/layout.user.php b/core/templates/layout.user.php index f5ac783b340..00e16414535 100644 --- a/core/templates/layout.user.php +++ b/core/templates/layout.user.php @@ -40,7 +40,7 @@ $getUserAvatar = static function (int $size) use ($_): string { - + > $initialState) { ?> diff --git a/lib/private/TemplateLayout.php b/lib/private/TemplateLayout.php index a379f23a1ef..b49ba49ebb8 100644 --- a/lib/private/TemplateLayout.php +++ b/lib/private/TemplateLayout.php @@ -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); -- 2.39.5