Skip to content

Commit

Permalink
Support theme meta w3c standard
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com>
Signed-off-by: Simon L <szaimen@e.mail.de>
  • Loading branch information
skjnldsv committed Feb 7, 2024
1 parent 4ce10d0 commit 4e2b616
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 22 deletions.
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 25.0.0
*/
public function getMeta(): array;

Check failure on line 80 in apps/theming/lib/ITheme.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidDocblock

apps/theming/lib/ITheme.php:80:2: InvalidDocblock: Saw : outside of object-like array in docblock for OCA\Theming\ITheme::getMeta (see https://psalm.dev/008)

Check failure

Code scanning / Psalm

InvalidDocblock Error

Saw : outside of object-like array in docblock for OCA\Theming\ITheme::getMeta

/**
* Get the media query triggering this theme
* Optional, ignored if falsy
Expand Down
18 changes: 12 additions & 6 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,7 +79,7 @@ public function injectHeaders() {
if ($theme->getId() === $this->defaultTheme->getId()) {
continue;
}
$this->addThemeHeader($theme->getId(), false);
$this->addThemeHeaders($theme, false);
}
}

Expand All @@ -88,9 +90,9 @@ public function injectHeaders() {
* @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 @@ -100,5 +102,9 @@ private function addThemeHeader(string $themeId, bool $plain = true, string $med
'href' => $linkToCSS,
'class' => 'theme'
]);

if (!empty($theme->getMeta())) {
\OCP\Util::addHeader('meta', $theme->getMeta());
}
}
}
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',
];
}
}

0 comments on commit 4e2b616

Please sign in to comment.