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

Add support for library themes #252

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Conversation

vacarsu
Copy link
Contributor

@vacarsu vacarsu commented Nov 15, 2021

Description

The pull request adds support for registering library themes, similar to static assets.

Themes are now registered and validated within scenic from Scenic.Themes.

Motivation and Context

Before the theme module was on Scenic.Primitive.Style.Theme, which felt like it would only be used in the context of primitives/components. However, it was imported everywhere that theme validation was needed, and had all the themes directly on that module. This means Scenic would not be able to validate custom themes that were not a map.

Also, there were times when scenic would pass around :dark, :light atoms, you had to know this ahead of time and know to validate or get the preset via Scenic.Primitive.Style.Theme. With all themes now registered through a single module, you can now fetch presets and validate any theme from Scenic.Themes.

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to
    not work as expected)
  • Improvement/refactoring (non-breaking change that doesn't add any feature
    but make things better)

Todos

  • implement custom themes
  • implement custom validation
  • implement extendable color palette

@vacarsu
Copy link
Contributor Author

vacarsu commented Nov 15, 2021

Added a way to add additional keys to be validated for custom themes.

@vacarsu
Copy link
Contributor Author

vacarsu commented Nov 15, 2021

With these changes we can now validate once during compile. We shouldn't need to validate in the viewport anymore. Is my thinking here correct?

@@ -278,11 +278,11 @@ defmodule Scenic.Component.Button do
)
end

defp do_special_theme_outline(graph, :dark, border) do
defp do_special_theme_outline(graph, {:scenic, :dark}, border) do
Graph.modify(graph, :btn, &update_opts(&1, stroke: {1, border}))
end

Copy link
Contributor Author

@vacarsu vacarsu Nov 15, 2021

Choose a reason for hiding this comment

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

I'm not sure about these, It doesn't seem like they will ever match. I left them as is since I was already making such a sweeping change.

@@ -395,7 +395,7 @@ defmodule Scenic.ViewPort do
def set_theme(viewport, theme)

def set_theme(%ViewPort{pid: pid}, theme) do
case Theme.validate(theme) do
case Themes.validate(theme) do
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think validating in the viewport is no longer necessary if the validate is moved to compile time.

@boydm
Copy link
Collaborator

boydm commented Dec 4, 2021

Love the Palettte module. lets do a call so you can walk me through the rest. Is a bit much to wrap my head around.

@vacarsu
Copy link
Contributor Author

vacarsu commented Dec 8, 2021

Okay, made the changes you requested. Moved the functions with logic out of the macro, and it will now fallback to Scenic.Themes as a default if no themes config is set.

error -> error
end
end
nil ->
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm worried that there are many other projects that already use the standard themes of :primary, :secondary etc that will break as they need to be updated to {:scenic, :primary}. Instead of just nil -> it feels like if the theme_name is just an atom it should try {:scenic, :theme_name} here and see if it works. If that fails too, then go to the error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This would still allow the dev to override the meaning of :primary, since that would be found first.

Copy link

Choose a reason for hiding this comment

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

@boydm Personally deprecations are ok for me having in mind that scenic is below 1.0.0 version … Only after that I would expect change of first version number in order to introduce deprecation. I believe that every developer using scenic have in mind it's current state and it's not using it because this project have unbelievably stable API, but because it have amazing features other projects does not provide. 😃

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Went ahead and added backwards compat for single atom theme names for normalize/1, preset/1, validate/1

Copy link
Contributor Author

@vacarsu vacarsu Dec 14, 2021

Choose a reason for hiding this comment

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

It looks like normalize/1 and preset/1 are exactly the same. Maybe one of these can trigger validation and a raise and the other not?

Probably normalize should not do validation as you've probably gotten the tuple directly from the viewport. Whereas with preset you are probably manually passing a theme and could be error prone.

@crertel crertel mentioned this pull request May 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants