Label: draw underline and strikethrough on top of text

This commit is contained in:
Emil Ernerfeldt 2021-01-31 00:41:26 +01:00
parent 14e0963e50
commit 17fdd3bb10
2 changed files with 24 additions and 17 deletions

View file

@ -117,10 +117,12 @@ impl Painter {
} }
pub fn extend(&self, shapes: Vec<Shape>) { pub fn extend(&self, shapes: Vec<Shape>) {
self.ctx if !shapes.is_empty() {
.graphics() self.ctx
.list(self.layer_id) .graphics()
.extend(self.clip_rect, shapes); .list(self.layer_id)
.extend(self.clip_rect, shapes);
}
} }
/// Modify an existing [`Shape`]. /// Modify an existing [`Shape`].

View file

@ -168,33 +168,38 @@ impl Label {
background_color = ui.style().visuals.code_bg_color; background_color = ui.style().visuals.code_bg_color;
} }
let mut lines = vec![];
if strikethrough || underline || background_color != Color32::TRANSPARENT { if strikethrough || underline || background_color != Color32::TRANSPARENT {
for row in &galley.rows { for row in &galley.rows {
let rect = row.rect().translate(pos.to_vec2()); 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 { if background_color != Color32::TRANSPARENT {
let rect = rect.expand(1.0); // looks better let rect = rect.expand(1.0); // looks better
ui.painter().rect_filled(rect, 0.0, background_color); 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()); let text_style = self.text_style_or_default(ui.style());
ui.painter() ui.painter()
.galley_with_italics(pos, galley, text_style, text_color, italics); .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 /// Read the text style, or get the default for the current style