Our previous example was a bit limited. In particular the use of a preference for the export path wasn’t very nice. We can do better than that by adding elements to the user interface in the export dialog.

UI elements are created via the Ansel_new_widget function. This function takes a type of widget as a parameter and returns a new object corresponding to that widget. You can then set various fields in that widget to set its parameters. You will then use that object as a parameter to various functions that will add it to the Ansel UI. The following simple example adds a lib in the lighttable view with a simple label:

1local my_label = Ansel.new_widget("label")
2my_label.label = "Hello, world !"
3
4dt.register_lib("test","test",false,{
5    [dt.gui.views.lighttable] = {"DT_UI_CONTAINER_PANEL_LEFT_CENTER",20},
6    },my_label)

There is a nice syntactic trick to make UI elements code easier to read and write. You can call these objects as functions with a table of key values as an argument. This allows the following example to work. It creates a container widget with two sub-widgets: a label and a text entry field.

1   local my_widget = Ansel.new_widget("box"){
2      orientation = "horizontal",
3      Ansel.new_widget("label"){ label = "here => " },
4      Ansel.new_widget("entry"){ tooltip = "please enter text here" }
5   }

Now that we know that, let’s improve our script a bit.

 1Ansel = require "Ansel"
 2
 3local scp_path = Ansel.new_widget("entry"){
 4  tooltip ="Complete path to copy to. Can include user and hostname",
 5  text = "",
 6  reset_callback = function(self) self.text = "" end
 7}
 8
 9Ansel.register_storage("scp_export","Export via scp",
10  function( storage, image, format, filename,
11     number, total, high_quality, extra_data)
12    if not Ansel.control.execute(scp "..filename.." "..
13      scp_path.text
14    ) then
15      Ansel.print_error("scp failed for "..tostring(image))
16    end
17    end,
18    nil, --finalize
19    nil, --supported
20    nil, --initialize
21    Ansel.new_widget("box") {
22    orientation ="horizontal",
23    Ansel.new_widget("label"){label = "target SCP PATH "},
24    scp_path,
25})