focusLoss is not run on destroy()

Mads Buvik Sandvei
2026-04-02T20:49:42Z

## Issue Description
if element:destroy() is run on an element containing a widget that is in focus, the `focusLoss` event is never run. Combine this with the lack of any notification on destroy(), and it is not possible to reliably pair `focusLoss` and `focusGain`. making it impossible to fully automate cleaning up e.g. tooltips.

example code:
e2 being destroyed, but its `focusLoss` event is never invoked, leaving e1 undestroyed.

```Lua
local ui = require('openmw.ui')
local I = require('openmw.interfaces')
local async = require('openmw.async')

local e1 = nil
e1 = ui.create{
template = I.MWUI.templates.boxSolid,
layer = 'Modal',
content = ui.content{{
type = ui.TYPE.Text,
props = {
text = 'I should be destroyed when you mouse over the widget below',
textSize = 32,
textColor = util.color.rgb(0.5, 0.5, 0.5),
},
}},
props = {
position = util.vector2(0, 50),
}
}

local e2 = nil
e2 = ui.create{
template = I.MWUI.templates.boxSolid,
layer = 'Modal',
content = ui.content{{
type = ui.TYPE.Text,
props = {
text = 'I should be destroyed when you mouse over me',
textSize = 32,
textColor = util.color.rgb(0.5, 0.5, 0.5),
},
}},
props = {
position = util.vector2(0, 100),
},
events = {
focusGain = async:callback(function()
e2:destroy()
end),
focusLoss = async:callback(function()
e1:destroy()
end)
}
}
```