Write body theme selector straight in the template

Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
pull/31751/head
John Molakvoæ 3 years ago
parent fa18a77fa2
commit 69d1d1a84e
No known key found for this signature in database
GPG Key ID: 60C25B8C072916CF
  1. 16
      apps/theming/lib/Service/JSDataService.php
  2. 1
      apps/theming/lib/Service/ThemeInjectionService.php
  3. 55
      apps/theming/lib/Service/ThemesService.php
  4. 1
      core/css/apps.scss
  5. 2
      core/templates/layout.user.php
  6. 8
      lib/private/TemplateLayout.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(),
];
}
}

@ -22,6 +22,7 @@
*/
namespace OCA\Theming\Service;
use OCA\Theming\AppInfo\Application;
use OCA\Theming\Themes\DefaultTheme;
use OCP\IURLGenerator;
use OCP\Util;

@ -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)));
}
}

@ -166,6 +166,7 @@ kbd {
&,
> a {
background-color: var(--color-primary-light);
color: var(--color-primary-text);
}
}

@ -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) { ?>

@ -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);

Loading…
Cancel
Save