Files
blockly/core/interfaces/i_icon.ts
Ben Henning 4074cee31b feat!: Make everything ISelectable focusable (#9004)
* feat!: Make bubbles, comments, and icons focusable

* feat!: Make ISelectable and ICopyable focusable.

* feat: Consolidate selection calls.

Now everything is based on focus with selection only being used as a
proxy.

* feat: Invert responsibility for setSelected().

Now setSelected() is only for quasi-external use.

* feat: Push up shadow check to getters.

Needed new common-level helper.

* chore: Lint fixes.

* feat!: Allow IFocusableNode to disable focus.

* chore: post-merge lint fixes

* fix: Fix tests + text bubble focusing.

This fixed then regressed a circular dependency causing the node and
advanced compilation steps to fail. This investigation is ongoing.

* fix: Clean up & fix imports.

This ensures the node and advanced compilation test steps now pass.

* fix: Lint fixes + revert commented out logic.

* chore: Remove unnecessary cast.

Addresses reviewer comment.

* fix: Some issues and a bunch of clean-ups.

This addresses a bunch of review comments, and fixes selecting workspace
comments.

* chore: Lint fix.

* fix: Remove unnecessary shadow consideration.

* chore: Revert import.

* chore: Some doc updates & added a warn statement.
2025-05-09 08:16:14 -07:00

117 lines
3.5 KiB
TypeScript

/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type {IconType} from '../icons/icon_types.js';
import type {Coordinate} from '../utils/coordinate.js';
import type {Size} from '../utils/size.js';
import {IFocusableNode, isFocusableNode} from './i_focusable_node.js';
export interface IIcon extends IFocusableNode {
/**
* @returns the IconType representing the type of the icon. This value should
* also be used to register the icon via `Blockly.icons.registry.register`.
*/
getType(): IconType<IIcon>;
/**
* Creates the SVG elements for the icon that will live on the block.
*
* @param pointerdownListener An event listener that must be attached to the
* root SVG element by the implementation of `initView`. Used by Blockly's
* gesture system to properly handle clicks and drags.
*/
initView(pointerdownListener: (e: PointerEvent) => void): void;
/**
* Disposes of any elements of the icon.
*
* @remarks
*
* In particular, if this icon is currently showing a bubble, this should be
* used to hide it.
*/
dispose(): void;
/**
* @returns the "weight" of the icon, which determines the static order which
* icons should be rendered in. More positive numbers are rendered farther
* toward the end of the block.
*/
getWeight(): number;
/** @returns The dimensions of the icon for use in rendering. */
getSize(): Size;
/** Updates the icon's color when the block's color changes.. */
applyColour(): void;
/** Hides the icon when it is part of an insertion marker. */
hideForInsertionMarker(): void;
/** Updates the icon's editability when the block's editability changes. */
updateEditable(): void;
/**
* Updates the icon's collapsed-ness/view when the block's collapsed-ness
* changes.
*/
updateCollapsed(): void;
/**
* @returns Whether this icon is shown when the block is collapsed. Used
* to allow renderers to account for padding.
*/
isShownWhenCollapsed(): boolean;
/**
* Notifies the icon where it is relative to its block's top-start, in
* workspace units.
*/
setOffsetInBlock(offset: Coordinate): void;
/**
* Notifies the icon that it has changed locations.
*
* @param blockOrigin The location of this icon's block's top-start corner
* in workspace coordinates.
*/
onLocationChange(blockOrigin: Coordinate): void;
/**
* Notifies the icon that it has been clicked.
*/
onClick(): void;
/**
* Check whether the icon should be clickable while the block is in a flyout.
* If this function is not defined, the icon will be clickable in all flyouts.
*
* @param autoClosingFlyout true if the containing flyout is an auto-closing one.
* @returns Whether the icon should be clickable while the block is in a flyout.
*/
isClickableInFlyout?(autoClosingFlyout: boolean): boolean;
}
/** Type guard that checks whether the given object is an IIcon. */
export function isIcon(obj: any): obj is IIcon {
return (
obj.getType !== undefined &&
obj.initView !== undefined &&
obj.dispose !== undefined &&
obj.getWeight !== undefined &&
obj.getSize !== undefined &&
obj.applyColour !== undefined &&
obj.hideForInsertionMarker !== undefined &&
obj.updateEditable !== undefined &&
obj.updateCollapsed !== undefined &&
obj.isShownWhenCollapsed !== undefined &&
obj.setOffsetInBlock !== undefined &&
obj.onLocationChange !== undefined &&
obj.onClick !== undefined &&
isFocusableNode(obj)
);
}