Move window focus code into a function.