Add better support for GtkTemplate
Open
Nobody has claimed this yet.
- Dominant language
- Lua
- Stars
- 491
- Forks
- 81
- PR merge metrics
- No merged PRs in 30d
Description
This is a feature of GtkWidget and GtkBuilder. If unfamiliar https://blogs.gnome.org/tvb/2013/04/09/announcing-composite-widget-templates/
Here is something I got working but is less than pretty:
local lgi = require('lgi')
local GLib = lgi.require('GLib')
local GObject = lgi.require('GObject')
local Gtk = lgi.require('Gtk', '3.0')
local Test = lgi.package('Test')
function template_connect_function(builder, object, signal_name, handler_name,
connect_object, flags, class_name)
local instance = builder:get_object(class_name)
assert(instance ~= nil)
local handler = instance[handler_name] -- TODO: Readable error
assert(connect_object == nil) -- TODO
-- FIXME: Need a closure around handler with the correct first arg, and extra data
-- TODO: This is ugly, should have more direct approach
object['on_' .. signal_name:gsub('%-', '_')]:connect(handler, nil, flags == GObject.ConnectFlags.AFTER)
end
-- Window ----------------
local ui = [[
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="TestWindow" parent="GtkApplicationWindow">
<property name="title">Title!?</property>
<child>
<object class="GtkButton" id="button">
<signal name="clicked" handler="on_clicked"/>
<property name="visible">1</property>
</object>
</child>
</template>
</interface>
]]
Test:class('Window', Gtk.ApplicationWindow)
function Test.Window._class_init(name, class)
Gtk.WidgetClass.set_template(class, GLib.Bytes(ui))
Gtk.WidgetClass.bind_template_child_full(class, "button", true, 0)
Gtk.WidgetClass.set_connect_func(class, function (builder, object, signal_name, handler_name,
connect_object, flags)
template_connect_function(builder, object, signal_name, handler_name,
connect_object, flags, tostring(name):gsub('%.', ''))
end)
end
function Test.Window._init(instance)
Gtk.Widget.init_template(instance)
instance.priv.button = instance:get_template_child(instance._type, "button")
end
function Test.Window:on_clicked(button)
print('clicked')
end
-- Application ------------
Test:class('Application', Gtk.Application)
function Test.Application:do_activate()
self._type._parent.do_activate(self)
end
function Test.Application:do_startup()
self._type._parent.do_startup(self)
local win = Test.Window({application=self})
print(win.title)
win:present()
end
function Test.Application:do_shutdown()
self._type._parent.do_shutdown(self)
end
-- Main ------------
local app = Test.Application()
app:run()
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the GtkTemplate example in the issue and review the GtkWidget and GtkBuilder APIs it references. Compare the current workaround for template children and signal handlers with the desired direct support; done should cover template setup, child binding, and signal connection without the shown custom helper.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- lua
- Domain
- desktop
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 28/100