-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathWindowManager.cs
107 lines (95 loc) · 5.49 KB
/
WindowManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using RuntimeUnityEditor.Core.REPL;
using UnityEngine;
namespace RuntimeUnityEditor.Core
{
/// <summary>
/// API for organizing and managing windows.
/// </summary>
public class WindowManager
{
/// <summary>
/// Default distance of windows from screen corners and other windows.
/// </summary>
private const int ScreenMargin = 10;
/// <summary>
/// Default width of windows shown on left and right sides.
/// </summary>
private const int SideWidth = 350;
/// <summary>
/// Get default size of a window for a given screen client rectangle and desired screen partition.
/// </summary>
public static Rect MakeDefaultWindowRect(Rect screenClientRect, ScreenPartition screenPartition)
{
//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)
switch (screenPartition)
{
case ScreenPartition.Left:
return new Rect(screenClientRect.xMin, screenClientRect.yMin, SideWidth, screenClientRect.height);
case ScreenPartition.LeftUpper:
return new Rect(screenClientRect.xMin, screenClientRect.yMin, SideWidth, screenClientRect.height / 2 - ScreenMargin);
case ScreenPartition.LeftLower:
return new Rect(screenClientRect.xMin, screenClientRect.yMin + screenClientRect.height / 2, SideWidth, screenClientRect.height / 2);
case ScreenPartition.Center:
{
var centerWidth = (int)Mathf.Min(850, screenClientRect.width);
var centerX = (int)(screenClientRect.xMin + screenClientRect.width / 2 - Mathf.RoundToInt((float)centerWidth / 2));
return new Rect(centerX, screenClientRect.yMin, centerWidth, screenClientRect.height);
}
case ScreenPartition.CenterUpper:
{
var centerWidth = (int)Mathf.Min(850, screenClientRect.width);
var centerX = (int)(screenClientRect.xMin + screenClientRect.width / 2 - Mathf.RoundToInt((float)centerWidth / 2));
var upperHeight = (int)(screenClientRect.height / 4) * 3;
return new Rect(centerX, screenClientRect.yMin, centerWidth, upperHeight);
}
case ScreenPartition.CenterLower:
{
var centerWidth = (int)Mathf.Min(850, screenClientRect.width);
var centerX = (int)(screenClientRect.xMin + screenClientRect.width / 2 - Mathf.RoundToInt((float)centerWidth / 2));
var upperHeight = (int)(screenClientRect.height / 4) * 3;
return new Rect(centerX, screenClientRect.yMin + upperHeight + ScreenMargin, centerWidth, screenClientRect.height - upperHeight - ScreenMargin);
}
case ScreenPartition.Right:
return new Rect(screenClientRect.xMax - SideWidth, screenClientRect.yMin, SideWidth, screenClientRect.height);
case ScreenPartition.RightUpper:
return new Rect(screenClientRect.xMax - SideWidth, screenClientRect.yMin, SideWidth, screenClientRect.height / 2);
case ScreenPartition.RightLower:
return new Rect(screenClientRect.xMax - SideWidth, screenClientRect.yMin + screenClientRect.height / 2, SideWidth, screenClientRect.height / 2);
case ScreenPartition.Full:
return screenClientRect;
case ScreenPartition.Default:
if (ReplWindow.Initialized) //todo figure out if any windows want to be in the lower part? set default partition for inspector and profiler then
goto case ScreenPartition.CenterUpper;
else
goto case ScreenPartition.Center;
default:
throw new ArgumentOutOfRangeException(nameof(screenPartition), screenPartition, null);
}
}
/// <summary>
/// Discard current window size and position, and set the default ones for the given window.
/// </summary>
public static void ResetWindowRect(IWindow window)
{
var screenClientRect = new Rect(
x: ScreenMargin,
y: ScreenMargin,
width: Screen.width - ScreenMargin * 2,
height: Screen.height - ScreenMargin * 2 - Taskbar.Instance.Height);
window.WindowRect = MakeDefaultWindowRect(screenClientRect, window.DefaultScreenPosition);
}
/// <summary>
/// Check if the window rect of a given window is visible on the screen and of appropriate size.
/// </summary>
public static bool IsWindowRectValid(IWindow window)
{
return window.WindowRect.width >= window.MinimumSize.x &&
window.WindowRect.height >= window.MinimumSize.y &&
window.WindowRect.x < Screen.width - ScreenMargin &&
window.WindowRect.y < Screen.height - ScreenMargin &&
window.WindowRect.x >= -window.WindowRect.width + ScreenMargin &&
window.WindowRect.y >= -window.WindowRect.height + ScreenMargin;
}
}
}