Skip to main content

azul_layout/widgets/
label.rs

1//! Label widget for displaying static text with platform-specific default styling.
2
3use azul_core::dom::{Dom, IdOrClass, IdOrClass::Class, IdOrClassVec};
4use azul_css::dynamic_selector::{CssPropertyWithConditions, CssPropertyWithConditionsVec};
5#[allow(clippy::wildcard_imports)] // widget/render module pulls in the css property/value types it builds with
6use azul_css::{
7    props::{
8        basic::*,
9        layout::*,
10        property::{CssProperty, *},
11        style::*,
12    },
13    *,
14};
15
16/// A static text label widget with platform-appropriate default styling.
17#[derive(Debug, Clone)]
18#[repr(C)]
19pub struct Label {
20    pub string: AzString,
21    pub label_style: CssPropertyWithConditionsVec,
22}
23
24const SANS_SERIF_STR: &str = "system:ui";
25const SANS_SERIF: AzString = AzString::from_const_str(SANS_SERIF_STR);
26const SANS_SERIF_FAMILIES: &[StyleFontFamily] = &[StyleFontFamily::System(SANS_SERIF)];
27const SANS_SERIF_FAMILY: StyleFontFamilyVec =
28    StyleFontFamilyVec::from_const_slice(SANS_SERIF_FAMILIES);
29
30/// Standard label text color (#4C4C4C), matching platform UI defaults.
31const COLOR_4C4C4C: ColorU = ColorU {
32    r: 76,
33    g: 76,
34    b: 76,
35    a: 255,
36};
37
38static LABEL_STYLE_DEFAULT: &[CssPropertyWithConditions] = &[
39    CssPropertyWithConditions::simple(CssProperty::const_display(LayoutDisplay::Flex)),
40    CssPropertyWithConditions::simple(CssProperty::const_flex_direction(
41        LayoutFlexDirection::Column,
42    )),
43    CssPropertyWithConditions::simple(CssProperty::const_justify_content(
44        LayoutJustifyContent::Center,
45    )),
46    CssPropertyWithConditions::simple(CssProperty::const_align_items(LayoutAlignItems::Center)),
47    CssPropertyWithConditions::simple(CssProperty::const_flex_grow(LayoutFlexGrow::const_new(1))),
48    CssPropertyWithConditions::simple(CssProperty::const_text_color(StyleTextColor {
49        inner: COLOR_4C4C4C,
50    })),
51    CssPropertyWithConditions::simple(CssProperty::const_font_size(StyleFontSize::const_px(13))),
52    CssPropertyWithConditions::simple(CssProperty::const_text_align(StyleTextAlign::Center)),
53    CssPropertyWithConditions::simple(CssProperty::const_font_family(SANS_SERIF_FAMILY)),
54];
55
56static LABEL_STYLE_MAC: &[CssPropertyWithConditions] = &[
57    CssPropertyWithConditions::simple(CssProperty::const_display(LayoutDisplay::Flex)),
58    CssPropertyWithConditions::simple(CssProperty::const_flex_direction(
59        LayoutFlexDirection::Column,
60    )),
61    CssPropertyWithConditions::simple(CssProperty::const_justify_content(
62        LayoutJustifyContent::Center,
63    )),
64    CssPropertyWithConditions::simple(CssProperty::const_align_items(LayoutAlignItems::Center)),
65    CssPropertyWithConditions::simple(CssProperty::const_flex_grow(LayoutFlexGrow::const_new(1))),
66    CssPropertyWithConditions::simple(CssProperty::const_text_color(StyleTextColor {
67        inner: COLOR_4C4C4C,
68    })),
69    CssPropertyWithConditions::simple(CssProperty::const_font_size(StyleFontSize::const_px(12))),
70    CssPropertyWithConditions::simple(CssProperty::const_text_align(StyleTextAlign::Center)),
71    CssPropertyWithConditions::simple(CssProperty::const_font_family(SANS_SERIF_FAMILY)),
72];
73
74/// No default styling on unsupported platforms (e.g. WASM, FreeBSD);
75/// callers should provide explicit styles via `label_style`.
76static LABEL_STYLE_OTHER: &[CssPropertyWithConditions] = &[];
77
78impl Label {
79    /// Creates a new label with the given text and platform-specific default styling.
80    #[inline]
81    #[must_use] pub fn create(string: AzString) -> Self {
82        Self {
83            string,
84            #[cfg(target_os = "windows")]
85            label_style: CssPropertyWithConditionsVec::from_const_slice(LABEL_STYLE_DEFAULT),
86            #[cfg(target_os = "linux")]
87            label_style: CssPropertyWithConditionsVec::from_const_slice(LABEL_STYLE_DEFAULT),
88            #[cfg(target_os = "macos")]
89            label_style: CssPropertyWithConditionsVec::from_const_slice(LABEL_STYLE_MAC),
90            #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
91            label_style: CssPropertyWithConditionsVec::from_const_slice(LABEL_STYLE_OTHER),
92        }
93    }
94
95    /// Replaces `self` with an empty default label, returning the original.
96    #[inline]
97    #[must_use]
98    pub fn swap_with_default(&mut self) -> Self {
99        let mut s = Self::create(AzString::from_const_str(""));
100        core::mem::swap(&mut s, self);
101        s
102    }
103
104    /// Converts this label into a DOM text node with the `__azul-native-label` class.
105    #[inline]
106    #[must_use] pub fn dom(self) -> Dom {
107        static LABEL_CLASS: &[IdOrClass] =
108            &[Class(AzString::from_const_str("__azul-native-label"))];
109
110        Dom::create_text(self.string)
111            .with_ids_and_classes(IdOrClassVec::from_const_slice(LABEL_CLASS))
112            .with_css_props(self.label_style)
113    }
114}
115
116impl From<Label> for Dom {
117    fn from(l: Label) -> Self {
118        l.dom()
119    }
120}