Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

fix(theming): only insert default theme if it's still used #7427

Closed

Conversation

devversion
Copy link
Member

Currently if I specify a default theme by using #setDefaultTheme, then the default theme will be still inserted.

Fixes #7425

Currently if I specify a default theme by using #setDefaultTheme, then the `default` theme will be still inserted.

Fixes angular#7425
@devversion devversion added the needs: review This PR is waiting on review from the team label Mar 6, 2016
@@ -528,7 +528,7 @@ function generateAllThemes($injector) {
if (generateOnDemand) return;

angular.forEach(THEMES, function(theme) {
if (!GENERATED[theme.name]) {
if (!GENERATED[theme.name] && !(theme.name !== $mdTheming.defaultTheme() && theme.name === 'default')) {
Copy link
Member

Choose a reason for hiding this comment

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

!(theme.name !== $mdTheming.defaultTheme() && theme.name === 'default')

is equals to

theme.name !== 'default'

Copy link
Member Author

Choose a reason for hiding this comment

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

No, that's wrong. Imagine you have set the default theme to myDefaultTheme
And it's iterating through the themes, and their is default now in progress.

theme.name !== 'default'
// 'default' !== 'default' === FALSE

So in this case it works.

But now, if you have no default theme set, then it will never insert the default theme and the theming breaks.

Copy link
Member

Choose a reason for hiding this comment

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

it's just that

!(theme.name !== $mdTheming.defaultTheme() && theme.name === 'default')

doesn't makes sense, you are checking if theme.name isn't equals to something AND equals to something else, if you know that you expect it to be default than why do you even bother to check if it's not equals to something else

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh it makes sense. An if-expression won't continue the check if it already failed.
For example:

true && false && true
-> The check will immediately cancel itself, because the second `AND` is false.

The theme should be skipped if the theme name is equal to default and if the default theme has been changed.

But maybe we can find a more elegant check, but your logic in the check isn't valid.

Copy link
Member

Choose a reason for hiding this comment

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

I'm trying to figure when your addition will be valid,

!(theme.name !== $mdTheming.defaultTheme() && theme.name === 'default')

let's say theme.name !== $mdTheming.defaultTheme() is true, than for sure theme.name === 'default' is false. - invalid
theme.name !== $mdTheming.defaultTheme() is false and theme.name === 'default' is false (or true) - invalid

it will only be valid if both are true which means $mdTheming.defaultTheme() !== 'default'

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's go through a few cases.

Case 1

We have the following mocked theme array

THEMES = ['default', ' blue']

And my default theme is set to blue

So the iteration will check:

// Replicates the iteration - case for `default`
var themeName = THEMES[0]; // `default`
// Negate the check, so if both conditions are true, 
// then the theme won't be inserted into the DOM
if (!(
  themeName !== 'blue' // true -> continue with the checks `AND`
  && themeName === 'default' // true -> both conditions are true
)

So in this case, only the theme blue will inserted into the DOM - Valid

Case 2

We have the following mocked theme array

THEMES = ['default', ' blue']

And my default theme is set to default (default behavior)

So the iteration will check:

// Replicates the iteration - case for `default`
var themeName = THEMES[0]; // `default`
// Negate the check, so if both conditions are true, 
// then the theme won't be inserted into the DOM
if (!(
  themeName !== 'default' // false -> cancel check and and return false
  && themeName === 'default' // Not reachable
)
// The check will pass, because of the negated expression 
// !(false) ==> true

So in this case, both themes will be inserted to the DOM

@EladBezalel
Copy link
Member

First of all let's use discrete to simplify the conditions

!(a != z && b == z) 
// => 
(a == z || b != z)

Ok so let's go through your cases.

Case 1

We have the following mocked theme array

THEMES = ['default', ' blue']

And my default theme is set to blue

So the iteration will check:

// Replicates the iteration - case for `default
var themeName = THEMES[0]; // `default`
// if one conditions is true, the theme won't be inserted into the DOM
if (themeName === 'blue' // false
  || themeName !== 'default' // false -> both conditions are false
)

So in this case, only the theme blue will inserted into the DOM - Valid

Case 2

We have the following mocked theme array

THEMES = ['default', ' blue']

And my default theme is set to default (default behavior)
So the iteration will check:

// Replicates the iteration - case for `default`
var themeName = THEMES[0]; // `default`
// if one conditions is true, the theme won't be inserted into the DOM
if (themeName === 'default' // true
  || themeName !== 'default' //  false
)
// The check will pass

So in this case, both themes will be inserted to the DOM

Finally

As you can see, in both cases

themeName !== 'default' //  false

Than it's clearly redundant..

@devversion
Copy link
Member Author

But imagine, as I said these are examples have a custom default theme set. So if you haven't changed the default theme, but you registered another theme.

So now take a look at Case 2. If we don't check both, as in mine implementation, then you'll have the following execution.

// Replicates the iteration - case for `default`
var themeName = THEMES[0]; // `default`

// This is the check, which can be simplified in your opinion.
if (themeName !== 'default' // FALSE
)
// The check won't pass

So this case won't be valid. Because here 'default' is our default theme and won't be injected into the DOM, because the check didn't pass.

We won't add the iterated theme when:
-The default theme has been changed by the user

  • And the iterated theme is 'default

@EladBezalel
Copy link
Member

I don't say you should use:

// Replicates the iteration - case for `default`
var themeName = THEMES[0]; // `default`

// This is the check, which can be simplified in your opinion.
if (themeName !== 'default' // FALSE
)
// The check won't pass

i say you should use:

// Replicates the iteration - case for `default`
var themeName = THEMES[0]; // `default`
// if one conditions is true, the theme won't be inserted into the DOM
if (themeName === 'default') // theme.name === $mdTheming.defaultTheme()

@ThomasBurleson ThomasBurleson added the in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs label Mar 13, 2016
@ThomasBurleson
Copy link
Contributor

@devversion, @EladBezalel - what is the status here? It appears that Elad's logic is valid. Will it pass all the test cases ?

@devversion
Copy link
Member Author

@ThomasBurleson We're still discussing.

I mean Elad says that, I should use this code:

if (themeName === 'default') // theme.name === $mdTheming.defaultTheme()

But isn't valid. Imagine if the user changed the default theme, then it's no longer default.

@devversion devversion added the needs: team discussion This issue requires a decision from the team before moving forward. label Mar 19, 2016
@devversion devversion changed the title fix(theming): do not insert default theme if default theme has changed. fix(theming): do not insert default theme if custom default theme Apr 19, 2016
@devversion devversion changed the title fix(theming): do not insert default theme if custom default theme fix(theming): only insert default theme if it's still used Apr 19, 2016
@devversion devversion removed the in progress Mainly for in progress PRs, but may be used for issues that require multiple PRs label Apr 19, 2016
@ThomasBurleson
Copy link
Contributor

tl;dr
@devversion - what is the status of this PR?

@ThomasBurleson ThomasBurleson added this to the 1.1.1 milestone Apr 19, 2016
@devversion
Copy link
Member Author

@ThomasBurleson We never had our dicussion. @EladBezalel Let's talk about that soon okay?

@ThomasBurleson ThomasBurleson modified the milestones: 1.1.1, Backlog Apr 21, 2016
@devversion
Copy link
Member Author

Closing this, as we both have different opinions about the logic. Let's revisit this anytime later.

@devversion devversion closed this May 27, 2016
@devversion devversion deleted the fix/theming-default-insert branch May 27, 2016 10:37
@Splaktar Splaktar removed this from the - Backlog milestone Jun 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs: review This PR is waiting on review from the team needs: team discussion This issue requires a decision from the team before moving forward. ui: theme
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants