From dbcf15b49e2a85677ca24c71e68955e436b84740 Mon Sep 17 00:00:00 2001 From: Jackson Kruger Date: Sun, 10 Sep 2023 07:06:02 -0500 Subject: [PATCH] Add method to RichText making it easier to construct layout jobs (#3319) * Slightly refactor and expose some parts of rich text to make it easier to construct layout jobs * Cargo fmt * Add documentation --- crates/egui/src/widget_text.rs | 83 ++++++++++++++++++++++++++++------ 1 file changed, 68 insertions(+), 15 deletions(-) diff --git a/crates/egui/src/widget_text.rs b/crates/egui/src/widget_text.rs index 531f3582..fe0de370 100644 --- a/crates/egui/src/widget_text.rs +++ b/crates/egui/src/widget_text.rs @@ -269,12 +269,66 @@ impl RichText { fonts.row_height(&font_id) } + /// Append to an existing [`LayoutJob`] + /// + /// Note that the color of the [`RichText`] must be set, or may default to an undesirable color. + /// + /// ### Example + /// ``` + /// use egui::{Style, RichText, text::LayoutJob, Color32, FontSelection, Align}; + /// + /// let style = Style::default(); + /// let mut layout_job = LayoutJob::default(); + /// RichText::new("Normal") + /// .color(style.visuals.text_color()) + /// .append_to( + /// &mut layout_job, + /// &style, + /// FontSelection::Default, + /// Align::Center, + /// ); + /// RichText::new("Large and underlined") + /// .color(style.visuals.text_color()) + /// .size(20.0) + /// .underline() + /// .append_to( + /// &mut layout_job, + /// &style, + /// FontSelection::Default, + /// Align::Center, + /// ); + /// ``` + pub fn append_to( + self, + layout_job: &mut LayoutJob, + style: &Style, + fallback_font: FontSelection, + default_valign: Align, + ) { + let (text, format) = self.into_text_and_format(style, fallback_font, default_valign); + + layout_job.append(&text, 0.0, format); + } + fn into_text_job( self, style: &Style, fallback_font: FontSelection, default_valign: Align, ) -> WidgetTextJob { + let job_has_color = self.get_text_color(&style.visuals).is_some(); + let (text, text_format) = self.into_text_and_format(style, fallback_font, default_valign); + + let job = LayoutJob::single_section(text, text_format); + WidgetTextJob { job, job_has_color } + } + + fn into_text_and_format( + self, + style: &Style, + fallback_font: FontSelection, + default_valign: Align, + ) -> (String, crate::text::TextFormat) { let text_color = self.get_text_color(&style.visuals); let Self { @@ -295,7 +349,6 @@ impl RichText { raised, } = self; - let job_has_color = text_color.is_some(); let line_color = text_color.unwrap_or_else(|| style.visuals.text_color()); let text_color = text_color.unwrap_or(crate::Color32::TEMPORARY_COLOR); @@ -336,20 +389,20 @@ impl RichText { default_valign }; - let text_format = crate::text::TextFormat { - font_id, - extra_letter_spacing, - line_height, - color: text_color, - background: background_color, - italics, - underline, - strikethrough, - valign, - }; - - let job = LayoutJob::single_section(text, text_format); - WidgetTextJob { job, job_has_color } + ( + text, + crate::text::TextFormat { + font_id, + extra_letter_spacing, + line_height, + color: text_color, + background: background_color, + italics, + underline, + strikethrough, + valign, + }, + ) } fn get_text_color(&self, visuals: &Visuals) -> Option {