//! `Executable` is a property to execute a closure use anyhow::Result; use std::sync::{Arc, RwLock}; use crate::prelude::*; /// `Executable` holds a closure which can be executed pub struct Executable { callback: RwLock Result<()> + Send + Sync>>>, } impl Executable { /// Factory method for `Executable`, returns `Arc` pub fn new() -> Arc { Arc::new(Executable { callback: RwLock::new(None), }) } /// Set callback closure /// /// # Arguments /// /// * `callback` is a `Option` closure pub fn set_callback(&self, callback: impl Into>) where F: Fn(&mut GuiHandler, I) -> Result<()> + Send + Sync + 'static, { let mut function = self.callback.write().unwrap(); match callback.into() { Some(f) => *function = Some(Arc::new(f)), None => *function = None, } } /// Execute the callback closure if possible pub fn execute(&self, gui_handler: &mut GuiHandler, input: I) -> Result<()> { if let Some(callback) = self.callback.read().unwrap().as_ref() { let callback = callback.clone(); gui_handler.add_callback(move |gui_handler| (callback)(gui_handler, input)); } Ok(()) } }