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

fix(viz): track draw state to apply recenter on fresh draws #740

Merged
merged 11 commits into from
Nov 7, 2023
4 changes: 2 additions & 2 deletions src/elm/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -2160,7 +2160,7 @@ update msg model =

cmd =
if not sameBuild then
renderBuildGraph updatedModel False
renderBuildGraph updatedModel True

else
Cmd.none
Expand Down Expand Up @@ -4376,7 +4376,7 @@ loadBuildGraphPage model org repo buildNumber =
, getBuild um org repo buildNumber
, getAllBuildSteps um org repo buildNumber Nothing False
, getBuildGraph um org repo buildNumber False
, renderBuildGraph um True
, renderBuildGraph um <| not sameResource
]
)

Expand Down
4 changes: 2 additions & 2 deletions src/elm/Pages/Build/Graph/Interop.elm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Vela exposing (encodeBuildGraphRenderData)
{-| renderBuildGraph : takes partial build model and render options, and returns a cmd for dispatching a graphviz+d3 render command
-}
renderBuildGraph : BuildModel.PartialModel a -> Bool -> Cmd msg
renderBuildGraph model centerOnDraw =
renderBuildGraph model freshDraw =
-- rendering the full graph requires repo, build and graph
case ( model.repo.repo, model.repo.build.build, model.repo.build.graph.graph ) of
( Success r, Success b, Success g ) ->
Expand All @@ -28,7 +28,7 @@ renderBuildGraph model centerOnDraw =
, showServices = model.repo.build.graph.showServices
, showSteps = model.repo.build.graph.showSteps
, focusedNode = model.repo.build.graph.focusedNode
, centerOnDraw = centerOnDraw
, freshDraw = freshDraw
}

_ ->
Expand Down
4 changes: 2 additions & 2 deletions src/elm/Vela.elm
Original file line number Diff line number Diff line change
Expand Up @@ -1398,7 +1398,7 @@ encodeBuildGraphRenderData graphData =
, ( "focusedNode", Encode.int graphData.focusedNode )
, ( "showServices", Encode.bool graphData.showServices )
, ( "showSteps", Encode.bool graphData.showSteps )
, ( "centerOnDraw", Encode.bool graphData.centerOnDraw )
, ( "freshDraw", Encode.bool graphData.freshDraw )
]


Expand All @@ -1409,7 +1409,7 @@ type alias BuildGraphRenderInteropData =
, focusedNode : Int
, showServices : Bool
, showSteps : Bool
, centerOnDraw : Bool
, freshDraw : Bool
}


Expand Down
10 changes: 7 additions & 3 deletions src/static/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export function drawGraph(opts, content) {

// check that a valid graph was rendered
if (buildGraphElement === null || buildGraphElement.node() === null) {
return;
// control draw state
return false;
}

drawViewbox(opts, buildGraphElement);
Expand All @@ -32,6 +33,9 @@ export function drawGraph(opts, content) {
var edges = drawEdges(opts, buildGraphElement, graphSelectors.edge);

drawNodes(opts, buildGraphElement, graphSelectors.node, edges);

// control draw state
return true;
}

function drawBaseGraphWithZoom(opts, selector, content) {
Expand Down Expand Up @@ -115,11 +119,11 @@ function drawBaseGraphWithZoom(opts, selector, content) {
buildGraphElement.html(content);

// recenter on draw, when necessary
if (!opts.isRefreshDraw) {
if (!opts.sameBuild) {
resetZoomAndCenter(opts, zoom);
}

if (opts.centerOnDraw) {
if (!opts.drawn) {
resetZoomAndCenter(opts, zoom);
}

Expand Down
4 changes: 2 additions & 2 deletions src/static/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export type GraphData = {
showServices: boolean;
/** @property showSteps: boolean */
showSteps: boolean;
/** @property centerOnDraw: boolean */
centerOnDraw: boolean;
/** @property freshDraw: boolean */
freshDraw: boolean;
};

export type GraphInteraction = {
Expand Down
18 changes: 13 additions & 5 deletions src/static/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,29 +130,37 @@ function envOrNull(env: string, subst: string): string | null {

// track rendering options globally to help determine draw logic
var opts = {
drawn: false,
currentBuild: -1,
isRefreshDraw: false,
centerOnDraw: false,
sameBuild: false,
freshDraw: false,
contentFilter: '',
onGraphInteraction: {},
};

app.ports.renderBuildGraph.subscribe(function (graphData) {
const graphviz = Graphviz.load().then(res => {
var content = res.layout(graphData.dot, 'svg', 'dot');

// construct graph building options
opts.isRefreshDraw = opts.currentBuild === graphData.buildID;
opts.centerOnDraw = graphData.centerOnDraw;
// reset the draw state when the build changes
if (opts.currentBuild !== graphData.buildID || graphData.freshDraw) {
opts.drawn = false;
}

opts.sameBuild = opts.currentBuild === graphData.buildID;
opts.freshDraw = graphData.freshDraw;
opts.contentFilter = graphData.filter;

// track the currently drawn build
opts.currentBuild = graphData.buildID;

// graph interactivity
opts.onGraphInteraction = app.ports.onGraphInteraction;

// dispatch the draw command to avoid elm/js rendering race condition
setTimeout(() => {
Graph.drawGraph(opts, content);
opts.drawn = Graph.drawGraph(opts, content);
}, 0);
});
});
Loading