Cursive - Passing parameters to callbacks


Given the simple Cursive structure that displays to buttons vertically (one “Choose” that calls choose_model and a quit button) how do I arrange for the function choose_model to be passed some extra information?

        let buttons = LinearLayout::vertical()
            .child(Button::new("Quit", Cursive::quit));

I would like the function to be passed a directory path in can look in Something like…

        let buttons = LinearLayout::vertical()
            .child(Button::new("Choose", choose_model, "/tmp")) // Ever hopeful
            .child(Button::new("Quit", Cursive::quit));

fn choose_models( s: &mut Cursive, dir:&str){
  // Read diretcory dir and get models
  // Display models in a widget
  // Use a callback to run models

Seems to me that there should be a simple solution using a closure, but I cannot find it.



It looks like Button::new takes a closure so you should be able to do that. For example:

child(Button::new("Choose", |s| choose_models(s, "/tmp")))


I cannot have the call back a method on the structure that implements my model, clearly.

In my case I have a complex model. I would be happy to hand ownership over to Cursive and that would be perfect. But there is no way.

What I want is:

Given a model Foo that has two methods run and stop

let c = Controller {c :Cursive<Foo>}

Then in at some point say: c.get_model().run()

But I cannot. So I will use pipes and pass messages back and forth. Sigh


Are you referring to the 'static requirement on the closure? If so, if you can clone your model (or pieces needed for the callback), then you can move those into the closure. Putting that state inside an Rc would make those clones cheap.

But maybe I misunderstood your concern.


I need the model in more than one place. It is needed from every aspect of the user interface

But I have quickly found other bugs in Cursive. It is very pretty but IMO not very good.

Moving on…