Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Theming using SCSS variables #3531

Merged
merged 5 commits into from
Apr 25, 2017
Merged

Theming using SCSS variables #3531

merged 5 commits into from
Apr 25, 2017

Conversation

juliusknorr
Copy link
Member

@juliusknorr juliusknorr commented Feb 18, 2017

Requires #3256

Custom themes as well as the theming app will get a lot simpler with #3256. We can just overwrite the existing SCSS variables and can drop most of the string concatenated CSS code inside of the ThemingController.

There will be also much less maintenance work for the theming app in order to keep up with any CSS code changes.

@skjnldsv @nextcloud/theming @nextcloud/designers

Custom themes

With this you can apply a different color scheme to custom themes like this:

class OC_Theme {
	public function getScssVariables() {
		return [
			'color-primary' => '#000000',
			'color-primary-text' => '#ffffff',
                        'color-main-text' => '#ffffff',
                        'color-main-background' => '#333333'
		];
	}
}

ToDo

  • Theming code cleanup
  • Fix tests

@mention-bot
Copy link

@juliushaertl, thanks for your PR! By analyzing the history of the files in this pull request, we identified @skjnldsv, @rullzer and @ChristophWurst to be potential reviewers.

@juliusknorr juliusknorr requested a review from skjnldsv February 18, 2017 09:40
'background-image: url(\'./logo?v='.$cacheBusterValue.'\');' .
'background-size: contain;' .
'}' . "\n" .
'#header .logo, ' .
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we pass the logo as a variable too? :D

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be possible. I'll have a look. :)

$injectedVariables = $this->getInjectedVariables();
if($injectedVariables !== '' && $this->config->getAppValue('core', 'scss.variables') !== md5($injectedVariables)) {
$this->resetCache();
$this->config->setAppValue('core', 'scss.variables', md5($injectedVariables));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather like to use ICache here but since there is no caching enabled as a default, we would be generating the css code on every request. It's a little missuse of the appConfig but since it is loaded into memory on every request, there is no overhead when requesting existing files. We only have one database update per changing scss variables. Feedback for that would be nice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't cache enable my default no matter we have redis/memcache enabled?
Isn't there a filecaching fallback for this purpose only?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, I 've been using the ICache function on my fbsync extension since a year and I remember it was working before I enabled redis iirc :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least it was not on my simple dev setup.

https://github.com/nextcloud/server/blob/master/lib/private/Server.php#L393-L406 looks like it was because PHPUNIT was enabled. Not sure if it is ok to regenerate everytime in dev setups, it is so slow then :(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/nextcloud/server/blob/master/lib/private/Server.php#L393-L406 looks like it was because PHPUNIT was enabled. Not sure if it is ok to regenerate everytime in dev setups, it is so slow then :(

The define there should only trigger if PHPUnit is actually running (e.g. on test execution)

Copy link
Member

@LukasReschke LukasReschke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it 🚀

Some nitpicks in the other comments here, mainly about unit tests and questions :)

@@ -150,6 +154,29 @@ public function getBackground() {
}
}


public function getScssVariables() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs PHPDoc and a small unit test :)

*/
public function __construct(ILogger $logger, IAppData $appData, IURLGenerator $urlGenerator, SystemConfig $systemConfig) {
public function __construct(ILogger $logger, IAppData $appData, IURLGenerator $urlGenerator, IConfig $config, \OC_Defaults $defaults) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer adding each of them on a newline to keep the code easier to read without having to scroll :)

* @param ISimpleFolder $folder
* @return bool
*/
private function variablesChanged($fileNameCSS, ISimpleFolder $folder) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests please :)

