Skip to content

Commit 679ede2

Browse files
authored
Ensure default window positions are always visible; Improve "Reset" taskbar button (#101)
1 parent 9e8ab76 commit 679ede2

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

RuntimeUnityEditor.Core/WindowManager.cs

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Linq;
23
using RuntimeUnityEditor.Core.REPL;
34
using 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>

RuntimeUnityEditor.Core/Windows/Taskbar.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,16 @@ private void DrawTaskbar(int id)
8484
GUI.color = new Color(1f, 1f, 1f, 0.75f);
8585
if (GUILayout.Button("Reset"))
8686
{
87+
// Needed to avoid WindowManager.ResetWindowRect using old rects as reference to where new windows should be placed.
8788
foreach (var window in _orderedFeatures.OfType<IWindow>())
89+
window.WindowRect = new Rect(-1000, -1000, 0, 0);
90+
91+
foreach (var window in _orderedFeatures.OfType<IWindow>().OrderBy(x => x.Title))
92+
{
93+
// Ensure that all title bars are visible
94+
GUI.BringWindowToFront(window.WindowId);
8895
WindowManager.ResetWindowRect(window);
96+
}
8997
}
9098
firstFeature = false;
9199
GUI.color = Color.white;

0 commit comments

Comments
 (0)