Gtk4 attach actions to adw.dialog

So, I trying to add a new dialog and attach some actions to the dialog object, but it crashes the application with this error:

thread 'main' panicked at C:\Users\OSA413\.cargo\registry\src\index.crates.io-1949cf8c6b5b557f\glib-0.21.0\src\object.rs:122:23:
assertion failed: self.is::<T>()

This error is not very informative but my guess is that there should be another type used for such things (I mean to be able to attach actions to the window). Does AdwDialog support attaching actions? If not, how should I call custom logic from a dialog (or something seemingly as a dialog window)?

Here’s the code of the dialog for reference:

use adw::{prelude::AdwDialogExt, subclass::prelude::*};
use gtk::{gio::{self, prelude::ActionMapExtManual}, glib};

mod imp {
    use super::*;

    #[derive(Debug, Default, gtk::CompositeTemplate)]
    #[template(resource = "/proj/proj/window_settings.ui")]
    pub struct SettingsWindow {
    }

    #[glib::object_subclass]
    impl ObjectSubclass for SettingsWindow {
        const NAME: &'static str = "Settings";
        type Type = super::SettingsWindow;
        type ParentType = adw::Dialog;

        fn class_init(klass: &mut Self::Class) {
            klass.bind_template();
        }

        fn instance_init(obj: &glib::subclass::InitializingObject<Self>) {
            obj.init_template();
        }
    }

    impl ObjectImpl for SettingsWindow {
        fn constructed(&self) {
            self.parent_constructed();
            self.obj().setup_actions();
        }
    }

    impl WidgetImpl for SettingsWindow {}
    impl AdwDialogImpl for SettingsWindow {}
}

glib::wrapper! {
    pub struct SettingsWindow(ObjectSubclass<imp::SettingsWindow>)
        @extends gtk::Widget, adw::Dialog,
        @implements 
            gio::ActionGroup,
            gio::ActionMap,
            gtk::ConstraintTarget,
            gtk::Buildable,
            gtk::Accessible,
            gtk::ShortcutManager,
            gtk::Root,
            gtk::Native;          
}

impl SettingsWindow {
    pub fn new() -> Self {
        glib::Object::builder().build()
    }

    fn setup_actions(&self) {
        let close_action = gio::ActionEntry::builder("close")
            .activate(move |app: &Self, _, _| {app.close();})
            .build();

        self.add_action_entries([
            close_action
        ]); //It crashes here
    }
}

It does not, it doesn’t implement half of the types you mentioned in @implements either.

It does support gtk_widget_class_install_action() and gtk_widget_insert_action_group(), like any other widget.

2 Likes

So I managed to solve the issue by changing the type from AdwDialog to AdwApplicationWindow (AdwWindow didn’t have the close function so I had to find a workaround).

The original purpose of the AdwDialog was to be used with split view, fortunately I found that it’s possible to do the same with AdwWindow from this article:

(it’s almost the same as from the AdwDialog example with split view except for a few property names).

As cons: the parent window doesn’t become inactive and you can spawn as many dialogs as you want, but I’ll leave it as is for now.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.