Skip to content

Commit 2afedf7

Browse files
committed
Adw override
Also reformats the code and related documentation to be more sane
1 parent 180057a commit 2afedf7

File tree

6 files changed

+525
-471
lines changed

6 files changed

+525
-471
lines changed

LuaGObject/override/Adw.lua

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
-- LuaGObject Adw (libadwaita) overrides
2+
-- © 2025 Victoria Lacroix
3+
-- Licensed under the terms of an MIT license: http://www.opensource.org/licenses/mit-license.php
4+
5+
local LuaGObject = require "LuaGObject"
6+
local Adw = LuaGObject.Adw
7+
local Gtk = LuaGObject.Gtk
8+
9+
local log = LuaGObject.log.domain "LuaGObject.Adw"
10+
11+
-- Constructor container support --
12+
13+
Adw.PreferencesDialog._container_add = Adw.PreferencesDialog._method.add
14+
Adw.PreferencesPage._container_add = Adw.PreferencesPage._method.add
15+
Adw.PreferencesGroup._container_add = Adw.PreferencesGroup._method.add
16+
Adw.ExpanderRow._container_add = Adw.ExpanderRow.add_row
17+
18+
-- These classes were introduced in later versions of Adw, and thus may not always be available.
19+
if Adw.WrapBox then
20+
Adw.WrapBox._container_add = Adw.WrapBox._method.append
21+
end
22+
if Adw.ShortcutsDialog then
23+
Adw.ShortcutsDialog._container_add = Adw.ShortcutsDialog.add
24+
end
25+
if Adw.ShortcutsSection then
26+
Adw.ShortcutsSection._container_add = Adw.ShortcutsSection.add
27+
end
28+
29+
-- Adw.ActionRow overrides --
30+
31+
Adw.ActionRow._attribute = {
32+
prefixes = {},
33+
suffixes = {},
34+
}
35+
36+
function Adw.ActionRow._attribute.prefixes:get()
37+
error("%s: Cannot read prefixes; attribute is write-only.", self.type.name)
38+
end
39+
function Adw.ActionRow._attribute.prefixes:set(value)
40+
if Gtk.Widget:is_type_of(value) then
41+
value = { value }
42+
end
43+
if type(value) ~= "table" then
44+
error("%s: Can only write table or Gtk.Widget to add_prefixes.", self.type.name)
45+
end
46+
for _, c in ipairs(value) do
47+
self:add_prefix(c)
48+
end
49+
end
50+
51+
function Adw.ActionRow._attribute.suffixes:get()
52+
error("%s: Cannot read suffixes; attribute is write-only.", self.type.name)
53+
end
54+
function Adw.ActionRow._attribute.suffixes:set(value)
55+
if Gtk.Widget:is_type_of(value) then
56+
value = { value }
57+
end
58+
if type(value) ~= "table" then
59+
error("%s: Can only write table or Widget to add_suffixes.", self.type.name)
60+
end
61+
for _, v in ipairs(value) do
62+
self:add_suffix(v)
63+
end
64+
end
65+
66+
-- Adw.HeaderBar overrides --
67+
68+
Adw.HeaderBar._attribute = {
69+
end_packs = {},
70+
start_packs = {},
71+
}
72+
73+
function Adw.HeaderBar._attribute.end_packs:get()
74+
error("%s: Cannot read end_packs; attribute is write-only.", self.type.name)
75+
end
76+
function Adw.HeaderBar._attribute.end_packs:set(value)
77+
if Gtk.Widget:is_type_of(value) then
78+
value = { value }
79+
end
80+
if type(value) ~= "table" then
81+
error("%s: Can only write table or Widget to end_packs.", self.type.name)
82+
end
83+
for _, v in ipairs(value) do
84+
self:pack_end(v)
85+
end
86+
end
87+
88+
function Adw.HeaderBar._attribute.start_packs:get()
89+
error("%s: Cannot read start_packs; attribute is write-only.", self.type.name)
90+
end
91+
function Adw.HeaderBar._attribute.start_packs:set(value)
92+
if Gtk.Widget:is_type_of(widget) then
93+
value = { value }
94+
end
95+
if type(value) ~= "table" then
96+
error("%s: Can only write table or Widget to start_packs.", self.type.name)
97+
end
98+
for _, v in ipairs(value) do
99+
self:pack_start(v)
100+
end
101+
end
102+
103+
-- Adw.ToolbarView overrides --
104+
105+
-- Adw.ToolbarView was introduced in Adw 1.4, and may not be available.
106+
if Adw.ToolbarView then
107+
Adw.ToolbarView._attribute = {
108+
bottom_bars = {},
109+
top_bars = {},
110+
}
111+
112+
function Adw.ToolbarView._attribute.bottom_bars:get()
113+
error("%s: Cannot read bottom_bars; attribute is write-only.", self.type.name)
114+
end
115+
function Adw.ToolbarView._attribute.bottom_bars:set(value)
116+
if Gtk.Widget:is_type_of(value) then
117+
value = { value }
118+
end
119+
if type(value) ~= "table" then
120+
error("%s: Can only write table or Gtk.Widget to add_bottom_bars.", self.type.name)
121+
end
122+
for _, v in ipairs(values) do
123+
self:add_bottom_bar(v)
124+
end
125+
end
126+
127+
function Adw.ToolbarView._attribute.top_bars:get()
128+
error("%s: Cannot read top_bars; attribute is write-only.", self.type.name)
129+
end
130+
function Adw.ToolbarView._attribute.top_bars:set(value)
131+
if Gtk.Widget:is_type_of(value) then
132+
value = { value }
133+
end
134+
if type(value) ~= "table" then
135+
error("%s: Can only write table or Gtk.Widget to add_top_bars.", self.type.name)
136+
end
137+
for _, v in ipairs(value) do
138+
self:add_top_bar(v)
139+
end
140+
end
141+
end