try {
$variablesFile = \OC::$SERVERROOT . '/core/css/variables.scss';
$cachedFile = $folder->getFile($fileNameCSS);
if ($cachedFile->getMTime() < filemtime($variablesFile)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this block is because of updates in dev environments?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. Otherwise changing anything in variables.scss will not trigger regenerating the css files.

* Reset scss cache by deleting all generated css files
* We need to regenerate all files when variables change
*/
public function resetCache() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to be public?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made those public so i can add unit test for those easily later on.

Copy link
Member

@LukasReschke LukasReschke Feb 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also test private methods. We have a helper for that, check \Test\TestCase::invokePrivate and it's usages :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we can also cover them just with the existing public methods.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah nice 👍 I wasn't aware of that.

/**
* @return string SCSS code for variables from OC_Defaults
*/
public function getInjectedVariables() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this have to be public?

@juliusknorr juliusknorr force-pushed the theming-scss branch 5 times, most recently from 4afee67 to 4d0386a Compare March 24, 2017 10:05
@juliusknorr juliusknorr added 3. to review Waiting for reviews and removed 2. developing Work in progress labels Mar 24, 2017
@juliusknorr
Copy link
Member Author

Time for review.

@juliusknorr juliusknorr requested a review from MorrisJobke March 24, 2017 11:11
@juliusknorr
Copy link
Member Author

@nextcloud/theming

@@ -58,27 +60,30 @@ class ThemingDefaults extends \OC_Defaults {
* @param IL10N $l
* @param IURLGenerator $urlGenerator
* @param \OC_Defaults $defaults
* @param IRootFolder $rootFolder
* @param IRootFolder $appData
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

class needs changing as well

Copy link
Member

@LukasReschke LukasReschke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Logo is disappearing with this. Let me try to find the responsible code :)

GET http://10.211.55.7/stable9//stable9/core/img/logo.svg?v=1 404 (Not Found)

The one /stable9/ seems too much.

@codecov-io
Copy link

codecov-io commented Mar 26, 2017

Codecov Report

Merging #3531 into master will decrease coverage by 51.33%.
The diff coverage is 0%.

@@             Coverage Diff              @@
##             master   #3531       +/-   ##
============================================
- Coverage     53.96%   2.62%   -51.34%     
- Complexity    21679   21694       +15     
============================================
  Files          1272    1272               
  Lines         75789   75096      -693     
============================================
- Hits          40898    1970    -38928     
- Misses        34891   73126    +38235
Impacted Files Coverage Δ Complexity Δ
lib/private/legacy/defaults.php 0% <0%> (-38.24%) 44 <2> (+2)
lib/private/Server.php 3.75% <0%> (-89.46%) 120 <0> (ø)
lib/private/Template/SCSSCacher.php 0% <0%> (-76.6%) 34 <11> (+12)
apps/theming/lib/Controller/ThemingController.php 0% <0%> (-78.2%) 27 <0> (-5)
lib/private/TemplateLayout.php 0% <0%> (ø) 35 <0> (+2) ⬆️
themes/example/defaults.php 0% <0%> (ø) 17 <1> (+1) ⬆️
apps/theming/lib/ThemingDefaults.php 0% <0%> (-86.05%) 33 <4> (+4)
lib/private/Security/CSRF/CsrfTokenManager.php 0% <0%> (-100%) 8% <0%> (ø)
apps/dav/lib/Connector/Sabre/Server.php 0% <0%> (-100%) 1% <0%> (ø)
apps/systemtags/lib/Settings/Admin.php 0% <0%> (-100%) 3% <0%> (ø)
... and 772 more

];

$variables['image-logo'] = "'../../".$this->getLogo()."'";
$variables['image-login-background'] = "'../../".$this->getBackground()."'";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/* override styles for login screen in guest.css */
/* line 42, /media/psf/stable9/apps/theming/css/theming.scss */
#header .logo, #header .logo-icon {
  background-size: contain;
  background-image: url(../../..//stable9/core/img/logo.svg?v=1);
}

Generated URL is wrong :)

@MorrisJobke
Copy link
Member

Failing tests (weirdly there is now a /owncloud/ in the path 😕

There were 3 failures:
1) OCA\Theming\Tests\ThemingDefaultsTest::testGetLogoDefault
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/core/img/logo.svg'
+'http://localhost/owncloud/core/img/logo.svg'
/drone/src/github.com/nextcloud/server/apps/theming/tests/ThemingDefaultsTest.php:443
2) OCA\Theming\Tests\ThemingDefaultsTest::testGetLogoCustom
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/index.php/apps/theming/logo'
+'http://localhost/owncloud/index.php/apps/theming/logo'
/drone/src/github.com/nextcloud/server/apps/theming/tests/ThemingDefaultsTest.php:458
3) OCA\Theming\Tests\ThemingDefaultsTest::testGetScssVariables
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
 Array (
     'theming-cachebuster' => ''0''
-    'image-logo' => ''http://localhost/index.php/apps/theming/logo''
+    'image-logo' => ''http://localhost/owncloud/index.php/apps/theming/logo''
     'image-login-background' => ''/index.php/apps/theming/logi...round''
     'color-primary' => '#000000'
     'color-primary-text' => '#ffffff'
 )
