-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add inflaters for flyout labels and buttons. (#8593)
* feat: Add inflaters for flyout labels and buttons. * chore: Temporarily re-add createDom(). * chore: fix JSDoc. * chore: Add license. * chore: Add TSDoc.
- Loading branch information
Showing
3 changed files
with
188 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import type {IFlyoutInflater} from './interfaces/i_flyout_inflater.js'; | ||
import type {IBoundedElement} from './interfaces/i_bounded_element.js'; | ||
import type {WorkspaceSvg} from './workspace_svg.js'; | ||
import {FlyoutButton} from './flyout_button.js'; | ||
import {ButtonOrLabelInfo} from './utils/toolbox.js'; | ||
import * as registry from './registry.js'; | ||
|
||
/** | ||
* Class responsible for creating buttons for flyouts. | ||
*/ | ||
export class ButtonFlyoutInflater implements IFlyoutInflater { | ||
/** | ||
* Inflates a flyout button from the given state and adds it to the flyout. | ||
* | ||
* @param state A JSON representation of a flyout button. | ||
* @param flyoutWorkspace The workspace to create the button on. | ||
* @returns A newly created FlyoutButton. | ||
*/ | ||
load(state: Object, flyoutWorkspace: WorkspaceSvg): IBoundedElement { | ||
const button = new FlyoutButton( | ||
flyoutWorkspace, | ||
flyoutWorkspace.targetWorkspace!, | ||
state as ButtonOrLabelInfo, | ||
false, | ||
); | ||
button.show(); | ||
return button; | ||
} | ||
|
||
/** | ||
* Returns the amount of space that should follow this button. | ||
* | ||
* @param state A JSON representation of a flyout button. | ||
* @param defaultGap The default spacing for flyout items. | ||
* @returns The amount of space that should follow this button. | ||
*/ | ||
gapForElement(state: Object, defaultGap: number): number { | ||
return defaultGap; | ||
} | ||
|
||
/** | ||
* Disposes of the given button. | ||
* | ||
* @param element The flyout button to dispose of. | ||
*/ | ||
disposeElement(element: IBoundedElement): void { | ||
if (element instanceof FlyoutButton) { | ||
element.dispose(); | ||
} | ||
} | ||
} | ||
|
||
registry.register( | ||
registry.Type.FLYOUT_INFLATER, | ||
'button', | ||
ButtonFlyoutInflater, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* @license | ||
* Copyright 2024 Google LLC | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import type {IFlyoutInflater} from './interfaces/i_flyout_inflater.js'; | ||
import type {IBoundedElement} from './interfaces/i_bounded_element.js'; | ||
import type {WorkspaceSvg} from './workspace_svg.js'; | ||
import {FlyoutButton} from './flyout_button.js'; | ||
import {ButtonOrLabelInfo} from './utils/toolbox.js'; | ||
import * as registry from './registry.js'; | ||
|
||
/** | ||
* Class responsible for creating labels for flyouts. | ||
*/ | ||
export class LabelFlyoutInflater implements IFlyoutInflater { | ||
/** | ||
* Inflates a flyout label from the given state and adds it to the flyout. | ||
* | ||
* @param state A JSON representation of a flyout label. | ||
* @param flyoutWorkspace The workspace to create the label on. | ||
* @returns A FlyoutButton configured as a label. | ||
*/ | ||
load(state: Object, flyoutWorkspace: WorkspaceSvg): IBoundedElement { | ||
const label = new FlyoutButton( | ||
flyoutWorkspace, | ||
flyoutWorkspace.targetWorkspace!, | ||
state as ButtonOrLabelInfo, | ||
true, | ||
); | ||
label.show(); | ||
return label; | ||
} | ||
|
||
/** | ||
* Returns the amount of space that should follow this label. | ||
* | ||
* @param state A JSON representation of a flyout label. | ||
* @param defaultGap The default spacing for flyout items. | ||
* @returns The amount of space that should follow this label. | ||
*/ | ||
gapForElement(state: Object, defaultGap: number): number { | ||
return defaultGap; | ||
} | ||
|
||
/** | ||
* Disposes of the given label. | ||
* | ||
* @param element The flyout label to dispose of. | ||
*/ | ||
disposeElement(element: IBoundedElement): void { | ||
if (element instanceof FlyoutButton) { | ||
element.dispose(); | ||
} | ||
} | ||
} | ||
|
||
registry.register(registry.Type.FLYOUT_INFLATER, 'label', LabelFlyoutInflater); |