LuaGObject/override/Gtk.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ local LuaGObject = require 'LuaGObject'
66
local Gtk = LuaGObject.Gtk
77

88
if Gtk.get_major_version() <= 3 then
9-
return require 'LuaGObject.override.Gtk3'
9+
return require 'LuaGObject.override.Gtk3'
1010
elseif Gtk.get_major_version() == 4 then
11-
return require 'LuaGObject.override.Gtk4'
11+
return require 'LuaGObject.override.Gtk4'
1212
elseif Gtk.get_major_version() > 4 then
13-
-- No override for Gtk5 or later… yet.
14-
return
13+
-- No override for Gtk5 or later… yet.
14+
return
1515
end

LuaGObject/override/Gtk3.lua

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ local cairo = LuaGObject.cairo
1919

2020
local log = LuaGObject.log.domain('LuaGObject.Gtk3')
2121

22-
assert(Gtk.get_major_version() <= 3)
23-
2422
-- Initialize GTK.
2523
Gtk.disable_setlocale()
2624
if not Gtk.init_check() then
@@ -110,7 +108,7 @@ Gdk.Window._attribute.widget = {
110108
get = Gdk.Window.get_widget,
111109
}
112110

113-
-------------------------------- Gtk.Buildable overrides.
111+
--------------------------------- Gtk.Buildable overrides.
114112
Gtk.Buildable._attribute = { id = {}, child = {} }
115113

116114
-- Create custom 'id' property, mapped to buildable name.
@@ -164,7 +162,7 @@ function container_property_item_mt:__index(name)
164162
local pspec = self._container._class:find_child_property(name)
165163
if not pspec then
166164
error(("%s: no child property `%s'"):format(
167-
self._container.type._name, name:gsub('%-', '_')), 2)
165+
self._container.type._name, name:gsub('%-', '_')), 2)
168166
end
169167
local value = GObject.Value(pspec.value_type)
170168
self._container:child_get_property(self._child, name, value)
@@ -175,7 +173,7 @@ function container_property_item_mt:__newindex(name, val)
175173
local pspec = self._container._class:find_child_property(name)
176174
if not pspec then
177175
error(("%s: no child property `%s'"):format(
178-
self._container.type._name, name:gsub('%-', '_')), 2)
176+
self._container.type._name, name:gsub('%-', '_')), 2)
179177
end
180178
local value = GObject.Value(pspec.value_type, val)
181179
self._container:child_set_property(self._child, name, value)
@@ -184,7 +182,7 @@ local container_property_mt = {}
184182
function container_property_mt:__index(child)
185183
if type(child) == 'string' then child = self._container.child[child] end
186184
return setmetatable({ _container = self._container, _child = child },
187-
container_property_item_mt)
185+
container_property_item_mt)
188186
end
189187
function Gtk.Container._attribute.property:get()
190188
return setmetatable({ _container = self }, container_property_mt)
@@ -198,11 +196,11 @@ local container_child_mt = {}
198196
function container_child_mt:__index(id)
199197
if type(id) ~= 'string' then return nil end
200198
local found = (core.object.env(self._container).id == id
201-
and self._container)
199+
and self._container)
202200
if not found then
203201
for _, child in ipairs(self) do
204-
found = child.child[id]
205-
if found then break end
202+
found = child.child[id]
203+
if found then break end
206204
end
207205
end
208206
return found or nil
@@ -544,9 +542,9 @@ function Gtk.Assistant:add(child)
544542
local widget = child[1]
545543
self:append_page(widget)
546544
for name, value in pairs(child) do
547-
if type(name) == 'string' then
548-
self['set_page_' .. name](self, widget, value)
549-
end
545+
if type(name) == 'string' then
546+
self['set_page_' .. name](self, widget, value)
547+
end
550548
end
551549
else
552550
self:append_page(widget)

LuaGObject/override/Gtk4.lua

Lines changed: 14 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@
22
-- © 2025 Victoria Lacroix
33
-- Licensed under the terms of an MIT license: http://www.opensource.org/licenses/mit-license.php
44

5-
local LuaGObject = require 'LuaGObject'
6-
local core = require 'LuaGObject.core'
5+
local LuaGObject = require "LuaGObject"
76
local Gtk = LuaGObject.Gtk
87
local Gdk = LuaGObject.Gdk
9-
local GObject = LuaGObject.GObject
10-
local cairo = LuaGObject.cairo
118

