0

I have created my first egui app. The update loop looks like

impl eframe::App for App {
    fn update(&mut self, ctx: &Context, _frame: &mut Frame) {
        egui::CentralPanel::default().show(ctx, |ui| {
            ui.with_layout(Layout::top_down(Align::Center), |ui| {
                ui.add(Label::new("bacon"));
                let clicked = ui.add(Button::new("foo")).clicked();
                if clicked {
                    println!("foo")
                }
                let clicked = ui.add(FurnaceGraph::default()).clicked();
                if clicked {
                    println!("clicked");
                }
            })
        });
    }

and the ui for FurnaceGraph looks like

impl Widget for FurnaceGraph {
    fn ui(mut self, ui: &mut Ui) -> Response {
        let (_id, rect) = ui.allocate_space(Vec2::new(self.width as f32, self.height as f32));

        let response = ui.allocate_response(rect.size(), Sense::click());

        // println!("enabled? {}", ui.is_enabled());

        let texture = self.get_texture(ui);
        let texture_size = texture.size_vec2();
        let img = Image::new(texture, texture_size);
        img.paint_at(ui, Rect::from_min_max(rect.min, rect.min + img.size()));

        response
    }
}

I think I am asking to Sense::click()s when I allocate_response, but it never prints "clicked" when I click on the graph. I do get "foo" when I click on the foo button.

What do I have to tweak to not miss clicks?

Mutant Bob
  • 3,121
  • 2
  • 27
  • 52
  • Maybe your `FurnaceGraph` width or height are 0? You are not showing your default implementation, and if you are deriving it it will be most likely 0. – rodrigo Sep 12 '22 at 11:00

1 Answers1

0

I eventually discovered that the rect returned by allocate_space is different than the one returned by allocate_response. I was allocating one rectangle to draw in, and a different one to harvest clicks from. I got rid of the call to allocate_space because allocate_response calls it for me.

Mutant Bob
  • 3,121
  • 2
  • 27
  • 52