| 1 | // The theme variables the renderer sets on the widget's root (docs/sdk-v1.md §2.3, §2.4, §3.4): the C++ twin of |
| 2 | // `themeVariables` in packages/core/src/shared/sdk-policy.ts, which the WebView renderers run. The host sends |
| 3 | // `{type:'theme', theme:{colorScheme, params, button, text}}` as given; this keeps the names of the fixed list and |
| 4 | // the values that match their grammar, completes the palette and the text tokens from the defaults, and the |
| 5 | // renderer writes the result inline and !important on <html>, under the profile's UA layer (tw_ua_stylesheet.h). |
| 6 | #pragma once |
| 7 | |
| 8 | #include "twidget/json.h" |
| 9 | |
| 10 | #include <string> |
| 11 | #include <utility> |
| 12 | #include <vector> |
| 13 | |
| 14 | namespace twlite::theme { |
| 15 | |
| 16 | /// `--tg-theme-*` (15, always), `--tg-color-scheme`, the `--tg-button-*` the client passed, `--tg-text-*` (3, always). |
| 17 | std::vector<std::pair<std::string, std::string>> variables(const twidget::Json& theme); |
| 18 | |
| 19 | /// `--tg-content-safe-area-inset-top|right|bottom|left` and `--tg-content-safe-area-corner-left|right` (6, always; the |
| 20 | /// twin of `contentSafeAreaVariables`, docs/sdk-v1.md §2.17): the host's `{type:'safeArea', inset:{top, right, bottom, |
| 21 | /// left, cornerLeft, cornerRight}}` in px, an inset in 0–64 and a corner in 0–200 with at most two decimals, else 0px. |
| 22 | std::vector<std::pair<std::string, std::string>> safe_area_variables(const twidget::Json& inset); |
| 23 | |
| 24 | /// A name of the fixed list: the widget's own style ops on <html> cannot set it. |
| 25 | bool is_variable(const std::string& name); |
| 26 | |
| 27 | /// `css` with every `var(--name[, fallback])` replaced by the value of `--name` in `vars`, else by its fallback |
| 28 | /// (a var() with neither stays as it is). |
| 29 | std::string substitute(const std::string& css, const std::vector<std::pair<std::string, std::string>>& vars); |
| 30 | |
| 31 | /// The UA layer for litehtml's master CSS: every `:where(X)` becomes `X`. litehtml sorts the master CSS by specificity |
| 32 | /// and applies all of it before the author's, so its own `body {margin: 8px}` would beat a zero-specificity rule that |
| 33 | /// comes later, while no author rule can lose to the master CSS anyway. A `:where()` of a selector list followed by |
| 34 | /// more of its compound selector stays (it cannot be unwrapped); the template has none. |
| 35 | std::string unwrap_where(const std::string& css); |
| 36 | |
| 37 | } // namespace twlite::theme |
| 38 | |