pub struct CheckboxEnabled<'a, W> {
on: &'a mut bool,
widget: W,
width: Option<f32>,
}
impl<'a, W: egui::Widget> CheckboxEnabled<'a, W> {
pub fn new(on: &'a mut bool, widget: W) -> Self {
Self {
on,
widget,
width: None,
}
}
pub fn width(mut self, width: f32) -> Self {
self.width = Some(width);
self
}
}
impl<W: egui::Widget> egui::Widget for CheckboxEnabled<'_, W> {
fn ui(self, ui: &mut egui::Ui) -> egui::Response {
let Self { on, widget, width } = self;
ui.horizontal(|ui| {
ui.spacing_mut().item_spacing.x *= 0.25;
let checkbox = ui.checkbox(on, "");
let enabled = *on;
let inner = ui
.add_enabled_ui(enabled, |ui| match width {
Some(w) => ui.add_sized([w, ui.spacing().interact_size.y], widget),
None => ui.add(widget),
})
.inner;
checkbox.union(inner)
})
.inner
}
}