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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
use crate::{widget_text::WidgetTextGalley, *};

/// Static text.
///
/// ```
/// # egui::__run_test_ui(|ui| {
/// ui.label("Equivalent");
/// ui.add(egui::Label::new("Equivalent"));
/// ui.add(egui::Label::new("With Options").wrap(false));
/// ui.label(egui::RichText::new("With formatting").underline());
/// # });
/// ```
#[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
pub struct Label {
    text: WidgetText,
    wrap: Option<bool>,
    sense: Sense,
}

impl Label {
    pub fn new(text: impl Into<WidgetText>) -> Self {
        Self {
            text: text.into(),
            wrap: None,
            sense: Sense::focusable_noninteractive(),
        }
    }

    pub fn text(&self) -> &str {
        self.text.text()
    }

    /// If `true`, the text will wrap to stay within the max width of the `Ui`.
    ///
    /// By default [`Self::wrap`] will be true in vertical layouts
    /// and horizontal layouts with wrapping,
    /// and false on non-wrapping horizontal layouts.
    ///
    /// Note that any `\n` in the text will always produce a new line.
    #[inline]
    pub fn wrap(mut self, wrap: bool) -> Self {
        self.wrap = Some(wrap);
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).text_style(…))"]
    pub fn text_style(mut self, text_style: TextStyle) -> Self {
        self.text = self.text.text_style(text_style);
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).heading())"]
    pub fn heading(mut self) -> Self {
        self.text = self.text.heading();
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).monospace())"]
    pub fn monospace(mut self) -> Self {
        self.text = self.text.monospace();
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).code())"]
    pub fn code(mut self) -> Self {
        self.text = self.text.code();
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).strong())"]
    pub fn strong(mut self) -> Self {
        self.text = self.text.strong();
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).weak())"]
    pub fn weak(mut self) -> Self {
        self.text = self.text.weak();
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).underline())"]
    pub fn underline(mut self) -> Self {
        self.text = self.text.underline();
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).strikethrough())"]
    pub fn strikethrough(mut self) -> Self {
        self.text = self.text.strikethrough();
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).italics())"]
    pub fn italics(mut self) -> Self {
        self.text = self.text.italics();
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).small())"]
    pub fn small(mut self) -> Self {
        self.text = self.text.small();
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).small_raised())"]
    pub fn small_raised(mut self) -> Self {
        self.text = self.text.small_raised();
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).raised())"]
    pub fn raised(mut self) -> Self {
        self.text = self.text.raised();
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).background_color(…))"]
    pub fn background_color(mut self, background_color: impl Into<Color32>) -> Self {
        self.text = self.text.background_color(background_color);
        self
    }

    #[deprecated = "Replaced by Label::new(RichText::new(…).text_color())"]
    pub fn text_color(mut self, text_color: impl Into<Color32>) -> Self {
        self.text = self.text.color(text_color);
        self
    }

    /// Make the label respond to clicks and/or drags.
    ///
    /// By default, a label is inert and does not respond to click or drags.
    /// By calling this you can turn the label into a button of sorts.
    /// This will also give the label the hover-effect of a button, but without the frame.
    ///
    /// ``` rust
    /// # use egui::{Label, Sense};
    /// # egui::__run_test_ui(|ui| {
    /// if ui.add(Label::new("click me").sense(Sense::click())).clicked() {
    ///     /* … */
    /// }
    /// # });
    /// ```
    pub fn sense(mut self, sense: Sense) -> Self {
        self.sense = sense;
        self
    }
}

impl Label {
    /// Do layout and position the galley in the ui, without painting it or adding widget info.
    pub fn layout_in_ui(self, ui: &mut Ui) -> (Pos2, WidgetTextGalley, Response) {
        if let WidgetText::Galley(galley) = self.text {
            // If the user said "use this specific galley", then just use it:
            let (rect, response) = ui.allocate_exact_size(galley.size(), self.sense);
            let pos = match galley.job.halign {
                Align::LEFT => rect.left_top(),
                Align::Center => rect.center_top(),
                Align::RIGHT => rect.right_top(),
            };
            let text_galley = WidgetTextGalley {
                galley,
                galley_has_color: true,
            };
            return (pos, text_galley, response);
        }

        let valign = ui.layout().vertical_align();
        let mut text_job = self.text.into_text_job(ui.style(), TextStyle::Body, valign);

        let should_wrap = self.wrap.unwrap_or_else(|| ui.wrap_text());
        let available_width = ui.available_width();

        if should_wrap
            && ui.layout().main_dir() == Direction::LeftToRight
            && ui.layout().main_wrap()
            && available_width.is_finite()
        {
            // On a wrapping horizontal layout we want text to start after the previous widget,
            // then continue on the line below! This will take some extra work:

            let cursor = ui.cursor();
            let first_row_indentation = available_width - ui.available_size_before_wrap().x;
            egui_assert!(first_row_indentation.is_finite());

            text_job.job.wrap_width = available_width;
            text_job.job.first_row_min_height = cursor.height();
            text_job.job.halign = Align::Min;
            text_job.job.justify = false;
            if let Some(first_section) = text_job.job.sections.first_mut() {
                first_section.leading_space = first_row_indentation;
            }
            let text_galley = text_job.into_galley(ui.fonts());

            let pos = pos2(ui.max_rect().left(), ui.cursor().top());
            assert!(
                !text_galley.galley.rows.is_empty(),
                "Galleys are never empty"
            );
            // collect a response from many rows:
            let rect = text_galley.galley.rows[0]
                .rect
                .translate(vec2(pos.x, pos.y));
            let mut response = ui.allocate_rect(rect, self.sense);
            for row in text_galley.galley.rows.iter().skip(1) {
                let rect = row.rect.translate(vec2(pos.x, pos.y));
                response |= ui.allocate_rect(rect, self.sense);
            }
            (pos, text_galley, response)
        } else {
            if should_wrap {
                text_job.job.wrap_width = available_width;
            } else {
                text_job.job.wrap_width = f32::INFINITY;
            };

            if ui.is_grid() {
                // TODO: remove special Grid hacks like these
                text_job.job.halign = Align::LEFT;
                text_job.job.justify = false;
            } else {
                text_job.job.halign = ui.layout().horizontal_placement();
                text_job.job.justify = ui.layout().horizontal_justify();
            };

            let text_galley = text_job.into_galley(ui.fonts());
            let (rect, response) = ui.allocate_exact_size(text_galley.size(), self.sense);
            let pos = match text_galley.galley.job.halign {
                Align::LEFT => rect.left_top(),
                Align::Center => rect.center_top(),
                Align::RIGHT => rect.right_top(),
            };
            (pos, text_galley, response)
        }
    }
}

impl Widget for Label {
    fn ui(self, ui: &mut Ui) -> Response {
        let (pos, text_galley, response) = self.layout_in_ui(ui);
        response.widget_info(|| WidgetInfo::labeled(WidgetType::Label, text_galley.text()));

        if ui.is_rect_visible(response.rect) {
            let response_color = ui.style().interact(&response).text_color();

            let underline = if response.has_focus() {
                Stroke::new(1.0, response_color)
            } else {
                Stroke::none()
            };

            let override_text_color = if text_galley.galley_has_color {
                None
            } else {
                Some(response_color)
            };

            ui.painter().add(epaint::TextShape {
                pos,
                galley: text_galley.galley,
                override_text_color,
                underline,
                angle: 0.0,
            });
        }

        response
    }
}