Skip to content
PedroAlvesV edited this page Apr 22, 2017 · 9 revisions

Widgets Callbacks

The last parameter of most widgets' (all interactive widgets) construction function* is callback. This parameter expects a function that will be invoked when the user interacts with the widget. If a button has a callback function, when clicked, that button will trigger the callback.

For example:

local callback = function(id, label)
   print("This is crazy!")
end
scr:add_button('button', "call me maybe", "Call it!", callback)

On the example above, every time the button is clicked, it prints "This is crazy!". Notice that this callback function receives caller id and label as parameters. With it, it's possible to make some more interesting things.

For example:

local callback = function(id, label)
   print([[
      Hey, I just met you
      And this is crazy!
      But here's my number
      So ]] .. label
   )
end
scr:add_button('button', "call me maybe", "Call it!", callback)

Now it prints a Carly Rae Jepsen hit chorus when clicked.

Callback parameters are provided by AbsTK. They may vary from widget to widget, but follow the convention of "id, [index,] value".

Widgets Callback Parameters
Button id, label
ButtonBox Item id, index, label
ComboBox id, index, label
Text Input id, text
Password Input id, text
CheckBox id, label
CheckList id, index, label
Selector id, index, label

*Note that the last parameter of the ButtonBox construction function expects an array containing callbacks functions to be referred to its Buttons.

Exit Callback

AbsTK was initially built to construct Wizards. Since a Wizard may hold a lot of data and take a considerably long time to be completed by the end-user, it's good practice having a confirmation dialog to prevent it from closing or submitting data accidentally. AbsTK has default confirmation dialogs for closing and submitting buttons on Wizards. For Screens, however, this is not a recommended behavior, because of its extension; it would be awful always having to confirm when exiting a short single-screen UI.

Its parameters are:

  • exit, a string representing the exit signal origin ("QUIT"* or "DONE")
  • data, the data table
  • screen, the screen in which the exit_callback() was triggered

*exit is "QUIT" for both 'Cancel' and 'X' buttons on AbsGtk and for 'Ctrl+Q' command on AbsCurses.

For example:

local exit_callback = function(exit, data, screen)
   local question
   if exit == "DONE" then
      question = "You are ready to go, then?"
   else -- it means 'exit' is "QUIT"
      question = "Are you really really really sure?"
   end
   local answer = screen:show_message_box(question, 'YES_NO')
   return answer == "YES"
end

On the above situation, the UI doesn't care about the data, it's just confirming the exiting action. But it is possible to block data submitting if some requisite doesn't match.

For example:

local exit_callback = function(exit, data, screen)
   local answer
   if exit == "DONE" then
      if data.page2.password_input == "" then
         screen:show_message_box("Must fill password field.", 'OK')
         return false
      else
         answer = screen:show_message_box("Ready to go?", 'YES_NO')
      end
   else -- it means 'exit' is "QUIT"
      answer = screen:show_message_box("Are you sure?", 'YES_NO')
   end
   return answer == "YES"
end
  • password_input is a password input field widget

Now, if the password field is empty, the "Done" button will throw a more alert-like dialog that doesn't allow submitting data; it gives the user an "OK" button that closes the dialog and returns to the main window. Otherwise, it is pretty much like the previous example.

Clone this wiki locally