Editing Viewbuilder Items

Is there any way to directly edit the item list of a Switch/Popup/Chooser instead of creating a new table and assigning that?

vb = renoise.ViewBuilder()
vb:popup { id = "popup", items = {"a", "b"} }
vb.views.popup.items[1] = nil
vb.views.popup.items[2] = "x"
vb.views.popup.items[3] = "c"
rprint(vb.views.popup.items)

[1] =>  a
[2] =>  b
1 Like

Edit: oh. the example below is for adding a new item. Make a similar :edit_item function, otherwise we’ll have to use the dreaded metatables…

You can overload renoise.Views.Popup and take care of things behind the scenes, thereby making things more convenient.

function renoise.Views.Popup:insert_item(pos, new_item)
  local tmp_list = table.copy(self.items)
  table.insert(tmp_list, pos, new_item) -- tmp_list:insert should work here but didn't. bug, @taktik?
  self.items = tmp_list
end

-- test. an item will be added anytime you use the popup
local pop = renoise.ViewBuilder():popup {
    items = { "b", "c", "d" }
  }
  
pop:add_notifier(
  function()
    pop:insert_item(1, "a")
  end
  )

renoise.app():show_custom_dialog("test", pop)

You might want to add some handling if you need to retain the selected item, but maybe you’ll find how to do that? (and potentially filter out the accompanying notifier bang that will follow)

PS. It would be possible to make your own new_items property in renoise.Views.Popup that works exactly like items (only better), but I think it would require metatables to catch the index you’re trying to access, and it’s a bit overkill if the above is good enough.

Ah, I was hoping I missed something obvious, but it’s good to have it confirmed that the only way is to work on a temporary copy of the table and use that. Cheers for the code, I’ll refer to this if editing items becomes a common thing here.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.