Skip to content

Commit 4e1b59e

Browse files
authored
Add extra_css_classes Gtk.Widget attribute (#3)
This new attribute functions like the css_classes built-in property of a Gtk.Widget, except it doesn't overwrite existing properties on an object. This allows classes like Gtk.Box—which adds a .vertical CSS class if it's oriented vertically—to function properly while setting CSS classes in the constructor.
1 parent 20435ae commit 4e1b59e

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

LuaGObject/override/Gtk4.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Gtk.Widget._attribute = {
2727
width = { get = Gtk.Widget.get_allocated_width },
2828
height = { get = Gtk.Widget.get_allocated_height },
2929
children = {},
30+
extra_css_classes = {},
3031
}
3132

3233
-- Allow to query a widget's currently-allocated dimensions by indexing .width or .height, and set the requested dimensions by assigning these pseudo-properties.
@@ -70,6 +71,21 @@ function Gtk.Widget._attribute.children:get()
7071
return setmetatable({ _widget = self }, widget_children_mt)
7172
end
7273

74+
function Gtk.Widget._attribute.extra_css_classes:get()
75+
error("%s: Cannot read extra_css_classes; attribute is read-only.", self._type.name)
76+
end
77+
function Gtk.Widget._attribute.extra_css_classes:set(value)
78+
if type(value) == "string" then
79+
value = { value }
80+
end
81+
if type(value) ~= "table" then
82+
error("%s: Cannot assign value of type %s to extra_css_classes", self._type.name, type(value))
83+
end
84+
for _, c in pairs(value) do
85+
self:add_css_class(c)
86+
end
87+
end
88+
7389
-- Simple container support --
7490

7591
Gtk.Box._container_add = Gtk.Box._method.append

tests/gtk.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,14 @@ function gtk.notebook_container()
134134
check(notebook:get_nth_page(1) == label2)
135135
check(notebook:get_nth_page(2) == label3)
136136
end
137+
138+
function gtk.extra_css_classes()
139+
local Gtk = LuaGObject.Gtk
140+
local box = Gtk.Box {
141+
orientation = "VERTICAL",
142+
extra_css_classes = { "linked" },
143+
}
144+
check(box:has_css_class "linked")
145+
-- The .vertical CSS class comes from setting the orientation to vertical. By checking it here, the test ensures that the existing class isn't overwritten by extra_css_classes.
146+
check(box:has_css_class "vertical")
147+
end

0 commit comments

Comments
 (0)