-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathscreen.js
166 lines (124 loc) · 5.41 KB
/
screen.js
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
////////////////////////////////////////////////////////////////////////////////
// -------------------------------------------------------------------------- //
// //
// (C) 2010-2018 Robot Developers //
// See LICENSE for licensing info //
// //
// -------------------------------------------------------------------------- //
////////////////////////////////////////////////////////////////////////////////
"use strict";
//----------------------------------------------------------------------------//
// Exports //
//----------------------------------------------------------------------------//
module.exports = function (robot, native)
{
//----------------------------------------------------------------------------//
// Constructor Screen //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
function Screen (bounds, usable)
{
// Auto instantiate the Screen
if (!(this instanceof Screen))
return new Screen (bounds, usable);
// Check if using defaults
if (bounds === undefined &&
usable === undefined)
{
this._bounds = robot.Bounds();
this._usable = robot.Bounds();
return;
}
// Check if assigning bounds values
if (bounds instanceof robot.Bounds &&
usable instanceof robot.Bounds)
{
this._bounds = bounds;
this._usable = usable;
return;
}
throw new TypeError ("Invalid arguments");
}
//----------------------------------------------------------------------------//
// Functions Screen //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
Screen.prototype.getBounds = function() { return this._bounds; };
Screen.prototype.getUsable = function() { return this._usable; };
Screen.prototype.isPortrait = function() { return this._bounds.w < this._bounds.h; };
Screen.prototype.isLandscape = function() { return this._bounds.w >= this._bounds.h; };
//----------------------------------------------------------------------------//
// Static Screen //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
Screen._screens = [ ];
Screen._totalBounds = robot.Bounds();
Screen._totalUsable = robot.Bounds();
////////////////////////////////////////////////////////////////////////////////
Screen.synchronize = function()
{
// Perform native screen synchronization
var result = native.Screen.synchronize();
this._screens = result.screens;
this._totalBounds = result.totalBounds;
this._totalUsable = result.totalUsable;
// Convert screen objects into screen classes
for (var i = 0; i < this._screens.length; ++i)
{
this._screens[i] = Screen
(this._screens[i].bounds,
this._screens[i].usable);
}
return result.status;
};
////////////////////////////////////////////////////////////////////////////////
Screen.getMain = function()
{
// The primary screen is always the first screen object
return this._screens.length ? this._screens[0] : null;
};
////////////////////////////////////////////////////////////////////////////////
Screen.getList = function() { return this._screens; };
////////////////////////////////////////////////////////////////////////////////
Screen.getScreen = function (ax, ay)
{
var p;
// Check if instance of window
if (ax instanceof robot.Window)
{
if (!ax.isValid()) return null;
p = ax.getBounds().getCenter();
}
// Attempt to parse as a regular point
else p = robot.Point.normalize (ax, ay);
// Loop through every available screen object
for (var i = 0; i < this._screens.length; ++i)
{
if (this._screens[i]._bounds.containsP
(p.x, p.y)) return this._screens[i];
}
// The primary screen is always the first screen object
return this._screens.length ? this._screens[0] : null;
};
////////////////////////////////////////////////////////////////////////////////
Screen.grabScreen = function (image, ax, ay, aw, ah, window)
{
// Check if argument is native image
if (!(image instanceof robot.Image))
throw new TypeError ("Invalid arguments");
var b = robot.Bounds.
normalize (ax, ay, aw, ah);
// Perform native image capture
return native.Screen.grabScreen
(image, b.x, b.y, b.w, b.h,
arguments[arguments.length - 1]);
};
////////////////////////////////////////////////////////////////////////////////
Screen.getTotalBounds = function() { return this._totalBounds; };
Screen.getTotalUsable = function() { return this._totalUsable; };
////////////////////////////////////////////////////////////////////////////////
Screen. isCompositing = native.Screen. isCompositing;
Screen.setCompositing = native.Screen.setCompositing;
////////////////////////////////////////////////////////////////////////////////
return Screen;
};