Yoda 0.12.10

Browser for Gemini Protocol
use gtk::{
    gio::SimpleAction,
    glib::{GString, uuid_string_random},
    prelude::{ActionExt, StaticVariantType, ToVariant},
};

/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Load` action of `Item` group
pub struct Load {
    pub simple_action: SimpleAction,
}

impl Default for Load {
    fn default() -> Self {
        Self::new()
    }
}

impl Load {
    // Constructors

    /// Create new `Self`
    pub fn new() -> Self {
        Self {
            simple_action: SimpleAction::new(
                &uuid_string_random(),
                Some(&<(String, bool, bool)>::static_variant_type()),
            ),
        }
    }

    // Actions

    /// Emit [activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
    /// with formatted for this action [Variant](https://docs.gtk.org/glib/struct.Variant.html) value
    pub fn activate(&self, request: Option<&str>, is_snap_history: bool, is_redirect: bool) {
        self.simple_action.activate(Some(
            &(request.unwrap_or_default(), is_snap_history, is_redirect).to_variant(),
        ));
    }

    // Events

    /// Define callback function for
    /// [SimpleAction::activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
    pub fn connect_activate(&self, callback: impl Fn(Option<GString>, bool, bool) + 'static) {
        self.simple_action.connect_activate(move |_, this| {
            let (request, is_snap_history, is_redirect) = this
                .expect("Expected (`request`,`is_snap_history`) variant")
                .get::<(String, bool, bool)>()
                .expect("Parameter type does not match (`String`,`bool`,`bool`) tuple");

            callback(
                match request.is_empty() {
                    true => None,
                    false => Some(request.into()),
                },
                is_snap_history,
                is_redirect,
            )
        });
    }
}