From 17fdd3bb10e47183f6910c6d64cd15ad24f12454 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 31 Jan 2021 00:41:26 +0100 Subject: [PATCH] Label: draw underline and strikethrough on top of text --- egui/src/painter.rs | 10 ++++++---- egui/src/widgets/label.rs | 31 ++++++++++++++++++------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/egui/src/painter.rs b/egui/src/painter.rs index 14051a77..e6f368c1 100644 --- a/egui/src/painter.rs +++ b/egui/src/painter.rs @@ -117,10 +117,12 @@ impl Painter { } pub fn extend(&self, shapes: Vec) { - self.ctx - .graphics() - .list(self.layer_id) - .extend(self.clip_rect, shapes); + if !shapes.is_empty() { + self.ctx + .graphics() + .list(self.layer_id) + .extend(self.clip_rect, shapes); + } } /// Modify an existing [`Shape`]. diff --git a/egui/src/widgets/label.rs b/egui/src/widgets/label.rs index 0004af51..9d0092e3 100644 --- a/egui/src/widgets/label.rs +++ b/egui/src/widgets/label.rs @@ -168,33 +168,38 @@ impl Label { background_color = ui.style().visuals.code_bg_color; } + let mut lines = vec![]; + if strikethrough || underline || background_color != Color32::TRANSPARENT { for row in &galley.rows { let rect = row.rect().translate(pos.to_vec2()); - let stroke_width = 1.0; - if strikethrough { - ui.painter().line_segment( - [rect.left_center(), rect.right_center()], - (stroke_width, text_color), - ); - } - if underline { - ui.painter().line_segment( - [rect.left_bottom(), rect.right_bottom()], - (stroke_width, text_color), - ); - } if background_color != Color32::TRANSPARENT { let rect = rect.expand(1.0); // looks better ui.painter().rect_filled(rect, 0.0, background_color); } + + let stroke_width = 1.0; + if strikethrough { + lines.push(Shape::line_segment( + [rect.left_center(), rect.right_center()], + (stroke_width, text_color), + )); + } + if underline { + lines.push(Shape::line_segment( + [rect.left_bottom(), rect.right_bottom()], + (stroke_width, text_color), + )); + } } } let text_style = self.text_style_or_default(ui.style()); ui.painter() .galley_with_italics(pos, galley, text_style, text_color, italics); + + ui.painter().extend(lines); } /// Read the text style, or get the default for the current style