nrelm 0.1.0

An idiomatic GUI library inspired by Elm and based on gtk3-rs
/// Dummy function for marking the `#[transition]` macro attribute as deprecated.
/// See <https://stackoverflow.com/a/77267752>.
#[deprecated = "The `#[transition]` macro attribute is deprecated. Call `set_transition_type` on the returned widget instead."]
pub const fn transition() {}

/// Shows a widget created by the `view!` macro if it implements
/// [`IsA<gtk::Widget>`](gtk::prelude::IsA).
///
/// GTK3 widgets are hidden by default, unlike GTK4. The `view!` macro
/// therefore calls this function on every widget it creates. Dialogs like
/// [`gtk::FileChooserNative`] are not widgets and are ignored, so they stay
/// hidden until they are explicitly shown.
///
/// Structs generated by the [`widget_template`](nrelm::widget_template)
/// macro implement `Deref` to their root widget; the macro dereferences
/// them before calling this function.
pub fn show_widget(widget: &impl AsRef<gtk::glib::Object>) {
    use gtk::prelude::{Cast, WidgetExt};

    if let Some(widget) = widget.as_ref().downcast_ref::<gtk::Widget>() {
        // Popovers and menus are shown and hidden by their parent (for example
        // a `MenuButton`). Showing them unconditionally makes them appear on
        // their own.
        if widget.downcast_ref::<gtk::Popover>().is_some()
            || widget.downcast_ref::<gtk::Menu>().is_some()
        {
            return;
        }
        widget.show();
    }
}

/// Shows the root widget of a component, unless it is a window.
///
/// [`RelmApp`](crate::RelmApp) shows the root window on activation, and
/// subcomponents like dialogs manage their own visibility. Showing a window
/// before the component's `init` has run would realize it too early (for
/// example triggering a "titlebar set on realized window" warning) and would
/// pop up dialogs before their initial `set_visible` is applied.
pub fn show_root_widget(widget: &impl AsRef<gtk::glib::Object>) {
    use gtk::prelude::{Cast, WidgetExt};

    if let Some(widget) = widget.as_ref().downcast_ref::<gtk::Widget>()
        && widget.downcast_ref::<gtk::Window>().is_none()
    {
        widget.show();
    }
}