Fix #10375: reorder window operations in event handling

rebases #828

Test on both Mac and Windows.