Add ability to override collapsing icons (#1147)
This commit is contained in:
parent
e957674467
commit
343f7da564
3 changed files with 37 additions and 3 deletions
|
@ -10,6 +10,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w
|
||||||
### Added ⭐
|
### 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)).
|
* `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 `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 🔧
|
### Changed 🔧
|
||||||
* ⚠️ `Context::input` and `Ui::input` now locks a mutex. This can lead to a dead-lock is used in an `if let` binding!
|
* ⚠️ `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 🙏
|
### Contributors 🙏
|
||||||
* [danielkeller](https://github.com/danielkeller): [#1050](https://github.com/emilk/egui/pull/1050).
|
* [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`
|
## 0.16.1 - 2021-12-31 - Add back `CtxRef::begin_frame,end_frame`
|
||||||
|
|
|
@ -98,7 +98,7 @@ impl State {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Paint the arrow icon that indicated if the region is open or not
|
/// 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 visuals = ui.style().interact(response);
|
||||||
let stroke = visuals.fg_stroke;
|
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));
|
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.
|
/// A header which can be collapsed/expanded, revealing a contained [`Ui`] region.
|
||||||
///
|
///
|
||||||
///
|
///
|
||||||
|
@ -141,6 +144,7 @@ pub struct CollapsingHeader {
|
||||||
selectable: bool,
|
selectable: bool,
|
||||||
selected: bool,
|
selected: bool,
|
||||||
show_background: bool,
|
show_background: bool,
|
||||||
|
icon: Option<IconPainter>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CollapsingHeader {
|
impl CollapsingHeader {
|
||||||
|
@ -162,6 +166,7 @@ impl CollapsingHeader {
|
||||||
selectable: false,
|
selectable: false,
|
||||||
selected: false,
|
selected: false,
|
||||||
show_background: false,
|
show_background: false,
|
||||||
|
icon: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,6 +241,28 @@ impl CollapsingHeader {
|
||||||
self.show_background = show_background;
|
self.show_background = show_background;
|
||||||
self
|
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 {
|
struct Prepared {
|
||||||
|
@ -251,6 +278,7 @@ impl CollapsingHeader {
|
||||||
"Horizontal collapsing is unimplemented"
|
"Horizontal collapsing is unimplemented"
|
||||||
);
|
);
|
||||||
let Self {
|
let Self {
|
||||||
|
icon,
|
||||||
text,
|
text,
|
||||||
default_open,
|
default_open,
|
||||||
open,
|
open,
|
||||||
|
@ -341,7 +369,11 @@ impl CollapsingHeader {
|
||||||
..header_response.clone()
|
..header_response.clone()
|
||||||
};
|
};
|
||||||
let openness = state.openness(ui.ctx(), id);
|
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);
|
text.paint_with_visuals(ui.painter(), text_pos, &visuals);
|
||||||
|
|
|
@ -782,7 +782,7 @@ fn show_title_bar(
|
||||||
collapsing.toggle(ui);
|
collapsing.toggle(ui);
|
||||||
}
|
}
|
||||||
let openness = collapsing.openness(ui.ctx(), collapsing_id);
|
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);
|
let title_galley = title.into_galley(ui, Some(false), f32::INFINITY, TextStyle::Heading);
|
||||||
|
|
Loading…
Reference in a new issue