Skip to content

Commit

Permalink
Add interactive_container demo
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmerlin committed Sep 2, 2024
1 parent 3556b72 commit d02906d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/egui_demo_lib/src/demo/demo_app_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl Default for Demos {
Box::<super::font_book::FontBook>::default(),
Box::<super::frame_demo::FrameDemo>::default(),
Box::<super::highlighting::Highlighting>::default(),
Box::<super::interactive_container::InteractiveContainerDemo>::default(),
Box::<super::MiscDemoWindow>::default(),
Box::<super::multi_touch::MultiTouch>::default(),
Box::<super::painting::Painting>::default(),
Expand Down
81 changes: 81 additions & 0 deletions crates/egui_demo_lib/src/demo/interactive_container.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use egui::{Frame, Label, RichText, Sense, UiBuilder, Widget};

/// Showcase [`egui::Ui::read_response`].
#[derive(PartialEq, Eq, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(default))]
pub struct InteractiveContainerDemo {
count: usize,
}

impl crate::Demo for InteractiveContainerDemo {
fn name(&self) -> &'static str {
"\u{20E3}TextEdit"
}

fn show(&mut self, ctx: &egui::Context, open: &mut bool) {
egui::Window::new(self.name())
.open(open)
.resizable(false)
.show(ctx, |ui| {
use crate::View as _;
self.ui(ui);
});
}
}

impl crate::View for InteractiveContainerDemo {
fn ui(&mut self, ui: &mut egui::Ui) {
ui.vertical_centered(|ui| {
ui.add(crate::egui_github_link_file!());
});

ui.label("This demo showcases how to use Ui::read_response to create interactive container widgets that may contain other widgets.");

let response = ui
.scope_builder(
UiBuilder::new()
.id_salt("interactive_container")
.sense(Sense::click()),
|ui| {
let response = ui.read_response();
let visuals = ui.style().interact(&response);
let text_color = visuals.text_color();

Frame::canvas(ui.style())
.fill(visuals.bg_fill.gamma_multiply(0.3))
.stroke(visuals.bg_stroke)
.inner_margin(ui.spacing().menu_margin)
.show(ui, |ui| {
ui.set_width(ui.available_width());

ui.add_space(32.0);
ui.vertical_centered(|ui| {
Label::new(
RichText::new(format!("{}", self.count))
.color(text_color)
.size(32.0),
)
.selectable(false)
.ui(ui);
});
ui.add_space(32.0);

ui.horizontal(|ui| {
if ui.button("Reset").clicked() {
self.count = 0;
}
if ui.button("+ 100").clicked() {
self.count += 100;
}
});
});
},
)
.response;

if response.clicked() {
self.count += 1;
}
}
}
1 change: 1 addition & 0 deletions crates/egui_demo_lib/src/demo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod extra_viewport;
pub mod font_book;
pub mod frame_demo;
pub mod highlighting;
pub mod interactive_container;
pub mod misc_demo_window;
pub mod multi_touch;
pub mod paint_bezier;
Expand Down

0 comments on commit d02906d

Please sign in to comment.