Setting dark mode color scheme initial state in official adwaita tutorial

Hi,

I tried to follow the official tutorial to learn Gnome application development with modern Adwaita widgets. I managed to get to the last step without any problem, but my app fails to compile when I try to run adw_style_manager_get_default in the text_viewer_application_init function.

Did I get something wrong or is the tutorial to blame?

The problematic code is in this page, in the second to last code snippet : Forcing The Dark Color Scheme - GNOME Developer Documentation

More specifically, here:

static void
text_viewer_application_init (TextViewerApplication *self)
{
  self->settings = g_settings_new ("com.example.TextViewer");

  gboolean dark_mode = g_settings_get_boolean (self->settings, "dark-mode");
  AdwStyleManager *style_manager = adw_style_manager_get_default ();
  […]

I get this error:

(process:2): Gdk-ERROR **: 00:31:06.667: gdk_display_manager_get() was called before gtk_init()

I managed to get the application to compile by adding a gtk_init(); at the beginning of the function, but I somehow know that’s not the right way to solve this problem.

Thanks in advance to those who can help me understand the issue at hand.

You called the fn too early, this has to happen after adw_init which is called during an AdwApplication’s startup phase. Init goes before that.

Thanks @Maximiliano for your answer.

I did understand that this function was called too early, but I followed the tutorial very strictly.

I am but a very beginner developer, but I guess the tutorial is wrong. Could someone check it to confirm my guess, and correct it ?

I’ll try to implement a _startup function in which I’ll call the problematic function, but I might still be wrong.

This is a change in GTK/libadwaita that was introduced after the tutorial was written.

The appropriate place to set up the style manager is the GApplicationClass.startup virtual function, where adw_init() is called, e.g.

static void
text_viewer_application_startup (GApplication *application)
{
  // chain up, to let GTK and libadwaita initialize themselves
  G_APPLICATION_CLASS (text_viewer_application_parent_class)->startup (application);

  TextViewerApplication *self = TEXT_VIEWER_APPLICATION (application);

  gboolean dark_mode = g_settings_get_boolean (self->settings, "dark-mode");
  AdwStyleManager *style_manager = adw_style_manager_get_default ();

  // ...
}
1 Like

Thanks a lot @ebassi for your answer. I did manage to find, by myself, the exact same solution to this problem by replicating what I previously learned in Gtk’s official tutorials. You confirmed I made the correct technical choice (and I have to admit I’m quite proud of myself…)

Who could I contact to help update Gnome’s tutorial ? There are several other minor differences between what is taught and Gnome Builder’s default application code.

You should file an issue on the developer docs website’s issue tracker.