use egui::{Frame, Margin, Response, RichText, Role, Stroke, Ui, WidgetInfo};
use crate::components::Component;
use crate::components::style::Tone;
const HEIGHT: f32 = 20.0;
#[derive(Clone, Copy, Debug)]
pub struct Badge<'a> {
label: &'a str,
tone: Tone,
dot: bool,
radius: Option<u8>,
}
impl<'a> Badge<'a> {
pub fn new(label: &'a str) -> Self {
Self {
label,
tone: Tone::Neutral,
dot: false,
radius: None,
}
}
pub fn tone(mut self, tone: Tone) -> Self {
self.tone = tone;
self
}
pub fn radius(mut self, radius: u8) -> Self {
self.radius = Some(radius);
self
}
pub fn dot(mut self, dot: bool) -> Self {
self.dot = dot;
self
}
}
impl Component for Badge<'_> {
type Output = Response;
fn show(self, ui: &mut Ui) -> Response {
let tone = self.tone;
let color = tone.text(ui.visuals());
let fill = tone.fill(ui.visuals());
let frame = Frame::new()
.fill(fill)
.stroke(Stroke::new(
crate::style::get(ui.ctx()).border_width,
if tone == Tone::Neutral {
ui.visuals().window_stroke.color
} else {
color.gamma_multiply(0.35)
},
))
.corner_radius(
self.radius
.map_or_else(|| crate::style::get(ui.ctx()).pill(HEIGHT), egui::CornerRadius::same),
)
.inner_margin(Margin::symmetric(8, 2));
let response = frame
.show(ui, |ui| {
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x = 5.0;
ui.set_min_height(16.0);
if self.dot {
let (rect, _) = ui.allocate_exact_size(egui::vec2(5.0, 5.0), egui::Sense::hover());
ui.painter().circle_filled(rect.center(), 2.5, color);
}
ui.add(
egui::Label::new(
RichText::new(self.label)
.size(crate::style::get(ui.ctx()).font_size(11.0))
.color(color),
)
.selectable(false),
);
});
})
.response;
response.widget_info(|| WidgetInfo::labeled(Role::Label, true, self.label));
response
}
}