feat: exposed label format

This commit is contained in:
2025-02-04 09:17:23 +03:00
parent b141db8cd4
commit 19b0ff5405
2 changed files with 11 additions and 3 deletions

View File

@@ -115,7 +115,8 @@ impl eframe::App for KnobExample {
egui::Color32::from_rgb(50, 220, 50), egui::Color32::from_rgb(50, 220, 50),
egui::Color32::from_rgb(100, 255, 100), egui::Color32::from_rgb(100, 255, 100),
) )
.with_size(50.0), .with_size(50.0)
.with_label_format(|v| format!("{:.2}%", v)),
); );
ui.add( ui.add(

View File

@@ -26,6 +26,7 @@ pub struct Knob<'a> {
label_position: LabelPosition, label_position: LabelPosition,
style: KnobStyle, style: KnobStyle,
label_offset: f32, label_offset: f32,
label_format: Box<dyn Fn(f32) -> String>,
} }
impl<'a> Knob<'a> { impl<'a> Knob<'a> {
@@ -44,6 +45,7 @@ impl<'a> Knob<'a> {
label_position: LabelPosition::Bottom, label_position: LabelPosition::Bottom,
style, style,
label_offset: 2.0, label_offset: 2.0,
label_format: Box::new(|v| format!("{:.2}", v)),
} }
} }
@@ -84,6 +86,11 @@ impl<'a> Knob<'a> {
self.label_offset = offset; self.label_offset = offset;
self self
} }
pub fn with_label_format(mut self, format: impl Fn(f32) -> String + 'static) -> Self {
self.label_format = Box::new(format);
self
}
} }
impl Widget for Knob<'_> { impl Widget for Knob<'_> {
@@ -94,7 +101,7 @@ impl Widget for Knob<'_> {
let font_id = egui::FontId::proportional(self.font_size); let font_id = egui::FontId::proportional(self.font_size);
ui.painter() ui.painter()
.layout( .layout(
format!("{}: {:.2}", label, self.value), format!("{}: {}", label, (self.label_format)(*self.value)),
font_id, font_id,
Color32::WHITE, Color32::WHITE,
f32::INFINITY, f32::INFINITY,
@@ -169,7 +176,7 @@ impl Widget for Knob<'_> {
} }
if let Some(label) = self.label { if let Some(label) = self.label {
let label_text = format!("{label}: {:.2}", self.value); let label_text = format!("{}: {}", label, (self.label_format)(*self.value));
let font_id = egui::FontId::proportional(self.font_size); let font_id = egui::FontId::proportional(self.font_size);
let (label_pos, alignment) = match self.label_position { let (label_pos, alignment) = match self.label_position {