-
I have a simple Cursive-based app that contains an edit view that is initially disabled. The app can be quit by pressing "q" while pressing "e" is intended to enable the edit view and give it focus so that the user can start typing in the edit view directly. Instead, on pressing "e", the edit view is enabled but the user cannot yet type directly in it. Instead, the user needs to click on the edit view before they can type in it. I would expect "take_focus" do this without requiring a mouse click. What additional calls do I need to make so that the user's keypresses go directly to the edit view. This program demonstrates this behaviour: use cursive::direction::{Direction, Orientation};
use cursive::view::{Nameable, Resizable};
use cursive::views::{EditView, LinearLayout, TextView};
use cursive::{Cursive, View};
fn main() {
let mut c = cursive::default();
c.add_fullscreen_layer(
LinearLayout::new(Orientation::Vertical)
.child(
TextView::new("Q to quit\nE to edit")
.min_height(5)
.full_width(),
)
.child(EditView::new().disabled().with_name("EDIT")),
);
c.add_global_callback('q', Cursive::quit);
c.add_global_callback('e', |c| {
c.call_on_name("EDIT", |edit_view: &mut EditView| {
edit_view.enable();
edit_view.take_focus(Direction::none()).unwrap();
});
});
c.run();
} See also main.rs. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi, and thanks for the question! The main issue here is the concept of focus: the You will need to give a name to that Note that you can call use cursive::traits::Finder;
c.call_on_name("LINEAR", |linear: &mut LinearLayout| {
linear.call_on_name("EDIT", EditView::enable).unwrap();
linear.focus_view(Selector::Name("EDIT")).unwrap();
}).unwrap(); As a bonus section (but probably unnecessary here), some views could have callback actions for when the focus changes. This is why both c.call_on_name("LINEAR", |linear: &mut LinearLayout| {
linear.call_on_name("EDIT", EditView::enable).unwrap();
linear.focus_view(Selector::Name("EDIT")).unwrap() // Return the value here
}).unwrap().process(c) But in this case, you might know that nothing there will trigger any callback on a focus change, and not bother forwarding it. |
Beta Was this translation helpful? Give feedback.
Hi, and thanks for the question!
The main issue here is the concept of focus: the
EditView
itself cannot control if it's in focus. Instead, its parents (in this case, theLinearLayout
) decide which view is active.You will need to give a name to that
LinearLayout
, and use something likefocus_view()
with a name selector, orset_focus_index
if you know it'll be the child at index1
.Note that you can call
call_on_name
on any view (with theFinder
trait), so in the callback, you can call it on the linear layout to get the editview (sincec
will be borrowed):