Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to override collapsing icons #1147

Merged
merged 4 commits into from Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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!
Expand All @@ -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`
Expand Down
36 changes: 34 additions & 2 deletions egui/src/containers/collapsing_header.rs
Expand Up @@ -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;

Expand All @@ -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.
///
///
Expand All @@ -141,6 +144,7 @@ pub struct CollapsingHeader {
selectable: bool,
selected: bool,
show_background: bool,
icon: Option<IconPainter>,
}

impl CollapsingHeader {
Expand All @@ -162,6 +166,7 @@ impl CollapsingHeader {
selectable: false,
selected: false,
show_background: false,
icon: None,
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -251,6 +278,7 @@ impl CollapsingHeader {
"Horizontal collapsing is unimplemented"
);
let Self {
icon,
text,
default_open,
open,
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion egui/src/containers/window.rs
Expand Up @@ -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);
Expand Down