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

new seekbar option to prevent label overlap #584

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/scss/skin-modern/components/_seekbarlabel.scss
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,13 @@
}
}
}

// move arrow if the seekbar label was offset too
&[style*='transform'] {
.#{$prefix}-seekbar-label-inner {
&::after {
transform: translateX(calc(1px * var(--bmpui-seekbar-label-overflow-offset)));
}
}
}
}
42 changes: 38 additions & 4 deletions src/ts/components/seekbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { i18n } from '../localization/i18n';
import { BrowserUtils } from '../browserutils';
import { TimelineMarkersHandler } from './timelinemarkershandler';
import { getMinBufferLevel } from './seekbarbufferlevel';
import {throttle} from '../throttle';

/**
* Configuration interface for the {@link SeekBar} component.
Expand Down Expand Up @@ -52,6 +53,11 @@ export interface SeekBarConfig extends ComponentConfig {
* Used to enable/disable seek preview
*/
enableSeekPreview?: boolean;

/**
* Prevent overflow of seekpreview
*/
preventSeekPreviewOverflow?: boolean;
}

/**
Expand Down Expand Up @@ -134,6 +140,8 @@ export class SeekBar extends Component<SeekBarConfig> {
onSeeked: new EventDispatcher<SeekBar, number>(),
};

private uimanager: UIInstanceManager;

constructor(config: SeekBarConfig = {}) {
super(config);

Expand All @@ -151,6 +159,7 @@ export class SeekBar extends Component<SeekBarConfig> {
tabIndex: 0,
snappingRange: 1,
enableSeekPreview: true,
preventSeekPreviewOverflow: false,
}, this.config);

this.label = this.config.label;
Expand Down Expand Up @@ -204,7 +213,7 @@ export class SeekBar extends Component<SeekBarConfig> {

configure(player: PlayerAPI, uimanager: UIInstanceManager, configureSeek: boolean = true): void {
super.configure(player, uimanager);

this.uimanager = uimanager;
this.player = player;

// Apply scaling transform to the backdrop bar to have all bars rendered similarly
Expand Down Expand Up @@ -991,6 +1000,33 @@ export class SeekBar extends Component<SeekBarConfig> {
this.seekBarEvents.onSeek.dispatch(this);
}

protected updateLabelPosition = throttle((seekPositionPercentage: number) => {
const labelDomElement = this.label.getDomElement();
labelDomElement.css({
'left': seekPositionPercentage + '%',
'transform': null,
});
if (this.config.preventSeekPreviewOverflow) {
const overflowMargin = 5;
const uiBounding = this.uimanager.getUI().getDomElement().get(0).getBoundingClientRect();
const labelBounding = labelDomElement.get(0).getBoundingClientRect();
const labelRight = (labelBounding.right - labelBounding.width / 2 );
const labelLeft = (labelBounding.left - labelBounding.width / 2);
let preventOverflowOffset = 0;
if (labelRight + overflowMargin > uiBounding.right) {
preventOverflowOffset = labelRight - uiBounding.right + overflowMargin;
} else if (labelLeft - overflowMargin < uiBounding.left) {
preventOverflowOffset = labelLeft - uiBounding.left - overflowMargin;
}
if (preventOverflowOffset) {
labelDomElement.css({
transform: `translateX(${-preventOverflowOffset}px)`,
});
labelDomElement.get(0).style.setProperty(`--${this.prefixCss('seekbar-label-overflow-offset')}`, String(preventOverflowOffset));
}
}
}, 50)

protected onSeekPreviewEvent(percentage: number, scrubbing: boolean) {
let snappedMarker = this.timelineMarkersHandler && this.timelineMarkersHandler.getMarkerAtPosition(percentage);

Expand All @@ -1014,9 +1050,7 @@ export class SeekBar extends Component<SeekBarConfig> {
}

if (this.label) {
this.label.getDomElement().css({
'left': seekPositionPercentage + '%',
});
this.updateLabelPosition(seekPositionPercentage);
}

this.seekBarEvents.onSeekPreview.dispatch(this, {
Expand Down
17 changes: 17 additions & 0 deletions src/ts/throttle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Returns a new function which will invocate the original no faster
* then every rateMs milliseconds.
*/
export function throttle(fn: Function, rateMs: number) {
let timerFlag : null | ReturnType<typeof setTimeout> = null;

return (...args: unknown[]) => {
if (timerFlag === null) {
fn(...args);
timerFlag = setTimeout(() => {
timerFlag = null;
}, rateMs);
}
};
}