-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Navigation of markers using new cursor class
Implements functionality to navigate through markers using "," & ".". Renames the new class with appropriate name: TimeGraphMarkersChartCursor. Removes duplicate code by using class inheritance. Fixes bug where canvas loses focus when selection is out of world range. Signed-off-by: Will Yang <william.yang@ericsson.com>
- Loading branch information
Showing
2 changed files
with
95 additions
and
18 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
66 changes: 66 additions & 0 deletions
66
timeline-chart/src/layer/time-graph-markers-chart-cursors.ts
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,66 @@ | ||
|
||
import { TimelineChart } from "../time-graph-model"; | ||
import { TimeGraphChartCursors } from "./time-graph-chart-cursors"; | ||
|
||
export class TimeGraphMarkersChartCursors extends TimeGraphChartCursors { | ||
|
||
protected afterAddToContainer() { | ||
super.afterAddToContainer(); | ||
this.removeOnCanvasEvent('keydown', this._keyboardShortcutKeyDownHandler); | ||
this._keyboardShortcutKeyDownHandler = (event: KeyboardEvent) => { | ||
switch(event.key) { | ||
case ",": | ||
this.selectClosestStateAndMakeSelectionRange('prev'); | ||
break; | ||
case ".": | ||
this.selectClosestStateAndMakeSelectionRange('next'); | ||
break; | ||
default: | ||
return; | ||
} | ||
}; | ||
this.onCanvasEvent('keydown', this._keyboardShortcutKeyDownHandler); | ||
} | ||
|
||
protected selectClosestStateAndMakeSelectionRange = (direction: 'next' | 'prev') => { | ||
|
||
const next = direction === 'next'; | ||
const selectedRow = this.rowController.selectedRow; | ||
|
||
if (this.unitController.selectionRange && selectedRow) { | ||
|
||
const { start, end } = this.unitController.selectionRange; | ||
let point1 = next ? end : start; | ||
let closestState: TimelineChart.TimeGraphState | undefined; | ||
let closestDiff = Number.POSITIVE_INFINITY; | ||
let isValid = false; | ||
|
||
selectedRow?.states.forEach((marker, key) => { | ||
|
||
const { start, end } = marker.range; | ||
|
||
if (start === this.unitController.selectionRange?.start && end === this.unitController.selectionRange?.end) { | ||
return; | ||
} | ||
|
||
let point2 = next ? start : end; | ||
let innerIsValid = next ? (point1 <= point2) : (point1 >= point2); | ||
let diff = Math.abs(Number(point1 - point2)); | ||
|
||
if (diff < closestDiff && innerIsValid) { | ||
closestDiff = diff; | ||
closestState = marker; | ||
isValid = innerIsValid; | ||
} | ||
|
||
}); | ||
|
||
if (isValid && closestState) { | ||
this.unitController.selectionRange = closestState.range; | ||
this.maybeCenterCursor(); | ||
this.update(); | ||
} | ||
|
||
} | ||
} | ||
} |