Theme - distinct style for the same lv_obj_class

What do you want to achieve?

One of the most basic use case for themes, is to apply distinct style to different types of the same object. Like in css, you can have a “red button” and a “blue button”. Both are buttons, but we apply different styles to them.
From the VERY SCARCE documentation for the themes it looks like we should now implement an apply_cb function that will apply styles to widgets based on the “lv_obj_class”. Documentation for lv_obj_class is nonexisting, but it seems, that every builtin widget has it’s one own class. Therefore it’s impossible to distinguish “red button” from “blue button” just looking on the lv_obj_class, because they have the same class.

What have you tried so far?

I tried hiding an enum of the button type inside user data of the widget, but i’m not satisfied with the solution as I need the user data for other usage.
Of course I can manualy apply styles to my widget but that’s not the idea behind themes I guess.
It seems that there is some way to create a new, inherited lv_obj_class to have my own “red button” and “blue button” class, but it’s not documented and seems like an overkill.

Question

So what is the proper way to do it? It seems like a very basic thing to do, yet there is no documented solution. Please point me in the right direction.

Environment

  • MCU/MPU/Board: PC simulator, ESP32S3
  • LVGL version: 9.5

Hi @kdluzynski, To achieve what you want I think you have to create your own class for each type of button to diferenciate it in the apply theme callback using lv_obj_check_type() function. If you use LVGL pro you can use subjects to change bind styles and create themes.

This can help you create your own class:

Or instead of two widgets you can create just one button and add custom flags/properties to change its types between red/blue on the theme apply callback