Skip to main content

azul_layout/widgets/
label.rs

1use azul_core::{
2    callbacks::{CoreCallbackData, Update},
3    dom::{Dom, IdOrClass, IdOrClass::Class, IdOrClassVec},
4    refany::RefAny,
5};
6use azul_css::dynamic_selector::{CssPropertyWithConditions, CssPropertyWithConditionsVec};
7use azul_css::{
8    props::{
9        basic::*,
10        layout::*,
11        property::{CssProperty, *},
12        style::*,
13    },
14    *,
15};
16
17use crate::callbacks::Callback;
18
19#[derive(Debug, Clone)]
20#[repr(C)]
21pub struct Label {
22    pub string: AzString,
23    pub label_style: CssPropertyWithConditionsVec,
24}
25
26const SANS_SERIF_STR: &str = "sans-serif";
27const SANS_SERIF: AzString = AzString::from_const_str(SANS_SERIF_STR);
28const SANS_SERIF_FAMILIES: &[StyleFontFamily] = &[StyleFontFamily::System(SANS_SERIF)];
29const SANS_SERIF_FAMILY: StyleFontFamilyVec =
30    StyleFontFamilyVec::from_const_slice(SANS_SERIF_FAMILIES);
31
32const COLOR_4C4C4C: ColorU = ColorU {
33    r: 76,
34    g: 76,
35    b: 76,
36    a: 255,
37}; // #4C4C4C
38
39static LABEL_STYLE_WINDOWS: &[CssPropertyWithConditions] = &[
40    CssPropertyWithConditions::simple(CssProperty::const_display(LayoutDisplay::Flex)),
41    CssPropertyWithConditions::simple(CssProperty::const_flex_direction(
42        LayoutFlexDirection::Column,
43    )),
44    CssPropertyWithConditions::simple(CssProperty::const_justify_content(
45        LayoutJustifyContent::Center,
46    )),
47    CssPropertyWithConditions::simple(CssProperty::const_align_items(LayoutAlignItems::Center)),
48    CssPropertyWithConditions::simple(CssProperty::const_flex_grow(LayoutFlexGrow::const_new(1))),
49    CssPropertyWithConditions::simple(CssProperty::const_text_color(StyleTextColor {
50        inner: COLOR_4C4C4C,
51    })),
52    CssPropertyWithConditions::simple(CssProperty::const_font_size(StyleFontSize::const_px(13))),
53    CssPropertyWithConditions::simple(CssProperty::const_text_align(StyleTextAlign::Center)),
54    CssPropertyWithConditions::simple(CssProperty::const_font_family(SANS_SERIF_FAMILY)),
55];
56
57static LABEL_STYLE_LINUX: &[CssPropertyWithConditions] = &[
58    CssPropertyWithConditions::simple(CssProperty::const_display(LayoutDisplay::Flex)),
59    CssPropertyWithConditions::simple(CssProperty::const_flex_direction(
60        LayoutFlexDirection::Column,
61    )),
62    CssPropertyWithConditions::simple(CssProperty::const_justify_content(
63        LayoutJustifyContent::Center,
64    )),
65    CssPropertyWithConditions::simple(CssProperty::const_align_items(LayoutAlignItems::Center)),
66    CssPropertyWithConditions::simple(CssProperty::const_flex_grow(LayoutFlexGrow::const_new(1))),
67    CssPropertyWithConditions::simple(CssProperty::const_text_color(StyleTextColor {
68        inner: COLOR_4C4C4C,
69    })),
70    CssPropertyWithConditions::simple(CssProperty::const_font_size(StyleFontSize::const_px(13))),
71    CssPropertyWithConditions::simple(CssProperty::const_text_align(StyleTextAlign::Center)),
72    CssPropertyWithConditions::simple(CssProperty::const_font_family(SANS_SERIF_FAMILY)),
73];
74
75static LABEL_STYLE_MAC: &[CssPropertyWithConditions] = &[
76    CssPropertyWithConditions::simple(CssProperty::const_display(LayoutDisplay::Flex)),
77    CssPropertyWithConditions::simple(CssProperty::const_flex_direction(
78        LayoutFlexDirection::Column,
79    )),
80    CssPropertyWithConditions::simple(CssProperty::const_justify_content(
81        LayoutJustifyContent::Center,
82    )),
83    CssPropertyWithConditions::simple(CssProperty::const_align_items(LayoutAlignItems::Center)),
84    CssPropertyWithConditions::simple(CssProperty::const_flex_grow(LayoutFlexGrow::const_new(1))),
85    CssPropertyWithConditions::simple(CssProperty::const_text_color(StyleTextColor {
86        inner: COLOR_4C4C4C,
87    })),
88    CssPropertyWithConditions::simple(CssProperty::const_font_size(StyleFontSize::const_px(12))),
89    CssPropertyWithConditions::simple(CssProperty::const_text_align(StyleTextAlign::Center)),
90    CssPropertyWithConditions::simple(CssProperty::const_font_family(SANS_SERIF_FAMILY)),
91];
92
93static LABEL_STYLE_OTHER: &[CssPropertyWithConditions] = &[];
94
95impl Label {
96    #[inline]
97    pub fn create(string: AzString) -> Self {
98        Self {
99            string,
100            #[cfg(target_os = "windows")]
101            label_style: CssPropertyWithConditionsVec::from_const_slice(LABEL_STYLE_WINDOWS),
102            #[cfg(target_os = "linux")]
103            label_style: CssPropertyWithConditionsVec::from_const_slice(LABEL_STYLE_LINUX),
104            #[cfg(target_os = "macos")]
105            label_style: CssPropertyWithConditionsVec::from_const_slice(LABEL_STYLE_MAC),
106            #[cfg(not(any(target_os = "windows", target_os = "linux", target_os = "macos")))]
107            label_style: CssPropertyWithConditionsVec::from_const_slice(LABEL_STYLE_OTHER),
108        }
109    }
110
111    #[inline]
112    pub fn swap_with_default(&mut self) -> Self {
113        let mut s = Label::create(AzString::from_const_str(""));
114        core::mem::swap(&mut s, self);
115        s
116    }
117
118    #[inline]
119    pub fn dom(self) -> Dom {
120        static LABEL_CLASS: &[IdOrClass] =
121            &[Class(AzString::from_const_str("__azul-native-label"))];
122
123        Dom::create_text(self.string)
124            .with_ids_and_classes(IdOrClassVec::from_const_slice(LABEL_CLASS))
125            .with_css_props(self.label_style)
126    }
127}
128
129impl From<Label> for Dom {
130    fn from(l: Label) -> Dom {
131        l.dom()
132    }
133}