diff --git a/CHANGELOG.md b/CHANGELOG.md index 723be8b8..f4541c6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w * Added `Plot::x_grid_spacer` and `Plot::y_grid_spacer` for custom grid spacing ([#1180](https://github.com/emilk/egui/pull/1180)). * Added `Ui::spinner()` shortcut method ([#1494](https://github.com/emilk/egui/pull/1494)). * Added `CursorIcon`s for resizing columns, rows, and the eight cardinal directions. +* Added `Ui::toggle_value`. ### Changed 🔧 * `ClippedMesh` has been replaced with `ClippedPrimitive` ([#1351](https://github.com/emilk/egui/pull/1351)). diff --git a/egui/src/ui.rs b/egui/src/ui.rs index 83e52639..5fb7ac69 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -1368,11 +1368,26 @@ impl Ui { } /// Show a checkbox. + /// + /// See also [`Self::toggle_value`]. #[inline] pub fn checkbox(&mut self, checked: &mut bool, text: impl Into) -> Response { Checkbox::new(checked, text).ui(self) } + /// Acts like a checkbox, but looks like a [`SelectableValue`]. + /// + /// Click to toggle to bool. + /// + /// See also [`Self::checkbox`]. + pub fn toggle_value(&mut self, selected: &mut bool, text: impl Into) -> Response { + let response = self.selectable_label(*selected, text); + if response.clicked() { + *selected = !*selected; + } + response + } + /// Show a [`RadioButton`]. /// Often you want to use [`Self::radio_value`] instead. #[must_use = "You should check if the user clicked this with `if ui.radio(…).clicked() { … } "] @@ -1416,7 +1431,7 @@ impl Ui { /// Show a label which can be selected or not. /// - /// See also [`SelectableLabel`]. + /// See also [`SelectableLabel`] and [`Self::toggle_value`]. #[must_use = "You should check if the user clicked this with `if ui.selectable_label(…).clicked() { … } "] pub fn selectable_label(&mut self, checked: bool, text: impl Into) -> Response { SelectableLabel::new(checked, text).ui(self) @@ -1427,7 +1442,7 @@ impl Ui { /// /// Example: `ui.selectable_value(&mut my_enum, Enum::Alternative, "Alternative")`. /// - /// See also [`SelectableLabel`]. + /// See also [`SelectableLabel`] and [`Self::toggle_value`]. pub fn selectable_value( &mut self, current_value: &mut Value,