Passed
Push — master ( f116c8...be892d )
by John
15:58 queued 12s
created

CommonThemeTrait   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
eloc 59
dl 0
loc 123
rs 10
c 2
b 2
f 0
wmc 24

3 Methods

Rating   Name   Duplication   Size   Complexity  
B generateUserBackgroundVariables() 0 35 7
B generatePrimaryVariables() 0 36 8
B generateGlobalBackgroundVariables() 0 33 9
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2022 Joas Schilling <[email protected]>
5
 *
6
 * @author Joas Schilling <[email protected]>
7
 * @author John Molakvoæ <[email protected]>
8
 *
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
namespace OCA\Theming\Themes;
26
27
use OCA\Theming\AppInfo\Application;
28
use OCA\Theming\ImageManager;
29
use OCA\Theming\Service\BackgroundService;
30
use OCA\Theming\Util;
31
32
trait CommonThemeTrait {
33
	public Util $util;
34
35
	/**
36
	 * Generate primary-related variables
37
	 * This is shared between multiple themes because colorMainBackground and colorMainText
38
	 * will change in between.
39
	 */
40
	protected function generatePrimaryVariables(string $colorMainBackground, string $colorMainText): array {
41
		$colorPrimaryLight = $this->util->mix($this->primaryColor, $colorMainBackground, -80);
42
		$colorPrimaryElement = $this->util->elementColor($this->primaryColor);
43
		$colorPrimaryElementLight = $this->util->mix($colorPrimaryElement, $colorMainBackground, -80);
44
45
		// primary related colours
46
		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
56
			'--color-primary' => $this->primaryColor,
57
			'--color-primary-default' => $this->defaultPrimaryColor,
58
			'--color-primary-text' => $this->util->invertTextColor($this->primaryColor) ? '#000000' : '#ffffff',
59
			'--color-primary-hover' => $this->util->mix($this->primaryColor, $colorMainBackground, 60),
60
			'--color-primary-light' => $colorPrimaryLight,
61
			'--color-primary-light-text' => $this->util->mix($this->primaryColor, $this->util->invertTextColor($colorPrimaryLight) ? '#000000' : '#ffffff', -20),
62
			'--color-primary-light-hover' => $this->util->mix($colorPrimaryLight, $colorMainText, 90),
63
			'--color-primary-text-dark' => $this->util->darken($this->util->invertTextColor($this->primaryColor) ? '#000000' : '#ffffff', 7),
64
65
			// used for buttons, inputs...
66
			'--color-primary-element' => $colorPrimaryElement,
67
			'--color-primary-element-text' => $this->util->invertTextColor($colorPrimaryElement) ? '#000000' : '#ffffff',
68
			'--color-primary-element-hover' => $this->util->mix($colorPrimaryElement, $colorMainBackground, 60),
69
			'--color-primary-element-light' => $colorPrimaryElementLight,
70
			'--color-primary-element-light-text' => $this->util->mix($colorPrimaryElement, $this->util->invertTextColor($colorPrimaryElementLight) ? '#000000' : '#ffffff', -20),
71
			'--color-primary-element-light-hover' => $this->util->mix($colorPrimaryElementLight, $colorMainText, 90),
72
			'--color-primary-element-text-dark' => $this->util->darken($this->util->invertTextColor($colorPrimaryElement) ? '#000000' : '#ffffff', 7),
73
74
			// to use like this: background-image: var(--gradient-primary-background);
75
			'--gradient-primary-background' => 'linear-gradient(40deg, var(--color-primary) 0%, var(--color-primary-hover) 100%)',
76
		];
77
	}
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
	}
156
}
157