1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
use crate::prelude::*;
/// A label used to display text.
///
/// # Examples
///
/// ## Basic label
///
/// A label can be used to simply display some text on the screen.
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # let cx = &mut Context::default();
/// #
/// Label::new(cx, "Text");
/// ```
///
/// ## Label bound to data
///
/// A label can be bound to data using a lens which automatically updates the text whenever the underlying data changes.
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # let cx = &mut Context::default();
/// #
/// #[derive(Lens)]
/// struct AppData {
/// text: String,
/// }
///
/// impl Model for AppData {}
///
/// AppData {
/// text: String::from("Text"),
/// }
/// .build(cx);
///
/// Label::new(cx, AppData::text);
/// ```
///
/// ## Label with text wrapping
///
/// A label automatically wraps the text if it doesn't fit inside of the width of the label.
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # let mut cx = &mut Context::default();
/// #
/// Label::new(
/// cx,
/// "This is a really long text to showcase the text wrapping support of a label.",
/// )
/// .width(Pixels(100.0));
/// ```
///
/// ## Label without text wrapping
///
/// A label can also be configured to never wrap the text by using the [`text_wrap`](crate::prelude::Handle::text_wrap) method.
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # let mut cx = &mut Context::default();
/// #
/// Label::new(
/// cx,
/// "This is a really long text to showcase disabled text wrapping of a label.",
/// )
/// .width(Pixels(100.0))
/// .text_wrap(false);
/// ```
///
/// ## Label for a button
///
/// A label can also be used inside of a button to be able to add text to it.
///
/// ```
/// # use vizia_core::prelude::*;
/// # let cx = &mut Context::default();
/// #
/// Button::new(cx, |_| {}, |cx| Label::new(cx, "Text"));
/// ```
pub struct Label {
describing: Option<String>,
}
impl Label {
/// Creates a new label.
///
/// # Examples
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # let cx = &mut Context::default();
/// #
/// Label::new(cx, "Text");
/// ```
pub fn new<T>(cx: &mut Context, text: impl Res<T> + Clone) -> Handle<Self>
where
T: ToStringLocalized,
{
Self { describing: None }.build(cx, |_| {}).text(text.clone()).role(Role::Label).name(text)
}
pub fn rich<T>(
cx: &mut Context,
text: impl Res<T> + Clone,
children: impl Fn(&mut Context),
) -> Handle<Self>
where
T: ToStringLocalized,
{
Self { describing: None }
.build(cx, |cx| {
children(cx);
})
.text(text.clone())
.role(Role::Label)
.name(text)
}
}
impl Handle<'_, Label> {
/// Which form element does this label describe.
///
/// # Examples
///
/// ```
/// # use vizia_core::prelude::*;
/// #
/// # #[derive(Lens)]
/// # struct AppData {
/// # value: bool,
/// # }
/// #
/// # impl Model for AppData {}
/// #
/// # enum AppEvent {
/// # ToggleValue,
/// # }
/// #
/// # let cx = &mut Context::default();
/// #
/// # AppData { value: false }.build(cx);
/// #
/// Checkbox::new(cx, AppData::value).on_toggle(|cx| cx.emit(AppEvent::ToggleValue)).id("checkbox_identifier");
/// Label::new(cx, "hello").describing("checkbox_identifier");
/// ```
pub fn describing(self, entity_identifier: impl Into<String>) -> Self {
let identifier = entity_identifier.into();
if let Some(id) = self.cx.resolve_entity_identifier(&identifier) {
self.cx.style.labelled_by.insert(id, self.entity);
}
self.modify(|label| label.describing = Some(identifier)).class("describing").hidden(true)
}
}
impl View for Label {
fn element(&self) -> Option<&'static str> {
Some("label")
}
fn event(&mut self, cx: &mut EventContext, event: &mut Event) {
event.map(|window_event, meta| match window_event {
WindowEvent::Press { .. } | WindowEvent::PressDown { .. } => {
if cx.current() == cx.mouse.left.pressed && meta.target == cx.current() {
if let Some(describing) = self
.describing
.as_ref()
.and_then(|identity| cx.resolve_entity_identifier(identity))
{
let old = cx.current;
cx.current = describing;
cx.focus_with_visibility(false);
let message = if matches!(window_event, WindowEvent::Press { .. }) {
WindowEvent::Press { mouse: false }
} else {
WindowEvent::PressDown { mouse: false }
};
cx.emit_to(describing, message);
cx.current = old;
}
}
}
_ => {}
});
}
}