Add Ui::toggle_value

This commit is contained in:
Emil Ernerfeldt 2022-04-26 21:36:22 +02:00
parent 4d2eb5b71e
commit 2fd20308e5
2 changed files with 18 additions and 2 deletions

View file

@ -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)).

View file

@ -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<WidgetText>) -> 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<WidgetText>) -> 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<WidgetText>) -> 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<Value: PartialEq>(
&mut self,
current_value: &mut Value,