12-
local log = LuaGObject.log.domain('LuaGObject.Gtk4')
9+
local log = LuaGObject.log.domain "LuaGObject.Gtk4"
1310

1411
assert(Gtk.get_major_version() == 4)
1512

@@ -29,7 +26,7 @@ Gtk.Allocation = Gdk.Rectangle
2926
Gtk.Widget._attribute = {
3027
width = { get = Gtk.Widget.get_allocated_width },
3128
height = { get = Gtk.Widget.get_allocated_height },
32-
child = {},
29+
children = {},
3330
}
3431

3532
-- Allow to query a widget's currently-allocated dimensions by indexing .width or .height, and set the requested dimensions by assigning these pseudo-properties.
@@ -41,8 +38,8 @@ function Gtk.Widget._attribute.height:set(height)
4138
end
4239

4340
-- Access children by index. [1] returns the first child, [2] returns the second, [-1] returns the last, [-2] returns the second-last. If no child exists at the given index, returns nil.
44-
local widget_child_mt = {}
45-
function widget_child_mt:__index(key)
41+
local widget_children_mt = {}
42+
function widget_children_mt:__index(key)
4643
if type(key) ~= "number" then
4744
error("%s: cannot access child at non-numeric index", self._widget.type.name)
4845
end
@@ -65,85 +62,17 @@ function widget_child_mt:__index(key)
6562
return child
6663
end
6764

68-
function widget_child_mt:__newindex()
69-
error("%s: cannot assign child", self._widget.type.name)
65+
function widget_children_mt:__newindex()
66+
error("%s: child widgets cannot be assigned", self._widget.type.name)
7067
end
7168

72-
function Gtk.Widget._attribute.child:get()
73-
return setmetatable({ _widget = self }, widget_child_mt)
69+
function Gtk.Widget._attribute.children:get()
70+
return setmetatable({ _widget = self }, widget_children_mt)
7471
end
7572

76-
-- Gtk.Box overrides --
73+
-- Constructor container support --
7774

78-
Gtk.Box._attribute = {
79-
child = {},
80-
}
81-
82-
function Gtk.Box:add(widget, props)
83-
if type(widget) == "table" or props then
84-
error("%s:add(): Gtk4 does not support child properties", self.type.name)
85-
end
86-
self:append(widget)
87-
end
88-
89-
-- Make Gtk.Box:add() available to constructors
90-
Gtk.Box._container_add = Gtk.Box.add
91-
92-
-- Access a Gtk.Box's children as usual. If assigning a child by number, it will replace the widget at the given index.
93-
local box_child_mt = { __index = widget_child_mt.__index }
94-
function box_child_mt:__newindex(key, child)
95-
if type(key) ~= "number" or key == 0 then
96-
error("%s: cannot write to index %q", self._widget.type.name, key)
97-
end
98-
local sibling = self[key]
99-
if not Gtk.Widget:is_type_of(child) then
100-
error("%s: attempt to insert non-widget child", self._widget.type.name)
101-
elseif not sibling and key > 0 then
102-
self._widget:append(child)
103-
elseif not sibling and key < 0 then
104-
self._widget:prepend(child)
105-
else
106-
self._widget:insert_child_after(child, sibling)
107-
self._widget:remove(sibling)
108-
end
109-
end
110-
111-
function Gtk.Box._attribute.child:get()
112-
return setmetatable({ _widget = self }, box_child_mt)
113-
end
114-
115-
-- Gtk.FlowBox overrides --
116-
117-
function Gtk.FlowBox:add(widget, props)
118-
if type(widget) == "table" or props then
119-
error("%s:add(): Gtk4 does not support child properties", self.type.name)
120-
end
121-
self:append(widget)
122-
end
123-
124-
-- Make Gtk.FlowBox:add() available to constructors.
125-
Gtk.FlowBox._container_add = Gtk.FlowBox.add
126-
127-
-- Gtk.ListBox overrides --
128-
129-
function Gtk.ListBox:add(widget, props)
130-
if type(widget) == "table" or props then
131-
error("%s:add(): Gtk4 does not support child properties", self.type.name)
132-
end
133-
self:append(widget)
134-
end
135-
136-
-- Make Gtk.ListBox:add() available to constructors.
137-
Gtk.ListBox._container_add = Gtk.ListBox.add
138-
139-
-- Gtk.Stack overrides --
140-
141-
function Gtk.Stack:add(widget, props)
142-
if type(widget) == "table" or props then
143-
error("%s:add(): Gtk4 does not support child properties", self.type.name)
144-
end
145-
self:add_child(widget)
146-
end
147-
148-
-- Make Gtk.Stack:add() available to Gtk.Stack's constructor.
149-
Gtk.Stack._container_add = Gtk.Stack.add
75+
Gtk.Box._container_add = Gtk.Box._method.append
76+
Gtk.FlowBox._container_add = Gtk.FlowBox._method.append
77+
Gtk.ListBox._container_add = Gtk.ListBox._method.append
78+
Gtk.Stack._container_add = Gtk.Stack._method.add_child

0 commit comments

Comments
 (0)