Optimized layout toggling function