-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce
ButtonContent
and use it in the selection panel (#6720)
### What ☝🏻 - Fixes #6707 ![Export-1719924918702](https://github.com/rerun-io/rerun/assets/49431240/de8637f7-6db1-425a-9fba-38f30a8f95e8) ### Checklist * [x] I have read and agree to [Contributor Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and the [Code of Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md) * [x] I've included a screenshot or gif (if applicable) * [x] I have tested the web demo (if applicable): * Using examples from latest `main` build: [rerun.io/viewer](https://rerun.io/viewer/pr/6720?manifest_url=https://app.rerun.io/version/main/examples_manifest.json) * Using full set of examples from `nightly` build: [rerun.io/viewer](https://rerun.io/viewer/pr/6720?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json) * [x] The PR title and labels are set such as to maximize their usefulness for the next release's CHANGELOG * [x] If applicable, add a new check to the [release checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)! - [PR Build Summary](https://build.rerun.io/pr/6720) - [Recent benchmark results](https://build.rerun.io/graphs/crates.html) - [Wasm size tracking](https://build.rerun.io/graphs/sizes.html) To run all checks from `main`, comment on the PR with `@rerun-bot full-check`.
- Loading branch information
Showing
3 changed files
with
113 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use crate::list_item::{ContentContext, ListItemContent}; | ||
|
||
/// Simple [`ListItemContent`] to easily display a button in a [`crate::list_item::ListItem`]-based UI. | ||
pub struct ButtonContent<'a> { | ||
label: egui::WidgetText, | ||
enabled: bool, | ||
on_click: Option<Box<dyn FnOnce() + 'a>>, | ||
hover_text: Option<String>, | ||
} | ||
|
||
impl<'a> ButtonContent<'a> { | ||
#[must_use] | ||
pub fn new(label: impl Into<egui::WidgetText>) -> Self { | ||
Self { | ||
label: label.into(), | ||
enabled: true, | ||
on_click: None, | ||
hover_text: None, | ||
} | ||
} | ||
|
||
/// Sets whether the button is enabled. | ||
#[inline] | ||
pub fn enabled(mut self, enabled: bool) -> Self { | ||
self.enabled = enabled; | ||
self | ||
} | ||
|
||
/// Called when the button is clicked. | ||
#[inline] | ||
pub fn on_click(mut self, on_click: impl FnOnce() + 'a) -> Self { | ||
self.on_click = Some(Box::new(on_click)); | ||
self | ||
} | ||
|
||
/// Sets the hover text of the button. | ||
#[inline] | ||
pub fn hover_text(mut self, hover_text: impl Into<String>) -> Self { | ||
self.hover_text = Some(hover_text.into()); | ||
self | ||
} | ||
} | ||
|
||
impl ListItemContent for ButtonContent<'_> { | ||
fn ui(self: Box<Self>, ui: &mut egui::Ui, context: &ContentContext<'_>) { | ||
let Self { | ||
label, | ||
enabled, | ||
on_click, | ||
hover_text, | ||
} = *self; | ||
|
||
let mut ui = ui.child_ui( | ||
context.rect, | ||
egui::Layout::left_to_right(egui::Align::Center), | ||
None, | ||
); | ||
|
||
// Compensate for the button padding such that the button text is aligned with other list | ||
// item contents. | ||
ui.add_space(-ui.spacing().button_padding.x); | ||
|
||
let response = ui.add_enabled(enabled, egui::Button::new(label)); | ||
if let Some(on_click) = on_click { | ||
if response.clicked() { | ||
on_click(); | ||
} | ||
} | ||
|
||
if let Some(hover_text) = hover_text { | ||
response.on_hover_text(hover_text); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters