11using System ;
2+ using System . Linq ;
23using RuntimeUnityEditor . Core . REPL ;
34using UnityEngine ;
45
@@ -24,50 +25,48 @@ public class WindowManager
2425 /// </summary>
2526 public static Rect MakeDefaultWindowRect ( Rect screenClientRect , ScreenPartition screenPartition )
2627 {
27- //todo if a window is alraedy visible in a given partition's position, return a slightly moved rect so both windows are easily visible (add 15,15 to position I guess, repeat until nothing is there or we run out of space)
28-
2928 switch ( screenPartition )
3029 {
3130 case ScreenPartition . Left :
32- return new Rect ( screenClientRect . xMin , screenClientRect . yMin , SideWidth , screenClientRect . height ) ;
31+ return EnsureVisible ( new Rect ( screenClientRect . xMin , screenClientRect . yMin , SideWidth , screenClientRect . height ) ) ;
3332 case ScreenPartition . LeftUpper :
34- return new Rect ( screenClientRect . xMin , screenClientRect . yMin , SideWidth , screenClientRect . height / 2 - ScreenMargin ) ;
33+ return EnsureVisible ( new Rect ( screenClientRect . xMin , screenClientRect . yMin , SideWidth , screenClientRect . height / 2 - ScreenMargin ) ) ;
3534 case ScreenPartition . LeftLower :
36- return new Rect ( screenClientRect . xMin , screenClientRect . yMin + screenClientRect . height / 2 , SideWidth , screenClientRect . height / 2 ) ;
35+ return EnsureVisible ( new Rect ( screenClientRect . xMin , screenClientRect . yMin + screenClientRect . height / 2 , SideWidth , screenClientRect . height / 2 ) ) ;
3736
3837 case ScreenPartition . Center :
3938 {
4039 var centerWidth = ( int ) Mathf . Min ( 850 , screenClientRect . width ) ;
4140 var centerX = ( int ) ( screenClientRect . xMin + screenClientRect . width / 2 - Mathf . RoundToInt ( ( float ) centerWidth / 2 ) ) ;
42- return new Rect ( centerX , screenClientRect . yMin , centerWidth , screenClientRect . height ) ;
41+ return EnsureVisible ( new Rect ( centerX , screenClientRect . yMin , centerWidth , screenClientRect . height ) ) ;
4342 }
4443 case ScreenPartition . CenterUpper :
4544 {
4645 var centerWidth = ( int ) Mathf . Min ( 850 , screenClientRect . width ) ;
4746 var centerX = ( int ) ( screenClientRect . xMin + screenClientRect . width / 2 - Mathf . RoundToInt ( ( float ) centerWidth / 2 ) ) ;
4847 var upperHeight = ( int ) ( screenClientRect . height / 4 ) * 3 ;
49- return new Rect ( centerX , screenClientRect . yMin , centerWidth , upperHeight ) ;
48+ return EnsureVisible ( new Rect ( centerX , screenClientRect . yMin , centerWidth , upperHeight ) ) ;
5049 }
5150 case ScreenPartition . CenterLower :
5251 {
5352 var centerWidth = ( int ) Mathf . Min ( 850 , screenClientRect . width ) ;
5453 var centerX = ( int ) ( screenClientRect . xMin + screenClientRect . width / 2 - Mathf . RoundToInt ( ( float ) centerWidth / 2 ) ) ;
5554 var upperHeight = ( int ) ( screenClientRect . height / 4 ) * 3 ;
56- return new Rect ( centerX , screenClientRect . yMin + upperHeight + ScreenMargin , centerWidth , screenClientRect . height - upperHeight - ScreenMargin ) ;
55+ return EnsureVisible ( new Rect ( centerX , screenClientRect . yMin + upperHeight + ScreenMargin , centerWidth , screenClientRect . height - upperHeight - ScreenMargin ) ) ;
5756 }
5857
5958 case ScreenPartition . Right :
60- return new Rect ( screenClientRect . xMax - SideWidth , screenClientRect . yMin , SideWidth , screenClientRect . height ) ;
59+ return EnsureVisible ( new Rect ( screenClientRect . xMax - SideWidth , screenClientRect . yMin , SideWidth , screenClientRect . height ) ) ;
6160 case ScreenPartition . RightUpper :
62- return new Rect ( screenClientRect . xMax - SideWidth , screenClientRect . yMin , SideWidth , screenClientRect . height / 2 ) ;
61+ return EnsureVisible ( new Rect ( screenClientRect . xMax - SideWidth , screenClientRect . yMin , SideWidth , screenClientRect . height / 2 ) ) ;
6362 case ScreenPartition . RightLower :
64- return new Rect ( screenClientRect . xMax - SideWidth , screenClientRect . yMin + screenClientRect . height / 2 , SideWidth , screenClientRect . height / 2 ) ;
63+ return EnsureVisible ( new Rect ( screenClientRect . xMax - SideWidth , screenClientRect . yMin + screenClientRect . height / 2 , SideWidth , screenClientRect . height / 2 ) ) ;
6564
6665 case ScreenPartition . Full :
6766 return screenClientRect ;
6867
6968 case ScreenPartition . Default :
70- if ( ReplWindow . Initialized ) //todo figure out if any windows want to be in the lower part? set default partition for inspector and profiler then
69+ if ( ReplWindow . Initialized )
7170 goto case ScreenPartition . CenterUpper ;
7271 else
7372 goto case ScreenPartition . Center ;
@@ -77,6 +76,21 @@ public static Rect MakeDefaultWindowRect(Rect screenClientRect, ScreenPartition
7776 }
7877 }
7978
79+ private static Rect EnsureVisible ( Rect rect )
80+ {
81+ var result = rect ;
82+ var allWindows = RuntimeUnityEditorCore . Instance . InitializedFeatures . OfType < IWindow > ( ) ;
83+ var allWindowsRects = allWindows . Select ( w => w . WindowRect ) . ToList ( ) ;
84+ // Check if any window near this position, move the rect until it's not near any other window
85+ while ( allWindowsRects . Any ( r => Mathf . Abs ( r . x - result . x ) < 7 && Mathf . Abs ( r . y - result . y ) < 7 ) )
86+ {
87+ result . x += 17 ;
88+ result . y += 17 ;
89+ }
90+ // Ensure the new rect is visible on screen
91+ return result . x < Screen . width - 50 && result . y < Screen . height - 50 ? result : rect ;
92+ }
93+
8094 /// <summary>
8195 /// Discard current window size and position, and set the default ones for the given window.
8296 /// </summary>
0 commit comments