79394086

Date: 2025-01-28 14:05:03
Score: 0.5
Natty:
Report link

Ok I found it from https://discourse.gnome.org/t/what-good-is-gtkcssprovider-without-gtkstylecontext/12621/2

Basically you should use gtk::style_context_add_provider_for_display() function.

Here a rust snippet that can be easily translated in other languages


 let my_textview = gtk::TextView::new(); //or any other widget with display
 let family = "Arial";
 let size = 14;

 let provider = gtk::CssProvider::new();

 let mut css = String::new();

 css.push_str("textview {");
 css.push_str("font-size: ");
 css.push_str(&size.to_string());
 css.push_str("px;\n");

 if let Some(family) = family {
   css.push_str("font-family: ");
   css.push('"');
   css.push_str(family);
   css.push_str("\";\n");
 } 
 css.push_str("}");

 provider.load_from_string(&css);

 gtk::style_context_add_provider_for_display(
 &my_textview.display(),
 &provider,
 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION as u32,
 );

Reasons:
  • Probably link only (1):
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Low reputation (0.5):
Posted by: Pier