mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 04:40:03 -04:00
Fix checkbox borrowing checked for too long
This commit is contained in:
@@ -19,16 +19,20 @@ use crate::{
|
|||||||
/// ui.add(egui::Checkbox::new(&mut my_bool, "Checked"));
|
/// ui.add(egui::Checkbox::new(&mut my_bool, "Checked"));
|
||||||
/// # });
|
/// # });
|
||||||
/// ```
|
/// ```
|
||||||
|
///
|
||||||
|
/// The borrow of `checked` and the borrows inside the atoms have separate lifetimes, so the
|
||||||
|
/// `&mut bool` is released as soon as the checkbox is added, even when the [`Atoms`] have to
|
||||||
|
/// live on inside an [`AtomLayout`].
|
||||||
#[must_use = "You should put this widget in a ui with `ui.add(widget);`"]
|
#[must_use = "You should put this widget in a ui with `ui.add(widget);`"]
|
||||||
pub struct Checkbox<'a> {
|
pub struct Checkbox<'checked, 'atoms> {
|
||||||
checked: &'a mut bool,
|
checked: &'checked mut bool,
|
||||||
atoms: Atoms<'a>,
|
atoms: Atoms<'atoms>,
|
||||||
indeterminate: bool,
|
indeterminate: bool,
|
||||||
classes: Classes,
|
classes: Classes,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Checkbox<'a> {
|
impl<'checked, 'atoms> Checkbox<'checked, 'atoms> {
|
||||||
pub fn new(checked: &'a mut bool, atoms: impl IntoAtoms<'a>) -> Self {
|
pub fn new(checked: &'checked mut bool, atoms: impl IntoAtoms<'atoms>) -> Self {
|
||||||
Checkbox {
|
Checkbox {
|
||||||
checked,
|
checked,
|
||||||
atoms: atoms.into_atoms(),
|
atoms: atoms.into_atoms(),
|
||||||
@@ -37,14 +41,14 @@ impl<'a> Checkbox<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn without_text(checked: &'a mut bool) -> Self {
|
pub fn without_text(checked: &'checked mut bool) -> Self {
|
||||||
Self::new(checked, ())
|
Self::new(checked, ())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Output the checkbox's [`Atoms`].
|
/// Output the checkbox's [`Atoms`].
|
||||||
///
|
///
|
||||||
/// This includes any images you have on the checkbox.
|
/// This includes any images you have on the checkbox.
|
||||||
pub fn atoms(&self) -> &Atoms<'a> {
|
pub fn atoms(&self) -> &Atoms<'atoms> {
|
||||||
&self.atoms
|
&self.atoms
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +63,7 @@ impl<'a> Checkbox<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> AtomWidget<'a> for Checkbox<'a> {
|
impl<'a> AtomWidget<'a> for Checkbox<'_, 'a> {
|
||||||
fn atom_ui(self, ui: &mut AtomWidgetContext, response: &mut Response) -> AtomLayout<'a> {
|
fn atom_ui(self, ui: &mut AtomWidgetContext, response: &mut Response) -> AtomLayout<'a> {
|
||||||
let Checkbox {
|
let Checkbox {
|
||||||
checked,
|
checked,
|
||||||
@@ -152,9 +156,9 @@ impl<'a> AtomWidget<'a> for Checkbox<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_widget_for_atom_widget!(Checkbox<'_>);
|
impl_widget_for_atom_widget!(Checkbox<'_, '_>);
|
||||||
|
|
||||||
impl HasClasses for Checkbox<'_> {
|
impl HasClasses for Checkbox<'_, '_> {
|
||||||
fn classes(&self) -> &Classes {
|
fn classes(&self) -> &Classes {
|
||||||
&self.classes
|
&self.classes
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user