Files
clapper/clapper_src/interface.js
Rafostar 70ec6311c0 Add seeking on slider drop and make it default behaviour
Seeking during slider drag is very CPU and HDD intensive task. We are requesting the player to keep seeking in VERY short amounts of time. This can be performed more effectively by doing a single seek after slider drop. Since this is a different behaviour then usually in media players, I am making this optional (enabled by default).
2020-09-02 18:31:22 +02:00

142 lines
4.0 KiB
JavaScript

const { GObject, Gtk, GstPlayer } = imports.gi;
const { Controls } = imports.clapper_src.controls;
var Interface = GObject.registerClass(
class ClapperInterface extends Gtk.Grid
{
_init(opts)
{
super._init();
let defaults = {
seekOnDrop: true
};
Object.assign(this, defaults, opts);
this.lastVolumeValue = null;
this.lastPositionValue = 0;
this.controls = new Controls();
this.attach(this.controls, 0, 1, 1, 1);
}
addPlayer(player)
{
this._player = player;
this._player.widget.expand = true;
this._player.connect('state-changed', this._onPlayerStateChanged.bind(this));
this._player.connect('volume-changed', this._onPlayerVolumeChanged.bind(this));
this._player.connect('duration-changed', this._onPlayerDurationChanged.bind(this));
this._player.connect('position-updated', this._onPlayerPositionUpdated.bind(this));
this.controls.togglePlayButton.connect(
'clicked', this._onControlsTogglePlayClicked.bind(this)
);
this.controls.positionScale.connect(
'value-changed', this._onControlsPositionChanged.bind(this)
);
this.controls.volumeButton.connect(
'value-changed', this._onControlsVolumeChanged.bind(this)
);
this.controls.connect(
'position-seeking-changed', this._onPositionSeekingChanged.bind(this)
);
this.attach(this._player.widget, 0, 0, 1, 1);
}
_onPlayerStateChanged(player, state)
{
switch(state) {
case GstPlayer.PlayerState.STOPPED:
break;
case GstPlayer.PlayerState.BUFFERING:
break;
case GstPlayer.PlayerState.PAUSED:
this.controls.togglePlayButton.image = this.controls.playImage;
break;
case GstPlayer.PlayerState.PLAYING:
this.controls.togglePlayButton.image = this.controls.pauseImage;
break;
default:
break;
}
}
_onPlayerDurationChanged(player)
{
let duration = player.get_duration() / 1000000000;
let increment = (duration < 1)
? 0
: (duration < 100)
? 1
: duration / 100;
this.controls.positionAdjustment.set_upper(duration);
this.controls.positionAdjustment.set_step_increment(increment);
this.controls.positionAdjustment.set_page_increment(increment);
}
_onPlayerPositionUpdated(player, position)
{
if(this.controls.isPositionSeeking)
return;
let positionSeconds = position / 1000000000;
let positionFloor = Math.floor(positionSeconds);
if(positionFloor === this.lastPositionValue)
return;
this.lastPositionValue = positionFloor;
this.controls.positionScale.set_value(positionSeconds);
}
_onPlayerVolumeChanged()
{
let volume = Number(this._player.get_volume().toFixed(2));
if(volume === this.lastVolumeValue)
return;
this.controls.volumeButton.set_value(volume);
}
_onPositionSeekingChanged(self, isPositionSeeking)
{
if(isPositionSeeking || !this.seekOnDrop)
return;
this._onControlsPositionChanged(this.controls.positionScale);
}
_onControlsTogglePlayClicked()
{
this._player.toggle_play();
}
_onControlsPositionChanged(positionScale)
{
if(this.seekOnDrop && this.controls.isPositionSeeking)
return;
let position = Math.floor(positionScale.get_value());
if(position === this.lastPositionValue)
return;
this.lastPositionValue = position;
this._player.seek_seconds(position);
}
_onControlsVolumeChanged(widget, value)
{
if(value === this.lastVolumeValue)
return;
this.lastVolumeValue = value;
this._player.set_volume(value);
}
});