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

Theme property #294

Closed
melursus23 opened this issue Apr 6, 2023 · 7 comments
Closed

Theme property #294

melursus23 opened this issue Apr 6, 2023 · 7 comments

Comments

@melursus23
Copy link
Contributor

Hello,

How can I set the "Theme" property using your library?

I'm trying to mimic something similar to this but there is no Border.theme :

<Border Theme="{StaticResource CardBorder}">
    <TextBlock>Card</TextBlock>
</Border>

https://github.com/irihitech/Semi.Avalonia/blob/922afa904b36b4b70669d9156e4f570bfffaebf4/demo/Semi.Avalonia.Demo/Pages/BorderDemo.axaml

@JaggerJo
Copy link
Member

JaggerJo commented Apr 6, 2023

Seems like the theme property does not have a binding yet. We should definitely add that, feel free to create a PR.

@melursus23
Copy link
Contributor Author

Sure, I can try for sure.

Right now, I was able to add the following function:

        static member theme<'t when 't :> Layoutable>(theme: ControlTheme) : IAttr<'t> =
            AttrBuilder<'t>.CreateProperty<ControlTheme>(Layoutable.ThemeProperty, theme, ValueNone)

However, I'm not sure I've found the cleanest / easiest way to retrieve the theme resource, any suggestions?

Border.theme ((ctx.control.FindResource("CardBorder")) :?> ControlTheme)

@JaggerJo
Copy link
Member

JaggerJo commented Apr 8, 2023

Nice 👍

Dealing with resources that need to be looked up is always a bit hairy. Having a control that's described with the following code also means that the FindResource is called every time a component/view is diffed.

Border.create [
    Border.theme ((ctx.control.FindResource("CardBorder")) :?> ControlTheme)
]

Another way of setting the theme would be:

Border.create [
    Border.init (fun border -> 
        border.Theme <- ((ctx.control.FindResource("CardBorder")) :?> ControlTheme)
    )
]

If you create a PR I'll merge the binding you created. 👍

@Numpsy
Copy link
Collaborator

Numpsy commented Apr 8, 2023

static member theme<'t when 't :> Layoutable>(theme: ControlTheme) : IAttr<'t> =
            AttrBuilder<'t>.CreateProperty<ControlTheme>(Layoutable.ThemeProperty, theme, ValueNone)

Would StyledElement.ThemeProperty work and be more generic, assuming https://github.com/AvaloniaUI/Avalonia/blob/532e89c837d61d37d7fb2273f852d51bdc0f1999/src/Avalonia.Base/StyledElement.cs#L75 is the 'base' definition? (Layoutable being a subclass of StyledElement?)

@melursus23
Copy link
Contributor Author

melursus23 commented Apr 9, 2023

@JaggerJo thanks for the suggestion, make sense to use the init! I was thinking at maybe creating a centralized place when all styles are defined and available. I don't really like the fact that right now I'm relying of a string as well, maybe a TypeProvider could be created for the purpose, but this is way to far from my current f# knowledge 😃

@Numpsy make sense, I've made the suggested change!

In all cases, PR created 😄

@Numpsy
Copy link
Collaborator

Numpsy commented Apr 9, 2023

maybe a TypeProvider could be created for the purpose

There have been attempts at xaml type providers before (e.g. https://github.com/fsprojects/FsXaml) but I don't know about usage with Avalonia (I once made one attempt at an F# WPF project using the type provider for generating bindings from xaml and couldn't get it to work, and that's about the limit of my knowledge)

@JaggerJo
Copy link
Member

JaggerJo commented Apr 9, 2023

@melursus23 merged 👌 thanks for the contribution.

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

No branches or pull requests

3 participants