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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Custom binding for the app_window API.
var appWindowNatives = requireNative('app_window_natives');
var runtimeNatives = requireNative('runtime');
var Binding = require('binding').Binding;
var Event = require('event_bindings').Event;
var forEach = require('utils').forEach;
var renderViewObserverNatives = requireNative('renderViewObserverNatives');
var sendRequest = require('sendRequest').sendRequest;
var appWindowData = null;
var currentAppWindow = null;
var appWindow = Binding.create('app.window');
appWindow.registerCustomHook(function(bindingsAPI) {
var apiFunctions = bindingsAPI.apiFunctions;
apiFunctions.setCustomCallback('create',
function(name, request, windowParams) {
var view = null;
if (windowParams.viewId) {
view = appWindowNatives.GetView(
windowParams.viewId, windowParams.injectTitlebar);
}
if (!view) {
// No route to created window. If given a callback, trigger it with an
// undefined object.
if (request.callback) {
request.callback();
delete request.callback;
}
return;
}
if (windowParams.existingWindow) {
// Not creating a new window, but activating an existing one, so trigger
// callback with existing window and don't do anything else.
if (request.callback) {
request.callback(view.chrome.app.window.current());
delete request.callback;
}
return;
}
// Initialize appWindowData in the newly created JS context
view.chrome.app.window.initializeAppWindow(windowParams);
var callback = request.callback;
if (callback) {
delete request.callback;
if (!view) {
callback(undefined);
return;
}
var willCallback =
renderViewObserverNatives.OnDocumentElementCreated(
windowParams.viewId,
function(success) {
if (success) {
callback(view.chrome.app.window.current());
} else {
callback(undefined);
}
});
if (!willCallback) {
callback(undefined);
}
}
});
apiFunctions.setHandleRequest('current', function() {
if (!currentAppWindow) {
console.error('The JavaScript context calling ' +
'chrome.app.window.current() has no associated AppWindow.');
return null;
}
return currentAppWindow;
});
apiFunctions.setHandleRequest('getAll', function() {
var views = runtimeNatives.GetExtensionViews(-1, 'SHELL');
return $Array.map(views, function(win) {
return win.chrome.app.window.current();
});
});
apiFunctions.setHandleRequest('get', function(id) {
var windows = $Array.filter(chrome.app.window.getAll(), function(win) {
return win.id == id;
});
return windows.length > 0 ? windows[0] : null;
});
// This is an internal function, but needs to be bound with setHandleRequest
// because it is called from a different JS context.
apiFunctions.setHandleRequest('initializeAppWindow', function(params) {
var currentWindowInternal =
Binding.create('app.currentWindowInternal').generate();
var AppWindow = function() {};
forEach(currentWindowInternal, function(key, value) {
AppWindow.prototype[key] = value;
});
AppWindow.prototype.moveTo = $Function.bind(window.moveTo, window);
AppWindow.prototype.resizeTo = $Function.bind(window.resizeTo, window);
AppWindow.prototype.contentWindow = window;
AppWindow.prototype.onClosed = new Event();
AppWindow.prototype.close = function() {
this.contentWindow.close();
};
AppWindow.prototype.getBounds = function() {
var bounds = appWindowData.bounds;
return { left: bounds.left, top: bounds.top,
width: bounds.width, height: bounds.height };
};
AppWindow.prototype.getMinWidth = function() {
return appWindowData.minWidth;
};
AppWindow.prototype.getMinHeight = function() {
return appWindowData.minHeight;
};
AppWindow.prototype.getMaxWidth = function() {
return appWindowData.maxWidth;
};
AppWindow.prototype.getMaxHeight = function() {
return appWindowData.maxHeight;
};
AppWindow.prototype.isFullscreen = function() {
return appWindowData.fullscreen;
};
AppWindow.prototype.isMinimized = function() {
return appWindowData.minimized;
};
AppWindow.prototype.isMaximized = function() {
return appWindowData.maximized;
};
AppWindow.prototype.isAlwaysOnTop = function() {
return appWindowData.alwaysOnTop;
};
Object.defineProperty(AppWindow.prototype, 'id', {get: function() {
return appWindowData.id;
}});
appWindowData = {
id: params.id || '',
bounds: { left: params.bounds.left, top: params.bounds.top,
width: params.bounds.width, height: params.bounds.height },
minWidth: params.minWidth,
minHeight: params.minHeight,
maxWidth: params.maxWidth,
maxHeight: params.maxHeight,
fullscreen: params.fullscreen,
minimized: params.minimized,
maximized: params.maximized,
alwaysOnTop: params.alwaysOnTop
};
currentAppWindow = new AppWindow;
});
});
function boundsEqual(bounds1, bounds2) {
if (!bounds1 || !bounds2)
return false;
return (bounds1.left == bounds2.left && bounds1.top == bounds2.top &&
bounds1.width == bounds2.width && bounds1.height == bounds2.height);
}
function dispatchEventIfExists(target, name) {
// Sometimes apps like to put their own properties on the window which
// break our assumptions.
var event = target[name];
if (event && (typeof event.dispatch == 'function'))
event.dispatch();
else
console.warn('Could not dispatch ' + name + ', event has been clobbered');
}
function updateAppWindowProperties(update) {
if (!appWindowData)
return;
var oldData = appWindowData;
update.id = oldData.id;
appWindowData = update;
var currentWindow = currentAppWindow;
if (!boundsEqual(oldData.bounds, update.bounds))
dispatchEventIfExists(currentWindow, "onBoundsChanged");
if (!oldData.fullscreen && update.fullscreen)
dispatchEventIfExists(currentWindow, "onFullscreened");
if (!oldData.minimized && update.minimized)
dispatchEventIfExists(currentWindow, "onMinimized");
if (!oldData.maximized && update.maximized)
dispatchEventIfExists(currentWindow, "onMaximized");
if ((oldData.fullscreen && !update.fullscreen) ||
(oldData.minimized && !update.minimized) ||
(oldData.maximized && !update.maximized))
dispatchEventIfExists(currentWindow, "onRestored");
};
function onAppWindowClosed() {
if (!currentAppWindow)
return;
dispatchEventIfExists(currentAppWindow, "onClosed");
}
exports.binding = appWindow.generate();
exports.onAppWindowClosed = onAppWindowClosed;
exports.updateAppWindowProperties = updateAppWindowProperties;
|