Add Ui::set_visible as a way to hide widgets

Closes https://github.com/emilk/egui/issues/460
This commit is contained in:
Emil Ernerfeldt
2021-06-07 22:12:49 +02:00
parent 3c603c55b8
commit ece25ee7f3
4 changed files with 79 additions and 21 deletions
+21 -2
View File
@@ -66,6 +66,15 @@ impl Painter {
self.fade_to_color = fade_to_color;
}
pub(crate) fn visible(&self) -> bool {
self.fade_to_color != Some(Color32::TRANSPARENT)
}
/// If `false`, nothing added to the painter will be visible
pub(crate) fn set_invisible(&mut self) {
self.fade_to_color = Some(Color32::TRANSPARENT)
}
/// Create a painter for a sub-region of this `Painter`.
///
/// The clip-rect of the returned `Painter` will be the intersection
@@ -145,14 +154,21 @@ impl Painter {
/// Can be used for free painting.
/// NOTE: all coordinates are screen coordinates!
pub fn add(&self, mut shape: Shape) -> ShapeIdx {
self.transform_shape(&mut shape);
self.paint_list.lock().add(self.clip_rect, shape)
if self.fade_to_color == Some(Color32::TRANSPARENT) {
self.paint_list.lock().add(self.clip_rect, Shape::Noop)
} else {
self.transform_shape(&mut shape);
self.paint_list.lock().add(self.clip_rect, shape)
}
}
/// Add many shapes at once.
///
/// Calling this once is generally faster than calling [`Self::add`] multiple times.
pub fn extend(&self, mut shapes: Vec<Shape>) {
if self.fade_to_color == Some(Color32::TRANSPARENT) {
return;
}
if !shapes.is_empty() {
if self.fade_to_color.is_some() {
for shape in &mut shapes {
@@ -166,6 +182,9 @@ impl Painter {
/// Modify an existing [`Shape`].
pub fn set(&self, idx: ShapeIdx, mut shape: Shape) {
if self.fade_to_color == Some(Color32::TRANSPARENT) {
return;
}
self.transform_shape(&mut shape);
self.paint_list.lock().set(idx, self.clip_rect, shape)
}
+35 -3
View File
@@ -215,14 +215,44 @@ impl Ui {
/// ```
pub fn set_enabled(&mut self, enabled: bool) {
self.enabled &= enabled;
if self.enabled {
self.painter.set_fade_to_color(None);
} else {
if !self.enabled && self.visible() {
self.painter
.set_fade_to_color(Some(self.visuals().window_fill()));
}
}
/// If `false`, any widgets added to the `Ui` will be invisible and non-interactive.
#[inline(always)]
pub fn visible(&self) -> bool {
self.painter.visible()
}
/// Calling `set_visible(false)` will cause all further widgets to be invisible,
/// yet still allocate space.
///
/// The widgets will not be interactive (`set_visible(false)` implies `set_enabled(false)`).
///
/// Calling `set_visible(true)` has no effect.
///
/// ### Example
/// ```
/// # let ui = &mut egui::Ui::__test();
/// # let mut visible = true;
/// ui.group(|ui|{
/// ui.checkbox(&mut visible, "Show subsection");
/// ui.set_visible(visible);
/// if ui.button("Button that is not always shown").clicked() {
/// /* … */
/// }
/// });
/// ```
pub fn set_visible(&mut self, visible: bool) {
self.set_enabled(visible);
if !visible {
self.painter.set_invisible();
}
}
#[inline(always)]
pub fn layout(&self) -> &Layout {
self.placer.layout()
@@ -1226,6 +1256,8 @@ impl Ui {
/// ui.label("Within a frame");
/// });
/// ```
///
/// Se also [`Self::scope`].
pub fn group<R>(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> InnerResponse<R> {
crate::Frame::group(self.style()).show(self, add_contents)
}