-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
windows.coffee
269 lines (214 loc) · 6.89 KB
/
windows.coffee
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
_ = require("lodash")
uri = require("url")
Promise = require("bluebird")
cyDesktop = require("@packages/desktop-gui")
extension = require("@packages/extension")
contextMenu = require("electron-context-menu")
BrowserWindow = require("electron").BrowserWindow
debug = require("debug")("cypress:server:windows")
cwd = require("../cwd")
user = require("../user")
savedState = require("../saved_state")
windows = {}
recentlyCreatedWindow = false
getUrl = (type) ->
switch type
when "INDEX"
cyDesktop.getPathToIndex()
else
throw new Error("No acceptable window type found for: '#{type}'")
getByType = (type) ->
windows[type]
getCookieUrl = (props) ->
extension.getCookieUrl(props)
firstOrNull = (cookies) ->
## normalize into null when empty array
cookies[0] ? null
setWindowProxy = (win) ->
if not process.env.HTTP_PROXY
return
return new Promise (resolve) ->
win.webContents.session.setProxy({
proxyRules: process.env.HTTP_PROXY
proxyBypassRules: process.env.NO_PROXY
}, resolve)
module.exports = {
reset: ->
windows = {}
destroy: (type) ->
if type and (win = getByType(type))
win.destroy()
get: (type) ->
getByType(type) ? throw new Error("No window exists for: '#{type}'")
showAll: ->
_.invoke windows, "showInactive"
hideAllUnlessAnotherWindowIsFocused: ->
## bail if we have another focused window
## or we are in the middle of creating a new one
return if BrowserWindow.getFocusedWindow() or recentlyCreatedWindow
## else hide all windows
_.invoke windows, "hide"
focusMainWindow: ->
getByType('INDEX').show()
getByWebContents: (webContents) ->
BrowserWindow.fromWebContents(webContents)
_newBrowserWindow: (options) ->
new BrowserWindow(options)
defaults: (options = {}) ->
_.defaultsDeep(options, {
x: null
y: null
show: true
frame: true
width: null
height: null
minWidth: null
minHeight: null
devTools: false
trackState: false
contextMenu: false
recordFrameRate: null
# extension: null ## TODO add these once we update electron
# devToolsExtension: null ## since these API's were added in 1.7.6
onPaint: null
onFocus: ->
onBlur: ->
onClose: ->
onCrashed: ->
onNewWindow: ->
webPreferences: {
partition: null
chromeWebSecurity: true
nodeIntegration: false
backgroundThrottling: false
}
})
create: (projectRoot, options = {}) ->
options = @defaults(options)
if options.show is false
options.frame = false
options.webPreferences.offscreen = true
if options.chromeWebSecurity is false
options.webPreferences.webSecurity = false
if options.partition
options.webPreferences.partition = options.partition
win = @_newBrowserWindow(options)
win.on "blur", ->
options.onBlur.apply(win, arguments)
win.on "focus", ->
options.onFocus.apply(win, arguments)
win.once "closed", ->
win.removeAllListeners()
options.onClose.apply(win, arguments)
## the webview loses focus on navigation, so we
## have to refocus it everytime top navigates in headless mode
## https://github.com/cypress-io/cypress/issues/2190
if options.show is false
win.webContents.on "did-start-loading", ->
if not win.isDestroyed()
win.focusOnWebView()
win.webContents.on "crashed", ->
options.onCrashed.apply(win, arguments)
win.webContents.on "new-window", ->
options.onNewWindow.apply(win, arguments)
if ts = options.trackState
@trackState(projectRoot, options.isTextTerminal, win, ts)
## open dev tools if they're true
if options.devTools
## and possibly detach dev tools if true
win.webContents.openDevTools()
if options.contextMenu
## adds context menu with copy, paste, inspect element, etc
contextMenu({
showInspectElement: true
window: win
})
if options.onPaint
setFrameRate = (num) ->
if win.webContents.getFrameRate() isnt num
win.webContents.setFrameRate(num)
win.webContents.on "paint", (event, dirty, image) ->
## https://github.com/cypress-io/cypress/issues/705
## if win is destroyed this will throw
try
if fr = options.recordFrameRate
setFrameRate(fr)
options.onPaint.apply(win, arguments)
catch err
## do nothing
win
open: (projectRoot, options = {}) ->
## if we already have a window open based
## on that type then just show + focus it!
if win = getByType(options.type)
win.show()
return Promise.resolve(win)
recentlyCreatedWindow = true
_.defaults(options, {
width: 600
height: 500
show: true
webPreferences: {
preload: cwd("lib", "ipc", "ipc.js")
}
})
if not options.url
options.url = getUrl(options.type)
win = @create(projectRoot, options)
debug("creating electron window with options %o", options)
windows[options.type] = win
win.webContents.id = _.uniqueId("webContents")
win.once "closed", ->
## slice the window out of windows reference
delete windows[options.type]
## enable our url to be a promise
## and wait for this to be resolved
Promise.join(
options.url,
setWindowProxy(win)
)
.spread (url) ->
## navigate the window here!
win.loadURL(url)
## reset this back to false
recentlyCreatedWindow = false
.thenReturn(win)
trackState: (projectRoot, isTextTerminal, win, keys) ->
isDestroyed = ->
win.isDestroyed()
win.on "resize", _.debounce ->
return if isDestroyed()
[width, height] = win.getSize()
[x, y] = win.getPosition()
newState = {}
newState[keys.width] = width
newState[keys.height] = height
newState[keys.x] = x
newState[keys.y] = y
savedState(projectRoot, isTextTerminal)
.then (state) ->
state.set(newState)
, 500
win.on "moved", _.debounce ->
return if isDestroyed()
[x, y] = win.getPosition()
newState = {}
newState[keys.x] = x
newState[keys.y] = y
savedState(projectRoot, isTextTerminal)
.then (state) ->
state.set(newState)
, 500
win.webContents.on "devtools-opened", ->
newState = {}
newState[keys.devTools] = true
savedState(projectRoot, isTextTerminal)
.then (state) ->
state.set(newState)
win.webContents.on "devtools-closed", ->
newState = {}
newState[keys.devTools] = false
savedState(projectRoot, isTextTerminal)
.then (state) ->
state.set(newState)
}