Error using GtkFontDialogButton and GtkColorDialogButton

I’m running into a problem using the new GtkColorDialogButton and GtkFontDialogButton widgets in Rust. The buttons appear in my preferences dialog correctly, but when clicked give the following error messages.

(eva:11525): Gtk-CRITICAL **: 22:00:00.396: gtk_font_dialog_choose_font: assertion 'GTK_IS_FONT_DIALOG (self)' failed

(eva:11525): Gtk-CRITICAL **: 22:00:06.575: gtk_color_dialog_choose_rgba: assertion 'GTK_IS_COLOR_DIALOG (self)' failed

Gtk is at version 4.10.0, and I’m using the latest release of gtk-rs with the crate feature “v4_10” enabled.

Can you share how you’re constructing these buttons?

I’ve seen this sort of error when I used a builder, and gave ‘inconsistent’ arguments to it. The arguments were inconsistent in that GTK was expecting a field to be a particular pointer, because I’d given another builder function a different pointer, but I set that (first) field to a pointer different to what it was expecting.

Rather than paste the entire files, this should be the relative parts of the ui file and imp module.

preferences_window.ui

    <child>
      <object class="AdwPreferencesPage">
        <property name="title">Fonts</property>
        <child>
          <object class="AdwPreferencesGroup">
            <property name="name">Fonts</property>
            <property name="title">Fonts</property>
            <child>
              <object class="AdwActionRow">
                <property name="title">Regular Font</property>
                <child>
                  <object class="GtkFontDialogButton" id="pg_font">
                    <property name="halign">fill</property>
                    <property name="valign">center</property>
                  </object>
                </child>
              </object>
            </child>
<!-- some lines omitted -->
    <child>
      <object class="AdwPreferencesPage">
        <property name="title">Colors</property>
        <child>
          <object class="AdwPreferencesGroup">
            <property name="name">Colors</property>
            <property name="title">Colors</property>
            <child>
              <object class="AdwActionRow">
                <property name="title" translatable="yes">Regular text</property>
                <child>
                  <object class="GtkColorDialogButton" id="fg_color">
                    <property name="hexpand">false</property>
                    <property name="vexpand">false</property>
                    <property name="valign">center</property>
                    <property name="rgba">rgb(153,193,241)</property>
                  </object>
                </child>
              </object>
            </child>

preferences_window/imp.rs

#[derive(CompositeTemplate, Default)]
#[template(file = "preferences_window.ui")]
pub struct PreferencesWindow {
    #[template_child]
    pub pg_font: TemplateChild\<gtk::FontDialogButton\>,
    // Some other widgets..
    #[template_child]
    pub fg_color: TemplateChild\<gtk::ColorDialogButton\>,

The only other parts that might be relevant are that I’m also binding the selected font to some items in a gio::Settings object. I was planning to do the same with the color settings but didn’t get that far, as it’s no use to me until I can make the new widgets actually work.

    pub fn bind_settings(&self, settings: &Settings) {
        settings
            .bind("paragraph-font", &self.pg_font.get(), "font-desc")
            .set_mapping(|_, font| {
                Some(font.as_str().into())
            })
            .mapping(|font,_| {
                let font = font.get::<String>().unwrap();
                Some(FontDescription::from_string(&font).into())
            })
            .build();

Are there any other errors in the log?

No, no compile errors and only the runtime errors I posted above.

There should maybe be a better error message, but: you need to set the GtkFontDialog for the the GtkFontDialogButton to work.

1 Like

Ah, I was assuming that would be handled during creation, like the old GtkFontButton. I’ll revisit it later today. Thanks.

@matthiasc thanks, that was indeed the problem.

For future reference, if anyone else encounters this, I just added an object for each FontDialog or ColorDialog corresponding to their respective buttons to my ui template, and then bound them using the “dialog” property on those buttons. The deprecated widgets automatically constructed those dialogs for you, while the new apparently expect you to explicitly do so. Not really a problem now that I know, it’s just not the behavior I expected.

<!-- preferences_window.ui-->
<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <requires lib="gtk" version="4.0"/>
  <object class="GtkFontDialog" id="pg_font_dialog"/>
<!-- lines omitted for brevity -->
    <child>
      <object class="AdwPreferencesPage">
        <property name="title">Fonts</property>
        <child>
          <object class="AdwPreferencesGroup">
            <property name="name">Fonts</property>
            <property name="title">Fonts</property>
            <child>
              <object class="AdwActionRow">
                <property name="title">Regular Font</property>
                <child>
                  <object class="GtkFontDialogButton" id="pg_font">
                    <property name="halign">fill</property>
                    <property name="valign">center</property>
                    <property name="dialog">pg_font_dialog</property>
                  </object>
                </child>
              </object>
            </child>

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