Improve popup layout calculation method