Specifically, I wanted to make the custom widget (a Gtk.Box
) have the background of Gtk.Entry
so that it will look interactive or harmony no matter in which theme.
In Gtk3, I got that with the following method:
imports.gi.versions.Gtk = "3.0";
const { Gtk, GObject } = imports.gi;
Gtk.init(null);
var ExampleApp = GObject.registerClass(
class ExampleApp extends Gtk.Application {
vfunc_activate() {
let appWindow = new Gtk.ApplicationWindow({ application: this, });
let add = new Gtk.Box({ halign: Gtk.Align.CENTER });
add.pack_start(new Gtk.Image({ icon_name: 'list-add-symbolic' }), false, false, 0);
let bgcolor = new Gtk.Entry().get_style_context().get_background_color(Gtk.STATE_FLAG_NORMAL).to_string();
let context = add.get_style_context();
let provider = new Gtk.CssProvider();
provider.load_from_data(`.abox { background-color: ${bgcolor}; min-height: 2em; } `);
context.add_class('abox');
context.add_provider(provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION);
appWindow.add(add);
appWindow.set_default_size(200, 200);
appWindow.show_all();
}
});
new ExampleApp().run([imports.system.programInvocationName].concat(ARGV));
But there is no Gtk.StyleContext.get_background_color()
in Gtk4, also, I tried adding the style class directly context.add_class('entry')
but it’s not working.
So, is it possible? If yes, How should I do it?