|
1 | 1 | // Take a look at the license at the top of the repository in the LICENSE file.
|
2 | 2 |
|
3 |
| -use glib::{prelude::*, Variant, VariantTy, VariantType}; |
| 3 | +use glib::{prelude::*, MainContext, Variant, VariantTy, VariantType}; |
| 4 | +use std::future::Future; |
4 | 5 |
|
5 | 6 | use crate::{ActionMap, SimpleAction};
|
6 | 7 |
|
|
87 | 88 | self
|
88 | 89 | }
|
89 | 90 |
|
| 91 | + pub fn activate_async<Fut, F>(mut self, callback: F) -> Self |
| 92 | + where |
| 93 | + F: Fn(&O, &SimpleAction, Option<&Variant>) -> Fut + 'static + Clone, |
| 94 | + Fut: Future<Output = ()>, |
| 95 | + { |
| 96 | + let future_cb = move |map: &O, action: &SimpleAction, variant: Option<&Variant>| { |
| 97 | + let ctx = MainContext::thread_default().unwrap_or_else(|| { |
| 98 | + let ctx = glib::MainContext::default(); |
| 99 | + assert!(ctx.is_owner(), "Current thread does not own the default main context and has no thread-default main context"); |
| 100 | + ctx |
| 101 | + }); |
| 102 | + |
| 103 | + let variant = variant.map(ToOwned::to_owned); |
| 104 | + ctx.spawn_local( |
| 105 | + glib::clone!(@strong callback, @strong map, @strong action => async move { |
| 106 | + callback(&map, &action, variant.as_ref()).await; |
| 107 | + }), |
| 108 | + ); |
| 109 | + }; |
| 110 | + |
| 111 | + self.0.activate = Some(Box::new(future_cb)); |
| 112 | + self |
| 113 | + } |
| 114 | + |
90 | 115 | pub fn change_state<F: Fn(&O, &SimpleAction, Option<&Variant>) + 'static>(
|
91 | 116 | mut self,
|
92 | 117 | callback: F,
|
|
0 commit comments