Skip to content

Commit 2ddd096

Browse files
committed
Added check to ensure we are in lighttable view prior to callling darktable.register_lib. If darktable was invoked in single image (darkroom) mode, then an event handler is registered to detect the change from darkroom mode to lighttable mode so that we can then call darktable.register_lib.
1 parent 4e68e42 commit 2ddd096

File tree

1 file changed

+66
-28
lines changed

1 file changed

+66
-28
lines changed

examples/moduleExample.lua

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,44 @@ local function _(msgid)
4646
return gettext.dgettext("moduleExample", msgid)
4747
end
4848

49+
-- declare a local namespace and a couple of variables we'll need to install the module
50+
local mE = {}
51+
mE.widgets = {}
52+
mE.event_registered = false -- keep track of whether we've added an event callback or not
53+
mE.module_installed = false -- keep track of whether the module is module_installed
54+
55+
--[[ We have to create the module in one of two ways depending on which view darktable starts
56+
in. In orker to not repeat code, we wrap the darktable.register_lib in a local function.
57+
]]
58+
59+
local function install_module()
60+
if not mE.module_installed then
61+
-- https://www.darktable.org/lua-api/index.html#darktable_register_lib
62+
dt.register_lib(
63+
"exampleModule", -- Module name
64+
"exampleModule", -- name
65+
true, -- expandable
66+
false, -- resetable
67+
{[dt.gui.views.lighttable] = {"DT_UI_CONTAINER_PANEL_RIGHT_CENTER", 100}}, -- containers
68+
-- https://www.darktable.org/lua-api/types_lua_box.html
69+
dt.new_widget("box") -- widget
70+
{
71+
orientation = "vertical",
72+
dt.new_widget("button")
73+
{
74+
label = _("MyButton"),
75+
clicked_callback = function (_)
76+
dt.print(_("Button clicked"))
77+
end
78+
},
79+
table.unpack(mE.widgets),
80+
},
81+
nil,-- view_enter
82+
nil -- view_leave
83+
)
84+
mE.module_installed = true
85+
end
86+
end
4987

5088
-- https://www.darktable.org/lua-api/types_lua_check_button.html
5189
local check_button = dt.new_widget("check_button"){label = _("MyCheck_button"), value = true}
@@ -90,35 +128,35 @@ local slider = dt.new_widget("slider")
90128
value = 52 -- The current value of the slider
91129
}
92130

93-
-- https://www.darktable.org/lua-api/index.html#darktable_register_lib
94-
dt.register_lib(
95-
"exampleModule", -- Module name
96-
"exampleModule", -- name
97-
true, -- expandable
98-
false, -- resetable
99-
{[dt.gui.views.lighttable] = {"DT_UI_CONTAINER_PANEL_RIGHT_CENTER", 100}}, -- containers
100-
-- https://www.darktable.org/lua-api/types_lua_box.html
101-
dt.new_widget("box") -- widget
102-
{
103-
orientation = "vertical",
104-
dt.new_widget("button")
105-
{
106-
label = _("MyButton"),
107-
clicked_callback = function (_)
108-
dt.print(_("Button clicked"))
131+
-- pack the widgets in a table for loading in the module
132+
133+
table.insert(mE.widgets, check_button)
134+
table.insert(mE.widgets, combobox)
135+
table.insert(mE.widgets, entry)
136+
table.insert(mE.widgets, file_chooser_button)
137+
table.insert(mE.widgets, label)
138+
table.insert(mE.widgets, separator)
139+
table.insert(mE.widgets, slider)
140+
141+
-- ... and tell dt about it all
142+
143+
144+
if dt.gui.current_view().name == "lighttable" then -- make sure we are in lighttable view
145+
install_module() -- register the lib
146+
else
147+
if not mE.event_registered then -- if we are not in lighttable view then register an event to signal when we might be
148+
-- https://www.darktable.org/lua-api/index.html#darktable_register_event
149+
dt.register_event(
150+
"view-changed", -- we want to be informed when the view changes
151+
function(event, old_view, new_view)
152+
if new_view.name == "lighttable" and old_view.name == "darkroom" then -- if the view changes from darkroom to lighttable
153+
install_module() -- register the lib
154+
end
109155
end
110-
},
111-
check_button,
112-
combobox,
113-
entry,
114-
file_chooser_button,
115-
label,
116-
separator,
117-
slider
118-
},
119-
nil,-- view_enter
120-
nil -- view_leave
121-
)
156+
)
157+
mE.event_registered = true -- keep track of whether we have an event handler installed
158+
end
159+
end
122160

123161
-- vim: shiftwidth=2 expandtab tabstop=2 cindent syntax=lua
124162
-- kate: hl Lua;

0 commit comments

Comments
 (0)