Skip to content

Commit 682f86d

Browse files
committed
Convert Painter
1 parent 52df54e commit 682f86d

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

egui/src/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ impl Layout {
767767
painter: &crate::Painter,
768768
region: &Region,
769769
stroke: epaint::Stroke,
770-
text: impl ToString,
770+
text: impl Into<crate::WidgetText>,
771771
) {
772772
let cursor = region.cursor;
773773
let next_pos = self.next_widget_position(region);

egui/src/painter.rs

+13-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
emath::{Align2, Pos2, Rect, Vec2},
33
layers::{LayerId, PaintList, ShapeIdx},
4-
Color32, CtxRef,
4+
Color32, CtxRef, WidgetText,
55
};
66
use epaint::{
77
mutex::Mutex,
@@ -195,41 +195,34 @@ impl Painter {
195195

196196
/// ## Debug painting
197197
impl Painter {
198-
#[allow(clippy::needless_pass_by_value)]
199-
pub fn debug_rect(&mut self, rect: Rect, color: Color32, text: impl ToString) {
198+
pub fn debug_rect(&mut self, rect: Rect, color: Color32, text: impl Into<WidgetText>) {
200199
self.rect_stroke(rect, 0.0, (1.0, color));
201200
let text_style = TextStyle::Monospace;
202-
self.text(
203-
rect.min,
204-
Align2::LEFT_TOP,
205-
text.to_string(),
206-
text_style,
207-
color,
208-
);
201+
self.text(rect.min, Align2::LEFT_TOP, text, text_style, color);
209202
}
210203

211204
pub fn error(&self, pos: Pos2, text: impl std::fmt::Display) -> Rect {
212205
self.debug_text(pos, Align2::LEFT_TOP, Color32::RED, format!("🔥 {}", text))
213206
}
214207

215208
/// text with a background
216-
#[allow(clippy::needless_pass_by_value)]
217209
pub fn debug_text(
218210
&self,
219211
pos: Pos2,
220212
anchor: Align2,
221213
color: Color32,
222-
text: impl ToString,
214+
text: impl Into<WidgetText>,
223215
) -> Rect {
224-
let galley = self.layout_no_wrap(text.to_string(), TextStyle::Monospace, color);
225-
let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size()));
216+
let text = text.into();
217+
let text_galley = text.into_galley_raw(self.ctx(), f32::INFINITY, TextStyle::Monospace);
218+
let rect = anchor.anchor_rect(Rect::from_min_size(pos, text_galley.size()));
226219
let frame_rect = rect.expand(2.0);
227220
self.add(Shape::rect_filled(
228221
frame_rect,
229222
0.0,
230223
Color32::from_black_alpha(240),
231224
));
232-
self.galley(rect.min, galley);
225+
text_galley.paint_with_fallback_color(self, rect.min, color);
233226
frame_rect
234227
}
235228
}
@@ -332,18 +325,18 @@ impl Painter {
332325
/// [`Self::layout`] or [`Self::layout_no_wrap`].
333326
///
334327
/// Returns where the text ended up.
335-
#[allow(clippy::needless_pass_by_value)]
336328
pub fn text(
337329
&self,
338330
pos: Pos2,
339331
anchor: Align2,
340-
text: impl ToString,
332+
text: impl Into<WidgetText>,
341333
text_style: TextStyle,
342334
text_color: Color32,
343335
) -> Rect {
344-
let galley = self.layout_no_wrap(text.to_string(), text_style, text_color);
345-
let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size()));
346-
self.galley(rect.min, galley);
336+
let text = text.into();
337+
let text_galley = text.into_galley_raw(self.ctx(), f32::INFINITY, text_style);
338+
let rect = anchor.anchor_rect(Rect::from_min_size(pos, text_galley.size()));
339+
text_galley.paint_with_fallback_color(self, rect.min, text_color);
347340
rect
348341
}
349342

egui/src/placer.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,11 @@ impl Placer {
262262
}
263263

264264
impl Placer {
265-
pub(crate) fn debug_paint_cursor(&self, painter: &crate::Painter, text: impl ToString) {
265+
pub(crate) fn debug_paint_cursor(
266+
&self,
267+
painter: &crate::Painter,
268+
text: impl Into<crate::WidgetText>,
269+
) {
266270
let stroke = Stroke::new(1.0, Color32::DEBUG_COLOR);
267271

268272
if let Some(grid) = &self.grid {

egui/src/ui.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,7 @@ impl Ui {
17871787

17881788
/// Shows the given text where the next widget is to be placed
17891789
/// if when [`Context::set_debug_on_hover`] has been turned on and the mouse is hovering the Ui.
1790-
pub fn trace_location(&self, text: impl ToString) {
1790+
pub fn trace_location(&self, text: impl Into<crate::WidgetText>) {
17911791
let rect = self.max_rect();
17921792
if self.style().debug.debug_on_hover && self.rect_contains_pointer(rect) {
17931793
self.placer

egui/src/widget_text.rs

+30
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,36 @@ impl WidgetText {
509509
},
510510
}
511511
}
512+
513+
pub fn into_galley_raw(
514+
self,
515+
ctx: &crate::Context,
516+
wrap_width: f32,
517+
default_text_style: TextStyle,
518+
) -> WidgetTextGalley {
519+
match self {
520+
Self::RichText(text) => {
521+
let valign = Align::Center;
522+
let mut text_job = text.into_text_job(&ctx.style(), default_text_style, valign);
523+
text_job.job.wrap_width = wrap_width;
524+
WidgetTextGalley {
525+
galley: ctx.fonts().layout_job(text_job.job),
526+
galley_has_color: text_job.job_has_color,
527+
}
528+
}
529+
Self::LayoutJob(mut job) => {
530+
job.wrap_width = wrap_width;
531+
WidgetTextGalley {
532+
galley: ctx.fonts().layout_job(job),
533+
galley_has_color: true,
534+
}
535+
}
536+
Self::Galley(galley) => WidgetTextGalley {
537+
galley,
538+
galley_has_color: true,
539+
},
540+
}
541+
}
512542
}
513543

514544
impl From<&str> for WidgetText {

0 commit comments

Comments
 (0)