1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-31 13:50:04 -04:00

Rich text for all widgets (#855)

Introduce `RichText` and `WidgetText`
This commit is contained in:
Emil Ernerfeldt
2021-11-01 21:30:10 +01:00
committed by GitHub
parent 9378cd5c6e
commit 09b8269326
30 changed files with 1121 additions and 712 deletions

View File

@@ -42,13 +42,10 @@ impl super::View for FontBook {
ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing.x = 0.0;
ui.label("You can add more characters by installing additional fonts with ");
ui.add(
egui::Hyperlink::from_label_and_url(
"Context::set_fonts",
"https://docs.rs/egui/latest/egui/struct.Context.html#method.set_fonts",
)
.text_style(egui::TextStyle::Monospace),
);
ui.add(egui::Hyperlink::from_label_and_url(
egui::RichText::new("Context::set_fonts").text_style(egui::TextStyle::Monospace),
"https://docs.rs/egui/latest/egui/struct.Context.html#method.set_fonts",
));
ui.label(".");
});
@@ -90,10 +87,13 @@ impl super::View for FontBook {
for (&chr, name) in named_chars {
if filter.is_empty() || name.contains(filter) || *filter == chr.to_string() {
let button = egui::Button::new(chr).text_style(text_style).frame(false);
let button = egui::Button::new(
egui::RichText::new(chr.to_string()).text_style(text_style),
)
.frame(false);
let tooltip_ui = |ui: &mut egui::Ui| {
ui.add(egui::Label::new(chr).text_style(text_style));
ui.label(egui::RichText::new(chr.to_string()).text_style(text_style));
ui.label(format!("{}\nU+{:X}\n\nClick to copy", name, chr as u32));
};

View File

@@ -142,7 +142,7 @@ impl Widgets {
// Trick so we don't have to add spaces in the text below:
ui.spacing_mut().item_spacing.x = ui.fonts()[TextStyle::Body].glyph_width(' ');
ui.add(Label::new("Text can have").text_color(Color32::from_rgb(110, 255, 110)));
ui.label(RichText::new("Text can have").color(Color32::from_rgb(110, 255, 110)));
ui.colored_label(Color32::from_rgb(128, 140, 255), "color"); // Shortcut version
ui.label("and tooltips.").on_hover_text(
"This is a multiline tooltip that demonstrates that you can easily add tooltips to any element.\nThis is the second line.\nThis is the third.",
@@ -365,7 +365,7 @@ impl SubTree {
) -> Action {
if depth > 0
&& ui
.add(Button::new("delete").text_color(Color32::RED))
.button(RichText::new("delete").color(Color32::RED))
.clicked()
{
return Action::Delete;
@@ -565,12 +565,7 @@ fn text_layout_ui(ui: &mut egui::Ui) {
},
);
job.wrap_width = ui.available_width();
let galley = ui.fonts().layout_job(job);
let (response, painter) = ui.allocate_painter(galley.size(), Sense::hover());
painter.add(Shape::galley(response.rect.min, galley));
ui.label(job);
ui.vertical_centered(|ui| {
ui.add(crate::__egui_github_link_file_line!());

View File

@@ -465,7 +465,7 @@ fn lorem_ipsum(ui: &mut egui::Ui, text: &str) {
ui.with_layout(
egui::Layout::top_down(egui::Align::LEFT).with_cross_justify(true),
|ui| {
ui.add(egui::Label::new(text).weak());
ui.label(egui::RichText::new(text).weak());
},
);
}

View File

@@ -84,7 +84,7 @@ fn lorem_ipsum(ui: &mut egui::Ui) {
ui.with_layout(
egui::Layout::top_down(egui::Align::LEFT).with_cross_justify(true),
|ui| {
ui.add(egui::Label::new(crate::LOREM_IPSUM_LONG).small().weak());
ui.label(egui::RichText::new(crate::LOREM_IPSUM_LONG).small().weak());
},
);
}

View File

@@ -116,9 +116,9 @@ impl epi::App for HttpApp {
}
Err(error) => {
// This should only happen if the fetch API isn't available or something similar.
ui.add(
egui::Label::new(if error.is_empty() { "Error" } else { error })
.text_color(egui::Color32::RED),
ui.colored_label(
egui::Color32::RED,
if error.is_empty() { "Error" } else { error },
);
}
}

View File

@@ -39,10 +39,10 @@ pub fn item_ui(ui: &mut Ui, item: easy_mark::Item<'_>) {
}
easy_mark::Item::Text(style, text) => {
ui.add(label_from_style(text, &style));
ui.label(rich_text_from_style(text, &style));
}
easy_mark::Item::Hyperlink(style, text, url) => {
let label = label_from_style(text, &style);
let label = rich_text_from_style(text, &style);
ui.add(Hyperlink::from_label_and_url(label, url));
}
@@ -87,7 +87,7 @@ pub fn item_ui(ui: &mut Ui, item: easy_mark::Item<'_>) {
};
}
fn label_from_style(text: &str, style: &easy_mark::Style) -> Label {
fn rich_text_from_style(text: &str, style: &easy_mark::Style) -> RichText {
let easy_mark::Style {
heading,
quoted,
@@ -102,34 +102,34 @@ fn label_from_style(text: &str, style: &easy_mark::Style) -> Label {
let small = small || raised; // Raised text is also smaller
let mut label = Label::new(text);
let mut rich_text = RichText::new(text);
if heading && !small {
label = label.heading().strong();
rich_text = rich_text.heading().strong();
}
if small && !heading {
label = label.small();
rich_text = rich_text.small();
}
if code {
label = label.code();
rich_text = rich_text.code();
}
if strong {
label = label.strong();
rich_text = rich_text.strong();
} else if quoted {
label = label.weak();
rich_text = rich_text.weak();
}
if underline {
label = label.underline();
rich_text = rich_text.underline();
}
if strikethrough {
label = label.strikethrough();
rich_text = rich_text.strikethrough();
}
if italics {
label = label.italics();
rich_text = rich_text.italics();
}
if raised {
label = label.raised();
rich_text = rich_text.raised();
}
label
rich_text
}
fn bullet_point(ui: &mut Ui, width: f32) -> Response {

View File

@@ -105,7 +105,10 @@ macro_rules! __egui_github_link_file {
crate::__egui_github_link_file!("(source code)")
};
($label: expr) => {
egui::github_link_file!("https://github.com/emilk/egui/blob/master/", $label).small()
egui::github_link_file!(
"https://github.com/emilk/egui/blob/master/",
egui::RichText::new($label).small()
)
};
}
@@ -117,7 +120,10 @@ macro_rules! __egui_github_link_file_line {
crate::__egui_github_link_file_line!("(source code)")
};
($label: expr) => {
egui::github_link_file_line!("https://github.com/emilk/egui/blob/master/", $label).small()
egui::github_link_file_line!(
"https://github.com/emilk/egui/blob/master/",
egui::RichText::new($label).small()
)
};
}

View File

@@ -237,5 +237,5 @@ fn clock_button(ui: &mut egui::Ui, seconds_since_midnight: f64) -> egui::Respons
(time % 1.0 * 100.0).floor()
);
ui.add(egui::Button::new(time).text_style(egui::TextStyle::Monospace))
ui.button(egui::RichText::new(time).monospace())
}