Skip to content

Commit 6cffcce

Browse files
committed
WidgetText in ComboBox
1 parent 2422542 commit 6cffcce

9 files changed

+28
-33
lines changed

egui/src/containers/collapsing_header.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ impl CollapsingHeader {
333333
paint_icon(ui, openness, &icon_response);
334334
}
335335

336-
text.paint(ui, text_pos, &visuals);
336+
text.paint(ui.painter(), text_pos, &visuals);
337337

338338
Prepared {
339339
id,

egui/src/containers/combo_box.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ impl ComboBox {
148148
}
149149
}
150150

151-
#[allow(clippy::needless_pass_by_value)]
152151
fn combo_box_dyn<'c, R>(
153152
ui: &mut Ui,
154153
button_id: Id,
@@ -183,7 +182,7 @@ fn combo_box_dyn<'c, R>(
183182
paint_icon(ui.painter(), icon_rect.expand(visuals.expansion), visuals);
184183

185184
let text_rect = Align2::LEFT_CENTER.align_size_within_rect(galley.size(), rect);
186-
galley.paint(ui, text_rect.min, visuals);
185+
galley.paint(ui.painter(), text_rect.min, visuals);
187186
});
188187

189188
if button_response.clicked() {

egui/src/containers/window.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ impl TitleBar {
832832
let text_pos = text_pos - self.title_galley.galley().rect.min.to_vec2();
833833
let text_pos = text_pos - 1.5 * Vec2::Y; // HACK: center on x-height of text (looks better)
834834
self.title_galley
835-
.paint_with_color(ui, text_pos, ui.visuals().text_color());
835+
.paint_with_color(ui.painter(), text_pos, ui.visuals().text_color());
836836

837837
if let Some(content_response) = &content_response {
838838
// paint separator between title and content:

egui/src/menu.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -442,8 +442,8 @@ impl SubMenuButton {
442442
);
443443

444444
let text_color = visuals.text_color();
445-
text_galley.paint_with_color(ui, text_pos, text_color);
446-
icon_galley.paint_with_color(ui, icon_pos, text_color);
445+
text_galley.paint_with_color(ui.painter(), text_pos, text_color);
446+
icon_galley.paint_with_color(ui.painter(), icon_pos, text_color);
447447
}
448448
response
449449
}

egui/src/widget_text.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -583,17 +583,15 @@ impl WidgetTextGalley {
583583
&self.galley
584584
}
585585

586-
pub fn paint(self, ui: &Ui, text_pos: Pos2, visuals: &WidgetVisuals) {
586+
pub fn paint(self, painter: &crate::Painter, text_pos: Pos2, visuals: &WidgetVisuals) {
587587
if self.galley_has_color {
588-
ui.painter().galley(text_pos, self.galley);
588+
painter.galley(text_pos, self.galley);
589589
} else {
590-
ui.painter()
591-
.galley_with_color(text_pos, self.galley, visuals.text_color());
590+
painter.galley_with_color(text_pos, self.galley, visuals.text_color());
592591
}
593592
}
594593

595-
pub fn paint_with_color(self, ui: &Ui, text_pos: Pos2, text_color: Color32) {
596-
ui.painter()
597-
.galley_with_color(text_pos, self.galley, text_color);
594+
pub fn paint_with_color(self, painter: &crate::Painter, text_pos: Pos2, text_color: Color32) {
595+
painter.galley_with_color(text_pos, self.galley, text_color);
598596
}
599597
}

egui/src/widgets/button.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl Widget for Button {
162162
);
163163
}
164164

165-
text.paint(ui, text_pos, visuals);
165+
text.paint(ui.painter(), text_pos, visuals);
166166
}
167167

168168
response
@@ -260,7 +260,7 @@ impl<'a> Widget for Checkbox<'a> {
260260
));
261261
}
262262

263-
text.paint(ui, text_pos, visuals);
263+
text.paint(ui.painter(), text_pos, visuals);
264264
response
265265
}
266266
}
@@ -360,7 +360,7 @@ impl Widget for RadioButton {
360360
});
361361
}
362362

363-
text.paint(ui, text_pos, visuals);
363+
text.paint(ui.painter(), text_pos, visuals);
364364
response
365365
}
366366
}

egui/src/widgets/label.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use crate::{widget_text::WidgetTextGalley, *};
66
/// # let ui = &mut egui::Ui::__test();
77
/// ui.label("Equivalent");
88
/// ui.add(egui::Label::new("Equivalent"));
9-
/// ui.add(egui::Label::new("With Options").text_color(egui::Color32::RED));
9+
/// ui.add(egui::Label::new("With Options").wrap(false));
10+
/// ui.label(egui::RichText::new("With formatting").underline());
1011
/// ```
1112
#[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
1213
pub struct Label {

egui/src/widgets/progress_bar.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::*;
22

33
enum ProgressBarText {
4-
Custom(String),
4+
Custom(WidgetText),
55
Percentage,
66
}
77

@@ -31,9 +31,8 @@ impl ProgressBar {
3131
}
3232

3333
/// A custom text to display on the progress bar.
34-
#[allow(clippy::needless_pass_by_value)]
35-
pub fn text(mut self, text: impl ToString) -> Self {
36-
self.text = Some(ProgressBarText::Custom(text.to_string()));
34+
pub fn text(mut self, text: impl Into<WidgetText>) -> Self {
35+
self.text = Some(ProgressBarText::Custom(text.into()));
3736
self
3837
}
3938

@@ -124,18 +123,16 @@ impl Widget for ProgressBar {
124123

125124
if let Some(text_kind) = text {
126125
let text = match text_kind {
127-
ProgressBarText::Custom(string) => string,
128-
ProgressBarText::Percentage => format!("{}%", (progress * 100.0) as usize),
126+
ProgressBarText::Custom(text) => text,
127+
ProgressBarText::Percentage => format!("{}%", (progress * 100.0) as usize).into(),
129128
};
130-
ui.painter().sub_region(outer_rect).text(
131-
outer_rect.left_center() + vec2(ui.spacing().item_spacing.x, 0.0),
132-
Align2::LEFT_CENTER,
133-
text,
134-
TextStyle::Button,
135-
visuals
136-
.override_text_color
137-
.unwrap_or(visuals.selection.stroke.color),
138-
);
129+
let galley = text.into_galley(ui, Some(false), f32::INFINITY, TextStyle::Button);
130+
let text_pos = outer_rect.left_center() - Vec2::new(0.0, galley.size().y / 2.0)
131+
+ vec2(ui.spacing().item_spacing.x, 0.0);
132+
let text_color = visuals
133+
.override_text_color
134+
.unwrap_or(visuals.selection.stroke.color);
135+
galley.paint_with_color(&ui.painter().sub_region(outer_rect), text_pos, text_color);
139136
}
140137

141138
response

egui/src/widgets/selected_label.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl Widget for SelectableLabel {
7373
.rect(rect, corner_radius, visuals.bg_fill, visuals.bg_stroke);
7474
}
7575

76-
text.paint(ui, text_pos, &visuals);
76+
text.paint(ui.painter(), text_pos, &visuals);
7777
response
7878
}
7979
}

0 commit comments

Comments
 (0)