/drone/src/github.com/nextcloud/server/apps/theming/tests/ThemingDefaultsTest.php:490

@rullzer
Copy link
Member

rullzer commented Apr 10, 2017

Needs a rebase

@MorrisJobke MorrisJobke added 2. developing Work in progress and removed 3. to review Waiting for reviews labels Apr 13, 2017
@MorrisJobke
Copy link
Member

Needs a rebase

Squashed and rebased, but I haven't tested it.

@MorrisJobke
Copy link
Member

Just in case I messed something up I also have a local backup branch available ;)

@juliusknorr
Copy link
Member Author

@MorrisJobke Works as expected. I've just added another commit that ensures that the css files will be reloaded if theming values are changed. Ready for review, I think. @nextcloud/theming

@juliusknorr juliusknorr added 3. to review Waiting for reviews and removed 2. developing Work in progress labels Apr 22, 2017
}

return $this->urlGenerator->linkToRoute('theming.Theming.getLogo') . '?v=' . $cacheBusterCounter;
return $this->urlGenerator->linkToRouteAbsolute('theming.Theming.getLogo') . '?v=' . $cacheBusterCounter;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Breaks HTML mails, will have a look.

@MorrisJobke
Copy link
Member

There was 1 failure:
1) OCA\Theming\Tests\ThemingDefaultsTest::testGetLogoDefault
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'/core/img/logo.svg?v=0'
+'/owncloud/core/img/logo.svg?v=0'
/drone/src/github.com/nextcloud/server/apps/theming/tests/ThemingDefaultsTest.php:457

@nickvergessen Could you have a look?

@@ -431,33 +454,61 @@ public function testGetLogoDefault() {
->method('getFolder')
->with('images')
->willThrowException(new \Exception());
$expected = $this->urlGenerator->imagePath('core','logo.svg') . '?v=0';
$this->assertEquals($expected, $this->template->getLogo());
$this->assertEquals('/core/img/logo.svg?v=0', $this->template->getLogo());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both should add the webroot...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I adjusted the test

juliusknorr and others added 4 commits April 25, 2017 11:39
Signed-off-by: Julius Haertl <[email protected]>

Add Scss variables to example theme and theming app

Signed-off-by: Julius Haertl <[email protected]>

Use SCSSCacher to build theming css

Signed-off-by: Julius Härtl <[email protected]>

Update theming.scss

Signed-off-by: Julius Härtl <[email protected]>

Code cleanup

Signed-off-by: Julius Härtl <[email protected]>

Fix tests

Signed-off-by: Julius Härtl <[email protected]>

Inject SCSSCacher for easier testing

Signed-off-by: Julius Härtl <[email protected]>

Fix typehint

Signed-off-by: Lukas Reschke <[email protected]>

Generate absolute URLs

Signed-off-by: Lukas Reschke <[email protected]>

Fix tests to always use absolute urls for theming images

Signed-off-by: Julius Härtl <[email protected]>

MailheaderColor -> ColorPrimary

Signed-off-by: Roeland Jago Douma <[email protected]>
Signed-off-by: Julius Härtl <[email protected]>
They are used way to often for such a change

Signed-off-by: Joas Schilling <[email protected]>

public function setUp() {
parent::setUp();
$this->config = $this->getMockBuilder(IConfig::class)->getMock();
$this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
$this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
$this->urlGenerator = \OC::$server->query(IURLGenerator::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this replaced anyway... I will revert this as well

Signed-off-by: Joas Schilling <[email protected]>
@jancborchardt
Copy link
Member

@juliushaertl @nickvergessen @rullzer @skjnldsv is there anything specifically blocking it, or anything to change/add? Seems to work well. :)

@MorrisJobke
Copy link
Member

Failing test is due to the JS unit problem - see #4494 for a fix.

Copy link
Member

@MorrisJobke MorrisJobke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and works 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants