Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GTK: get rid of the system theme/CSS alltogether

GTK3 applications can be styled and themed with CSS. How can I force GTK to use solely the CSS shipped with my application, instead of combining / cascading it with the theme installed on the user's system?

Why would one want to do that? In theory, CSS would be a great rules-based styling system, where you just add one screen full judiciously chosen rules to define the looks of your application consistently. Unfortunately, in practice, people tend to add hundreds of brain-dead, repetitious definitions into their stylesheets, thereby counterfeiting the very nature of cascading.

The actual problem is that my application shall get the typical, subdued dark-grey look of a media handling application, irrespective of the global theme installed on the user's system. From reading the GTK documentation and the GTKmm tutorial, I came up with the following code to read my own CSS stylesheet:

auto screen = Gdk::Screen::get_default();
auto css_provider = Gtk::CssProvider::create();
try {
    css_provider->load_from_path(lib::resolveModulePath (stylesheetName, resourceSerachPath));

} catch(Glib::Error const& failure) {
    WARN(gui, "Failure while loading stylesheet '%s': %s", cStr(stylesheetName), cStr(failure.what()));
}

Gtk::StyleContext::add_provider_for_screen(screen, css_provider, GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

Technically this works perfect. However -- now I am forced to work "against" the CSS rules present on the user's system. If I start with setting a suitable border colour in the default rule and a dark grey background and suitable text colour in a rule for GtkWindow, plus some rule for text entry fields, I get the desired look on some parts, but whenever the user's system theme adds a specific definition for some specific widget or widget combination, thereby repeating explicitly the colours instead of inheriting them from a generic rule, I need to add yet another specific rule into my application style sheet to fix that specific case. That is, the possibly ill-guided structure of the installed system theme forces my application stylesheet into the same ill-guided bad practice and have hundreds of single-case definitions repeating the same values over and over.

Questions

  • How can I tell GTK to use only my stylesheet for my application?
  • Is there any viable alternative, which is not so radical, yet still allows me to use CSS the way it was intended to be used (minimal set of rules + cascading)?
like image 902
Ichthyo Avatar asked Oct 04 '18 21:10

Ichthyo


People also ask

Why does GTK use CSS?

GTK supports the CSS import rule, in order to load another style sheet in addition to the currently parsed one.

How do I change my theme on GTK?

If you use Cinnamon, use Themes tool (cinnamon-settings themes): go to System Settings > Themes. If you use Enlightenment: go to Settings > All > Look > Application Theme. If you use GNOME, use GNOME Tweaks (gnome-tweaks): install gnome-tweaks.

Where is GTK CSS located?

Gtk-3.0 folder will contain the gtk. css file that defines the look and behavior of the theme on the Gnome 3 desktop and its derivatives, like Cinnamon and Ubuntu's Unity. The themes are stored in two locations. Globally, they go under /usr/share/themes.

What is GTK theme?

GTK+ is a graphical user interface library used by the SpeX Applications. One of the cool feature of GTK+ is its support of themes. Themes allows you to customize the color and shape of widget by assigning colors and/or pixmap to GTK application.


1 Answers

You can use the reset.css file from the GTK demos to unset all existing rules.

A less radical alternative might be to just ignore user themes other than the default. Presumably if a user has a theme built with bad CSS practices, they nonetheless like that theme. So why shouldn't they be able to view your application with their preferred theme?

like image 71
ptomato Avatar answered Oct 13 '22 06:10

ptomato