Skip to content

Commit 7a96c80

Browse files
skjnldsvnextcloud-command
authored andcommitted
Simplify variable names
Signed-off-by: John Molakvoæ <skjnldsv@protonmail.com> Signed-off-by: nextcloud-command <nextcloud-command@users.noreply.github.com>
1 parent 3c6da41 commit 7a96c80

File tree

8 files changed

+115
-79
lines changed

8 files changed

+115
-79
lines changed

apps/theming/lib/ImageManager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use OCP\IURLGenerator;
4646

4747
class ImageManager {
48+
public const SupportedImageKeys = ['background', 'logo', 'logoheader', 'favicon'];
4849

4950
/** @var IConfig */
5051
private $config;
@@ -53,7 +54,6 @@ class ImageManager {
5354
/** @var IURLGenerator */
5455
private $urlGenerator;
5556
/** @var array */
56-
private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon'];
5757
/** @var ICacheFactory */
5858
private $cacheFactory;
5959
/** @var ILogger */
@@ -142,7 +142,7 @@ public function hasImage(string $key): bool {
142142
*/
143143
public function getCustomImages(): array {
144144
$images = [];
145-
foreach ($this->supportedImageKeys as $key) {
145+
foreach ($this::SupportedImageKeys as $key) {
146146
$images[$key] = [
147147
'mime' => $this->config->getAppValue('theming', $key . 'Mime', ''),
148148
'url' => $this->getImageUrl($key),

apps/theming/lib/Themes/CommonThemeTrait.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
namespace OCA\Theming\Themes;
2626

27+
use OCA\Theming\AppInfo\Application;
28+
use OCA\Theming\ImageManager;
29+
use OCA\Theming\Service\BackgroundService;
2730
use OCA\Theming\Util;
2831

2932
trait CommonThemeTrait {
@@ -41,6 +44,15 @@ protected function generatePrimaryVariables(string $colorMainBackground, string
4144

4245
// primary related colours
4346
return [
47+
// invert filter if primary is too bright
48+
// to be used for legacy reasons only. Use inline
49+
// svg with proper css variable instead or material
50+
// design icons.
51+
// ⚠️ Using 'no' as a value to make sure we specify an
52+
// invalid one with no fallback. 'unset' could here fallback to some
53+
// other theme with media queries
54+
'--primary-invert-if-bright' => $this->util->invertTextColor($this->primaryColor) ? 'invert(100%)' : 'no',
55+
4456
'--color-primary' => $this->primaryColor,
4557
'--color-primary-default' => $this->defaultPrimaryColor,
4658
'--color-primary-text' => $this->util->invertTextColor($this->primaryColor) ? '#000000' : '#ffffff',
@@ -63,4 +75,82 @@ protected function generatePrimaryVariables(string $colorMainBackground, string
6375
'--gradient-primary-background' => 'linear-gradient(40deg, var(--color-primary) 0%, var(--color-primary-hover) 100%)',
6476
];
6577
}
78+
79+
/**
80+
* Generate admin theming background-related variables
81+
*/
82+
protected function generateGlobalBackgroundVariables(): array {
83+
$backgroundDeleted = $this->config->getAppValue(Application::APP_ID, 'backgroundMime', '') === 'backgroundColor';
84+
$hasCustomLogoHeader = $this->imageManager->hasImage('logo') || $this->imageManager->hasImage('logoheader');
85+
86+
$variables = [];
87+
88+
// If primary as background has been request or if we have a custom primary colour
89+
// let's not define the background image
90+
if ($backgroundDeleted && $this->themingDefaults->isUserThemingDisabled()) {
91+
$variables['--image-background-plain'] = 'true';
92+
$variables['--color-background-plain'] = $this->themingDefaults->getColorPrimary();
93+
}
94+
95+
// Register image variables only if custom-defined
96+
foreach (ImageManager::SupportedImageKeys as $image) {
97+
if ($this->imageManager->hasImage($image)) {
98+
$imageUrl = $this->imageManager->getImageUrl($image);
99+
if ($image === 'background') {
100+
// If background deleted is set, ignoring variable
101+
if ($backgroundDeleted) {
102+
continue;
103+
}
104+
$variables['--image-background-size'] = 'cover';
105+
}
106+
$variables["--image-$image"] = "url('" . $imageUrl . "')";
107+
}
108+
}
109+
110+
if ($hasCustomLogoHeader) {
111+
$variables["--image-logoheader-custom"] = 'true';
112+
}
113+
114+
return $variables;
115+
}
116+
117+
/**
118+
* Generate user theming background-related variables
119+
*/
120+
protected function generateUserBackgroundVariables(): array {
121+
$user = $this->userSession->getUser();
122+
if ($user !== null
123+
&& !$this->themingDefaults->isUserThemingDisabled()
124+
&& $this->appManager->isEnabledForUser(Application::APP_ID)) {
125+
$themingBackground = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background', 'default');
126+
$currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0');
127+
128+
// The user uploaded a custom background
129+
if ($themingBackground === 'custom') {
130+
$cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8);
131+
return [
132+
'--image-background' => "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')",
133+
// TODO: implement primary color from custom background --color-background-plain
134+
];
135+
}
136+
137+
// The user picked a shipped background
138+
if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$themingBackground])) {
139+
return [
140+
'--image-background' => "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "/img/background/$themingBackground") . "')",
141+
'--color-background-plain' => $this->themingDefaults->getColorPrimary(),
142+
];
143+
}
144+
145+
// The user picked a static colour
146+
if (substr($themingBackground, 0, 1) === '#') {
147+
return [
148+
'--image-background' => 'no',
149+
'--color-background-plain' => $this->themingDefaults->getColorPrimary(),
150+
];
151+
}
152+
}
153+
154+
return [];
155+
}
66156
}

apps/theming/lib/Themes/DefaultTheme.php

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
*/
2525
namespace OCA\Theming\Themes;
2626

27-
use OCA\Theming\AppInfo\Application;
2827
use OCA\Theming\ImageManager;
2928
use OCA\Theming\ITheme;
3029
use OCA\Theming\Service\BackgroundService;
@@ -35,7 +34,6 @@
3534
use OCP\IL10N;
3635
use OCP\IURLGenerator;
3736
use OCP\IUserSession;
38-
use OCP\Server;
3937

4038
class DefaultTheme implements ITheme {
4139
use CommonThemeTrait;
@@ -47,6 +45,7 @@ class DefaultTheme implements ITheme {
4745
public ImageManager $imageManager;
4846
public IConfig $config;
4947
public IL10N $l;
48+
public IAppManager $appManager;
5049

5150
public string $defaultPrimaryColor;
5251
public string $primaryColor;
@@ -57,14 +56,16 @@ public function __construct(Util $util,
5756
IURLGenerator $urlGenerator,
5857
ImageManager $imageManager,
5958
IConfig $config,
60-
IL10N $l) {
59+
IL10N $l,
60+
IAppManager $appManager) {
6161
$this->util = $util;
6262
$this->themingDefaults = $themingDefaults;
6363
$this->userSession = $userSession;
6464
$this->urlGenerator = $urlGenerator;
6565
$this->imageManager = $imageManager;
6666
$this->config = $config;
6767
$this->l = $l;
68+
$this->appManager = $appManager;
6869

6970
$this->defaultPrimaryColor = $this->themingDefaults->getDefaultColorPrimary();
7071
$this->primaryColor = $this->themingDefaults->getColorPrimary();
@@ -108,8 +109,6 @@ public function getCSSVariables(): array {
108109
$colorBoxShadow = $this->util->darken($colorMainBackground, 70);
109110
$colorBoxShadowRGB = join(',', $this->util->hexToRGB($colorBoxShadow));
110111

111-
$hasCustomLogoHeader = $this->imageManager->hasImage('logo') || $this->imageManager->hasImage('logoheader');
112-
113112
$variables = [
114113
'--color-main-background' => $colorMainBackground,
115114
'--color-main-background-rgb' => $colorMainBackgroundRGB,
@@ -188,71 +187,18 @@ public function getCSSVariables(): array {
188187

189188
// mobile. Keep in sync with core/js/js.js
190189
'--breakpoint-mobile' => '1024px',
191-
192-
// invert filter if primary is too bright
193-
// to be used for legacy reasons only. Use inline
194-
// svg with proper css variable instead or material
195-
// design icons.
196-
// ⚠️ Using 'no' as a value to make sure we specify an
197-
// invalid one with no fallback. 'unset' could here fallback to some
198-
// other theme with media queries
199-
'--primary-invert-if-bright' => $this->util->invertTextColor($this->primaryColor) ? 'invert(100%)' : 'no',
200190
'--background-invert-if-dark' => 'no',
201191
'--background-invert-if-bright' => 'invert(100%)',
202192

203193
// Default last fallback values
204-
'--image-main-background' => "url('" . $this->urlGenerator->imagePath('core', 'app-background.jpg') . "')",
205-
'--color-main-background-plain' => $this->defaultPrimaryColor,
194+
'--image-background' => "url('" . $this->urlGenerator->imagePath('core', 'app-background.jpg') . "')",
195+
'--color-background-plain' => $this->defaultPrimaryColor,
206196
];
207197

208198
// Primary variables
209199
$variables = array_merge($variables, $this->generatePrimaryVariables($colorMainBackground, $colorMainText));
210-
211-
$backgroundDeleted = $this->config->getAppValue(Application::APP_ID, 'backgroundMime', '') === 'backgroundColor';
212-
// If primary as background has been request or if we have a custom primary colour
213-
// let's not define the background image
214-
if ($backgroundDeleted && $this->themingDefaults->isUserThemingDisabled()) {
215-
$variables['--image-background-plain'] = 'true';
216-
$variables['--color-main-background-plain'] = $this->themingDefaults->getColorPrimary();
217-
}
218-
219-
// Register image variables only if custom-defined
220-
foreach (['logo', 'logoheader', 'favicon', 'background'] as $image) {
221-
if ($this->imageManager->hasImage($image)) {
222-
$imageUrl = $this->imageManager->getImageUrl($image);
223-
if ($image === 'background') {
224-
// If background deleted is set, ignoring variable
225-
if ($backgroundDeleted) {
226-
continue;
227-
}
228-
$variables['--image-background-size'] = 'cover';
229-
$variables['--image-main-background'] = "url('" . $imageUrl . "')";
230-
}
231-
$variables["--image-$image"] = "url('" . $imageUrl . "')";
232-
}
233-
}
234-
235-
if ($hasCustomLogoHeader) {
236-
$variables["--image-logoheader-custom"] = 'true';
237-
}
238-
239-
$appManager = Server::get(IAppManager::class);
240-
$user = $this->userSession->getUser();
241-
if (!$this->themingDefaults->isUserThemingDisabled() && $appManager->isEnabledForUser(Application::APP_ID) && $user !== null) {
242-
$themingBackground = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background', 'default');
243-
$currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0');
244-
245-
246-
if ($themingBackground === 'custom') {
247-
$cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8);
248-
$variables['--image-main-background'] = "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')";
249-
} elseif (isset(BackgroundService::SHIPPED_BACKGROUNDS[$themingBackground])) {
250-
$variables['--image-main-background'] = "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "/img/background/$themingBackground") . "')";
251-
} elseif (substr($themingBackground, 0, 1) === '#') {
252-
unset($variables['--image-main-background']);
253-
$variables['--color-main-background-plain'] = $this->themingDefaults->getColorPrimary();
254-
}
255-
}
200+
$variables = array_merge($variables, $this->generateGlobalBackgroundVariables());
201+
$variables = array_merge($variables, $this->generateUserBackgroundVariables());
256202

257203
return $variables;
258204
}

core/css/apps.css

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/css/apps.scss

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ html {
3939
width: 100%;
4040
height: 100%;
4141
position: absolute;
42-
background-color: var(--color-main-background-plain, var(--color-main-background));
43-
background-image: var(--image-main-background);
42+
background-color: var(--color-background-plain, var(--color-main-background));
43+
background-image: var(--image-background);
4444
background-size: cover;
4545
background-position: center;
4646
}
4747

4848
body {
49-
background-color: var(--color-main-background-plain, var(--color-main-background));
50-
background-image: var(--image-background-plain, var(--image-main-background));
49+
background-color: var(--color-background-plain, var(--color-main-background));
50+
background-image: var(--image-background-plain, var(--image-background));
5151
background-size: cover;
5252
background-position: center;
5353
position: fixed;

core/css/server.css

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/theming-theming-settings.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/theming-theming-settings.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)