Fix custom perspective closing

If the user is trying to close the current perspective, close it before
deleting. If it was the last perspective, close current page.
Merge branch 'bug-18158-fix-layout-breaking'