LSS: LVGL Style Sheet

Last year, I had an idea to make theme development easier. I didn’t like the C approach and wanted something a bit less technical. The result is a syntax that is more compact and easier to visually process.

I played with the idea to have a CSS/SCSS-like syntax to generate themes and got reasonably far with it, yet didn’t reach something that was functional. This week I decided to feed the rest of my thoughts into Claude and it went beyond what I expected.

I’ve put it under a permissive MIT license, available on GitHub:
https://github.com/ByteWelder/LSS

The editor/ folder contains syntax plugin support for CLion and VS Code.

In my personal project, I now have a theme that implements Material3 design patterns. Let’s take a look at some of the features.

@theme tag defines the theme name, which is relevant for code generation (files and functions). @config defines the parameters that will go into a custom struct MaterialConfig. These are the parameters that can be used to initialise your theme from code:

@theme material;

@config {
    isDark: bool = false;
    isCompact: bool = false;
    animationsEnabled: bool = true;
    colorPrimary: color = #00bbff;
    colorSecondary: color = #ee81c3;
    colorError: color = #B3261E;
    fontSmall: font = raw("LV_FONT_DEFAULT");
    fontNormal: font = raw("LV_FONT_DEFAULT");
    fontLarge: font = raw("LV_FONT_DEFAULT");
}

You can define additional custom widgets:

@widget toolbar: lvgl_toolbar_class;
@widget icon_button: lvgl_icon_button_class;

And then tokens: a proper design system has layers of tokens, for example:

// Base tokens

// Colors
@neutral-4: #0E0E0E;
@neutral-6: #131313;
// Motion
@duration-short: 100;
@duration-medium: 250;
@duration-long: 500;

// Semantic tokens

@color-primary: if($isDark, mix($colorPrimary, white, 55%), $colorPrimary);
@color-on-primary: if($isDark, mix($colorPrimary, black, 50%), white);

// Widget tokens

// Scrollbar
@scrollbar-color: @color-outline;
@scrollbar-opacity: 40%;
@scrollbar-scrolled-opacity: 70%;
@scrollbar-width: if($isCompact, 2, @space-1);
@scrollbar-pad: if($isCompact, 1, @space-1);

You can define transitions:

@transition state-enter {
    props: bgColor, bgOpa, borderColor, textColor, outlineWidth, outlineOpa;
    path: ease-out;
    duration: @motion-duration-short;
    delay: 0;
}

@transition state-exit {
    props: bgColor, bgOpa, borderColor, textColor, outlineWidth, outlineOpa;
    path: ease-out;
    duration: @motion-duration-short;
    delay: 70;
}

And then the actual widgets:

button {
    bgColor: @button-container-color;
    bgOpa: cover;
    textColor: @button-label-color;
    radius: @button-shape;
    padHor: @button-pad-hor;
    padVer: @button-pad-ver;
    padGap: @button-gap;
}

And rules for the hierarchy:

apply(tabview > obj[1]) {
    // Tab content area: no styles
}

And even logic evaluation:

@dialog-gap: if($isCompact, 2, @space-2);

Example theme source (1711 lines):

https://raw.githubusercontent.com/TactilityProject/Tactility/b088c354491d8a64ba0c7472591cd42dd3b9b9af/Modules/lvgl-module/themes/material.lss

Example theme generated code (2309 + 60 lines):

https://github.com/TactilityProject/Tactility/blob/b088c354491d8a64ba0c7472591cd42dd3b9b9af/Modules/lvgl-module/source/themes/lv_theme_material.c

https://github.com/TactilityProject/Tactility/blob/b088c354491d8a64ba0c7472591cd42dd3b9b9af/Modules/lvgl-module/source/themes/lv_theme_material.h

The resulting LSS file is about 28% smaller than the combined source files.

Every feature has its own test, which doubles as documentation:

https://github.com/ByteWelder/LSS/tree/main/tests/cases

Very nice! In the meantime we have also released our DSL, called LVGL XML. A general README is here, and docs is here.

In the next version we will have intuitive expressions and data bindings too, like

<lv_label text="{'Hi' . name . ', you have ' . 
                  count == 0 ? 'no messages' : count . ' messages'}">

I’d love to brainstorm about these things!

Very nice!

I’m always up for a brainstorming session! I have lots of opinions on UX/UI :smiley:

Is there a reason it uses . instead of + for combining strings?

I was thinking a lot about it and kind of lean into going for + instead to free up . for future scoping (e.g. panel1.label1 if we every want to add something like this)