Skip to content

Commit

Permalink
Fix mouse click and segment insert at edge of waveform view
Browse files Browse the repository at this point in the history
See #545
  • Loading branch information
chrisn committed Sep 7, 2024
1 parent 2673092 commit 67f56df
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/mouse-drag-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ MouseDragHandler.prototype._mouseUp = function(event) {
MouseDragHandler.prototype._getMousePosX = function(clientX) {
const containerPos = this._stage.getContainer().getBoundingClientRect();

return clientX - containerPos.left;
return Math.floor(clientX - containerPos.left);
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/scrollbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Scrollbar.prototype._onScrollbarClick = function(event) {
if (event.target === this._stage) {
if (this._zoomview) {
// Centre the scrollbox where the user clicked.
let x = Math.floor(event.evt.layerX - this._scrollboxWidth / 2);
let x = Math.floor(event.evt.offsetX - this._scrollboxWidth / 2);

if (x < 0) {
x = 0;
Expand Down
20 changes: 17 additions & 3 deletions src/segment.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,12 +220,26 @@ Segment.prototype.update = function(options) {
* @param {Number} startTime The start of the time region, in seconds.
* @param {Number} endTime The end of the time region, in seconds.
* @returns {Boolean}
*
* @see http://wiki.c2.com/?TestIfDateRangesOverlap
*/

Segment.prototype.isVisible = function(startTime, endTime) {
return this.startTime < endTime && startTime < this.endTime;
// A special case, where the segment has zero duration
// and is at the start of the region.
if (this.startTime === this.endTime && this.startTime === startTime) {
return true;
}

// Segment ends before start of region.
if (this.endTime <= startTime) {
return false;
}

// Segment starts after end of region
if (this.startTime >= endTime) {
return false;
}

return true;
};

Segment.prototype._setStartTime = function(time) {
Expand Down
9 changes: 7 additions & 2 deletions src/waveform-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ WaveformView.prototype._onContextMenu = function(event) {
};

WaveformView.prototype._clickHandler = function(event, eventName) {
let offsetX = event.evt.offsetX;

if (offsetX < 0) {
offsetX = 0;
}

let emitViewEvent = true;

if (event.target !== this._stage) {
Expand Down Expand Up @@ -387,8 +393,7 @@ WaveformView.prototype._clickHandler = function(event, eventName) {
}

if (emitViewEvent) {
const mousePosX = event.evt.layerX;
const time = this.pixelOffsetToTime(mousePosX);
const time = this.pixelOffsetToTime(offsetX);
const viewName = this.getName();

this._peaks.emit(viewName + '.' + eventName, {
Expand Down
4 changes: 3 additions & 1 deletion test/helpers/input-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ InputController.prototype._dispatchMouseEvent = function(type, pos) {
const event = new MouseEvent(type, {
bubbles: true,
clientX: this._left + pos.x,
clientY: this._top + pos.y
clientY: this._top + pos.y,
offsetX: pos.x,
offsetY: pos.y
});

this._target.dispatchEvent(event);
Expand Down
15 changes: 15 additions & 0 deletions test/segment-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -406,5 +406,20 @@ describe('Segment', function() {

expect(segment.isVisible(10.0, 20.0)).to.equal(true);
});

it('should return true if segment starts at time zero and has zero end time', function() {
const peaks = { emit: function() {} };
const pid = 0;

const segment = new Segment(peaks, pid, {
id: 'segment.1',
labelText: '',
editable: true,
startTime: 0.0,
endTime: 0.0
});

expect(segment.isVisible(0.0, 10.0)).to.equal(true);
});
});
});

0 comments on commit 67f56df

Please sign in to comment.