Skip to content

Commit

Permalink
Simplify variable names
Browse files Browse the repository at this point in the history
Signed-off-by: John Molakvoæ <[email protected]>
  • Loading branch information
skjnldsv committed Oct 19, 2022
1 parent 746710a commit d89da9b
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 77 deletions.
4 changes: 2 additions & 2 deletions apps/theming/lib/ImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use OCP\IURLGenerator;

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

/** @var IConfig */
private $config;
Expand All @@ -53,7 +54,6 @@ class ImageManager {
/** @var IURLGenerator */
private $urlGenerator;
/** @var array */
private $supportedImageKeys = ['background', 'logo', 'logoheader', 'favicon'];
/** @var ICacheFactory */
private $cacheFactory;
/** @var ILogger */
Expand Down Expand Up @@ -142,7 +142,7 @@ public function hasImage(string $key): bool {
*/
public function getCustomImages(): array {
$images = [];
foreach ($this->supportedImageKeys as $key) {
foreach ($this::SupportedImageKeys as $key) {
$images[$key] = [
'mime' => $this->config->getAppValue('theming', $key . 'Mime', ''),
'url' => $this->getImageUrl($key),
Expand Down
90 changes: 90 additions & 0 deletions apps/theming/lib/Themes/CommonThemeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/
namespace OCA\Theming\Themes;

use OCA\Theming\AppInfo\Application;
use OCA\Theming\ImageManager;
use OCA\Theming\Service\BackgroundService;
use OCA\Theming\Util;

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

// primary related colours
return [
// invert filter if primary is too bright
// to be used for legacy reasons only. Use inline
// svg with proper css variable instead or material
// design icons.
// ⚠️ Using 'no' as a value to make sure we specify an
// invalid one with no fallback. 'unset' could here fallback to some
// other theme with media queries
'--primary-invert-if-bright' => $this->util->invertTextColor($this->primaryColor) ? 'invert(100%)' : 'no',

'--color-primary' => $this->primaryColor,
'--color-primary-default' => $this->defaultPrimaryColor,
'--color-primary-text' => $this->util->invertTextColor($this->primaryColor) ? '#000000' : '#ffffff',
Expand All @@ -63,4 +75,82 @@ protected function generatePrimaryVariables(string $colorMainBackground, string
'--gradient-primary-background' => 'linear-gradient(40deg, var(--color-primary) 0%, var(--color-primary-hover) 100%)',
];
}

/**
* Generate admin theming background-related variables
*/
protected function generateGlobalBackgroundVariables(): array {
$backgroundDeleted = $this->config->getAppValue(Application::APP_ID, 'backgroundMime', '') === 'backgroundColor';
$hasCustomLogoHeader = $this->imageManager->hasImage('logo') || $this->imageManager->hasImage('logoheader');

$variables = [];

// If primary as background has been request or if we have a custom primary colour
// let's not define the background image
if ($backgroundDeleted && $this->themingDefaults->isUserThemingDisabled()) {
$variables['--image-background-plain'] = 'true';
$variables['--color-background-plain'] = $this->themingDefaults->getColorPrimary();
}

// Register image variables only if custom-defined
foreach (ImageManager::SupportedImageKeys as $image) {
if ($this->imageManager->hasImage($image)) {
$imageUrl = $this->imageManager->getImageUrl($image);
if ($image === 'background') {
// If background deleted is set, ignoring variable
if ($backgroundDeleted) {
continue;
}
$variables['--image-background-size'] = 'cover';
}
$variables["--image-$image"] = "url('" . $imageUrl . "')";
}
}

if ($hasCustomLogoHeader) {
$variables["--image-logoheader-custom"] = 'true';
}

return $variables;
}

/**
* Generate user theming background-related variables
*/
protected function generateUserBackgroundVariables(): array {
$user = $this->userSession->getUser();
if ($user !== null
&& !$this->themingDefaults->isUserThemingDisabled()
&& $this->appManager->isEnabledForUser(Application::APP_ID)) {
$themingBackground = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background', 'default');
$currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0');

// The user uploaded a custom background
if ($themingBackground === 'custom') {
$cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8);
return [
'--image-background' => "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')",
// TODO: implement primary color from custom background --color-background-plain
];
}

// The user picked a shipped background
if (isset(BackgroundService::SHIPPED_BACKGROUNDS[$themingBackground])) {
return [
'--image-background' => "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "/img/background/$themingBackground") . "')",
'--color-background-plain' => $this->themingDefaults->getColorPrimary(),
];
}

// The user picked a static colour
if (substr($themingBackground, 0, 1) === '#') {
return [
'--image-background' => 'no',
'--color-background-plain' => $this->themingDefaults->getColorPrimary(),
];
}
}

return [];
}
}
70 changes: 8 additions & 62 deletions apps/theming/lib/Themes/DefaultTheme.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
namespace OCA\Theming\Themes;

use OCA\Theming\AppInfo\Application;
use OCA\Theming\ImageManager;
use OCA\Theming\ITheme;
use OCA\Theming\Service\BackgroundService;
Expand All @@ -35,7 +34,6 @@
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\Server;

class DefaultTheme implements ITheme {
use CommonThemeTrait;
Expand All @@ -47,6 +45,7 @@ class DefaultTheme implements ITheme {
public ImageManager $imageManager;
public IConfig $config;
public IL10N $l;
public IAppManager $appManager;

public string $defaultPrimaryColor;
public string $primaryColor;
Expand All @@ -57,14 +56,16 @@ public function __construct(Util $util,
IURLGenerator $urlGenerator,
ImageManager $imageManager,
IConfig $config,
IL10N $l) {
IL10N $l,
IAppManager $appManager) {
$this->util = $util;
$this->themingDefaults = $themingDefaults;
$this->userSession = $userSession;
$this->urlGenerator = $urlGenerator;
$this->imageManager = $imageManager;
$this->config = $config;
$this->l = $l;
$this->appManager = $appManager;

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

$hasCustomLogoHeader = $this->imageManager->hasImage('logo') || $this->imageManager->hasImage('logoheader');

$variables = [
'--color-main-background' => $colorMainBackground,
'--color-main-background-rgb' => $colorMainBackgroundRGB,
Expand Down Expand Up @@ -188,71 +187,18 @@ public function getCSSVariables(): array {

// mobile. Keep in sync with core/js/js.js
'--breakpoint-mobile' => '1024px',

// invert filter if primary is too bright
// to be used for legacy reasons only. Use inline
// svg with proper css variable instead or material
// design icons.
// ⚠️ Using 'no' as a value to make sure we specify an
// invalid one with no fallback. 'unset' could here fallback to some
// other theme with media queries
'--primary-invert-if-bright' => $this->util->invertTextColor($this->primaryColor) ? 'invert(100%)' : 'no',
'--background-invert-if-dark' => 'no',
'--background-invert-if-bright' => 'invert(100%)',

// Default last fallback values
'--image-main-background' => "url('" . $this->urlGenerator->imagePath('core', 'app-background.jpg') . "')",
'--color-main-background-plain' => $this->defaultPrimaryColor,
'--image-background' => "url('" . $this->urlGenerator->imagePath('core', 'app-background.jpg') . "')",
'--color-background-plain' => $this->defaultPrimaryColor,
];

// Primary variables
$variables = array_merge($variables, $this->generatePrimaryVariables($colorMainBackground, $colorMainText));

$backgroundDeleted = $this->config->getAppValue(Application::APP_ID, 'backgroundMime', '') === 'backgroundColor';
// If primary as background has been request or if we have a custom primary colour
// let's not define the background image
if ($backgroundDeleted && $this->themingDefaults->isUserThemingDisabled()) {
$variables['--image-background-plain'] = 'true';
$variables['--color-main-background-plain'] = $this->themingDefaults->getColorPrimary();
}

// Register image variables only if custom-defined
foreach (['logo', 'logoheader', 'favicon', 'background'] as $image) {
if ($this->imageManager->hasImage($image)) {
$imageUrl = $this->imageManager->getImageUrl($image);
if ($image === 'background') {
// If background deleted is set, ignoring variable
if ($backgroundDeleted) {
continue;
}
$variables['--image-background-size'] = 'cover';
$variables['--image-main-background'] = "url('" . $imageUrl . "')";
}
$variables["--image-$image"] = "url('" . $imageUrl . "')";
}
}

if ($hasCustomLogoHeader) {
$variables["--image-logoheader-custom"] = 'true';
}

$appManager = Server::get(IAppManager::class);
$user = $this->userSession->getUser();
if (!$this->themingDefaults->isUserThemingDisabled() && $appManager->isEnabledForUser(Application::APP_ID) && $user !== null) {
$themingBackground = $this->config->getUserValue($user->getUID(), Application::APP_ID, 'background', 'default');
$currentVersion = (int)$this->config->getUserValue($user->getUID(), Application::APP_ID, 'userCacheBuster', '0');


if ($themingBackground === 'custom') {
$cacheBuster = substr(sha1($user->getUID() . '_' . $currentVersion), 0, 8);
$variables['--image-main-background'] = "url('" . $this->urlGenerator->linkToRouteAbsolute('theming.userTheme.getBackground') . "?v=$cacheBuster')";
} elseif (isset(BackgroundService::SHIPPED_BACKGROUNDS[$themingBackground])) {
$variables['--image-main-background'] = "url('" . $this->urlGenerator->linkTo(Application::APP_ID, "/img/background/$themingBackground") . "')";
} elseif (substr($themingBackground, 0, 1) === '#') {
unset($variables['--image-main-background']);
$variables['--color-main-background-plain'] = $this->themingDefaults->getColorPrimary();
}
}
$variables = array_merge($variables, $this->generateGlobalBackgroundVariables());
$variables = array_merge($variables, $this->generateUserBackgroundVariables());

return $variables;
}
Expand Down
8 changes: 4 additions & 4 deletions core/css/apps.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions core/css/apps.scss
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ html {
width: 100%;
height: 100%;
position: absolute;
background-color: var(--color-main-background-plain, var(--color-main-background));
background-image: var(--image-main-background);
background-color: var(--color-background-plain, var(--color-main-background));
background-image: var(--image-background);
background-size: cover;
background-position: center;
}

body {
background-color: var(--color-main-background-plain, var(--color-main-background));
background-image: var(--image-background-plain, var(--image-main-background));
background-color: var(--color-background-plain, var(--color-main-background));
background-image: var(--image-background-plain, var(--image-background));
background-size: cover;
background-position: center;
position: fixed;
Expand Down
8 changes: 4 additions & 4 deletions core/css/server.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/theming-theming-settings.js.map

Large diffs are not rendered by default.

0 comments on commit d89da9b

Please sign in to comment.