Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Feature Request or Guidance on Customizing Submit Key for SelectView #774

Open
DenisVASI9 opened this issue Mar 8, 2024 · 1 comment

Comments

@DenisVASI9
Copy link

Hello Cursive Team,

I'm currently working on a Rust application using the Cursive TUI library and I'm utilizing the SelectView for a form-like interaction within my application. The default behavior of SelectView (and similar views) is to trigger a submit action or finalize a selection upon pressing the Enter key.

For my specific use case, I'm interested in customizing this behavior to use the Space key as the trigger for the submission action instead of the Enter key. This adjustment would better suit the interaction design of my application, making it more intuitive for my users.

I've gone through the documentation and examples but haven't found a clear way to implement this customization. I'm reaching out to ask if this functionality is currently supported by Cursive and, if so, could you please provide guidance on how to implement this? Alternatively, if this functionality is not supported, I'd like to formally request it as a feature addition to the library.

The ability to customize the key binding for submission actions in SelectView (and potentially other views) would greatly enhance the flexibility of Cursive for developing interactive TUI applications.

Thank you for your time and for the excellent work on the Cursive library. I look forward to your response.

@gyscos
Copy link
Owner

gyscos commented Mar 10, 2024

Hi, and thanks for the report!

You can wrap a SelectView (or any other view) in another view that can intercept events, and, for example, trigger another action on "space" events.

The OnEventView is a convenient way to do that.

There are several ways to configure the callback, but here we'll go with the method that gives us the most control: on_pre_event_inner. It runs before the event is given to the wrapped view, and will have direct access to the wrapped view itself, allowing us to return another callback to run on &mut Cursive.

use cursive::views::{SelectView, OnEventView};

let select_view = SelectView::new();

let wrapped = OnEventView::new(select_view).on_pre_event_inner(' ', |view, _event| {
    use cursive::event::{Event, Key};
    Some(view.on_event(Event::Key(Key::Enter)))
});

Here we just pretend it was an Enter key. If SelectView::submit was public, you could directly call that instead. I'm not sure why it's not public at the moment - I'll probably make it public soon, so you could do the same more explicitly (without having to fake an Enter key).

You could also override the Enter key, to prevent the SelectView from seeing it:

use cursive::views::{SelectView, OnEventView};

let select_view = SelectView::new();

use cursive::event::{Event, Key};

let wrapped = OnEventView::new(select_view)
    .on_pre_event_inner(' ', |view, _event| {
        Some(view.on_event(Event::Key(Key::Enter)))
    })
    .on_pre_event_inner(Key::Event, |_view, _event| {
        // Returning `Some` here means the wrapped view does not get to see that event.
        Some(EventResult::Ignored)
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants