0

I am passing the sender of on async channel to a modal window so that the modal window can send messages to the parent window:

...
let (tx, rx): (Sender<ApplicationMessage>, Receiver<ApplicationMessage>) = async_channel::bounded(1);

        let window = MainWindow::new(&*self.obj());
        window.connect_close_request(move |window| {
            debug!("connect_close_request");

            let dialog = ConfirmCloseDialog::new(window, &tx);
            dialog.set_transient_for(Some(window));
            dialog.present();

            // glib::Propagation::Proceed
            glib::Propagation::Stop
        });

and I added pub fn new<P: IsA<gtk::Window>>(window: &P, tx: &Sender<ApplicationMessage>) -> Self to the impl of the dialog. How do I store the tx in the imp struct of the dialog?

1 Answer 1

0

First of all you should change tx: &Sender<...> to tx: Sender<...> in your constructor. Senders are cheap to clone, so even if you will need multiple ones, you just can tx.clone() them. Your dialog struct would then look something like this:


struct Dialog {
    tx: Sender<ApplicationMessage>, 
    // rest of your fields
}

impl Dialog {
    pub fn new<P: IsA<gtk::Window>>(window: &P, tx: Sender<ApplicationMessage>) -> Self {
    Self {
        tx,
        // rest of your fields
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.