This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 973
/
windowState.js
188 lines (159 loc) · 6.3 KB
/
windowState.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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
const { makeImmutable, isMap, isList } = require('./immutableUtil')
const Immutable = require('immutable')
const assert = require('assert')
// TODO(bridiver) - make these generic validation functions
const validateId = function (propName, id) {
assert.ok(id, `${propName} cannot be null`)
id = parseInt(id)
assert.ok(id === -1 || id >= 1, `${propName} must be positive or -1`)
return id
}
const validateState = function (state) {
state = makeImmutable(state)
assert.ok(isMap(state), 'state must be an Immutable.Map')
assert.ok(isList(state.get('windows')), 'state must contain an Immutable.List of windows')
return state
}
const validateWindowValue = function (windowValue) {
windowValue = makeImmutable(windowValue)
assert.ok(isMap(windowValue), 'windowValue must be an Immutable.Map')
assert.ok(windowValue.get('windowId'), 'window must have a windowId')
return windowValue
}
const validateAction = function (action) {
action = makeImmutable(action)
assert.ok(isMap(action), 'action must be an Immutable.Map')
return action
}
const api = {
WINDOW_ID_NONE: -1,
WINDOW_ID_CURRENT: -2,
getWindowIndex: (state, windowValue) => {
state = validateState(state)
let windowId = validateId('windowId', windowValue.get('windowId'))
return api.getWindowIndexByWindowId(state, windowId)
},
insertWindow: (state, action) => {
action = validateAction(action)
state = validateState(state)
let windowValue = validateWindowValue(action.get('windowValue'))
assert.ok(!api.getWindow(state, windowValue), 'Window already exists')
return state.set('windows', state.get('windows').push(windowValue))
},
getWindow: (state, windowValue) => {
state = validateState(state)
windowValue = validateWindowValue(windowValue)
let windowId = windowValue.get('windowId')
return api.getByWindowId(state, windowId)
},
maybeCreateWindow: (state, action) => {
action = validateAction(action)
state = validateState(state)
let windowValue = validateWindowValue(action.get('windowValue'))
if (api.getWindow(state, windowValue)) {
return api.updateWindow(state, action)
} else {
return api.insertWindow(state, action)
}
},
getActiveWindow: (state) => {
state = validateState(state)
return state.get('windows').find((win) => win.get('focused') === true)
},
getActiveWindowId: (state) => {
const win = api.getActiveWindow(state)
return (win && win.get('windowId')) || api.WINDOW_ID_NONE
},
getByWindowId: (state, windowId) => {
state = validateState(state)
windowId = validateId('windowId', windowId)
return state.get('windows').find((win) => win.get('windowId') === windowId)
},
getWindowIndexByWindowId: (state, windowId) => {
state = validateState(state)
windowId = validateId('windowId', windowId)
return state.get('windows').findIndex((win) => win.get('windowId') === windowId)
},
removeWindowByWindowId: (state, windowId) => {
windowId = validateId('windowId', windowId)
state = validateState(state)
let index = api.getWindowIndexByWindowId(state, windowId)
if (index === -1) {
return state
}
return api.removeWindowByIndex(state, index)
},
removeWindowByIndex: (state, index) => {
index = parseInt(index)
assert.ok(index >= 0, 'index must be positive')
state = validateState(state)
return state.set('windows', state.get('windows').delete(index))
},
removeWindow: (state, action) => {
action = validateAction(action)
state = validateState(state)
let windowValue = validateWindowValue(action.get('windowValue'))
let windowId = validateId('windowId', windowValue.get('windowId'))
return api.removeWindowByWindowId(state, windowId)
},
updateWindow: (state, action) => {
action = validateAction(action)
state = validateState(state)
let windowValue = validateWindowValue(action.get('windowValue'))
let windows = state.get('windows')
let index = api.getWindowIndex(state, windowValue)
if (index === -1) {
return state
}
let currentWindowValue = windows.get(index)
let windowId = windowValue.get('windowId')
if (windowId) {
windowId = validateId('windowId', windowId)
let currentWindowId = currentWindowValue.get('windowId')
if (currentWindowId) {
assert.ok(windowId === currentWindowId, 'Changing a windowId is not allowed')
}
}
if (!action.get('replace')) {
windowValue = currentWindowValue.mergeDeep(windowValue)
}
return state.set('windows', windows.delete(index).insert(index, windowValue))
},
getWindows: (state) => {
state = validateState(state)
return state.get('windows')
},
getPersistentState: (state) => {
// TODO(bridiver) handle restoring state
state = makeImmutable(state)
return state.set('windows', Immutable.List())
},
// TODO (nejc) we should only pass in one state
// refactor when window state is merged into app state
shouldAllowWindowDrag: (state, windowState, frame, isFocused) => {
const shieldState = require('./shieldState')
const defaultBrowserState = require('./defaultBrowserState')
const braveryPanelIsVisible = shieldState.braveShieldsEnabled(frame) &&
windowState.get('braveryPanelDetail')
const checkDefaultBrowserDialogIsVisible = isFocused &&
defaultBrowserState.shouldDisplayDialog(state)
return !state.get('contextMenuDetail') &&
!windowState.get('bookmarkDetail') &&
!windowState.getIn(['ui', 'siteInfo', 'isVisible']) &&
!braveryPanelIsVisible &&
!windowState.getIn(['ui', 'isClearBrowsingDataPanelVisible']) &&
!windowState.get('importBrowserDataDetail') &&
!windowState.getIn(['widevinePanelDetail', 'shown']) &&
!windowState.get('autofillAddressDetail') &&
!windowState.get('autofillCreditCardDetail') &&
!windowState.getIn(['ui', 'releaseNotes', 'isVisible']) &&
!checkDefaultBrowserDialogIsVisible &&
!windowState.getIn(['ui', 'noScriptInfo', 'isVisible']) &&
frame && !frame.getIn(['security', 'loginRequiredDetail']) &&
!windowState.getIn(['ui', 'menubar', 'selectedIndex'])
}
}
module.exports = api