Where to call app.add_action()?

I’m porting my gtk-3 + perl app to gtk-4 + rust. I’m also at the bottom end of the learning curve for rust, and so I’ll have some stupid questions:

As it seemed like best practice for separating the ui and the rest of the code, I’ve started with the composite_templates examples and added a main menu with tags in the .ui file. That all works fine. Now I’m trying to add actions. I can obviously add code like:

    // Add action "close" to `window` taking no parameter
    let action_close = SimpleAction::new("close", None);
    action_close.connect_activate(clone!(@weak window => move |_, _| {
        window.close();
    }));
    window.add_action(&action_close);

in main.rs, and it all works fine, but it seems better to keep that as small as possible, and put it in mod.rs instead. However, there, I get the following error:

error[E0282]: type annotations needed
  --> src/ui/mod.rs:23:35
   |
23 |       action_close.connect_activate(clone!(@weak window => move |_, _| {
   |  ___________________________________^
24 | |         window.close();
25 | |     }));
   | |______^
   |
   = note: type must be known at this point
   = note: this error originates in the macro `clone` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider giving `window` an explicit type
   |
25 |     }): _);
   |       +++

For more information about this error, try `rustc --explain E0282`.

I can get rid of the error by commenting out the close() call, and calling add_action on the app strut, rather than the window, but then it obvious no longer closes the app:

    // Add action "close" to `window` taking no parameter
    let action_close = SimpleAction::new("close", None);
    action_close.connect_activate(clone!(@weak window => move |_, _| {
        //window.close();
    }));
    app.add_action(&action_close);

Why does the window.close() affect the clone macro?

How can I get it working? Or is there a better structure for the app?

Regards

Jeff

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.