mirror of
https://github.com/emilk/egui.git
synced 2026-08-31 22:00:03 -04:00
Add ui.set_enabled(false) to disable all widgets in a Ui
Closes https://github.com/emilk/egui/issues/50
This commit is contained in:
@@ -51,20 +51,24 @@ pub fn drop_target<R>(
|
||||
|
||||
let style = if is_being_dragged && can_accept_what_is_being_dragged && response.hovered() {
|
||||
ui.visuals().widgets.active
|
||||
} else if is_being_dragged && can_accept_what_is_being_dragged {
|
||||
ui.visuals().widgets.inactive
|
||||
} else if is_being_dragged && !can_accept_what_is_being_dragged {
|
||||
ui.visuals().widgets.disabled
|
||||
} else {
|
||||
ui.visuals().widgets.inactive
|
||||
};
|
||||
|
||||
let mut fill = style.bg_fill;
|
||||
let mut stroke = style.bg_stroke;
|
||||
if is_being_dragged && !can_accept_what_is_being_dragged {
|
||||
// gray out:
|
||||
fill = color::tint_color_towards(fill, ui.visuals().window_fill());
|
||||
stroke.color = color::tint_color_towards(stroke.color, ui.visuals().window_fill());
|
||||
}
|
||||
|
||||
ui.painter().set(
|
||||
where_to_put_background,
|
||||
Shape::Rect {
|
||||
corner_radius: style.corner_radius,
|
||||
fill: style.bg_fill,
|
||||
stroke: style.bg_stroke,
|
||||
fill,
|
||||
stroke,
|
||||
rect,
|
||||
},
|
||||
);
|
||||
|
||||
@@ -83,9 +83,12 @@ fn toggle_compact(ui: &mut egui::Ui, on: &mut bool) -> egui::Response {
|
||||
|
||||
pub fn demo(ui: &mut egui::Ui, on: &mut bool) {
|
||||
ui.horizontal_wrapped_for_text(egui::TextStyle::Button, |ui| {
|
||||
ui.label("It's easy to create your own widgets!");
|
||||
ui.label("This toggle switch is just one function and 15 lines of code:");
|
||||
toggle(ui, on).on_hover_text("Click to toggle");
|
||||
ui.add(crate::__egui_github_link_file!());
|
||||
});
|
||||
})
|
||||
.1
|
||||
.on_hover_text(
|
||||
"It's easy to create your own widgets!\n\
|
||||
This toggle switch is just one function and 20 lines of code.",
|
||||
);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ enum Enum {
|
||||
|
||||
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
|
||||
pub struct WidgetGallery {
|
||||
enabled: bool,
|
||||
boolean: bool,
|
||||
radio: Enum,
|
||||
scalar: f32,
|
||||
@@ -18,6 +19,7 @@ pub struct WidgetGallery {
|
||||
impl Default for WidgetGallery {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
enabled: true,
|
||||
boolean: false,
|
||||
radio: Enum::First,
|
||||
scalar: 42.0,
|
||||
@@ -46,6 +48,7 @@ impl super::Demo for WidgetGallery {
|
||||
impl super::View for WidgetGallery {
|
||||
fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
let Self {
|
||||
enabled,
|
||||
boolean,
|
||||
radio,
|
||||
scalar,
|
||||
@@ -53,6 +56,12 @@ impl super::View for WidgetGallery {
|
||||
color,
|
||||
} = self;
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.radio_value(enabled, true, "Enabled");
|
||||
ui.radio_value(enabled, false, "Disabled");
|
||||
});
|
||||
ui.set_enabled(*enabled);
|
||||
|
||||
let grid = egui::Grid::new("my_grid")
|
||||
.striped(true)
|
||||
.spacing([40.0, 4.0]);
|
||||
@@ -65,7 +74,7 @@ impl super::View for WidgetGallery {
|
||||
ui.add(egui::Hyperlink::new("https://github.com/emilk/egui").text(" egui home page"));
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Text Input:");
|
||||
ui.label("TextEdit:");
|
||||
ui.add(egui::TextEdit::singleline(string).hint_text("Write something here"));
|
||||
ui.end_row();
|
||||
|
||||
@@ -73,7 +82,7 @@ impl super::View for WidgetGallery {
|
||||
ui.checkbox(boolean, "Checkbox");
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Radio buttons:");
|
||||
ui.label("RadioButton:");
|
||||
ui.horizontal(|ui| {
|
||||
ui.radio_value(radio, Enum::First, "First");
|
||||
ui.radio_value(radio, Enum::Second, "Second");
|
||||
@@ -143,6 +152,10 @@ impl super::View for WidgetGallery {
|
||||
});
|
||||
});
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Custom widget");
|
||||
super::toggle_switch::demo(ui, boolean);
|
||||
ui.end_row();
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
@@ -134,8 +134,5 @@ impl Widgets {
|
||||
|
||||
ui.label("Multiline text input:");
|
||||
ui.text_edit_multiline(&mut self.multiline_text_input);
|
||||
|
||||
ui.separator();
|
||||
super::toggle_switch::demo(ui, &mut self.toggle_switch);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ pub struct WindowOptions {
|
||||
collapsible: bool,
|
||||
resizable: bool,
|
||||
scroll: bool,
|
||||
disabled_time: f64,
|
||||
}
|
||||
|
||||
impl Default for WindowOptions {
|
||||
@@ -18,6 +19,7 @@ impl Default for WindowOptions {
|
||||
collapsible: true,
|
||||
resizable: true,
|
||||
scroll: false,
|
||||
disabled_time: f64::NEG_INFINITY,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,15 +38,22 @@ impl super::Demo for WindowOptions {
|
||||
collapsible,
|
||||
resizable,
|
||||
scroll,
|
||||
disabled_time,
|
||||
} = self.clone();
|
||||
|
||||
let enabled = ctx.input().time - disabled_time > 2.0;
|
||||
if !enabled {
|
||||
ctx.request_repaint();
|
||||
}
|
||||
|
||||
use super::View;
|
||||
let mut window = egui::Window::new(title)
|
||||
.id(egui::Id::new("demo_window_options")) // required since we change the title
|
||||
.resizable(resizable)
|
||||
.collapsible(collapsible)
|
||||
.title_bar(title_bar)
|
||||
.scroll(scroll);
|
||||
.scroll(scroll)
|
||||
.enabled(enabled);
|
||||
if closable {
|
||||
window = window.open(open);
|
||||
}
|
||||
@@ -63,6 +72,7 @@ impl super::View for WindowOptions {
|
||||
collapsible,
|
||||
resizable,
|
||||
scroll,
|
||||
disabled_time,
|
||||
} = self;
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
@@ -77,5 +87,9 @@ impl super::View for WindowOptions {
|
||||
ui.vertical_centered(|ui| {
|
||||
ui.add(crate::__egui_github_link_file!());
|
||||
});
|
||||
|
||||
if ui.button("Disable for 2 seconds").clicked() {
|
||||
*disabled_time = ui.input().time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user