|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2024 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
1 | 7 | import * as Blockly from "blockly/core"; |
2 | 8 | import { ContinuousFlyout } from "@blockly/continuous-toolbox"; |
| 9 | +import { RecyclableBlockFlyoutInflater } from "./recyclable_block_flyout_inflater.js"; |
3 | 10 |
|
4 | 11 | export class CheckableContinuousFlyout extends ContinuousFlyout { |
5 | 12 | /** |
6 | | - * Size of a checkbox next to a variable reporter. |
7 | | - * @type {number} |
8 | | - * @const |
9 | | - */ |
10 | | - static CHECKBOX_SIZE = 25; |
11 | | - |
12 | | - /** |
13 | | - * Amount of touchable padding around reporter checkboxes. |
14 | | - * @type {number} |
15 | | - * @const |
16 | | - */ |
17 | | - static CHECKBOX_TOUCH_PADDING = 12; |
18 | | - |
19 | | - /** |
20 | | - * SVG path data for checkmark in checkbox. |
21 | | - * @type {string} |
22 | | - * @const |
23 | | - */ |
24 | | - static CHECKMARK_PATH = |
25 | | - "M" + |
26 | | - CheckableContinuousFlyout.CHECKBOX_SIZE / 4 + |
27 | | - " " + |
28 | | - CheckableContinuousFlyout.CHECKBOX_SIZE / 2 + |
29 | | - "L" + |
30 | | - (5 * CheckableContinuousFlyout.CHECKBOX_SIZE) / 12 + |
31 | | - " " + |
32 | | - (2 * CheckableContinuousFlyout.CHECKBOX_SIZE) / 3 + |
33 | | - "L" + |
34 | | - (3 * CheckableContinuousFlyout.CHECKBOX_SIZE) / 4 + |
35 | | - " " + |
36 | | - CheckableContinuousFlyout.CHECKBOX_SIZE / 3; |
37 | | - |
38 | | - /** |
39 | | - * Size of the checkbox corner radius |
40 | | - * @type {number} |
41 | | - * @const |
| 13 | + * Creates a new CheckableContinuousFlyout. |
| 14 | + * |
| 15 | + * @param {!Blockly.Options} workspaceOptions Configuration options for the |
| 16 | + * flyout workspace. |
42 | 17 | */ |
43 | | - static CHECKBOX_CORNER_RADIUS = 5; |
44 | | - |
45 | | - /** |
46 | | - * @type {number} |
47 | | - * @const |
48 | | - */ |
49 | | - static CHECKBOX_MARGIN = ContinuousFlyout.prototype.MARGIN; |
50 | | - |
51 | | - /** |
52 | | - * Total additional width of a row that contains a checkbox. |
53 | | - * @type {number} |
54 | | - * @const |
55 | | - */ |
56 | | - static CHECKBOX_SPACE_X = |
57 | | - CheckableContinuousFlyout.CHECKBOX_SIZE + |
58 | | - 2 * CheckableContinuousFlyout.CHECKBOX_MARGIN; |
59 | | - |
60 | 18 | constructor(workspaceOptions) { |
61 | 19 | workspaceOptions.modalInputs = false; |
62 | 20 | super(workspaceOptions); |
63 | 21 | this.tabWidth_ = -2; |
64 | 22 | this.MARGIN = 12; |
65 | 23 | this.GAP_Y = 12; |
66 | | - CheckableContinuousFlyout.CHECKBOX_MARGIN = this.MARGIN; |
67 | | - |
68 | | - /** |
69 | | - * Map of checkboxes that correspond to monitored blocks. |
70 | | - * Each element is an object containing the SVG for the checkbox, a boolean |
71 | | - * for its checked state, and the block the checkbox is associated with. |
72 | | - * @type {!Object.<string, !Object>} |
73 | | - * @private |
74 | | - */ |
75 | | - this.checkboxes_ = new Map(); |
76 | | - } |
77 | | - |
78 | | - initFlyoutButton_(button, x, y) { |
79 | | - if (button.isLabel()) { |
80 | | - button.height = 40; |
81 | | - } |
82 | | - super.initFlyoutButton_(button, x, y); |
83 | 24 | } |
84 | 25 |
|
| 26 | + /** |
| 27 | + * Displays the given contents in the flyout. |
| 28 | + * |
| 29 | + * @param {!Object} flyoutDef The new contents to show in the flyout. |
| 30 | + */ |
85 | 31 | show(flyoutDef) { |
86 | | - this.clearOldCheckboxes(); |
87 | 32 | super.show(flyoutDef); |
| 33 | + const inflater = this.getInflaterForType("block"); |
| 34 | + if (inflater instanceof RecyclableBlockFlyoutInflater) { |
| 35 | + inflater.emptyRecycledBlocks(); |
| 36 | + } |
88 | 37 | } |
89 | 38 |
|
| 39 | + /** |
| 40 | + * Serializes a block to JSON in order to copy it to the main workspace. |
| 41 | + * |
| 42 | + * @param {!Blockly.BlockSvg} block The block to serialize. |
| 43 | + * @returns {!Object} A JSON representation of the block. |
| 44 | + */ |
90 | 45 | serializeBlock(block) { |
91 | 46 | const json = super.serializeBlock(block); |
92 | 47 | // Delete the serialized block's ID so that a new one is generated when it is |
@@ -97,214 +52,75 @@ export class CheckableContinuousFlyout extends ContinuousFlyout { |
97 | 52 | return json; |
98 | 53 | } |
99 | 54 |
|
100 | | - clearOldCheckboxes() { |
101 | | - for (const checkbox of this.checkboxes_.values()) { |
102 | | - checkbox.svgRoot.remove(); |
103 | | - } |
104 | | - this.checkboxes_.clear(); |
105 | | - } |
106 | | - |
107 | | - layout_(contents, gaps) { |
108 | | - super.layout_(contents, gaps); |
109 | | - // We want large gaps between categories (see GAP_Y), but don't want those |
110 | | - // counted as part of the category for purposes of scrolling to show the |
111 | | - // category, so we reset/adjust the label gaps used for the scroll position |
112 | | - // calculation here. |
113 | | - this.labelGaps.fill( |
114 | | - this.getWorkspace().getRenderer().getConstants().GRID_UNIT |
115 | | - ); |
116 | | - } |
117 | | - |
118 | | - calculateBottomPadding(contentMetrics, viewMetrics) { |
119 | | - // Since we're messing with the alignment by munging the label gaps, we also |
120 | | - // need to adjust the bottom padding. |
121 | | - return ( |
122 | | - super.calculateBottomPadding(contentMetrics, viewMetrics) - |
123 | | - this.getWorkspace().getRenderer().getConstants().GRID_UNIT * 4 |
124 | | - ); |
| 55 | + /** |
| 56 | + * Set the state of a checkbox by block ID. |
| 57 | + * @param {string} blockId ID of the block whose checkbox should be set |
| 58 | + * @param {boolean} value Value to set the checkbox to. |
| 59 | + * @public |
| 60 | + */ |
| 61 | + setCheckboxState(blockId, value) { |
| 62 | + this.getWorkspace() |
| 63 | + .getBlockById(blockId) |
| 64 | + ?.getIcon("checkbox") |
| 65 | + ?.setChecked(value); |
125 | 66 | } |
126 | 67 |
|
127 | | - addBlockListeners_(root, block, rect) { |
128 | | - if (block.checkboxInFlyout) { |
129 | | - const coordinates = block.getRelativeToSurfaceXY(); |
130 | | - const checkbox = this.createCheckbox_( |
131 | | - block, |
132 | | - coordinates.x, |
133 | | - coordinates.y, |
134 | | - block.getHeightWidth() |
135 | | - ); |
136 | | - let moveX = coordinates.x; |
137 | | - if (this.RTL) { |
138 | | - moveX -= |
139 | | - CheckableContinuousFlyout.CHECKBOX_SIZE + |
140 | | - CheckableContinuousFlyout.CHECKBOX_MARGIN; |
141 | | - } else { |
142 | | - moveX += |
143 | | - CheckableContinuousFlyout.CHECKBOX_SIZE + |
144 | | - CheckableContinuousFlyout.CHECKBOX_MARGIN; |
145 | | - } |
146 | | - block.moveBy(moveX, 0); |
147 | | - this.listeners.push( |
148 | | - Blockly.browserEvents.bind( |
149 | | - checkbox.svgRoot, |
150 | | - "mousedown", |
151 | | - null, |
152 | | - this.checkboxClicked_(checkbox) |
153 | | - ) |
154 | | - ); |
155 | | - } |
156 | | - super.addBlockListeners_(root, block, rect); |
| 68 | + getFlyoutScale() { |
| 69 | + return 0.675; |
157 | 70 | } |
158 | 71 |
|
159 | | - /** |
160 | | - * Respond to a click on a checkbox in the flyout. |
161 | | - * @param {!Object} checkboxObj An object containing the svg element of the |
162 | | - * checkbox, a boolean for the state of the checkbox, and the block the |
163 | | - * checkbox is associated with. |
164 | | - * @return {!Function} Function to call when checkbox is clicked. |
165 | | - * @private |
166 | | - */ |
167 | | - checkboxClicked_(checkboxObj) { |
168 | | - return function (e) { |
169 | | - this.setCheckboxState(checkboxObj.block.id, !checkboxObj.clicked); |
170 | | - // This event has been handled. No need to bubble up to the document. |
171 | | - e.stopPropagation(); |
172 | | - e.preventDefault(); |
173 | | - }.bind(this); |
| 72 | + getWidth() { |
| 73 | + return 250; |
174 | 74 | } |
175 | 75 |
|
176 | 76 | /** |
177 | | - * Create and place a checkbox corresponding to the given block. |
178 | | - * @param {!Blockly.Block} block The block to associate the checkbox to. |
179 | | - * @param {number} cursorX The x position of the cursor during this layout pass. |
180 | | - * @param {number} cursorY The y position of the cursor during this layout pass. |
181 | | - * @param {!{height: number, width: number}} blockHW The height and width of the |
182 | | - * block. |
183 | | - * @private |
| 77 | + * Sets whether or not block recycling is enabled in the flyout. |
| 78 | + * |
| 79 | + * @param {boolean} enabled True if recycling should be enabled. |
184 | 80 | */ |
185 | | - createCheckbox_(block, cursorX, cursorY, blockHW) { |
186 | | - var checkboxState = this.getCheckboxState(block.id); |
187 | | - var svgRoot = block.getSvgRoot(); |
188 | | - var extraSpace = |
189 | | - CheckableContinuousFlyout.CHECKBOX_SIZE + |
190 | | - CheckableContinuousFlyout.CHECKBOX_MARGIN; |
191 | | - var xOffset = this.RTL |
192 | | - ? this.getWidth() / this.workspace_.scale - extraSpace |
193 | | - : cursorX; |
194 | | - var yOffset = |
195 | | - cursorY + |
196 | | - blockHW.height / 2 - |
197 | | - CheckableContinuousFlyout.CHECKBOX_SIZE / 2; |
198 | | - var touchMargin = CheckableContinuousFlyout.CHECKBOX_TOUCH_PADDING; |
199 | | - var checkboxGroup = Blockly.utils.dom.createSvgElement( |
200 | | - "g", |
201 | | - { |
202 | | - transform: `translate(${xOffset}, ${yOffset})`, |
203 | | - fill: "transparent", |
204 | | - }, |
205 | | - null |
206 | | - ); |
207 | | - Blockly.utils.dom.createSvgElement( |
208 | | - "rect", |
209 | | - { |
210 | | - class: "blocklyFlyoutCheckbox", |
211 | | - height: CheckableContinuousFlyout.CHECKBOX_SIZE, |
212 | | - width: CheckableContinuousFlyout.CHECKBOX_SIZE, |
213 | | - rx: CheckableContinuousFlyout.CHECKBOX_CORNER_RADIUS, |
214 | | - ry: CheckableContinuousFlyout.CHECKBOX_CORNER_RADIUS, |
215 | | - }, |
216 | | - checkboxGroup |
217 | | - ); |
218 | | - Blockly.utils.dom.createSvgElement( |
219 | | - "path", |
220 | | - { |
221 | | - class: "blocklyFlyoutCheckboxPath", |
222 | | - d: CheckableContinuousFlyout.CHECKMARK_PATH, |
223 | | - }, |
224 | | - checkboxGroup |
225 | | - ); |
226 | | - Blockly.utils.dom.createSvgElement( |
227 | | - "rect", |
228 | | - { |
229 | | - class: "blocklyTouchTargetBackground", |
230 | | - x: -touchMargin + "px", |
231 | | - y: -touchMargin + "px", |
232 | | - height: CheckableContinuousFlyout.CHECKBOX_SIZE + 2 * touchMargin, |
233 | | - width: CheckableContinuousFlyout.CHECKBOX_SIZE + 2 * touchMargin, |
234 | | - }, |
235 | | - checkboxGroup |
236 | | - ); |
237 | | - var checkboxObj = { |
238 | | - svgRoot: checkboxGroup, |
239 | | - clicked: checkboxState, |
240 | | - block: block, |
241 | | - }; |
242 | | - |
243 | | - if (checkboxState) { |
244 | | - Blockly.utils.dom.addClass(checkboxObj.svgRoot, "checked"); |
| 81 | + setRecyclingEnabled(enabled) { |
| 82 | + const inflater = this.getInflaterForType("block"); |
| 83 | + if (inflater instanceof RecyclableBlockFlyoutInflater) { |
| 84 | + inflater.setRecyclingEnabled(enabled); |
245 | 85 | } |
246 | | - |
247 | | - this.workspace_.getCanvas().insertBefore(checkboxGroup, svgRoot); |
248 | | - this.checkboxes_.set(block.id, checkboxObj); |
249 | | - return checkboxObj; |
250 | 86 | } |
251 | 87 |
|
252 | 88 | /** |
253 | | - * Set the state of a checkbox by block ID. |
254 | | - * @param {string} blockId ID of the block whose checkbox should be set |
255 | | - * @param {boolean} value Value to set the checkbox to. |
256 | | - * @public |
| 89 | + * Records scroll position for each category in the toolbox. |
| 90 | + * The scroll position is determined by the coordinates of each category's |
| 91 | + * label after the entire flyout has been rendered. |
| 92 | + * @package |
257 | 93 | */ |
258 | | - setCheckboxState(blockId, value) { |
259 | | - var checkboxObj = this.checkboxes_.get(blockId); |
260 | | - if (!checkboxObj || checkboxObj.clicked === value) { |
261 | | - return; |
262 | | - } |
263 | | - |
264 | | - var oldValue = checkboxObj.clicked; |
265 | | - checkboxObj.clicked = value; |
266 | | - |
267 | | - if (checkboxObj.clicked) { |
268 | | - Blockly.utils.dom.addClass(checkboxObj.svgRoot, "checked"); |
269 | | - } else { |
270 | | - Blockly.utils.dom.removeClass(checkboxObj.svgRoot, "checked"); |
271 | | - } |
272 | | - |
273 | | - Blockly.Events.fire( |
274 | | - new Blockly.Events.BlockChange( |
275 | | - checkboxObj.block, |
276 | | - "checkbox", |
277 | | - null, |
278 | | - oldValue, |
279 | | - value |
| 94 | + recordScrollPositions() { |
| 95 | + // TODO(#211) Remove this once the continuous toolbox has been updated. |
| 96 | + this.scrollPositions = []; |
| 97 | + const categoryLabels = this.getContents() |
| 98 | + .filter( |
| 99 | + (item) => |
| 100 | + item.type === "label" && |
| 101 | + item.element.isLabel() && |
| 102 | + this.getParentToolbox_().getCategoryByName( |
| 103 | + item.element.getButtonText() |
| 104 | + ) |
280 | 105 | ) |
281 | | - ); |
| 106 | + .map((item) => item.element); |
| 107 | + for (const [index, label] of categoryLabels.entries()) { |
| 108 | + this.scrollPositions.push({ |
| 109 | + name: label.getButtonText(), |
| 110 | + position: label.getPosition(), |
| 111 | + }); |
| 112 | + } |
282 | 113 | } |
283 | 114 |
|
284 | 115 | /** |
285 | | - * Gets the checkbox state for a block |
286 | | - * @param {string} blockId The ID of the block in question. |
287 | | - * @return {boolean} Whether the block is checked. |
288 | | - * @public |
| 116 | + * Positions the contents of the flyout. |
| 117 | + * |
| 118 | + * @param {!Blockly.FlyoutItem[]} The flyout items to position. |
289 | 119 | */ |
290 | | - getCheckboxState() { |
291 | | - // Patched by scratch-gui in src/lib/blocks.js. |
292 | | - return false; |
293 | | - } |
294 | | - |
295 | | - getFlyoutScale() { |
296 | | - return 0.675; |
297 | | - } |
298 | | - |
299 | | - getWidth() { |
300 | | - return 250; |
301 | | - } |
302 | | - |
303 | | - blockIsRecyclable_(block) { |
304 | | - const recyclable = super.blockIsRecyclable_(block); |
305 | | - // Exclude blocks with output connections, because they are able to report their current |
306 | | - // value in a popover and recycling them interacts poorly with the VM's maintenance of its |
307 | | - // state. |
308 | | - return recyclable && !block.outputConnection; |
| 120 | + layout_(contents) { |
| 121 | + // TODO(#211) Remove this once the continuous toolbox has been updated. |
| 122 | + // Bypass the continuous flyout's layout method until the plugin is |
| 123 | + // updated for the new flyout API. |
| 124 | + Blockly.VerticalFlyout.prototype.layout_.call(this, contents); |
309 | 125 | } |
310 | 126 | } |