Skip to content

Instantly share code, notes, and snippets.

@sireliah
Last active February 23, 2019 17:04
Show Gist options
  • Select an option

  • Save sireliah/de02ba76220c46c7b4f6f23eac9b43ca to your computer and use it in GitHub Desktop.

Select an option

Save sireliah/de02ba76220c46c7b4f6f23eac9b43ca to your computer and use it in GitHub Desktop.
// Instantiate Option which will help us mutate the image
// from within the closure.
let image_wrapper: Rc<RefCell<Option<gtk::Image>>> = Rc::new(RefCell::new(None));
// (...)
// Bind action to the generate button
generate_button.connect_clicked(clone!(
box_vert, image_wrapper, spiral, adj_x, radio_primes => move |_| {
let window = match window_weak.upgrade() {
Some(window) => window,
None => return
};
// Remove existing image from the Option container
let mut internal_image = image_wrapper.borrow_mut();
let image = internal_image.as_ref().unwrap();
box_vert.remove(image);
// Create new image
let image_gtk: gtk::Image = spiral.borrow().generate_to_gtk();
let img_clone = image_gtk.clone();
// Replace the reference to the newly generated image in the wrapper
*internal_image = Some(image_gtk);
// Put the reference of the new image to the layout container
box_vert.pack_start(&img_clone, false, false, 20);
window.show_all();
}));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment