I'm listening to user input in an gtk-rs input element. input.connect_changed triggers when the input changes and input.connect_activate triggers when Enter is pressed.
use gtk::prelude::*; use gtk::{Application, ApplicationWindow}; use std::process::{Command, Output}; fn main() { let app = Application::builder() .application_id("com.jwestall.ui-demo") .build(); app.connect_activate(build_ui); app.run(); } fn run_command(command: &str) -> Output { Command::new("sh") .arg("-c") .arg(command) .output() .unwrap_or_else(|_| panic!("failed to execute {}'", command)) } fn build_ui(app: &Application) { let input = gtk::Entry::builder() .placeholder_text("input") .margin_top(12) .margin_bottom(12) .margin_start(12) .margin_end(12) .build(); let window = ApplicationWindow::builder() .application(app) .title("gtk-app") .child(&input) .build(); window.show_all(); input.connect_changed(|entry| { let input_text = entry.text(); let command = format!("xdotool search --onlyvisible --name {}", input_text); let window_id_output = run_command(&command); if window_id_output.status.success() { println!( "stdout: {}", String::from_utf8_lossy(&window_id_output.stdout) ); } else { println!( "sterr: {}", String::from_utf8_lossy(&window_id_output.stderr) ); } }); input.connect_activate(move |entry| { let input_text = entry.text(); // // `xdotool windowactivate` doesn't produce any output let command = format!("xdotool windowactivate {}", window_id_output); let window_activate_output = run_command(&command); println!("window_activate: {}", window_activate_output); window.hide(); window.close(); }); } I want to set window_id_output in input.connect_changed, then use it in input.connect_activate (in the xdotool windowactivate {} command).
How can I use window_id_output this way in these two closures?
'static– you don't needSendorSync. Wrappingwindow_id_outputinRc<RefCell<…>>should be all you need.Rc<RefCell<...>>: play.rust-lang.org/…. But it's producing compiling errors like:move occurs becausewindow_id_output_rc` has typeRc<RefCell<Option│ <Output>>>, which does not implement theCopytrait`.RcandRefCellin the Rust book.