API design for egui widgets which take a `ref mut` as one of the arguments
Closed this issue · 1 comments
for egui widgets like ui:label("string")
, its easy to just make a lua binding. lua will give us a String
and we can use it for the ui.label()
function. then, we just return Response
struct to the lua api.
but some widgets like ui.text_edit()
take a &mut String
as an argument. unfortunately, lua strings, numbers, bools are immutable. so, we will never get a mutable reference from lua. so, we have two different designs for such mut ref functions.
Design 1
take a Table
as argument for the function. tables can be mutable with setter functions for fields. we can simply ask user to provide us a table with the "value" field set to the editable data we need. we will just make a clone of the data, use mut ref value, and then set the changed value to that "value" field. we will return a Response
as usual.
local my_text = {value: "some random string"}
ui:text_edit(my_text)
Design 2
take the value as argument, but return the changed value along the response as a tuple. this is much more intuitive.
local my_text = "some random string"
local my_text, response = ui:text_edit(my_text)
ofcourse, we can always have both if necessary. i am leaning towards the second design for its simplicity.
I will go with the first design of using a table. The second design will confuse new people as they may forget to use/store the return value properly. Both should be roughly equal in terms of performance.
Instead, we can provide some custom functions which deal with rust String
and egui text_edit
, as a performant alternative but with a more clunky interface.