/
window.js
315 lines (285 loc) · 8.08 KB
/
window.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
var NativeWindow = require('./bindings').Window,
App = require('./bindings').App,
bridge = require('./bridge'),
EventEmitter = process.EventEmitter,
_hasOwn = Object.prototype.hasOwnProperty,
_slice = Array.prototype.slice,
_apply = Function.prototype.apply,
_bind = Function.prototype.bind,
screenHeight = App.prototype.screenHeight,
screenWidth = App.prototype.screenWidth,
frames = new WeakMap,
handlers = new WeakMap;
function unwrap(o){
return frames.get(o);
}
function neuter(o){
var handler = handlers.get(o);
if (handler) {
handler.window = {};
handler.target = {};
}
}
module.exports = Window;
function Window(nativeWindow){
if (!(this instanceof Window))
return new Window(nativeWindow);
var self = this;
var handler = new WindowHandler(nativeWindow, this);
var window = Proxy.create(handler, Window.prototype);
var frame = this.frame = new Frame(nativeWindow);
nativeWindow.on('ready', function(){
nativeWindow.runInBrowser(bridge);
bridge(nativeWindow, handler, window);
process.nextTick(function(){
var windowProto = window.__proto__;
windowProto._events = self._events;
['on', 'off', 'emit', 'once'].forEach(function(key){
windowProto[key] = EventEmitter.prototype[key];
});
window.console.open = function open(){ nativeWindow.openDevTools() };
window.console.close = function close(){ nativeWindow.closeDevTools() };
window.frame = frame;
window.emit('ready');
});
});
nativeWindow.on('context-released', function(){
handler.__proto__ = WindowHandler.prototype;
});
return window;
}
Window.prototype = Object.create(EventEmitter.prototype, {
constructor: {
configurable: true,
writable: true,
value: Window
}
});
function Reflector(target){
this.target = target;
}
Reflector.prototype = {
keys: function keys(){
return Object.keys(this.target);
},
enumerate: function enumerate(){
var i=0, k=[];
for (k[i++] in this.target);
return k;
},
getOwnPropertyNames: function getOwnPropertyNames(){
return Object.getOwnPropertyNames(this.target);
},
get: function get(rcvr, key){
return this.target[key];
},
set: function set(rcvr, key, value){
this.target[key] = value;
return true;
},
has: function has(key){
return key in this.target;
},
hasOwn: function hasOwn(key){
return _hasOwn.call(this.target, key);
},
delete: function delete_(key){
delete this.target[key];
return true;
},
defineProperty: function defineProperty(key, desc){
Object.defineProperty(this.target, key, desc);
return true;
},
getOwnPropertyDescriptor: function getOwnPropertyDescriptor(key){
var desc = Object.getOwnPropertyDescriptor(this.target, key);
desc && (desc.configurable = true);
return desc;
},
apply: function apply(rcvr, args){
return _apply.call(this.target, rcvr, args);
},
construct: function construct(args){
return new (_bind.apply(this.target, [null].concat(args)));
}
};
function WindowHandler(nativeWindow, target){
this.window = nativeWindow;
this.target = target;
}
WindowHandler.prototype = Object.create(Reflector.prototype);
function RefType(properties, accessors){
this.refs = Object.create(null);
if (Array.isArray(properties)) {
this.names = properties;
properties.forEach(function(key){
this[key] = key;
}, this.refs);
} else {
this.names = Object.keys(properties);
this.names.forEach(function(key){
this[key] = properties[key];
}, this.refs);
}
this.accessors = Object.create(null);
if (accessors) {
var accessorNames = Object.keys(accessors);
this.names = this.names.concat(accessorNames);
accessorNames.forEach(function(key){
this[key] = Object.getOwnPropertyDescriptor(accessors, key);
}, this.accessors);
}
function RefHandler(ref, target){
this.ref = ref;
this.target = target;
}
RefHandler.prototype = this;
return function Creator(ref){
var handler = new RefHandler(ref, this);
var proxy = Proxy.create(handler, Creator.prototype);
handlers.set(proxy, handler);
return proxy;
};
}
RefType.prototype = {
keys: function keys(){
return this.names.concat(Object.keys(this.target));
},
enumerate: function enumerate(){
var i = this.names.length, k = this.names.slice();
for (k[i++] in this.target);
return k;
},
getOwnPropertyNames: function getOwnPropertyNames(){
return this.names.concat(Object.getOwnPropertyNames(this.target));
},
get: function get(rcvr, key){
if (key === '__proto__') {
return this.target.__proto__;
} else if (key in this.refs) {
return this.ref[this.refs[key]];
} else if (key in this.accessors) {
return this.accessors[key].get.call(this.ref);
} else {
return this.target[key];
}
},
set: function set(rcvr, key, value){
if (key in this.refs) {
this.ref[this.refs[key]] = value;
} else if (key in this.accessors) {
this.accessors[key].set.call(this.ref, value);
} else {
this.target[key] = value;
}
return true;
},
has: function has(key){
return key in this.refs || key in this.accessors || key in this.target;
},
hasOwn: function hasOwn(key){
return key in this.refs || key in this.accessors || _hasOwn.call(this.target, key);
},
delete: function delete_(key){
if (key in this.refs) {
delete this.ref[this.refs[key]];
} else {
delete this.target[key];
}
return true;
},
defineProperty: function defineProperty(key, desc){
if (key in this.refs) {
Object.defineProperty(this.ref, this.refs[key], desc);
} else if (key in this.accessors) {
this.accessors[key].set.call(this.ref, desc.value);
} else {
Object.defineProperty(this.target, key, desc);
}
return true;
},
getOwnPropertyDescriptor: function getOwnPropertyDescriptor(key){
if (key in this.refs) {
var desc = Object.getOwnPropertyDescriptor(this.ref, this.refs[key]);
} else if (key in this.accessors) {
return {
enumerable: true, configurable: true, writable: true,
value: this.accessors[key].get.call(this.ref)
};
} else {
var desc = Object.getOwnPropertyDescriptor(this.target, key);
}
desc && (desc.configurable = true);
return desc;
},
apply: function apply(rcvr, args){
return _apply.call(this.ref, rcvr, args);
},
construct: function construct(args){
return new (_bind.apply(this.ref, [null].concat(args)));
}
};
var props = ['left', 'top', 'width', 'height', 'title', 'state', 'topmost', 'showChrome', 'resizable'];
process.platform !== 'linux' && props.push('alpha');
process.platform !== 'win32' && props.push('opacity');
var FrameImpl = new RefType(props, {
get right(){
return screenWidth() - this.left - this.width;
},
set right(v){
this.width = Math.max(0, screenWidth() - this.left - v);
},
get bottom(){
return screenHeight() - this.top - this.height;
},
set bottom(v){
this.height = Math.max(0, screenHeight() - this.top - v);
}
});
function Frame(win){
var frame = new FrameImpl(win);
frames.set(frame, win);
return frame;
}
Frame.prototype = FrameImpl.prototype = {
center: function center(){
unwrap(this).move((screenHeight() - this.height) / 2, (screenWidth() - this.width) / 2);
return this;
},
drag: function drag(){
unwrap(this).drag();
return this;
},
minimize: function minimize(){
unwrap(this).minimize();
return this;
},
maximize: function maximize(){
unwrap(this).maximize();
return this;
},
restore: function restore(){
unwrap(this).restore();
return this;
},
fullscreen: function fullscreen(){
unwrap(this).fullscreen();
return this;
},
show: function show(){
unwrap(this).show();
return this;
},
hide: function hide(){
unwrap(this).hide();
return this;
},
move: function move(top, left, width, height){
unwrap(this).move(top, left, width, height);
return this;
},
resize: function resize(width, height){
unwrap(this).resize(width, height)
return this
}
};