From 2ea4281923f4dad3c85f335d49d07a3f8fe1c9f6 Mon Sep 17 00:00:00 2001 From: Patrick Tasse Date: Wed, 11 Sep 2024 14:15:14 -0400 Subject: [PATCH] Update time graph navigator on absolute range change Add support for absolute range change handlers in unit controller. Make time graph navigator register one such handler so that it can update itself when the absolute range changes. Signed-off-by: Patrick Tasse --- .../src/layer/time-graph-navigator.ts | 2 ++ .../src/time-graph-unit-controller.ts | 32 ++++++++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/timeline-chart/src/layer/time-graph-navigator.ts b/timeline-chart/src/layer/time-graph-navigator.ts index a9a52b2..bd9305e 100644 --- a/timeline-chart/src/layer/time-graph-navigator.ts +++ b/timeline-chart/src/layer/time-graph-navigator.ts @@ -16,6 +16,7 @@ export class TimeGraphNavigator extends TimeGraphLayer { afterAddToContainer() { this._updateHandler = (): void => this.update(); + this.unitController.onAbsoluteRangeChanged(this._updateHandler); this.unitController.onViewRangeChanged(this._updateHandler); this.navigatorBackground = new TimeGraphNavigatorBackground(this.unitController, this.stateController); this.addChild(this.navigatorBackground); @@ -59,6 +60,7 @@ export class TimeGraphNavigator extends TimeGraphLayer { destroy() : void { if (this.unitController) { + this.unitController.removeAbsoluteRangeChangedHandler(this._updateHandler); this.unitController.removeViewRangeChangedHandler(this._updateHandler); this.unitController.removeSelectionRangeChangedHandler(this._updateHandler); } diff --git a/timeline-chart/src/time-graph-unit-controller.ts b/timeline-chart/src/time-graph-unit-controller.ts index c37cd51..f41e27b 100644 --- a/timeline-chart/src/time-graph-unit-controller.ts +++ b/timeline-chart/src/time-graph-unit-controller.ts @@ -3,6 +3,8 @@ import { TimeGraphRenderController } from "./time-graph-render-controller"; export class TimeGraphUnitController { + protected absoluteRangeChangedHandlers: ((absoluteRange: bigint) => void)[]; + protected _absoluteRange: bigint; protected viewRangeChangedHandlers: ((oldRange: TimelineChart.TimeGraphRange, newRange: TimelineChart.TimeGraphRange) => void)[]; protected _viewRange: TimelineChart.TimeGraphRange; @@ -27,15 +29,21 @@ export class TimeGraphUnitController { numberTranslator?: (theNumber: bigint) => string | undefined; scaleSteps?: number[] - constructor(public absoluteRange: bigint, viewRange?: TimelineChart.TimeGraphRange) { + constructor(absoluteRange: bigint, viewRange?: TimelineChart.TimeGraphRange) { + this._absoluteRange = absoluteRange; this._viewRange = viewRange || { start: BigInt(0), end: absoluteRange }; + this.absoluteRangeChangedHandlers = []; this.viewRangeChangedHandlers = []; this.selectionRangeChangedHandlers = []; this._renderer = new TimeGraphRenderController(); } + protected handleAbsoluteRangeChange() { + this.absoluteRangeChangedHandlers.forEach(handler => handler(this._absoluteRange)); + } + protected handleViewRangeChange(oldRange: TimelineChart.TimeGraphRange) { this.viewRangeChangedHandlers.forEach(handler => handler(oldRange, this._viewRange)); } @@ -44,10 +52,21 @@ export class TimeGraphUnitController { this.selectionRangeChangedHandlers.forEach(handler => handler(this._selectionRange)); } + onAbsoluteRangeChanged(handler: (absoluteRange: bigint) => void) { + this.absoluteRangeChangedHandlers.push(handler); + } + onViewRangeChanged(handler: (oldRange: TimelineChart.TimeGraphRange, viewRange: TimelineChart.TimeGraphRange) => void) { this.viewRangeChangedHandlers.push(handler); } + removeAbsoluteRangeChangedHandler(handler: (absoluteRange: bigint) => void) { + const index = this.absoluteRangeChangedHandlers.indexOf(handler); + if (index > -1) { + this.absoluteRangeChangedHandlers.splice(index, 1); + } + } + removeViewRangeChangedHandler(handler: (oldRange: TimelineChart.TimeGraphRange, viewRange: TimelineChart.TimeGraphRange) => void) { const index = this.viewRangeChangedHandlers.indexOf(handler); if (index > -1) { @@ -66,6 +85,17 @@ export class TimeGraphUnitController { } } + get absoluteRange(): bigint { + return this._absoluteRange; + } + + set absoluteRange(absoluteRange: bigint) { + if (this._absoluteRange !== absoluteRange) { + this._absoluteRange = absoluteRange; + this.handleAbsoluteRangeChange(); + } + } + get viewRange(): TimelineChart.TimeGraphRange { return this._viewRange; }