Finding a view doesn't work

How to find a view? I have a code:

 horizontalLayout.add_child(
            OnEventView::new(NamedView::new(format!("v {}",1), ResizedView::with_full_screen(
            Dialog::around(table)))
            .on_event(cursive::event::Key::Enter, move |s| show_popup(s)));

fn show_popup(app: &mut Cursive) {
          let v:ViewRef<NamedView<ResizedView<TextView>>> = app.find_name(format!("v {}",1).as_str()).unwrap();}

After pressing enter application quits. I assume that it didn't find the view and tried to unwrap and panicked.
Why doesn't it finds the view? How to make it so I can access that view.

As with your other thread please add what crate your using.
Otherwise no one will be able to help you.

Sure thing:

https://docs.rs/cursive/0.15.0/cursive/struct.Cursive.html#method.find_name

Quoting the documentation:

Convenient method to find a view wrapped in NamedView .

(emphasis mine).

Just change the result type to ViewRef<ResizedView<TextView>>, and all should work.

Hi and thanks for the info. Unfortunately, application exits, don't know why...

Well, the code you provided can exit the application only with panic. Did you try logging progress to the file, to find exactly the point where it exits?

Why that code would exit with panic?

In case you're unwrapping the None, of course. But I think that exit point is somewhere else.

But if the view is found shouldn't unwrap succeed? And thus no panic should occur?

fn main() {

    let mut app = cursive::default();
     let mut horizontalLayout_MainPanels = LinearLayout::new(Orientation::Horizontal);
 
        let mut table = create_table();
        
 ResizedView::with_full_screen(Dialog::around(table)));
        horizontalLayout_MainPanels.add_child(
            OnEventView::new(NamedView::new(format!("v {}",i), ResizedView::with_full_screen(
            Dialog::around(table).title(format!("a {}",i)))))
            .on_event(cursive::event::Key::Enter, move |s| show_popup(s,i)));
 
}

fn show_popup(app: &mut Cursive,inx: i64) {
    let v:ViewRef<ResizedView<TextView>> = app.find_name(format!("v {}",inx).as_str()).unwrap();
}

That is the entire code, how on earth is it possible that the code panics???

It doesn't panic. It simply exits cleanly, because you don't run the event loop.

Sorry, I've tried to edit the code little so to make it more readable and avoid unnecessary bits. This is the full, copied version:
fn main() {

    let mut app = cursive::default();
    create_menu(&mut app);
    let mut horizontalLayout_MainPanels = LinearLayout::new(Orientation::Horizontal);
    for i in 0..2
    {
        let mut table = create_table();
        
        //table.set_on_submit(move |app, row, index| enter_fn(app, row, index, i,format!("a {}",i)));
       // table.set_on_submit( enter_fn);
    //    let i = 1;
    
        //let nv = NamedView::new(format!("a {}",i), ResizedView::with_full_screen(Dialog::around(table).title(format!("a {}",i))));
        horizontalLayout_MainPanels.add_child(
            OnEventView::new(NamedView::new(format!("v {}",i), ResizedView::with_full_screen(
            Dialog::around(table).title(format!("a {}",i)))))
            .on_event(cursive::event::Key::Enter, move |s| show_popup(s,i)));
    }
    let mut horizontal_layout_functions = LinearLayout::new(Orientation::Horizontal);
    horizontal_layout_functions.add_child(ResizedView::with_full_width( cursive::views::TextView::new("[ F3 View ]").h_align(HAlign::Center)));
    app.add_global_callback(cursive::event::Key::F3, fk::f3_view_file);
    horizontal_layout_functions.add_child(ResizedView::with_full_width( cursive::views::TextView::new("[ F4 Edit ]").h_align(HAlign::Center)));
    app.add_global_callback(cursive::event::Key::F4, fk::f4_edit_file);
    horizontal_layout_functions.add_child(ResizedView::with_full_width( cursive::views::TextView::new("[ F5 Copy ]").h_align(HAlign::Center)));
    app.add_global_callback(cursive::event::Key::F4, fk::f5_copy);
    horizontal_layout_functions.add_child(ResizedView::with_full_width( cursive::views::TextView::new("[ F6 Move ]").h_align(HAlign::Center)));
    app.add_global_callback(cursive::event::Key::F4, fk::f6_move);
    horizontal_layout_functions.add_child(ResizedView::with_full_width( cursive::views::TextView::new("[ F7 Move ]").h_align(HAlign::Center)));
    app.add_global_callback(cursive::event::Key::F4, fk::f7_mkdir);
    horizontal_layout_functions.add_child(ResizedView::with_full_width( cursive::views::TextView::new("[ F8 Move ]").h_align(HAlign::Center)));
    app.add_global_callback(cursive::event::Key::F4, fk::f8_delete);

    let mut verticalLayout_MainPanels_Functions = LinearLayout::new(Orientation::Vertical);
    verticalLayout_MainPanels_Functions.add_child(horizontalLayout_MainPanels);
    verticalLayout_MainPanels_Functions.add_child(horizontal_layout_functions);
    app.add_fullscreen_layer(ResizedView::with_full_screen(verticalLayout_MainPanels_Functions));
   
    app.run();
}

As you can see the even loop is being run.

Quoting myself:

You mean, put log statements in the code and see which one is being executed and which one isn't?
I didn't, but simply removing a line from show_popup makes the problem disappear, that is, application doesn't exit:

fn show_popup(app: &mut Cursive,inx: i64) {
   // let v:ViewRef<ResizedView<TextView>> = app.find_name(format!("v {}",inx).as_str()).unwrap();
}

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.