I'm using a system of callbacks for ui buttons that I learned from the replies to a post I made a while ago. It worked rather well, until now. Maybe someone can help me with this.
This is what I'm doing:
# I have a callback proc type
type
Callback* = proc()
# which gets stored in a sequence in the buttons
type
BaseButton* = ref object of Widget
mouse_hit_actions*:seq[Callback]
method add_action*(self:BaseButton, fn:Callback) {.base.} =
self.mouse_hit_actions.add(fn)
# and it works fine with procs, like this
proc create_buttons() =
# (...)
let button = new_button( x, y, icon, button_style )
button.add_action( proc() = do_things(button.id) )
proc do_things(index:int) =
echo index # >> prints the given id
The problem now is when trying to pass in methods, I get an error. So if the above code is like this:
proc new_panel():LayersPanel {.base.} =
new result
# (...)
let button = new_button( x, y, icon, button_style )
button.add_action( proc() = result.select_layer(button.id) ) # <--- ERROR !!!
method select_layer(self:LayersPanel, index:int) {.base.} =
echo index
Error: 'result' is of type <LayersPanel> which cannot be captured as it would violate memory safety
How can I get around this?