Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support theme meta w3c standard #32231

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apps/theming/lib/ITheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ public function getEnableLabel(): string;
*/
public function getDescription(): string;

/**
* Get the meta attribute matching the theme
* e.g. https://html.spec.whatwg.org/multipage/semantics.html#meta-color-scheme
* @return array{name?: string, content?: string}[]
* @since 29.0.0
*/
public function getMeta(): array;

/**
* Get the media query triggering this theme
* Optional, ignored if falsy
Expand Down
51 changes: 44 additions & 7 deletions apps/theming/lib/Service/ThemeInjectionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
namespace OCA\Theming\Service;

use OCA\Theming\ITheme;
use OCA\Theming\Themes\DefaultTheme;
use OCA\Theming\Util;
use OCP\IConfig;
Expand All @@ -48,14 +49,15 @@ public function __construct(IURLGenerator $urlGenerator,
$this->defaultTheme = $defaultTheme;
$this->util = $util;
$this->config = $config;

if ($userSession->getUser() !== null) {
$this->userId = $userSession->getUser()->getUID();
} else {
$this->userId = null;
}
}

public function injectHeaders() {
public function injectHeaders(): void {
$themes = $this->themesService->getThemes();
$defaultTheme = $themes[$this->defaultTheme->getId()];
$mediaThemes = array_filter($themes, function ($theme) {
Expand All @@ -64,11 +66,11 @@ public function injectHeaders() {
});

// Default theme fallback
$this->addThemeHeader($defaultTheme->getId());
$this->addThemeHeaders($defaultTheme);

// Themes applied by media queries
foreach($mediaThemes as $theme) {
$this->addThemeHeader($theme->getId(), true, $theme->getMediaQuery());
$this->addThemeHeaders($theme, true, $theme->getMediaQuery());
}

// Themes
Expand All @@ -77,20 +79,23 @@ public function injectHeaders() {
if ($theme->getId() === $this->defaultTheme->getId()) {
continue;
}
$this->addThemeHeader($theme->getId(), false);
$this->addThemeHeaders($theme, false);
}

// Meta headers
$this->addThemeMetaHeaders($themes);
}

/**
* Inject theme header into rendered page
*
* @param string $themeId the theme ID
* @param ITheme $theme the theme
* @param bool $plain request the :root syntax
* @param string $media media query to use in the <link> element
*/
private function addThemeHeader(string $themeId, bool $plain = true, string $media = null) {
private function addThemeHeaders(ITheme $theme, bool $plain = true, string $media = null): void {
$linkToCSS = $this->urlGenerator->linkToRoute('theming.Theming.getThemeStylesheet', [
'themeId' => $themeId,
'themeId' => $theme->getId(),
'plain' => $plain,
'v' => $this->util->getCacheBuster(),
]);
Expand All @@ -101,4 +106,36 @@ private function addThemeHeader(string $themeId, bool $plain = true, string $med
'class' => 'theme'
]);
}

/**
* Inject meta headers into rendered page
*
* @param ITheme[] $themes the theme
*/
private function addThemeMetaHeaders(array $themes): void {
$metaHeaders = [];

// Meta headers
foreach($this->themesService->getThemes() as $theme) {
if (!empty($theme->getMeta())) {
foreach($theme->getMeta() as $meta) {
if (!isset($meta['name']) || !isset($meta['content'])) {
continue;
}

if (!isset($metaHeaders[$meta['name']])) {
$metaHeaders[$meta['name']] = [];
}
$metaHeaders[$meta['name']][] = $meta['content'];
}
}
}

foreach($metaHeaders as $name => $content) {
\OCP\Util::addHeader('meta', [
'name' => $name,
'content' => join(' ', array_unique($content)),
]);
}
}
}
8 changes: 4 additions & 4 deletions apps/theming/lib/Themes/DarkHighContrastTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public function getId(): string {
return 'dark-highcontrast';
}

public function getMediaQuery(): string {
return '(prefers-color-scheme: dark) and (prefers-contrast: more)';
}

public function getTitle(): string {
return $this->l->t('Dark theme with high contrast mode');
}
Expand All @@ -49,6 +45,10 @@ public function getDescription(): string {
return $this->l->t('Similar to the high contrast mode, but with dark colours.');
}

public function getMediaQuery(): string {
return '(prefers-color-scheme: dark) and (prefers-contrast: more)';
}

/**
* Keep this consistent with other HighContrast Themes
*/
Expand Down
16 changes: 12 additions & 4 deletions apps/theming/lib/Themes/DarkTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public function getId(): string {
return 'dark';
}

public function getMediaQuery(): string {
return '(prefers-color-scheme: dark)';
}

public function getTitle(): string {
return $this->l->t('Dark theme');
}
Expand All @@ -49,6 +45,18 @@ public function getDescription(): string {
return $this->l->t('A dark theme to ease your eyes by reducing the overall luminosity and brightness.');
}

public function getMediaQuery(): string {
return '(prefers-color-scheme: dark)';
}

public function getMeta(): array {
// https://html.spec.whatwg.org/multipage/semantics.html#meta-color-scheme
return [[
'name' => 'color-scheme',
'content' => 'dark',
]];
}

public function getCSSVariables(): array {
$defaultVariables = parent::getCSSVariables();

Expand Down
4 changes: 4 additions & 0 deletions apps/theming/lib/Themes/DefaultTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public function getMediaQuery(): string {
return '';
}

public function getMeta(): array {
return [];
}

public function getCSSVariables(): array {
$colorMainText = '#222222';
$colorMainTextRgb = join(',', $this->util->hexToRGB($colorMainText));
Expand Down
8 changes: 4 additions & 4 deletions apps/theming/lib/Themes/HighContrastTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public function getId(): string {
return 'light-highcontrast';
}

public function getMediaQuery(): string {
return '(prefers-contrast: more)';
}

public function getTitle(): string {
return $this->l->t('High contrast mode');
}
Expand All @@ -49,6 +45,10 @@ public function getDescription(): string {
return $this->l->t('A high contrast mode to ease your navigation. Visual quality will be reduced but clarity will be increased.');
}

public function getMediaQuery(): string {
return '(prefers-contrast: more)';
}

/**
* Keep this consistent with other HighContrast Themes
*/
Expand Down
12 changes: 8 additions & 4 deletions apps/theming/lib/Themes/LightTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public function getId(): string {
return 'light';
}

public function getType(): int {
return ITheme::TYPE_THEME;
}

public function getTitle(): string {
return $this->l->t('Light theme');
}
Expand All @@ -52,4 +48,12 @@ public function getDescription(): string {
public function getMediaQuery(): string {
return '(prefers-color-scheme: light)';
}

public function getMeta(): array {
// https://html.spec.whatwg.org/multipage/semantics.html#meta-color-scheme
return [[
'name' => 'color-scheme',
'content' => 'light',
]];
}
}