Library for routing the window events

Today I faced with impossibility to remove listener on window events. Defold API has only window.set_listener method for add callback and has no method to remove it.

I made the library for simple adding and removing listeners to any script with message receiver (with on_message method) for the window events:

function init(self)
...
    ag_window.add_focus_listener(msg.url()) -- for the lost and gained events
    ag_window.add_resize_listener(msg.url()) -- for the resize event
...
end
function on_message(self, message_id, message, sender)
...
	if message_id == ag_window.FOCUS_LOST then
	    print("focus lost")
	elseif message_id == ag_window.FOCUS_GAINED then
	    print("focus gained")
	elseif message_id == ag_window.RESIZED then
	    print("Resized", message.width, message.height)
	end
...
end
function final(self)
...
    ag_window.remove_focus_listener(msg.url())
    ag_window.remove_resize_listener(msg.url())
...
end

With example project, of course.

5 Likes

Looks good. Thank you for sharing!

PS In your example you have function funal(self). Note: funal not final

1 Like

Thank you!
I corrected a typo

2 Likes