Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for IAutoHideable #4855

Merged
merged 7 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions blockly_uncompressed.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 10 additions & 17 deletions core/blockly.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,27 +294,20 @@ Blockly.onContextMenu_ = function(e) {

/**
* Close tooltips, context menus, dropdown selections, etc.
* @param {boolean=} opt_allowToolbox If true, don't close the toolbox.
* @param {boolean=} opt_onlyClosePopups Whether only popups should be closed.
moniika marked this conversation as resolved.
Show resolved Hide resolved
*/
Blockly.hideChaff = function(opt_allowToolbox) {
Blockly.hideChaff = function(opt_onlyClosePopups) {
Blockly.Tooltip.hide();
Blockly.WidgetDiv.hide();
Blockly.DropDownDiv.hideWithoutAnimation();
if (!opt_allowToolbox) {
var workspace = Blockly.getMainWorkspace();
// For now the trashcan flyout always autocloses because it overlays the
// trashcan UI (no trashcan to click to close it).
if (workspace.trashcan &&
workspace.trashcan.flyout) {
workspace.trashcan.closeFlyout();
}
var toolbox = workspace.getToolbox();
if (toolbox &&
toolbox.getFlyout() &&
toolbox.getFlyout().autoClose) {
toolbox.clearSelection();
}
}

var onlyClosePopups = !!opt_onlyClosePopups;
var workspace = Blockly.getMainWorkspace();
var autoHideables = workspace.getComponentManager().getComponents(
Blockly.ComponentManager.Capability.AUTOHIDEABLE, true);
autoHideables.forEach(function(autoHideable) {
autoHideable.autoHide(onlyClosePopups);
});
};

/**
Expand Down
7 changes: 7 additions & 0 deletions core/component_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

goog.provide('Blockly.ComponentManager');

goog.requireType('Blockly.IAutoHideable');
goog.requireType('Blockly.IComponent');
goog.requireType('Blockly.IPositionable');


/**
* Manager for all items registered with the workspace.
Expand Down Expand Up @@ -136,3 +140,6 @@ Blockly.ComponentManager.Capability.prototype.toString = function() {
/** @type {!Blockly.ComponentManager.Capability<!Blockly.IPositionable>} */
Blockly.ComponentManager.Capability.POSITIONABLE =
new Blockly.ComponentManager.Capability('positionable');
/** @type {!Blockly.ComponentManager.Capability<!Blockly.IAutoHideable>} */
Blockly.ComponentManager.Capability.AUTOHIDEABLE =
new Blockly.ComponentManager.Capability('autohideable');
33 changes: 33 additions & 0 deletions core/interfaces/i_autohideable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview The interface for a component that is automatically hidden
* when Blockly.hideChaff is called.
* @author kozbial@google.com (Monica Kozbial)
*/

'use strict';

goog.provide('Blockly.IAutoHideable');


goog.require('Blockly.IComponent');


/**
* Interface for a component that can be automatically hidden.
* @extends {Blockly.IComponent}
* @interface
*/
Blockly.IAutoHideable = function() {};

/**
* Hides the component. Called in Blockly.hideChaff.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly I think this could use some more documentation, about how to use/register it, and what the implementer should do in this method. But I think if you wanted to do that separately, that's fine too since perhaps the PluginManager api is changing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I expect to add documentation, probably during bugbash, when adding documentation for the PluginManager.

* @param {boolean} onlyClosePopups Whether only popups should be closed.
* Flyouts should not be closed if this is true.
*/
Blockly.IAutoHideable.prototype.autoHide;
22 changes: 22 additions & 0 deletions core/toolbox/toolbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ goog.require('Blockly.Css');
goog.require('Blockly.Events');
/** @suppress {extraRequire} */
goog.require('Blockly.Events.ToolboxItemSelect');
goog.require('Blockly.IAutoHideable');
goog.require('Blockly.IDeleteArea');
goog.require('Blockly.IKeyboardAccessible');
goog.require('Blockly.IStyleable');
Expand Down Expand Up @@ -47,6 +48,7 @@ goog.requireType('Blockly.WorkspaceSvg');
* @param {!Blockly.WorkspaceSvg} workspace The workspace in which to create new
* blocks.
* @constructor
* @implements {Blockly.IAutoHideable}
* @implements {Blockly.IKeyboardAccessible}
* @implements {Blockly.IDeleteArea}
* @implements {Blockly.IStyleable}
Expand Down Expand Up @@ -187,6 +189,15 @@ Blockly.Toolbox.prototype.init = function() {
themeManager.subscribe(this.HtmlDiv, 'toolboxBackgroundColour',
'background-color');
themeManager.subscribe(this.HtmlDiv, 'toolboxForegroundColour', 'color');

this.workspace_.getComponentManager().addComponent({
id: 'toolbox',
component: this,
weight: 1,
capabilities: [
Blockly.ComponentManager.Capability.AUTOHIDEABLE
]
});
};

/**
Expand Down Expand Up @@ -696,6 +707,17 @@ Blockly.Toolbox.prototype.setVisible = function(isVisible) {
this.HtmlDiv.style.display = isVisible ? 'block' : 'none';
};

/**
* Hides the component. Called in Blockly.hideChaff.
* @param {boolean} onlyClosePopups Whether only popups should be closed.
* Flyouts should not be closed if this is true.
*/
Blockly.Toolbox.prototype.autoHide = function(onlyClosePopups) {
if (!onlyClosePopups && this.flyout_ && this.flyout_.autoClose) {
this.clearSelection();
}
};

/**
* Sets the given item as selected.
* No-op if the item is not selectable.
Expand Down
25 changes: 24 additions & 1 deletion core/trashcan.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ goog.require('Blockly.constants');
goog.require('Blockly.Events');
/** @suppress {extraRequire} */
goog.require('Blockly.Events.TrashcanOpen');
goog.require('Blockly.IAutoHideable');
goog.require('Blockly.IDeleteArea');
goog.require('Blockly.IPositionable');
goog.require('Blockly.Options');
Expand All @@ -38,6 +39,7 @@ goog.requireType('Blockly.WorkspaceSvg');
* Class for a trash can.
* @param {!Blockly.WorkspaceSvg} workspace The workspace to sit in.
* @constructor
* @implements {Blockly.IAutoHideable}
* @implements {Blockly.IDeleteArea}
* @implements {Blockly.IPositionable}
*/
Expand Down Expand Up @@ -357,7 +359,15 @@ Blockly.Trashcan.prototype.init = function() {
this.workspace_.getParentSvg());
this.flyout.init(this.workspace_);
}

this.workspace_.getComponentManager().addComponent({
id: 'trashcan',
component: this,
weight: 1,
capabilities: [
Blockly.ComponentManager.Capability.POSITIONABLE,
Blockly.ComponentManager.Capability.AUTOHIDEABLE
]
});
this.initialized_ = true;
this.setLidOpen(false);
};
Expand Down Expand Up @@ -422,6 +432,19 @@ Blockly.Trashcan.prototype.closeFlyout = function() {
this.fireUiEvent_(false);
};

