From ad5f0a484999df93240b9117c20a9049c7215160 Mon Sep 17 00:00:00 2001 From: Christopher Davis Date: Wed, 18 Jan 2023 21:15:43 -0500 Subject: [PATCH] action_entry: Add activate_async helper This is a helper function in a similar vein to gtk4-rs's `install_action_async ()` for widgets. It allows applications to install actions that use async/await without having to manually create a glib::MainContext in the callback. --- gio/src/action_entry.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/gio/src/action_entry.rs b/gio/src/action_entry.rs index 118d418a8748..e9a7ee63cc41 100644 --- a/gio/src/action_entry.rs +++ b/gio/src/action_entry.rs @@ -1,6 +1,7 @@ // Take a look at the license at the top of the repository in the LICENSE file. use glib::{prelude::*, Variant, VariantTy, VariantType}; +use std::future::Future; use crate::{ActionMap, SimpleAction}; @@ -87,6 +88,25 @@ where self } + pub fn activate_async(mut self, callback: F) -> Self + where + F: Fn(&O, &SimpleAction, Option<&Variant>) -> Fut + 'static + Clone, + Fut: Future, + { + let future_cb = move |map: &O, action: &SimpleAction, variant: Option<&Variant>| { + let ctx = glib::MainContext::default(); + let variant = variant.map(ToOwned::to_owned); + ctx.spawn_local( + glib::clone!(@strong callback, @strong map, @strong action => async move { + callback(&map, &action, variant.as_ref()).await; + }), + ); + }; + + self.0.activate = Some(Box::new(future_cb)); + self + } + pub fn change_state) + 'static>( mut self, callback: F,