ratatui_kit/hooks/
use_on_drop.rs1use crate::Hook;
2
3mod private {
4 pub trait Sealed {}
5 impl Sealed for crate::Hooks<'_, '_> {}
6}
7
8pub trait UseOnDrop: private::Sealed {
10 fn use_on_drop<F>(&mut self, f: F)
11 where
12 F: FnMut() + Send + 'static;
13}
14
15#[derive(Default)]
16struct UseOnDropImpl {
17 callback: Option<Box<dyn FnMut() + Send>>,
18}
19
20impl Hook for UseOnDropImpl {
21 fn on_drop(&mut self) {
22 if let Some(mut callback) = self.callback.take() {
23 callback();
24 }
25 }
26}
27
28impl UseOnDrop for crate::Hooks<'_, '_> {
29 fn use_on_drop<F>(&mut self, f: F)
31 where
32 F: FnMut() + Send + 'static,
33 {
34 let hook = self.use_hook(UseOnDropImpl::default);
35 hook.callback.replace(Box::new(f));
36 }
37}