I have done some things to eliminate small context switches, as I found even those were a trigger to unfocus. I no longer use two monitors, I use i3 to manage windows so I don't have to move to my mouse, same reason I use vimium. I have all my tools inside the IDE and then a terminal sitting below it for all else.
When I have to do frontend implementations, switching between tabs and screens, I can work for much less time and I constsntly lose focus before essentially burning out for the day.