forked from scratchfoundation/scratch-gui
-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathstage-header.jsx
More file actions
273 lines (258 loc) · 10.8 KB
/
stage-header.jsx
File metadata and controls
273 lines (258 loc) · 10.8 KB
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
import classNames from 'classnames';
import {defineMessages, injectIntl, intlShape} from 'react-intl';
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import VM from 'scratch-vm';
import Box from '../box/box.jsx';
import Button from '../button/button.jsx';
import ToggleButtons from '../toggle-buttons/toggle-buttons.jsx';
import Controls from '../../containers/controls.jsx';
import {getStageDimensions} from '../../lib/screen-utils';
import {STAGE_DISPLAY_SIZES, STAGE_SIZE_MODES, STAGE_DISPLAY_SCALE_METADATA, FIXED_WIDTH} from '../../lib/layout-constants';
import fullScreenIcon from './icon--fullscreen.svg';
import unFullScreenIcon from './icon--unfullscreen.svg';
import largeStageIcon from '!../../lib/tw-recolor/build!./icon--large-stage.svg';
import smallStageIcon from '!../../lib/tw-recolor/build!./icon--small-stage.svg';
import fullStageIcon from '!../../lib/tw-recolor/build!./icon--full-stage.svg';
import settingsIcon from './icon--settings.svg';
import styles from './stage-header.css';
import FullscreenAPI from '../../lib/tw-fullscreen-api';
const messages = defineMessages({
largeStageSizeMessage: {
defaultMessage: 'Switch to large stage',
description: 'Button to change stage size to large',
id: 'gui.stageHeader.stageSizeLarge'
},
smallStageSizeMessage: {
defaultMessage: 'Switch to small stage',
description: 'Button to change stage size to small',
id: 'gui.stageHeader.stageSizeSmall'
},
fullStageSizeMessage: {
defaultMessage: 'Switch to full stage',
description: 'Button to change stage size to its full size',
id: 'tw.stageHeader.full'
},
fullScreenMessage: {
defaultMessage: 'Enter full screen mode',
description: 'Button to change stage size to full screen',
id: 'gui.stageHeader.stageSizeFull'
},
unFullScreenMessage: {
defaultMessage: 'Exit full screen mode',
description: 'Button to get out of full screen mode',
id: 'gui.stageHeader.stageSizeUnFull'
},
fullscreenControl: {
defaultMessage: 'Full Screen Control',
description: 'Button to enter/exit full screen mode',
id: 'gui.stageHeader.fullscreenControl'
},
openSettingsMessage: {
defaultMessage: 'Open advanced settings',
description: 'Button to open advanced settings in embeds',
id: 'tw.openAdvanced'
}
});
const enableSettingsButton = new URLSearchParams(location.search).has('settings-button');
const StageHeaderComponent = function (props) {
const {
customStageSize,
showFixedLargeSize,
isFullScreen,
isPlayerOnly,
onKeyPress,
onSetStageFullScreen,
onSetStageUnFullScreen,
onSetStageLarge,
onSetStageSmall,
onSetStageFull,
onOpenSettings,
isEmbedded,
stageSize,
stageSizeMode,
vm
} = props;
let header = null;
const stageDimensions = getStageDimensions(stageSize, customStageSize, isFullScreen || isEmbedded);
if (isFullScreen || isEmbedded) {
const settingsButton = isEmbedded && enableSettingsButton ? (
<div className={classNames(styles.settingsButton, styles.unselectWrapper)}>
<Button
className={styles.stageButton}
onClick={onOpenSettings}
>
<img
alt={props.intl.formatMessage(messages.openSettingsMessage)}
className={styles.stageButtonIcon}
draggable={false}
src={settingsIcon}
title={props.intl.formatMessage(messages.openSettingsMessage)}
/>
</Button>
</div>
) : null;
const fullscreenButton = isFullScreen ? (
<div className={styles.unselectWrapper}>
<Button
className={styles.stageButton}
onClick={onSetStageUnFullScreen}
onKeyPress={onKeyPress}
>
<img
alt={props.intl.formatMessage(messages.unFullScreenMessage)}
className={styles.stageButtonIcon}
draggable={false}
src={unFullScreenIcon}
title={props.intl.formatMessage(messages.fullscreenControl)}
/>
</Button>
</div>
) : FullscreenAPI.available() ? (
<div className={styles.unselectWrapper}>
<Button
className={styles.stageButton}
onClick={onSetStageFullScreen}
>
<img
alt={props.intl.formatMessage(messages.fullScreenMessage)}
className={styles.stageButtonIcon}
draggable={false}
src={fullScreenIcon}
title={props.intl.formatMessage(messages.fullscreenControl)}
/>
</Button>
</div>
) : null;
header = (
<Box
className={classNames(styles.stageHeaderWrapperOverlay, {
[styles.embedded]: isEmbedded
})}
>
<Box
className={styles.stageMenuWrapper}
style={{width: stageDimensions.width}}
>
<Controls vm={vm} />
<div
className={styles.fullscreenButtonsRow}
key="fullscreen" // addons require the HTML element to be not be re-used by in-editor buttons
>
{settingsButton}
{fullscreenButton}
</div>
</Box>
</Box>
);
} else {
const stageControls =
isPlayerOnly ? (
[]
) : (
<div className={styles.stageSizeToggleGroup}>
<ToggleButtons
buttons={[
{
handleClick: onSetStageSmall,
icon: smallStageIcon,
iconClassName: styles.stageButtonIcon,
isSelected: stageSizeMode === STAGE_SIZE_MODES.small,
title: props.intl.formatMessage(messages.smallStageSizeMessage)
},
...(showFixedLargeSize ? [
{
handleClick: onSetStageLarge,
icon: largeStageIcon,
iconClassName: styles.stageButtonIcon,
isSelected: stageSizeMode === STAGE_SIZE_MODES.large,
title: props.intl.formatMessage(messages.largeStageSizeMessage)
}
] : []),
{
handleClick: onSetStageFull,
icon: showFixedLargeSize ? fullStageIcon : largeStageIcon,
iconClassName: styles.stageButtonIcon,
isSelected: stageSizeMode === STAGE_SIZE_MODES.full,
title: props.intl.formatMessage(messages.fullStageSizeMessage)
}
]}
/>
</div>
);
header = (
<Box
className={styles.stageHeaderWrapper}
// + 2 px because the stage will have 2 pixels of border around it
style={{minWidth: `${
stageSize === STAGE_DISPLAY_SIZES.constrained
? STAGE_DISPLAY_SCALE_METADATA.constrained.scale * Math.max(stageDimensions.widthDefault, FIXED_WIDTH) + 2
: (stageSizeMode === STAGE_SIZE_MODES.full || stageSizeMode === STAGE_SIZE_MODES.large) && stageDimensions.width <= FIXED_WIDTH
? FIXED_WIDTH + 2
: stageDimensions.width + 2
}px`
}}
>
<Box className={styles.stageMenuWrapper}>
<Controls
vm={vm}
isSmall={stageSizeMode === STAGE_SIZE_MODES.small}
/>
<div
className={styles.stageSizeRow}
key="editor" // addons require the HTML element to be not be re-used by in-editor buttons
>
{stageControls}
<div>
<Button
className={styles.stageButton}
onClick={onSetStageFullScreen}
>
<img
alt={props.intl.formatMessage(messages.fullStageSizeMessage)}
className={styles.stageButtonIcon}
draggable={false}
src={fullScreenIcon}
title={props.intl.formatMessage(messages.fullscreenControl)}
/>
</Button>
</div>
</div>
</Box>
</Box>
);
}
return header;
};
const mapStateToProps = state => ({
// This is the button's mode, as opposed to the actual current state
stageSizeMode: state.scratchGui.stageSize.stageSize
});
StageHeaderComponent.propTypes = {
intl: intlShape,
customStageSize: PropTypes.shape({
width: PropTypes.number,
height: PropTypes.number
}),
showFixedLargeSize: PropTypes.bool,
isFullScreen: PropTypes.bool.isRequired,
isPlayerOnly: PropTypes.bool.isRequired,
onKeyPress: PropTypes.func.isRequired,
onSetStageFullScreen: PropTypes.func.isRequired,
onSetStageUnFullScreen: PropTypes.func.isRequired,
onSetStageLarge: PropTypes.func.isRequired,
onSetStageSmall: PropTypes.func.isRequired,
onSetStageFull: PropTypes.func.isRequired,
onOpenSettings: PropTypes.func.isRequired,
isEmbedded: PropTypes.bool.isRequired,
stageSize: PropTypes.oneOf(Object.keys(STAGE_DISPLAY_SIZES)),
stageSizeMode: PropTypes.oneOf(Object.keys(STAGE_SIZE_MODES)),
vm: PropTypes.instanceOf(VM).isRequired
};
StageHeaderComponent.defaultProps = {
stageSizeMode: STAGE_SIZE_MODES.large
};
export default injectIntl(connect(
mapStateToProps
)(StageHeaderComponent));