kas_core/widgets/
label.rs1use super::adapt::MapAny;
9use crate::event::ConfigCx;
10use crate::geom::Rect;
11use crate::layout::AlignHints;
12use crate::text::format::FormattableText;
13use crate::theme::{SizeCx, Text, TextClass};
14use crate::{Events, Layout, Role, RoleCx, Tile};
15use kas_macros::impl_self;
16use std::fmt::Debug;
17
18#[impl_self]
19mod Label {
20 #[derive(Debug)]
34 #[widget]
35 #[layout(self.text)]
36 pub struct Label<T: FormattableText + 'static = String> {
37 core: widget_core!(),
38 text: Text<T>,
39 }
40
41 impl Self {
42 #[inline]
44 pub fn new(text: T) -> Self {
45 Label {
46 core: Default::default(),
47 text: Text::new(text, TextClass::Label, true),
48 }
49 }
50
51 #[inline]
53 pub fn new_any<A>(text: T) -> MapAny<A, Self> {
54 MapAny::new(Label::new(text))
55 }
56
57 #[inline]
59 pub fn class(&self) -> TextClass {
60 self.text.class()
61 }
62
63 #[inline]
67 pub fn set_class(&mut self, class: TextClass) {
68 self.text.set_class(class);
69 }
70
71 #[inline]
75 pub fn with_class(mut self, class: TextClass) -> Self {
76 self.text.set_class(class);
77 self
78 }
79
80 #[inline]
82 pub fn wrap(&self) -> bool {
83 self.text.wrap()
84 }
85
86 #[inline]
90 pub fn set_wrap(&mut self, wrap: bool) {
91 self.text.set_wrap(wrap);
92 }
93
94 #[inline]
96 pub fn with_wrap(mut self, wrap: bool) -> Self {
97 self.text.set_wrap(wrap);
98 self
99 }
100
101 #[inline]
103 pub fn text(&self) -> &Text<T> {
104 &self.text
105 }
106
107 pub fn set_text(&mut self, cx: &mut ConfigCx, text: T) {
109 self.text.set_text(text);
110 self.text.reprepare_action(cx);
111 }
112
113 pub fn as_str(&self) -> &str {
115 self.text.as_str()
116 }
117 }
118
119 impl Layout for Self {
120 fn set_rect(&mut self, cx: &mut SizeCx, rect: Rect, hints: AlignHints) {
121 self.text
122 .set_rect(cx, rect, hints.combine(AlignHints::VERT_CENTER));
123 }
124 }
125
126 impl Tile for Self {
127 fn role(&self, _: &mut dyn RoleCx) -> Role<'_> {
128 Role::Label(self.text.as_str())
129 }
130 }
131
132 impl Events for Self {
133 type Data = ();
134
135 fn configure(&mut self, cx: &mut ConfigCx) {
136 self.text.configure(&mut cx.size_cx());
137 }
138 }
139
140 impl Label<String> {
141 pub fn set_string(&mut self, cx: &mut ConfigCx, string: String) {
143 if self.text.set_string(string) {
144 self.text.reprepare_action(cx);
145 }
146 }
147 }
148}
149
150impl<T: FormattableText + 'static> From<T> for Label<T> {
159 fn from(text: T) -> Self {
160 Label::new(text)
161 }
162}
163
164impl<'a> From<&'a str> for Label<String> {
165 fn from(text: &'a str) -> Self {
166 Label::new(text.to_string())
167 }
168}