Skip to content

Commit

Permalink
[shared-ui] Add control over when to animate (#3944)
Browse files Browse the repository at this point in the history
  • Loading branch information
paullewis authored Dec 5, 2024
1 parent df9b8b9 commit 37c3f15
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changeset/soft-cherries-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@breadboard-ai/visual-editor": patch
"@breadboard-ai/shared-ui": patch
---

Add control over when to animate
1 change: 1 addition & 0 deletions packages/shared-ui/src/elements/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,7 @@ export class Editor extends LitElement {
.showPortTooltips=${this.showPortTooltips}
.showSubgraphsInline=${this.showSubgraphsInline}
.selectionChangeId=${this.selectionState?.selectionChangeId}
.moveToSelection=${this.selectionState?.moveToSelection}
></bb-graph-renderer>
</div>`;

Expand Down
10 changes: 8 additions & 2 deletions packages/shared-ui/src/elements/editor/graph-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import {
WorkspaceVisualState,
WorkspaceVisualChangeId,
GraphVisualState,
WorkspaceSelectionStateWithChangeId,
} from "../../types/types.js";
import { MAIN_BOARD_ID } from "../../constants/constants.js";

Expand Down Expand Up @@ -105,6 +106,10 @@ export class GraphRenderer extends LitElement {
@property()
selectionChangeId: WorkspaceSelectionChangeId | null = null;

@property()
moveToSelection: WorkspaceSelectionStateWithChangeId["moveToSelection"] =
false;

@property()
topGraphUrl: string | null = null;

Expand Down Expand Up @@ -692,10 +697,11 @@ export class GraphRenderer extends LitElement {
});
}

if (this.#selectionHasChanged) {
if (this.#selectionHasChanged && this.moveToSelection) {
this.#selectionHasChanged = false;

let shouldAnimate = true;
let shouldAnimate =
this.moveToSelection && this.moveToSelection === "animated";
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
shouldAnimate = false;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-ui/src/elements/editor/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class Graph extends PIXI.Container {
#highlightPadding = 8;
#subGraphOutlineMarker = new PIXI.Graphics();
#subGraphOutline = new PIXI.Graphics();
#subGraphOutlinePadding = 20;
#subGraphOutlinePadding = 24;
#latestPendingValidateRequest = new WeakMap<GraphEdge, symbol>();
#edgeValues: TopGraphEdgeValues | null = null;
#nodeInfo: TopGraphNodeInfo | null = null;
Expand Down
1 change: 1 addition & 0 deletions packages/shared-ui/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,4 +459,5 @@ export type WorkspaceSelectionState = {
export interface WorkspaceSelectionStateWithChangeId {
selectionChangeId: WorkspaceSelectionChangeId;
selectionState: WorkspaceSelectionState;
moveToSelection: "immediate" | "animated" | false;
}
1 change: 1 addition & 0 deletions packages/visual-editor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ export class Main extends LitElement {
this.#selectionState = {
selectionChangeId: evt.selectionChangeId,
selectionState: evt.selectionState,
moveToSelection: evt.moveToSelection,
};

this.requestUpdate();
Expand Down
3 changes: 2 additions & 1 deletion packages/visual-editor/src/runtime/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ export class RuntimeSelectionChangeEvent extends Event {

constructor(
public readonly selectionChangeId: WorkspaceSelectionChangeId,
public readonly selectionState: WorkspaceSelectionState
public readonly selectionState: WorkspaceSelectionState,
public readonly moveToSelection: "immediate" | "animated" | false
) {
super(RuntimeSelectionChangeEvent.eventName, { ...eventInit });
}
Expand Down
26 changes: 15 additions & 11 deletions packages/visual-editor/src/runtime/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,14 @@ export class Select extends EventTarget {
this.#selectionState.set(tab, createEmptyWorkspaceSelectionState());
}

#emit(tab: TabId, selectionChangeId: WorkspaceSelectionChangeId) {
#emit(
tab: TabId,
selectionChangeId: WorkspaceSelectionChangeId,
moveToSelection: "immediate" | "animated" | false = false
) {
const state = this.#createWorkspaceSelectionStateIfNeeded(tab);
this.dispatchEvent(
new RuntimeSelectionChangeEvent(selectionChangeId, state)
new RuntimeSelectionChangeEvent(selectionChangeId, state, moveToSelection)
);
}

Expand Down Expand Up @@ -124,7 +128,7 @@ export class Select extends EventTarget {
nodeId: NodeIdentifier
) {
this.#addToGraphsCollection(tab, graphId, "nodes", nodeId);
this.#emit(tab, selectionChangeId);
this.#emit(tab, selectionChangeId, "animated");
}

removeNode(
Expand All @@ -134,7 +138,7 @@ export class Select extends EventTarget {
nodeId: NodeIdentifier
) {
this.#removeFromGraphsCollection(tab, graphId, "nodes", nodeId);
this.#emit(tab, selectionChangeId);
this.#emit(tab, selectionChangeId, "animated");
}

addComment(
Expand All @@ -144,7 +148,7 @@ export class Select extends EventTarget {
commentId: string
) {
this.#addToGraphsCollection(tab, graphId, "comments", commentId);
this.#emit(tab, selectionChangeId);
this.#emit(tab, selectionChangeId, "animated");
}

removeComment(
Expand All @@ -154,7 +158,7 @@ export class Select extends EventTarget {
commentId: string
) {
this.#removeFromGraphsCollection(tab, graphId, "comments", commentId);
this.#emit(tab, selectionChangeId);
this.#emit(tab, selectionChangeId, "animated");
}

addEdge(
Expand All @@ -164,7 +168,7 @@ export class Select extends EventTarget {
edgeId: string
) {
this.#addToGraphsCollection(tab, graphId, "edges", edgeId);
this.#emit(tab, selectionChangeId);
this.#emit(tab, selectionChangeId, "animated");
}

removeEdge(
Expand All @@ -174,7 +178,7 @@ export class Select extends EventTarget {
edgeId: string
) {
this.#removeFromGraphsCollection(tab, graphId, "edges", edgeId);
this.#emit(tab, selectionChangeId);
this.#emit(tab, selectionChangeId, "animated");
}

addModule(
Expand All @@ -183,7 +187,7 @@ export class Select extends EventTarget {
moduleId: ModuleIdentifier
) {
this.#addToModulesCollection(tab, moduleId);
this.#emit(tab, selectionChangeId);
this.#emit(tab, selectionChangeId, "animated");
}

removeModule(
Expand All @@ -192,7 +196,7 @@ export class Select extends EventTarget {
moduleId: ModuleIdentifier
) {
this.#removeFromModulesCollection(tab, moduleId);
this.#emit(tab, selectionChangeId);
this.#emit(tab, selectionChangeId, "animated");
}

processSelections(
Expand Down Expand Up @@ -234,7 +238,7 @@ export class Select extends EventTarget {
this.#addToModulesCollection(tab, id);
}

this.#emit(tab, selectionChangeId);
this.#emit(tab, selectionChangeId, "animated");
}

selectNode(
Expand Down
1 change: 1 addition & 0 deletions packages/visual-editor/src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export type WorkspaceSelectionState = {
export interface WorkspaceSelectionStateWithChangeId {
selectionChangeId: WorkspaceSelectionChangeId;
selectionState: WorkspaceSelectionState;
moveToSelection: "immediate" | "animated" | false;
}

export type TabSelectionState = Map<TabId, WorkspaceSelectionState>;
Expand Down

0 comments on commit 37c3f15

Please sign in to comment.