Dialog: Restore focus to the previously focused element when window regains focus

Fixes #9101 - Dialog: Track last focused element instead of always focusing the first tabbable element

This addresses the issue, but it doesn't have a unit test. I don't know how to write one, since the issue only occurs when switching tabs in Firefox. The only difference I see, compared to Chrome, are additional focusout/focusin events with the `document` as the `event.target`, otherwise the behaviour seems to be the same.

Ideas for writing a unit tests for this?