Skip to main content

waft_winui/theme/
typography.rs

1//! WinUI's type ramp: the `*TextBlockStyle` styles of `TextBlock_themeresources.xaml` (Fluent 2 sizes) as reveal text styles in the bundled Selawik, Microsoft's open metric-compatible stand-in for Segoe UI Variable.
2
3use super::generated::*;
4use waft_embedder::{Color, FontWeight};
5use waft_painting::TextStyle;
6
7/// The family name an app registers Selawik under, the kit's stand-in for `XamlAutoFontFamily` (Segoe UI Variable on Windows) and the default `content_control_theme_font_family`.
8pub const FONT_FAMILY: &str = "Selawik";
9
10/// One of WinUI's text block styles; XAML `{StaticResource BodyTextBlockStyle}` and its siblings.
11#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
12pub enum TextBlockStyle {
13    Caption,
14    Body,
15    BodyStrong,
16    BodyLarge,
17    BodyLargeStrong,
18    Subtitle,
19    Title,
20    TitleLarge,
21    Display,
22}
23
24impl TextBlockStyle {
25    /// The style's size and weight from `TextBlock_themeresources.xaml`: every style is based on `BaseTextBlockStyle` (SemiBold) and the body and caption styles set `Normal`.
26    pub fn font(self, r: &ThemeResources) -> (f64, FontWeight) {
27        match self {
28            TextBlockStyle::Caption => (r.caption_text_block_font_size, FontWeight::W400),
29            TextBlockStyle::Body => (r.body_text_block_font_size, FontWeight::W400),
30            TextBlockStyle::BodyStrong => (r.body_text_block_font_size, FontWeight::W600),
31            TextBlockStyle::BodyLarge => (r.body_large_text_block_font_size, FontWeight::W400),
32            TextBlockStyle::BodyLargeStrong => {
33                (r.body_large_text_block_font_size, FontWeight::W600)
34            }
35            TextBlockStyle::Subtitle => (r.subtitle_text_block_font_size, FontWeight::W600),
36            TextBlockStyle::Title => (r.title_text_block_font_size, FontWeight::W600),
37            TextBlockStyle::TitleLarge => (r.title_large_text_block_font_size, FontWeight::W600),
38            TextBlockStyle::Display => (r.display_text_block_font_size, FontWeight::W600),
39        }
40    }
41
42    /// The reveal text style in `color` (a `TextBlock`'s `Foreground`).
43    pub fn text_style(self, r: &ThemeResources, color: Color) -> TextStyle {
44        let (size, weight) = self.font(r);
45        control_text_style(r, size, weight, color)
46    }
47}
48
49/// A text style in the control font, `ContentControlThemeFontFamily`, at `size` and `weight`.
50pub fn control_text_style(
51    r: &ThemeResources,
52    size: f64,
53    weight: FontWeight,
54    color: Color,
55) -> TextStyle {
56    TextStyle::new()
57        .font_family(r.content_control_theme_font_family)
58        .font_size(size)
59        .font_weight(weight)
60        .color(color)
61}