What is the current correct way to use css in Gtk 4.12.3 wayland?
Am getting conflicting information about StyleContext, where some docs say its depreciated, while others say its OK for the Display.
I've tried Gtk.StyleContext.add_provider_for_display()
and set_css_classes()
, which works but spews heaps of warnings like this:
/home/cpb/Desktop/txt/source/valatemp_nil.vala:31.3-31.18: warning: `Gtk.StyleContext' has been deprecated since 4.10
31 | Gtk.StyleContext.add_provider_for_display(thisdisplay, allcsp, Gtk.STYLE_PROVIDER_PRIORITY_USER);
| ^~~~~~~~~~~~~~~~
/home/cpb/Desktop/txt/source/valatemp_nil.vala.c: In function ‘__lambda4_’:
/home/cpb/Desktop/txt/source/valatemp_nil.vala.c:127:61: warning: passing argument 2 of ‘gtk_widget_set_css_classes’ from incompatible pointer type [-Wincompatible-pointer-types]
127 | gtk_widget_set_css_classes ((GtkWidget*) container, _tmp10_);
| ^~~~~~~
| |
| gchar ** {aka char **}
In file included from /usr/include/gtk-4.0/gtk/gtkapplication.h:26,
from /usr/include/gtk-4.0/gtk/gtkwindow.h:32,
from /usr/include/gtk-4.0/gtk/gtkaboutdialog.h:29,
from /usr/include/gtk-4.0/gtk/gtk.h:33,
from /home/cpb/Desktop/txt/source/valatemp_nil.vala.c:10:
/usr/include/gtk-4.0/gtk/gtkwidget.h:929:71: note: expected ‘const char **’ but argument is of type ‘gchar **’ {aka ‘char **’}
929 | const char **classes);
...
and a sample application, css is a string, fed to a CssProvider which is fed to a StyleContext for default display, which is used by set_css_classes per widget:
using Gtk;
int main (string[] args) {
Gtk.Application wut = new Gtk.Application ("com.test.test", GLib.ApplicationFlags.DEFAULT_FLAGS);
wut.activate.connect(() => {
Gtk.ApplicationWindow win = new Gtk.ApplicationWindow(wut);
Gtk.Box container = new Gtk.Box(VERTICAL,0);
Gtk.Box socket = new Gtk.Box(HORIZONTAL,0);
Gtk.ToggleButton btn = new Gtk.ToggleButton();
string csses = """
.rr {
background: #FF5555;
}
.bb {
background: #5555FF;
}
.gg {
background: #55ff55;
}
""";
Gtk.CssProvider allcsp = new Gtk.CssProvider();
allcsp.load_from_string(csses);
Gdk.Display thisdisplay = Gdk.Display.get_default();
Gtk.StyleContext.add_provider_for_display(thisdisplay, allcsp, Gtk.STYLE_PROVIDER_PRIORITY_USER);
container.set_css_classes({"bb"});
socket.set_css_classes({"gg"});
btn.set_css_classes({"rr"});
socket.append(btn);
container.append(socket);
win.set_child(container);
win.default_width = 300;
win.default_height = 300;
win.present();
});
return wut.run(args);
}
After some research, I discovered the answer: It's a documentation error. Specifically in the Gtk documentation and Vala's bind.
The Gtk documentation says that the entire
Gtk.StyleContext
class is deprecated, but this is an error. Only the functions are deprecated, the static functions of the class (Gtk.StyleContext.add_provider_for_display
andGtk.StyleContext.remove_provider_for_display
) and the class itself are not deprecated.Here is an issue that talks about it.
In short, the correct way is to use
Gtk.StyleContext.add_provider_for_display()
, the exceptions thrown are an error in Vala's bind, which follows the error in the Gtk documentation.