/**
* Hides the component. Called in Blockly.hideChaff.
* @param {boolean} onlyClosePopups Whether only popups should be closed.
* Flyouts should not be closed if this is true.
*/
Blockly.Trashcan.prototype.autoHide = function(onlyClosePopups) {
// For now the trashcan flyout always autocloses because it overlays the
// trashcan UI (no trashcan to click to close it).
if (!onlyClosePopups && this.flyout) {
this.closeFlyout();
}
};

/**
* Empties the trashcan's contents. If the contents-flyout is currently open
* it will be closed.
Expand Down
14 changes: 1 addition & 13 deletions core/workspace_svg.js
Original file line number Diff line number Diff line change
Expand Up @@ -1003,12 +1003,6 @@ Blockly.WorkspaceSvg.prototype.addTrashcan = function() {
this.trashcan = new Blockly.Trashcan(this);
var svgTrashcan = this.trashcan.createDom();
this.svgGroup_.insertBefore(svgTrashcan, this.svgBlockCanvas_);
this.componentManager_.addComponent({
id: 'trashcan',
component: this.trashcan,
weight: 1,
capabilities: [Blockly.ComponentManager.Capability.POSITIONABLE]
});
};

/**
Expand All @@ -1023,12 +1017,6 @@ Blockly.WorkspaceSvg.prototype.addZoomControls = function() {
this.zoomControls_ = new Blockly.ZoomControls(this);
var svgZoomControls = this.zoomControls_.createDom();
this.svgGroup_.appendChild(svgZoomControls);
this.componentManager_.addComponent({
id: 'zoomControls',
component: this.zoomControls_,
weight: 2,
capabilities: [Blockly.ComponentManager.Capability.POSITIONABLE]
});
};

/**
Expand Down Expand Up @@ -2261,7 +2249,7 @@ Blockly.WorkspaceSvg.prototype.getScale = function() {
* @package
*/
Blockly.WorkspaceSvg.prototype.scroll = function(x, y) {
Blockly.hideChaff(/* opt_allowToolbox */ true);
Blockly.hideChaff(/* opt_onlyClosePopups */ true);

// Keep scrolling within the bounds of the content.
var metrics = this.getMetrics();
Expand Down
6 changes: 6 additions & 0 deletions core/zoom_controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,12 @@ Blockly.ZoomControls.prototype.createDom = function() {
* Initializes the zoom controls.
*/
Blockly.ZoomControls.prototype.init = function() {
this.workspace_.getComponentManager().addComponent({
id: 'zoomControls',
component: this,
weight: 2,
capabilities: [Blockly.ComponentManager.Capability.POSITIONABLE]
});
this.initialized_ = true;
};

Expand Down