Project

General

Profile

Bug #11184

setting lua combobox values from unpack(table) doesn't work correctly

Added by Bill Ferguson over 1 year ago. Updated 8 months ago.

Status:
New
Priority:
Low
Category:
Lua
Start date:
09/28/2016
Due date:
% Done:

0%

Affected Version:
2.0.6
System:
Ubuntu
bitness:
64-bit
hardware architecture:
amd64/x86

Description

Setting combobox values from a table of strings such as:

local choices = {"Option 1", "Option 2", "Option 3"}
local utwc = dt.new_widget("combobox"){
  label = "Unpack Table With Callback",
  tooltip = "Broke",
  value = 1, unpack(choices),
  changed_callback = function(self)
    dt.print(self.value)
  end
}

doesn't work correctly. The first value displays, but the drop down doesn't work. The drop down works correctly if the changed_callback isn't there.

I've included a script that demonstrates what works and what doesn't.

combobox_error.lua - lua script demonstrating the problem (1.37 KB) Bill Ferguson, 09/28/2016 06:46 PM

History

#1 Updated by Bill Ferguson over 1 year ago

Tested against the dev version and it doesn't work there either.

Added some print statements to src/lua/widget/combobox.c to see what was going on. combobox_numindex is only getting called once for the unpack table with callback widget so the first option is all that gets added.

LUA ERROR Loading test/combobox_error
* This is hard coded, no callback
in combobox_numindex
adding string Option 1
in combobox_numindex
adding string Option 2
in combobox_numindex
adding string Option 3
* This is unpack(table) no callback
in combobox_numindex
adding string Option 1
in combobox_numindex
adding string Option 2
in combobox_numindex
adding string Option 3
* This is hard coded with callback
in combobox_numindex
adding string Option 1
in combobox_numindex
adding string Option 2
in combobox_numindex
adding string Option 3
* This is unpack(table) with callback
in combobox_numindex
adding string Option 1
LUA ERROR Loaded test/combobox_error

Hope this helps...

#2 Updated by Bill Ferguson over 1 year ago

Figured out a work around for this, so it's no longer a priority for me.

I did a little more debugging and the one difference that I saw was a call to value_member in combobox.c with a non existent value, prior to adding the options.

#3 Updated by Dominik Markiewicz 8 months ago

This is not a bug, this is how Lua works. Just make sure you unpack is at the end of call. An example:

> v={'a','b','c'}
> d={aa='aa', bb='bb', cc='cc', table.unpack(v)}
> for i,j in pairs(d) do print(i,j) end
1    a
2    b
3    c
cc    cc
bb    bb
aa    aa

but

> v={'a','b','c'}
> d={aa='aa', bb='bb', table.unpack(v), cc='cc'}
> for i,j in pairs(d) do print(i,j) end
1    a
cc    cc
bb    bb
aa    aa

For reference please go to https://www.lua.org/manual/5.3/manual.html#3.4.9 and https://www.lua.org/manual/5.3/manual.html#3.4.11

In your case, you can simply switch parameters order and make sure `unpack` is at the end of args list:

local choices = {"Option 1", "Option 2", "Option 3"}
local utwc = dt.new_widget("combobox"){
  label = "Unpack Table With Callback",
  tooltip = "Broke",
  value = 1,
  changed_callback = function(self)
    dt.print(self.value)
  end,
  unpack(choices)
}

Also available in: Atom PDF