How to process events in cursive without implementing a View

Viewed 127

I am writing an application with a cursive GUI, and I would like to do certain things when the user presses certain keys. For example, when using a TableView I would like to perform different actions when the user has selected a row and presses a key (other than Enter which is handled by TableView and allows me to implement on_submit). Those actions would be parametrised by the selected row, i.e. using the index or some value from the row.

Since I don't have access to on_event and there doesn't seem to be an associated callback that I can register with the TableView, I can only think about storing some "global" parameters from on_select and then register global cursive handlers ala add_global_callback. This would be a big mess very soon, so I'm hoping there's a better way.

I thought I could do the equivalent of extending TableView as one would do with inheritance in OOP, but I asked here without much success, and was instead given the smart advice in the comments to just ask for what I want to accomplish instead.

1 Answers

I think you just need to wrap your TableView in an OnEventView and you should be able to trap whatever events you want, and it'll pass through whatever you don't.

cursive.add_layer(
    OnEventView::new(
        TableView::new( ... )
    ).on_event(...)

I saw your other post. My two cents about that :

So instead of the OOP way of extending TableView you insert a view "above" TableView that does some work. This can be thought of the same as extending, but it's more generic. The OnEventView can "wrap" any View, whereas extending a specific class is only useful for that class. Extending usually means you have to implement a bunch of boilerplate that has nothing to do with traping events, which is all you want to do in this case.

Related