Today, Jakub Steiner from the GNOME design team is going to talk about Adwaita, the default theme for GTK+; the tools that the designers can use to style GTK+; and how the toolkit changed to allow a better design workflow.
Adwaita is the user facing façade of GTK+. In the past GTK+ had no face; there was no properly defined look for the toolkit. Like many things in the FOSS world, it was a bring your own. There was Raleigh, a fallback skin that only showed up if something went sideways with theming or the system settings. And you didn’t really want to see that.
With GTK+ 3.0 a bold new effort has started. An effort to put visual designers in charge of visual design, using tools they understand. Instead of resorting to theme engines to draw unique controls, a styling engine used on the web has been chosen. The “everything is a box” CSS model applied to GTK+ rather well. It took a lot of effort, mainly on the shoulders of Benjamin Otte, who over the years managed to give us what we dreamed of: a CSS-like box model, allowing us to space elements/controls using padding, margins, borders and nifty features like minimum width. On the selector side, we aren’t dealing with the direct nested widget structure that changes from release to release, but we were given an abstracted, HTML-like DOM structure, with nodes and classes. Nodes are also consistently carrying state and are easier to animate.
In GTK+ there are a lot of controls that look like a button but aren’t a button. Every programmer is on the lazy side, and that’s a good thing. Designers aren’t any different. It’s so positive that there is an acronym for it, DRY — don’t repeat yourself. So in the old Adwaita, when we designed the look of some things that all looked the same, we only had one block of properties and a ton of selectors — the targets of that look. Buttons, dropdowns, you name it. Not much typing, but insanity to alter.
SASS came to the rescue by providing means to define a common drawing procedure once, but reuse it in a well structured stylesheet. You would be able to draw things “like a button” but not define it as a button. You would still find a dropdown nicely semantically organized in a dropdown section. SASS calls these macros mixins and you will find our drawing ones in src/gtk/theme/Adwaita/_drawing.scss.
/* Switch Slider being a button */
/* ... */
@include button(normal, $edge: $shadow_color);
A massive improvement for the designer’s workflow has been the introduction of the Inspector. The inspector is an invaluable tool to test out new style interactively or to figure out why a particular selector isn’t working. There are a couple of powerful tools it provides:
Widget selector. You can interactively point at a widget to learn about its properties or where it lives in the widget tree stack. Since 3.20 you can also learn about its CSS nodes, learn what sort of states it can get to, learn all the classes it has been assigned. It can also tell you where in the stylesheet the set property has been defined. This helps you figure out why your selector isn’t working. Somewhat. It would be real nice to see all matching selectors, even those that have been overriden by those that take precedence.
Interactive CSS stylesheet. You can write a CSS rule and have it applied in real time. This is not only useful to figure out a proper selector, but also experiment with drawing using GTK+ directly rather than using tools like Inkscape. Being able to iterate fast and try out things results in better design.
If this all sounds very similar to what modern browsers provide, it’s not much of a coincidence.
A major factor that’s making us less flexible in terms of being able to alter Adwaita are the graphic assets. There are still a couple of things that we have to resort to using image assets for. Those are actually in a large asset sheet SVG and we have a bunch of scripts to chop up multiple sized images (for HiDPI). It remains a hassle to add or change a particular bit.
To make this a little less boring, here’s a little web demo of how we could possibly avoid using image assets to draw GtkScale sliders and use simple CSS boxes instead:
The topic of how GTK+ draws the content of a window is a fairly complex one; it involves drilling down from GtkWidget, to GdkWindow, to Cairo, to the windowing system currently in use. This task can seem somewhat daunting, even for people that are familiar with the GTK+ API from an application development standpoint, so I decided to write down a quick introduction of how GTK+ draws, going from widgets, to windows, to surfaces, to native windowing resources.
How it starts
GTK+ always draws because something asked it to. This request may come from the windowing system — for instance, because the window manager presented your application window to the user, or because the user resized it — but more often it’ll come from a widget updating its contents. Let’s say, a progress bar going from 50% to 60%; or a label, changing its text; or a spinner, doing a new iteration. This request invalidates the backing GdkWindow of the widget — which usually it’s the GdkWindow of the top-level GtkWindow that contains the widget. Each invalidation carries with itself the region of the window to be invalidated (the “damage”), so that when we get to actually drawing, we know which parts of the window need to be updated, and we can avoid drawing outside of the damaged areas.
Race the clock
The first invalidation will start the “frame clock”; this clock is an object that keeps track of each phase inside a frame, like painting windows, laying out widgets, or processing the event queue. This allows GTK+ to be synchronized to things like the windowing system compositor, and to avoid performing unnecessary work that won’t be seen by the user — for instance, drawing something at 1000 frames per second when your display can only run at 60 Hz.
Once the clock reaches the “paint” phase, we process all the scheduled updates on a window; this will cause a GDK_EXPOSE event to be emitted. The GDK_EXPOSE event contains the GdkWindow that needs to be updated, and the union of all the invalidated areas. It’s important to note that, by and large, only top level windows will receive a GDK_EXPOSE event; for historical reasons, though, some widgets may apply a particular event mask that will cause GDK_EXPOSE events to be delivered to them as well. You should not write code that depends on that, and if you have legacy code ported from older versions of GTK+ 2.x you should really consider dropping the GDK_EXPOSURE_MASK from the event mask.
GTK+ takes the window and invalidated region out of the GDK_EXPOSE event and figures out which top level widget they belong to. Once that’s found, GTK+ will begin the actual rendering process. First of all, GTK+ will ask the GdkWindow to create a buffer where to draw the contents of the window; the buffer is going to be clipped to the region that needs to be drawn, and will be cleared with the background color of the window. GDK will create a “drawing context” — a transient object that keeps track of things like OpenGL and Cairo drawing. Then, GTK+ will ask the widget to draw itself using a Cairo context. For leaf widgets this means drawing themeselves on that context; for container widgets, this additionally means recursing through all their children. At the end of this process, GTK+ will end the frame by telling GDK to take the buffer that contains all the rendered widgets and use it to replace the current contents of the window. GDK will then ask the windowing system to present the window to the user, whenever it’s more appropriate.
The process outlined above has various caveats, and the code that deals with invalidation and validation of windows inside GDK is fairly complex; it also has a long history, which means that its API is littered by the headstones of ages past.
Before GTK+ 3.0, for instance, you were supposed to handle the “expose” events yourself, and create a Cairo context to draw on a widget by using gdk_cairo_create(); this has long since been unnecessary, because the GtkWidget::draw virtual function already provides us with a Cairo context with which to draw. The gdk_cairo_create() function, though, has been deprecated in GTK+ 3.22, and should not be used in newly written code; if you need a Cairo context you should create a similar Cairo surface, call cairo_create() on it, and then use the surface as the source for the Cairo context that GTK+ provides to you when drawing a widget. On the other hand, if you were using gdk_cairo_create() to draw on a top-level, native GdkWindow in response to a GDK_EXPOSE event then you should use the newly added gdk_window_begin_draw_frame(), gdk_window_end_draw_frame(), and GdkDrawingContext API instead.
The internals of the drawing code in GTK+ have been progressively updated over the years, to cope with things like new windowing systems, as well as other rendering API. It’s fairly certain that they will change again, especially when it comes to improving the rendering performance. Many of the changes that may seem arbitrary are, in reality, stepping stones towards reducing the time spent inside the toolkit in each frame, and leave more time to the application logic.
In this last week, GTK+ has seen 35 commits, with 3140 lines added and 2353 lines removed.
Planning and Status
The GTK+ hackfest starts today; topics include: CSS, layout management, portals for sandboxing, and GDK.
Matthias Clasen and Alex Larsson are working on an initial implementation of the “portal” for opening files in a sandboxed application.
Emmanuele Bassi merged his branch for simplifying the GdkWindow drawing entry points; this branch introduced a few new functions and a GdkDrawingContext class, while deprecating the old gdk_window_begin_paint* family of functions; gdk_window_end_paint(); gdk_cairo_create(); and gtk_widget_send_expose().
Tristan Van Berkom worked on fixing the fallout from the introduction of the new content sizing properties in GtkScrolledWindow, and ensured that the sizing requests are consistent.
Bug 767312 –gtk_widget_path_append_for_widget() misses classes unless gtk_widget_get_style_context() has been called
Bug 79229 –GtkScale with a big number of digits and value pos set to GTK_POS_TOP/BOTTOM is panted incorrectly
Starting from the next stable release, Gtk+ will also provide the maximum size counterparts of those properties.
What Do They Do?
The minimum sizes properties, as the name implies, define the minimum size, be it width or height, that the scrollable area will have – even if its child does not completely fill the available space.
The maximum content sizes, on the other hand, define how much the scrollable area is allowed to grow before its contents will starts scrolling.
Lets see it in action:
Where & How to Use Them
You want to use the new properties whenever you want to limit the size of the scrollable area. For example, GtkPopover always shrinks its children widgets to their minimum sizes. The following section exemplifies how to make the content grow to at most 300px, both width and height wise:
In this last week, GTK+ has seen 55 commits, with 2378 lines added and 1493 lines removed.
Planning and Status
Carlos Soriano has been working on further experiments on the new path bar widget in varioustopicbranches.
Emmanuele Bassi has been working on a new API for simplifying the drawing entry points inside GtkWidget, in preparation for the new rendering API inside GSK.
Timm Bäder added an accessible representation for GtkStack to only show the current visible child in accessibility tools.
Matthias Clasen added a warning in case we emit the GtkWidget::draw call on a widget without an allocation; this is not supposed to happen, and a warning allows tracking badly behaving widgets.
Lapo Calamandrei fixed Adwaita with regards to menu items, info bars, and the caret color of selectable lables.
Matthias Clasen deprecated the GtkSizeGroup:ignore-hidden property, and documented it as broken; sizing of invisible widgets is not really possible, as they lack access to windowing system and style resources. It is recommended to use a GtkStack, instead, to reserve space for hidden widgets.
A new GDK device source type, GDK_SOURCE_TRACKPOINT, has been added to represent Trackpoint/point stick pointing devices; this new device type can be used to implement device-specific behavior in widgets.
CSS text styling properties can now be used on the value and marks of a GtkScale and GtkProgressbar widgets.
Georges Basile Stavracas Neto implemented the max-content-width and max-content-height properties in GtkScrolledWindow; Tristan Van Berkom fixed long-standing sizing issues with the existing min-content-width and min-content-height properties.
Bug 745622 –Selected text not highlighted in GtkInfoBar
Bug 767058 –GtkInfoBar: right-click/context menu all white
Bug 767052 –Wayland: Iconifying a modal dialog makes the app unusable
Bug 767100 –Add an input source type for trackpoints
Bug 767108 –Separators not correctly placed in GtkPopover
Bug 767093 –wayland: Provide information about scroll devices