Skip to content

Commit adde759

Browse files
committed
Document new extra_css_classes attribute
1 parent 4e1b59e commit adde759

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

docs/gtk.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@ GTK 4's internals and class hierarchy are quite different from GTK 3, despite ma
1414

1515
LuaGObject adds the `.width` and `.height` pseudo-property to all GTK 4 widgets. When read, these properties return the dimensions of the widget's current allocated space. When written, they set the value of the `width-request` or `height-request` properties which determine a widget's minimum size.
1616

17+
## Adding CSS Classes
18+
19+
GTK Widgets have a built-in property called `css-classes`, which is a list of CSS classes that the widget should use for styling purposes. When assigning to this property, any CSS class that the widget already has is removed. This often leads to odd behaviour, as certain widgets will automatically add their own CSS classes. One such example is `Gtk.Box`; When its `orientation` is set to `VERTICAL`, it gains the `.vertical` CSS class. This means that the given example produces subtly incorrect behaviour:
20+
21+
local box = Gtk.Box {
22+
orientation = "VERTICAL",
23+
css_classes = { "linked" },
24+
}
25+
print(box:has_css_class "vertical") -- prints "false"
26+
27+
LuaGObject provides a pseudo-property called `.extra_css_classes` for this purpose. This attribute can be set either to a string or a table containing any number of strings in the array part, each of which is added as a CSS class. To correct the previous example:
28+
29+
local box = Gtk.Box {
30+
orientation = "VERTICAL",
31+
extra_css_classes = { "linked" },
32+
-- extra_css_classes = "linked" also works!
33+
}
34+
print(box:has_css_class "vertical") -- prints "true"
35+
36+
**It is recommended to use this `.extra_css_classes` attribute when initializing widgets,** in order to prevent bad behaviour.
37+
1738
## Accessing Child Widgets
1839

1940
Any widget's children may be accessed through the `.children` pseudo-property. This returns a table which—when indexed with a number n—returns the n-th child of that widget, or `nil` if there is no n-th child.

0 commit comments

Comments
 (0)