Skip to content

Commit e4f7c7f

Browse files
committed
Use color preset of shipped background as primary color
Signed-off-by: Christopher Ng <chrng8@gmail.com>
1 parent fafc1ee commit e4f7c7f

File tree

4 files changed

+58
-49
lines changed

4 files changed

+58
-49
lines changed

apps/theming/lib/Themes/DefaultTheme.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use OCA\Theming\AppInfo\Application;
2828
use OCA\Theming\ImageManager;
2929
use OCA\Theming\ITheme;
30+
use OCA\Theming\Service\BackgroundService;
3031
use OCA\Theming\ThemingDefaults;
3132
use OCA\Theming\Util;
3233
use OCP\App\IAppManager;
@@ -41,6 +42,7 @@ class DefaultTheme implements ITheme {
4142

4243
public Util $util;
4344
public ThemingDefaults $themingDefaults;
45+
public IUserSession $userSession;
4446
public IURLGenerator $urlGenerator;
4547
public ImageManager $imageManager;
4648
public IConfig $config;
@@ -50,12 +52,14 @@ class DefaultTheme implements ITheme {
5052

5153
public function __construct(Util $util,
5254
ThemingDefaults $themingDefaults,
55+
IUserSession $userSession,
5356
IURLGenerator $urlGenerator,
5457
ImageManager $imageManager,
5558
IConfig $config,
5659
IL10N $l) {
5760
$this->util = $util;
5861
$this->themingDefaults = $themingDefaults;
62+
$this->userSession = $userSession;
5963
$this->urlGenerator = $urlGenerator;
6064
$this->imageManager = $imageManager;
6165
$this->config = $config;
@@ -221,19 +225,15 @@ public function getCSSVariables(): array {
221225
}
222226

223227
$appManager = Server::get(IAppManager::class);
224-
$userSession = Server::get(IUserSession::class);
225-
$user = $userSession->getUser();
228+
$user = $this->userSession->getUser();
226229
if ($appManager->isEnabledForUser(Application::APP_ID) && $user !== null) {
227230
$themingBackground = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background', 'default');
228231

229232
if ($themingBackground === 'custom') {
230-
// Custom
231233
$variables['--image-main-background'] = "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "')";
232-
} elseif ($themingBackground !== 'default' && substr($themingBackground, 0, 1) !== '#') {
233-
// Shipped background
234+
} elseif (isset(BackgroundService::SHIPPED_BACKGROUNDS[$themingBackground])) {
234235
$variables['--image-main-background'] = "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "/img/background/$themingBackground") . "')";
235236
} elseif (substr($themingBackground, 0, 1) === '#') {
236-
// Color
237237
unset($variables['--image-main-background']);
238238
$variables['--color-main-background-plain'] = $this->primaryColor;
239239
}

apps/theming/lib/ThemingDefaults.php

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
*/
4141
namespace OCA\Theming;
4242

43+
use OCA\Theming\AppInfo\Application;
44+
use OCA\Theming\Service\BackgroundService;
4345
use OCP\App\AppPathNotFoundException;
4446
use OCP\App\IAppManager;
4547
use OCP\Files\NotFoundException;
@@ -49,61 +51,47 @@
4951
use OCP\IL10N;
5052
use OCP\INavigationManager;
5153
use OCP\IURLGenerator;
54+
use OCP\IUserSession;
5255

5356
class ThemingDefaults extends \OC_Defaults {
5457

55-
/** @var IConfig */
56-
private $config;
57-
/** @var IL10N */
58-
private $l;
59-
/** @var ImageManager */
60-
private $imageManager;
61-
/** @var IURLGenerator */
62-
private $urlGenerator;
63-
/** @var ICacheFactory */
64-
private $cacheFactory;
65-
/** @var Util */
66-
private $util;
67-
/** @var IAppManager */
68-
private $appManager;
69-
/** @var INavigationManager */
70-
private $navigationManager;
71-
72-
/** @var string */
73-
private $name;
74-
/** @var string */
75-
private $title;
76-
/** @var string */
77-
private $entity;
78-
/** @var string */
79-
private $productName;
80-
/** @var string */
81-
private $url;
82-
/** @var string */
83-
private $color;
84-
85-
/** @var string */
86-
private $iTunesAppId;
87-
/** @var string */
88-
private $iOSClientUrl;
89-
/** @var string */
90-
private $AndroidClientUrl;
91-
/** @var string */
92-
private $FDroidClientUrl;
58+
private IConfig $config;
59+
private IL10N $l;
60+
private ImageManager $imageManager;
61+
private IUserSession $userSession;
62+
private IURLGenerator $urlGenerator;
63+
private ICacheFactory $cacheFactory;
64+
private Util $util;
65+
private IAppManager $appManager;
66+
private INavigationManager $navigationManager;
67+
68+
private string $name;
69+
private string $title;
70+
private string $entity;
71+
private string $productName;
72+
private string $url;
73+
private string $color;
74+
75+
private string $iTunesAppId;
76+
private string $iOSClientUrl;
77+
private string $AndroidClientUrl;
78+
private string $FDroidClientUrl;
9379

9480
/**
9581
* ThemingDefaults constructor.
9682
*
9783
* @param IConfig $config
9884
* @param IL10N $l
9985
* @param ImageManager $imageManager
86+
* @param IUserSession $userSession
10087
* @param IURLGenerator $urlGenerator
10188
* @param ICacheFactory $cacheFactory
10289
* @param Util $util
10390
* @param IAppManager $appManager
10491
*/
10592
public function __construct(IConfig $config,
10693
IL10N $l,
94+
IUserSession $userSession,
10795
IURLGenerator $urlGenerator,
10896
ICacheFactory $cacheFactory,
10997
Util $util,
@@ -115,6 +103,7 @@ public function __construct(IConfig $config,
115103
$this->config = $config;
116104
$this->l = $l;
117105
$this->imageManager = $imageManager;
106+
$this->userSession = $userSession;
118107
$this->urlGenerator = $urlGenerator;
119108
$this->cacheFactory = $cacheFactory;
120109
$this->util = $util;
@@ -229,10 +218,24 @@ public function getShortFooter() {
229218
* @return string
230219
*/
231220
public function getColorPrimary() {
232-
$color = $this->config->getAppValue('theming', 'color', $this->color);
221+
$user = $this->userSession->getUser();
222+
$color = $this->config->getAppValue(Application::APP_ID, 'color', '');
223+
224+
if ($color === '' && !empty($user)) {
225+
$themingBackground = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background', 'default');
226+
if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$themingBackground]['primary_color'])) {
227+
$this->increaseCacheBuster();
228+
return BackgroundService::SHIPPED_BACKGROUNDS[$themingBackground]['primary_color'];
229+
} else if ($themingBackground === 'default') {
230+
$this->increaseCacheBuster();
231+
return BackgroundService::SHIPPED_BACKGROUNDS['kamil-porembinski-clouds.jpg']['primary_color'];
232+
}
233+
}
234+
233235
if (!preg_match('/^\#([0-9a-f]{3}|[0-9a-f]{6})$/i', $color)) {
234-
$color = '#0082c9';
236+
return BackgroundService::SHIPPED_BACKGROUNDS['kamil-porembinski-clouds.jpg']['primary_color'];
235237
}
238+
236239
return $color;
237240
}
238241

apps/theming/tests/ThemingDefaultsTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,16 @@
4646
use OCP\IL10N;
4747
use OCP\INavigationManager;
4848
use OCP\IURLGenerator;
49+
use OCP\IUserSession;
4950
use Test\TestCase;
5051

5152
class ThemingDefaultsTest extends TestCase {
5253
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
5354
private $config;
5455
/** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
5556
private $l10n;
57+
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
58+
private $userSession;
5659
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
5760
private $urlGenerator;
5861
/** @var \OC_Defaults|\PHPUnit\Framework\MockObject\MockObject */
@@ -78,6 +81,7 @@ protected function setUp(): void {
7881
parent::setUp();
7982
$this->config = $this->createMock(IConfig::class);
8083
$this->l10n = $this->createMock(IL10N::class);
84+
$this->userSession = $this->createMock(IUserSession::class);
8185
$this->urlGenerator = $this->createMock(IURLGenerator::class);
8286
$this->cacheFactory = $this->createMock(ICacheFactory::class);
8387
$this->cache = $this->createMock(ICache::class);
@@ -93,6 +97,7 @@ protected function setUp(): void {
9397
$this->template = new ThemingDefaults(
9498
$this->config,
9599
$this->l10n,
100+
$this->userSession,
96101
$this->urlGenerator,
97102
$this->cacheFactory,
98103
$this->util,
@@ -419,7 +424,7 @@ public function testgetColorPrimaryWithDefault() {
419424
$this->config
420425
->expects($this->once())
421426
->method('getAppValue')
422-
->with('theming', 'color', $this->defaults->getColorPrimary())
427+
->with('theming', 'color', null)
423428
->willReturn($this->defaults->getColorPrimary());
424429

425430
$this->assertEquals($this->defaults->getColorPrimary(), $this->template->getColorPrimary());
@@ -429,7 +434,7 @@ public function testgetColorPrimaryWithCustom() {
429434
$this->config
430435
->expects($this->once())
431436
->method('getAppValue')
432-
->with('theming', 'color', $this->defaults->getColorPrimary())
437+
->with('theming', 'color', null)
433438
->willReturn('#fff');
434439

435440
$this->assertEquals('#fff', $this->template->getColorPrimary());
@@ -542,7 +547,7 @@ public function testUndoColor() {
542547
->method('getAppValue')
543548
->withConsecutive(
544549
['theming', 'cachebuster', '0'],
545-
['theming', 'color', $this->defaults->getColorPrimary()],
550+
['theming', 'color', null],
546551
)->willReturnOnConsecutiveCalls(
547552
'15',
548553
$this->defaults->getColorPrimary(),

lib/private/Server.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,6 +1222,7 @@ public function __construct($webRoot, \OC\Config $config) {
12221222
return new ThemingDefaults(
12231223
$c->get(\OCP\IConfig::class),
12241224
$c->getL10N('theming'),
1225+
$c->get(IUserSession::class),
12251226
$c->get(IURLGenerator::class),
12261227
$c->get(ICacheFactory::class),
12271228
new Util($c->get(\OCP\IConfig::class), $this->get(IAppManager::class), $c->getAppDataDir('theming')),

0 commit comments

Comments
 (0)