Add ability to override collapsing icons (#1147)

This commit is contained in:
Juan Campa 2022-01-22 04:32:02 -05:00 committed by GitHub
parent e957674467
commit 343f7da564
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 3 deletions

View file

@ -10,6 +10,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
### Added ⭐
* `Context::load_texture` to convert an image into a texture which can be displayed using e.g. `ui.image(texture, size)` ([#1110](https://github.com/emilk/egui/pull/1110)).
* Added `Ui::add_visible` and `Ui::add_visible_ui`.
* Added `CollapsingHeader::icon` to override the default open/close icon using a custom function. ([1147](https://github.com/emilk/egui/pull/1147))
### Changed 🔧
* ⚠️ `Context::input` and `Ui::input` now locks a mutex. This can lead to a dead-lock is used in an `if let` binding!
@ -26,6 +27,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
### Contributors 🙏
* [danielkeller](https://github.com/danielkeller): [#1050](https://github.com/emilk/egui/pull/1050).
* [juancampa](https://github.com/juancampa): [#1147](https://github.com/emilk/egui/pull/1147).
## 0.16.1 - 2021-12-31 - Add back `CtxRef::begin_frame,end_frame`

View file

@ -98,7 +98,7 @@ impl State {
}
/// Paint the arrow icon that indicated if the region is open or not
pub(crate) fn paint_icon(ui: &mut Ui, openness: f32, response: &Response) {
pub(crate) fn paint_default_icon(ui: &mut Ui, openness: f32, response: &Response) {
let visuals = ui.style().interact(response);
let stroke = visuals.fg_stroke;
@ -117,6 +117,9 @@ pub(crate) fn paint_icon(ui: &mut Ui, openness: f32, response: &Response) {
ui.painter().add(Shape::closed_line(points, stroke));
}
/// A function that paints an icon indicating if the region is open or not
pub type IconPainter = Box<dyn FnOnce(&mut Ui, f32, &Response)>;
/// A header which can be collapsed/expanded, revealing a contained [`Ui`] region.
///
///
@ -141,6 +144,7 @@ pub struct CollapsingHeader {
selectable: bool,
selected: bool,
show_background: bool,
icon: Option<IconPainter>,
}
impl CollapsingHeader {
@ -162,6 +166,7 @@ impl CollapsingHeader {
selectable: false,
selected: false,
show_background: false,
icon: None,
}
}
@ -236,6 +241,28 @@ impl CollapsingHeader {
self.show_background = show_background;
self
}
/// Use the provided function to render a different `CollapsingHeader` icon.
/// Defaults to a triangle that animates as the `CollapsingHeader` opens and closes.
///
/// For example:
/// ```
/// # egui::__run_test_ui(|ui| {
/// fn circle_icon(ui: &mut egui::Ui, openness: f32, response: &egui::Response) {
/// let stroke = ui.style().interact(&response).fg_stroke;
/// let radius = egui::lerp(2.0..=3.0, openness);
/// ui.painter().circle_filled(response.rect.center(), radius, stroke.color);
/// }
///
/// egui::CollapsingHeader::new("Circles")
/// .icon(circle_icon)
/// .show(ui, |ui| { ui.label("Hi!"); });
/// # });
/// ```
pub fn icon(mut self, icon_fn: impl FnOnce(&mut Ui, f32, &Response) + 'static) -> Self {
self.icon = Some(Box::new(icon_fn));
self
}
}
struct Prepared {
@ -251,6 +278,7 @@ impl CollapsingHeader {
"Horizontal collapsing is unimplemented"
);
let Self {
icon,
text,
default_open,
open,
@ -341,7 +369,11 @@ impl CollapsingHeader {
..header_response.clone()
};
let openness = state.openness(ui.ctx(), id);
paint_icon(ui, openness, &icon_response);
if let Some(icon) = icon {
icon(ui, openness, &icon_response);
} else {
paint_default_icon(ui, openness, &icon_response);
}
}
text.paint_with_visuals(ui.painter(), text_pos, &visuals);

View file

@ -782,7 +782,7 @@ fn show_title_bar(
collapsing.toggle(ui);
}
let openness = collapsing.openness(ui.ctx(), collapsing_id);
collapsing_header::paint_icon(ui, openness, &collapse_button_response);
collapsing_header::paint_default_icon(ui, openness, &collapse_button_response);
}
let title_galley = title.into_galley(ui, Some(false), f32::INFINITY, TextStyle::Heading);