Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to RichText making it easier to construct layout jobs #3319

Merged
merged 3 commits into from
Sep 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 68 additions & 15 deletions crates/egui/src/widget_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);

Expand Down Expand Up @@ -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<Color32> {
Expand Down