DobbyTheElf
DobbyTheElf

Reputation: 789

Cannot move out a captured variable in an `Fn` closure

I'm getting these error messages in my gkt-rs code:

error[E0507]: cannot move out of `image`, a captured variable in an `Fn` closure
error[E0382]: borrow of moved value: `window`

I've read this question and this question, but don't see how to apply it to my problem. I assume the answer involves RefCell, but can't get the invocation right.

This is my code:

use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Box, Button, FileChooserDialog, Image};
const APP_ID: &str = "org.gtk_rs.test";

fn main() {
    let app = Application::builder().application_id(APP_ID).build();
    app.connect_activate(build_ui);
    app.run();
}

fn build_ui(app: &Application) {
    let image = Image::builder().build();
    let button = Button::builder().label("Press me!").build();
    let mbox = Box::builder().build();
    mbox.append(&button);
    mbox.append(&image);
    let window = ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .child(&mbox)
        .build();
    button.connect_clicked(move |_| {
        let file_chooser = FileChooserDialog::new(
            Some("Open image"),
            Some(&window),
            gtk::FileChooserAction::Open,
            &[
                ("_Cancel", gtk::ResponseType::Cancel),
                ("_Open", gtk::ResponseType::Accept),
            ],      );
            file_chooser.connect_response(move |file_chooser, response| {
                if response == gtk::ResponseType::Accept {
                    let file = file_chooser.file().expect("Couldn't get file");
                    let filename = file.path().expect("Couldn't get file path");
                    image.set_from_file(Some(filename));
                }
                file_chooser.destroy();
            });
        file_chooser.show();
    });
    window.present();
}

I'd be grateful for any suggestions.

Upvotes: 1

Views: 248

Answers (1)

DobbyTheElf
DobbyTheElf

Reputation: 789

Thanks @Masklinn, the text_viewer example had the answer:

use glib_macros::clone;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Box, Button, FileChooserDialog, Image};
const APP_ID: &str = "org.gtk_rs.test";

fn main() {
    let app = Application::builder().application_id(APP_ID).build();
    app.connect_activate(build_ui);
    app.run();
}

fn build_ui(app: &Application) {
    let image = Image::builder().build();
    let button = Button::builder().label("Press me!").build();
    let mbox = Box::builder().build();
    mbox.append(&button);
    mbox.append(&image);
    let window = ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .child(&mbox)
        .build();
    button.connect_clicked(clone!(@weak window => move |_| {
        let file_chooser = FileChooserDialog::new(
            Some("Open image"),
            Some(&window),
            gtk::FileChooserAction::Open,
            &[
                ("_Cancel", gtk::ResponseType::Cancel),
                ("_Open", gtk::ResponseType::Accept),
            ],      );
            file_chooser.connect_response(clone!(@weak image => move |file_chooser, response| {
                if response == gtk::ResponseType::Accept {
                    let file = file_chooser.file().expect("Couldn't get file");
                    let filename = file.path().expect("Couldn't get file path");
                    image.set_from_file(Some(filename));
                }
                file_chooser.destroy();
            }));
        file_chooser.show();
    }));
    window.present();
}

Upvotes: 1

Related Questions