1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::AccelFlags;
use crate::AccelGroup;
use glib::object::{Cast, IsA};
use glib::translate::*;
use glib::ToValue;

pub trait AccelGroupExtManual: 'static {
    fn connect_accel_group<F>(
        &self,
        accel_key: u32,
        accel_mods: gdk::ModifierType,
        accel_flags: AccelFlags,
        func: F,
    ) -> glib::Closure
    where
        F: Fn(&Self, &glib::Object, u32, gdk::ModifierType) -> bool + 'static;

    fn connect_accel_group_by_path<F>(&self, accel_path: &str, func: F) -> glib::Closure
    where
        F: Fn(&Self, &glib::Object, u32, gdk::ModifierType) -> bool + 'static;
}

impl<O: IsA<AccelGroup>> AccelGroupExtManual for O {
    fn connect_accel_group<F>(
        &self,
        accel_key: u32,
        accel_mods: gdk::ModifierType,
        accel_flags: AccelFlags,
        func: F,
    ) -> glib::Closure
    where
        F: Fn(&Self, &glib::Object, u32, gdk::ModifierType) -> bool + 'static,
    {
        let closure = glib::Closure::new_local(move |values| {
            assert_eq!(values.len(), 4);
            let s = values[0]
                .get::<AccelGroup>()
                .expect("Wrong argument type for first closure argument");
            let s = s
                .downcast::<Self>()
                .expect("Wrong argument type for first closure argument");

            let obj = values[1]
                .get::<glib::Object>()
                .expect("Wrong argument type for second closure argument");
            let accel_key = values[2]
                .get::<u32>()
                .expect("Wrong argument type for third closure argument");
            let accel_mods = values[3]
                .get::<gdk::ModifierType>()
                .expect("Wrong argument type for fourth closure argument");

            let ret = func(&s, &obj, accel_key, accel_mods);

            Some(ret.to_value())
        });

        unsafe {
            ffi::gtk_accel_group_connect(
                self.as_ref().to_glib_none().0,
                accel_key,
                accel_mods.into_glib(),
                accel_flags.into_glib(),
                closure.to_glib_none().0,
            );
        }

        closure
    }

    fn connect_accel_group_by_path<F>(&self, accel_path: &str, func: F) -> glib::Closure
    where
        F: Fn(&Self, &glib::Object, u32, gdk::ModifierType) -> bool + 'static,
    {
        let closure = glib::Closure::new_local(move |values| {
            assert_eq!(values.len(), 4);
            let s = values[0]
                .get::<AccelGroup>()
                .expect("Wrong argument type for first closure argument");
            let s = s
                .downcast::<Self>()
                .expect("Wrong argument type for first closure argument");
            let obj = values[1]
                .get::<glib::Object>()
                .expect("Wrong argument type for second closure argument");
            let accel_key = values[2]
                .get::<u32>()
                .expect("Wrong argument type for third closure argument");
            let accel_mods = values[3]
                .get::<gdk::ModifierType>()
                .expect("Wrong argument type for fourth closure argument");

            let ret = func(&s, &obj, accel_key, accel_mods);

            Some(ret.to_value())
        });

        unsafe {
            ffi::gtk_accel_group_connect_by_path(
                self.as_ref().to_glib_none().0,
                accel_path.to_glib_none().0,
                closure.to_glib_none().0,
            );
        }

        closure
    }
}