Skip to content
Closed
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
Add setting to set a different primary color for dark themes
The primary color can be inadapted to dark colors, this new settings allow to choose a different one.

Signed-off-by: Louis Chemineau <[email protected]>
  • Loading branch information
artonge committed May 19, 2022
commit da1a91f6532844e3dd53ff1ddaba052e0db8d705
25 changes: 24 additions & 1 deletion apps/theming/js/settings-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ function hideUndoButton(setting, value) {
slogan: t('lib', 'a safe home for all your data'),
url: 'https://nextcloud.com',
color: '#0082c9',
color_dark_theme: document.getElementById('theming-color').value,
logoMime: '',
backgroundMime: '',
imprintUrl: '',
Expand Down Expand Up @@ -99,6 +100,8 @@ window.addEventListener('DOMContentLoaded', function () {
// manually instantiate jscolor to work around new Function call which violates strict CSP
var colorElement = $('#theming-color')[0];
var jscolor = new window.jscolor(colorElement, {hash: true});
var colorDarkThemeElement = $('#theming-color_dark_theme')[0];
var jscolorDarkTheme = new window.jscolor(colorDarkThemeElement, {hash: true});

$('#theming .theme-undo').each(function() {
var setting = $(this).data('setting');
Expand Down Expand Up @@ -180,6 +183,13 @@ window.addEventListener('DOMContentLoaded', function () {
if (value.indexOf('#') !== 0) {
value = '#' + value;
}

// Hack: detect if color_dark_theme should follow color by checking if the undo button is visible.
if ($(".theme-undo[data-setting='color_dark_theme']").css('display') === 'none') {
var colorDarkThemePicker = document.getElementById('theming-color_dark_theme');
colorDarkThemePicker.style.backgroundColor = value;
colorDarkThemePicker.value = value.toUpperCase();
}
}
if(setting === 'name') {
if(checkName()){
Expand Down Expand Up @@ -215,8 +225,21 @@ window.addEventListener('DOMContentLoaded', function () {
).done(function(response) {
if (setting === 'color') {
var colorPicker = document.getElementById('theming-color');
var colorDarkThemePicker = document.getElementById('theming-color_dark_theme');

// If color_dark_theme is the same as color
// then we need to update it too as it is probably unset and defaulting to color.
if (colorPicker.value === colorDarkThemePicker.value) {
colorDarkThemePicker.style.backgroundColor = response.data.value;
colorDarkThemePicker.value = response.data.value.toUpperCase();
}

colorPicker.style.backgroundColor = response.data.value;
colorPicker.value = response.data.value.slice(1).toUpperCase();
colorPicker.value = response.data.value.toUpperCase();
} else if (setting === 'color_dark_theme') {
var colorDarkThemePicker = document.getElementById('theming-color_dark_theme');
colorDarkThemePicker.style.backgroundColor = response.data.value;
colorDarkThemePicker.value = response.data.value.toUpperCase();
} else if (!image) {
var input = document.getElementById('theming-'+setting);
input.value = response.data.value;
Expand Down
3 changes: 2 additions & 1 deletion apps/theming/lib/Controller/ThemingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public function updateStylesheet($setting, $value) {
}
break;
case 'color':
case 'color_dark_theme':
if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $value)) {
$error = $this->l10n->t('The given color is invalid');
}
Expand Down Expand Up @@ -328,7 +329,7 @@ public function getThemeStylesheet(string $themeId, bool $plain = false, bool $w
// If plain is set, the browser decides of the css priority
if ($plain) {
$css = ":root { $variables } " . $customCss;
} else {
} else {
// If not set, we'll rely on the body class
$compiler = new Compiler();
$compiledCss = $compiler->compileString("body[data-theme-$themeId] { $variables $customCss }");
Expand Down
8 changes: 8 additions & 0 deletions apps/theming/lib/ITheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,12 @@ public function getCSSVariables(): array;
* @since 25.0.0
*/
public function getCustomCss(): string;

/**
* Whether the theme is dark
* Will be used to when selecting the primary color.
*
* @since 25.0.0
*/
public function isDark(): bool;
}
1 change: 1 addition & 0 deletions apps/theming/lib/Settings/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function getForm(): TemplateResponse {
'url' => $this->themingDefaults->getBaseUrl(),
'slogan' => $this->themingDefaults->getSlogan(),
'color' => $this->themingDefaults->getColorPrimary(),
'color_dark_theme' => $this->themingDefaults->getColorPrimary(true),
'uploadLogoRoute' => $this->urlGenerator->linkToRoute('theming.Theming.uploadImage'),
'canThemeIcons' => $this->imageManager->shouldReplaceIcons(),
'iconDocs' => $this->urlGenerator->linkToDocs('admin-theming-icons'),
Expand Down
6 changes: 5 additions & 1 deletion apps/theming/lib/Themes/DarkTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

class DarkTheme extends DefaultTheme implements ITheme {

public function isDark(): bool {
return true;
}

public function getId(): string {
return 'dark';
}
Expand Down Expand Up @@ -57,7 +61,7 @@ public function getCSSVariables(): array {
$colorBoxShadow = $this->util->darken($colorMainBackground, 70);
$colorBoxShadowRGB = join(',', $this->util->hexToRGB($colorBoxShadow));

return array_merge($defaultVariables, [
return array_merge($defaultVariables, [
'--color-main-text' => $colorMainText,
'--color-main-background' => $colorMainBackground,
'--color-main-background-rgb' => $colorMainBackgroundRGB,
Expand Down
6 changes: 5 additions & 1 deletion apps/theming/lib/Themes/DefaultTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public function __construct(Util $util,
$this->config = $config;
$this->l = $l;

$this->primaryColor = $this->themingDefaults->getColorPrimary();
$this->primaryColor = $this->themingDefaults->getColorPrimary($this->isDark());
}

public function isDark(): bool {
return false;
}

public function getId(): string {
Expand Down
21 changes: 19 additions & 2 deletions apps/theming/lib/ThemingDefaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,22 @@ public function getShortFooter() {
*
* @return string
*/
public function getColorPrimary() {
$color = $this->config->getAppValue('theming', 'color', $this->color);
public function getColorPrimary(bool $dark = false) {
$color = $this->config->getAppValue(
'theming',
$dark ? 'color_dark_theme' : 'color',
'none'
);

// Use the light theme color if the dark theme color is not set
if ($dark && $color === 'none') {
$color = $this->config->getAppValue(
'theming',
'color',
$this->color
);
}

if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
$color = '#0082c9';
}
Expand Down Expand Up @@ -450,6 +464,9 @@ public function undo($setting) {
case 'color':
$returnValue = $this->getColorPrimary();
break;
case 'color_dark_theme':
$returnValue = $this->getColorPrimary(true);
break;
case 'logo':
case 'logoheader':
case 'background':
Expand Down
7 changes: 7 additions & 0 deletions apps/theming/templates/settings-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@
<div data-setting="color" data-toggle="tooltip" data-original-title="<?php p($l->t('Reset to default')); ?>" class="theme-undo icon icon-history"></div>
</label>
</div>
<div>
<label>
<span><?php p($l->t('Color (dark theme)')) ?></span>
<input id="theming-color_dark_theme" type="text" maxlength="7" value="<?php p($_['color_dark_theme']) ?>" />
<div data-setting="color_dark_theme" data-toggle="tooltip" data-original-title="<?php p($l->t('Reset to default')); ?>" class="theme-undo icon icon-history"></div>
</label>
</div>
<div>
<form class="uploadButton" method="post" action="<?php p($_['uploadLogoRoute']) ?>" data-image-key="logo">
<input type="hidden" id="theming-logoMime" value="<?php p($_['images']['logo']['mime']); ?>" />
Expand Down