mirror of
https://github.com/Rafostar/clapper.git
synced 2025-08-30 16:02:00 +02:00
Major code cleanup
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
const { Gio, GObject, Gst, GstPlayer, Gtk } = imports.gi;
|
||||
const { Gdk, Gio, GLib, GObject, Gst, GstPlayer, Gtk } = imports.gi;
|
||||
const ByteArray = imports.byteArray;
|
||||
const Debug = imports.clapper_src.debug;
|
||||
|
||||
@@ -44,15 +44,13 @@ class ClapperPlayer extends GstPlayer.Player
|
||||
video_renderer: renderer
|
||||
});
|
||||
|
||||
/* Assign elements to player for later access */
|
||||
this.gtkglsink = gtkglsink;
|
||||
this.glsinkbin = glsinkbin;
|
||||
this.dispatcher = dispatcher;
|
||||
this.renderer = renderer;
|
||||
|
||||
this.gstRegistry = Gst.Registry.get();
|
||||
this.is_local_file = false;
|
||||
this.seek_done = false;
|
||||
this.seek_done = true;
|
||||
this.dragAllowed = false;
|
||||
|
||||
this.posX = 0;
|
||||
this.posY = 0;
|
||||
this.keyPressCount = 0;
|
||||
|
||||
this._playerSignals = [];
|
||||
this._widgetSignals = [];
|
||||
@@ -73,37 +71,54 @@ class ClapperPlayer extends GstPlayer.Player
|
||||
this.set_plugin_rank('vah264dec', 300);
|
||||
|
||||
this.widget = gtkglsink.widget;
|
||||
this.widget.vexpand = true;
|
||||
this.widget.hexpand = true;
|
||||
|
||||
this.state = GstPlayer.PlayerState.STOPPED;
|
||||
this.visualization_enabled = false;
|
||||
this.fast_seeking = opts.fast_seeking || false;
|
||||
|
||||
this._playlist = [];
|
||||
this._trackId = 0;
|
||||
this.playlist_ext = opts.playlist_ext || 'claps';
|
||||
|
||||
this.keyController = new Gtk.EventControllerKey();
|
||||
this.motionController = new Gtk.EventControllerMotion();
|
||||
this.scrollController = new Gtk.EventControllerScroll();
|
||||
this.clickGesture = new Gtk.GestureClick();
|
||||
this.dragGesture = new Gtk.GestureDrag();
|
||||
this._hideCursorTimeout = null;
|
||||
this._hideControlsTimeout = null;
|
||||
this._updateTimeTimeout = null;
|
||||
|
||||
this.scrollController.set_flags(
|
||||
Gtk.EventControllerScrollFlags.BOTH_AXES
|
||||
);
|
||||
this.clickGesture.set_button(0);
|
||||
this.cursorInPlayer = false;
|
||||
|
||||
this.widget.add_controller(this.keyController);
|
||||
this.widget.add_controller(this.motionController);
|
||||
this.widget.add_controller(this.scrollController);
|
||||
this.widget.add_controller(this.clickGesture);
|
||||
this.widget.add_controller(this.dragGesture);
|
||||
let clickGesture = new Gtk.GestureClick();
|
||||
clickGesture.set_button(0);
|
||||
clickGesture.connect('pressed', this._onWidgetPressed.bind(this));
|
||||
this.widget.add_controller(clickGesture);
|
||||
|
||||
this.connect('state-changed', this._onStateChanged.bind(this));
|
||||
this.connect('uri-loaded', this._onUriLoaded.bind(this));
|
||||
this.connect('end-of-stream', this._onStreamEnded.bind(this));
|
||||
this.connect('warning', this._onPlayerWarning.bind(this));
|
||||
this.connect('error', this._onPlayerError.bind(this));
|
||||
this.connectWidget('destroy', this._onWidgetDestroy.bind(this));
|
||||
let dragGesture = new Gtk.GestureDrag();
|
||||
dragGesture.connect('drag-update', this._onWidgetDragUpdate.bind(this));
|
||||
this.widget.add_controller(dragGesture);
|
||||
|
||||
let keyController = new Gtk.EventControllerKey();
|
||||
keyController.connect('key-pressed', this._onWidgetKeyPressed.bind(this));
|
||||
keyController.connect('key-released', this._onWidgetKeyReleased.bind(this));
|
||||
this.widget.add_controller(keyController);
|
||||
|
||||
let scrollController = new Gtk.EventControllerScroll();
|
||||
scrollController.set_flags(Gtk.EventControllerScrollFlags.BOTH_AXES);
|
||||
scrollController.connect('scroll', this._onScroll.bind(this));
|
||||
this.widget.add_controller(scrollController);
|
||||
|
||||
let motionController = new Gtk.EventControllerMotion();
|
||||
motionController.connect('enter', this._onWidgetEnter.bind(this));
|
||||
motionController.connect('leave', this._onWidgetLeave.bind(this));
|
||||
motionController.connect('motion', this._onWidgetMotion.bind(this));
|
||||
this.widget.add_controller(motionController);
|
||||
|
||||
this.selfConnect('state-changed', this._onStateChanged.bind(this));
|
||||
this.selfConnect('uri-loaded', this._onUriLoaded.bind(this));
|
||||
this.selfConnect('end-of-stream', this._onStreamEnded.bind(this));
|
||||
this.selfConnect('warning', this._onPlayerWarning.bind(this));
|
||||
this.selfConnect('error', this._onPlayerError.bind(this));
|
||||
|
||||
this._realizeSignal = this.widget.connect('realize', this._onWidgetRealize.bind(this));
|
||||
}
|
||||
|
||||
set_media(source)
|
||||
@@ -133,7 +148,7 @@ class ClapperPlayer extends GstPlayer.Player
|
||||
return this.set_media(this._playlist[this._trackId]);
|
||||
}
|
||||
|
||||
if(file.get_path().endsWith(`.${this.playlist_ext}`))
|
||||
if(file.get_path().endsWith('.claps'))
|
||||
return this.load_playlist_file(file);
|
||||
|
||||
this.is_local_file = true;
|
||||
@@ -200,6 +215,12 @@ class ClapperPlayer extends GstPlayer.Player
|
||||
|
||||
seek(position)
|
||||
{
|
||||
if(this.state === GstPlayer.PlayerState.STOPPED)
|
||||
this.pause();
|
||||
|
||||
if(position < 0)
|
||||
position = 0;
|
||||
|
||||
this.seek_done = false;
|
||||
debug(`player is seeking to position: ${position}`);
|
||||
|
||||
@@ -219,6 +240,34 @@ class ClapperPlayer extends GstPlayer.Player
|
||||
this.seek(position * 1000000000);
|
||||
}
|
||||
|
||||
set_volume(volume)
|
||||
{
|
||||
if(volume < 0)
|
||||
volume = 0;
|
||||
else if(volume > 2)
|
||||
volume = 2;
|
||||
|
||||
super.set_volume(volume);
|
||||
}
|
||||
|
||||
adjust_position(isIncrease)
|
||||
{
|
||||
let { controls } = this.widget.get_ancestor(Gtk.Grid);
|
||||
|
||||
let value = (isIncrease) ? 10 : -10;
|
||||
let positionSeconds = controls.positionScale.get_value() + value;
|
||||
controls.positionScale.set_value(positionSeconds);
|
||||
}
|
||||
|
||||
adjust_volume(isIncrease)
|
||||
{
|
||||
let { controls } = this.widget.get_ancestor(Gtk.Grid);
|
||||
|
||||
let value = (isIncrease) ? 0.05 : -0.05;
|
||||
let volume = controls.volumeScale.get_value() + value;
|
||||
controls.volumeScale.set_value(volume);
|
||||
}
|
||||
|
||||
toggle_play()
|
||||
{
|
||||
let action = (this.state === GstPlayer.PlayerState.PLAYING)
|
||||
@@ -238,7 +287,8 @@ class ClapperPlayer extends GstPlayer.Player
|
||||
{
|
||||
debug(`changing rank of plugin: ${name}`);
|
||||
|
||||
let feature = this.gstRegistry.lookup_feature(name);
|
||||
let gstRegistry = Gst.Registry.get();
|
||||
let feature = gstRegistry.lookup_feature(name);
|
||||
if(!feature)
|
||||
return debug(`plugin unavailable: ${name}`);
|
||||
|
||||
@@ -248,20 +298,72 @@ class ClapperPlayer extends GstPlayer.Player
|
||||
debug(`changed rank: ${oldRank} -> ${rank} for ${name}`);
|
||||
}
|
||||
|
||||
connect(signal, fn)
|
||||
selfConnect(signal, fn)
|
||||
{
|
||||
let connection = super.connect(signal, fn);
|
||||
this._playerSignals.push(connection);
|
||||
|
||||
return connection;
|
||||
this._playerSignals.push(
|
||||
super.connect(signal, fn)
|
||||
);
|
||||
}
|
||||
|
||||
connectWidget(signal, fn)
|
||||
_setHideCursorTimeout()
|
||||
{
|
||||
let connection = this.widget.connect(signal, fn);
|
||||
this._widgetSignals.push(connection);
|
||||
this._clearTimeout('hideCursor');
|
||||
this._hideCursorTimeout = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1, () => {
|
||||
this._hideCursorTimeout = null;
|
||||
|
||||
return connection;
|
||||
if(this.cursorInPlayer) {
|
||||
let blankCursor = Gdk.Cursor.new_from_name('none', null);
|
||||
this.widget.set_cursor(blankCursor);
|
||||
}
|
||||
|
||||
return GLib.SOURCE_REMOVE;
|
||||
});
|
||||
}
|
||||
|
||||
_setHideControlsTimeout()
|
||||
{
|
||||
this._clearTimeout('hideControls');
|
||||
this._hideControlsTimeout = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 3, () => {
|
||||
this._hideControlsTimeout = null;
|
||||
|
||||
if(this.cursorInPlayer) {
|
||||
let clapperWidget = this.widget.get_ancestor(Gtk.Grid);
|
||||
if(clapperWidget.fullscreenMode) {
|
||||
this._clearTimeout('updateTime');
|
||||
clapperWidget.revealControls(false);
|
||||
}
|
||||
}
|
||||
|
||||
return GLib.SOURCE_REMOVE;
|
||||
});
|
||||
}
|
||||
|
||||
_setUpdateTimeInterval()
|
||||
{
|
||||
this._clearTimeout('updateTime');
|
||||
let clapperWidget = this.widget.get_ancestor(Gtk.Grid);
|
||||
let nextUpdate = clapperWidget.updateTime();
|
||||
|
||||
this._updateTimeTimeout = GLib.timeout_add(GLib.PRIORITY_DEFAULT, nextUpdate, () => {
|
||||
this._updateTimeTimeout = null;
|
||||
|
||||
if(clapperWidget.fullscreenMode)
|
||||
this._setUpdateTimeInterval();
|
||||
|
||||
return GLib.SOURCE_REMOVE;
|
||||
});
|
||||
}
|
||||
|
||||
_clearTimeout(name)
|
||||
{
|
||||
if(!this[`_${name}Timeout`])
|
||||
return;
|
||||
|
||||
GLib.source_remove(this[`_${name}Timeout`]);
|
||||
this[`_${name}Timeout`] = null;
|
||||
|
||||
if(name === 'updateTime')
|
||||
debug('cleared update time interval');
|
||||
}
|
||||
|
||||
_onStateChanged(player, state)
|
||||
@@ -272,6 +374,11 @@ class ClapperPlayer extends GstPlayer.Player
|
||||
this.seek_done = true;
|
||||
debug('seeking finished');
|
||||
}
|
||||
|
||||
let clapperWidget = this.widget.get_ancestor(Gtk.Grid);
|
||||
if(!clapperWidget) return;
|
||||
|
||||
clapperWidget._onPlayerStateChanged(player, state);
|
||||
}
|
||||
|
||||
_onStreamEnded(player)
|
||||
@@ -300,12 +407,227 @@ class ClapperPlayer extends GstPlayer.Player
|
||||
debug(error);
|
||||
}
|
||||
|
||||
_onWidgetDestroy()
|
||||
_onWidgetRealize()
|
||||
{
|
||||
this.widget.disconnect(this._realizeSignal);
|
||||
this._realizeSignal = null;
|
||||
|
||||
let root = this.widget.get_root();
|
||||
if(!root) return;
|
||||
|
||||
root.connect('close-request', this._onCloseRequest.bind(this));
|
||||
}
|
||||
|
||||
_onWidgetKeyPressed(controller, keyval, keycode, state)
|
||||
{
|
||||
this.keyPressCount++;
|
||||
|
||||
let bool = false;
|
||||
let clapperWidget = this.widget.get_ancestor(Gtk.Grid);
|
||||
|
||||
switch(keyval) {
|
||||
case Gdk.KEY_Up:
|
||||
bool = true;
|
||||
case Gdk.KEY_Down:
|
||||
this.adjust_volume(bool);
|
||||
break;
|
||||
case Gdk.KEY_Right:
|
||||
bool = true;
|
||||
case Gdk.KEY_Left:
|
||||
clapperWidget.controls.isPositionSeeking = true;
|
||||
this._clearTimeout('hideControls');
|
||||
if(this.keyPressCount > 1) {
|
||||
clapperWidget.revealerBottom.set_can_focus(false);
|
||||
clapperWidget.revealerBottom.revealChild(true);
|
||||
}
|
||||
this.adjust_position(bool);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_onWidgetKeyReleased(controller, keyval, keycode, state)
|
||||
{
|
||||
this.keyPressCount = 0;
|
||||
|
||||
let value;
|
||||
let clapperWidget = this.widget.get_ancestor(Gtk.Grid);
|
||||
|
||||
switch(keyval) {
|
||||
case Gdk.KEY_space:
|
||||
this.toggle_play();
|
||||
break;
|
||||
case Gdk.KEY_Return:
|
||||
if(clapperWidget.fullscreenMode) {
|
||||
clapperWidget.revealControls(true);
|
||||
this._setHideControlsTimeout();
|
||||
}
|
||||
break;
|
||||
case Gdk.KEY_Right:
|
||||
case Gdk.KEY_Left:
|
||||
value = clapperWidget.controls.positionScale.get_value();
|
||||
this.seek_seconds(value);
|
||||
this._setHideControlsTimeout();
|
||||
clapperWidget.controls.isPositionSeeking = false;
|
||||
break;
|
||||
case Gdk.KEY_F11:
|
||||
clapperWidget.toggleFullscreen();
|
||||
break;
|
||||
case Gdk.KEY_Escape:
|
||||
if(clapperWidget.fullscreenMode) {
|
||||
let root = this.widget.get_root();
|
||||
root.unfullscreen();
|
||||
}
|
||||
break;
|
||||
case Gdk.KEY_q:
|
||||
case Gdk.KEY_Q:
|
||||
let root = this.widget.get_root();
|
||||
root.emit('close-request');
|
||||
root.destroy();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_onWidgetPressed(gesture, nPress, x, y)
|
||||
{
|
||||
let button = gesture.get_current_button();
|
||||
let isDouble = (nPress % 2 == 0);
|
||||
this.dragAllowed = !isDouble;
|
||||
|
||||
switch(button) {
|
||||
case Gdk.BUTTON_PRIMARY:
|
||||
if(isDouble) {
|
||||
let clapperWidget = this.widget.get_ancestor(Gtk.Grid);
|
||||
clapperWidget.toggleFullscreen();
|
||||
}
|
||||
break;
|
||||
case Gdk.BUTTON_SECONDARY:
|
||||
this.toggle_play();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_onWidgetEnter(controller, x, y)
|
||||
{
|
||||
this.cursorInPlayer = true;
|
||||
|
||||
this._setHideCursorTimeout();
|
||||
|
||||
let clapperWidget = this.widget.get_ancestor(Gtk.Grid);
|
||||
if(clapperWidget.fullscreenMode)
|
||||
this._setHideControlsTimeout();
|
||||
}
|
||||
|
||||
_onWidgetLeave(controller)
|
||||
{
|
||||
this.cursorInPlayer = false;
|
||||
|
||||
this._clearTimeout('hideCursor');
|
||||
this._clearTimeout('hideControls');
|
||||
}
|
||||
|
||||
_onWidgetMotion(controller, posX, posY)
|
||||
{
|
||||
this.cursorInPlayer = true;
|
||||
|
||||
/* GTK4 sometimes generates motions with same coords */
|
||||
if(this.posX === posX && this.posY === posY)
|
||||
return;
|
||||
|
||||
/* Do not show cursor on small movements */
|
||||
if(
|
||||
Math.abs(this.posX - posX) >= 0.5
|
||||
|| Math.abs(this.posY - posY) >= 0.5
|
||||
) {
|
||||
let defaultCursor = Gdk.Cursor.new_from_name('default', null);
|
||||
this.widget.set_cursor(defaultCursor);
|
||||
this._setHideCursorTimeout();
|
||||
|
||||
let clapperWidget = this.widget.get_ancestor(Gtk.Grid);
|
||||
|
||||
if(clapperWidget.fullscreenMode) {
|
||||
if(!this._updateTimeTimeout)
|
||||
this._setUpdateTimeInterval();
|
||||
|
||||
if(!clapperWidget.revealerTop.get_reveal_child()) {
|
||||
/* Do not grab controls key focus on mouse movement */
|
||||
clapperWidget.revealerBottom.set_can_focus(false);
|
||||
clapperWidget.revealControls(true);
|
||||
}
|
||||
this._setHideControlsTimeout();
|
||||
}
|
||||
else {
|
||||
if(this._hideControlsTimeout)
|
||||
this._clearTimeout('hideControls');
|
||||
if(this._updateTimeTimeout)
|
||||
this._clearTimeout('updateTime');
|
||||
}
|
||||
}
|
||||
|
||||
this.posX = posX;
|
||||
this.posY = posY;
|
||||
}
|
||||
|
||||
_onWidgetDragUpdate(gesture, offsetX, offsetY)
|
||||
{
|
||||
if(!this.dragAllowed)
|
||||
return;
|
||||
|
||||
let clapperWidget = this.widget.get_ancestor(Gtk.Grid);
|
||||
if(clapperWidget.fullscreenMode)
|
||||
return;
|
||||
|
||||
let { gtk_double_click_distance } = this.widget.get_settings();
|
||||
|
||||
if (
|
||||
Math.abs(offsetX) > gtk_double_click_distance
|
||||
|| Math.abs(offsetY) > gtk_double_click_distance
|
||||
) {
|
||||
let [isActive, startX, startY] = gesture.get_start_point();
|
||||
if(!isActive) return;
|
||||
|
||||
let root = this.widget.get_root();
|
||||
if(!root) return;
|
||||
|
||||
root.get_surface().begin_move(
|
||||
gesture.get_device(),
|
||||
gesture.get_current_button(),
|
||||
startX,
|
||||
startY,
|
||||
gesture.get_current_event_time()
|
||||
);
|
||||
|
||||
gesture.reset();
|
||||
}
|
||||
}
|
||||
|
||||
_onScroll(controller, dx, dy)
|
||||
{
|
||||
let isHorizontal = Math.abs(dx) >= Math.abs(dy);
|
||||
let isIncrease = (isHorizontal) ? dx < 0 : dy < 0;
|
||||
|
||||
if(isHorizontal)
|
||||
this.adjust_position(isIncrease);
|
||||
else
|
||||
this.adjust_volume(isIncrease);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
_onCloseRequest(window)
|
||||
{
|
||||
while(this._widgetSignals.length)
|
||||
this.widget.disconnect(this._widgetSignals.pop());
|
||||
|
||||
while(this._playerSignals.length)
|
||||
this.disconnect(this._playerSignals.pop());
|
||||
|
||||
if(this.state !== GstPlayer.PlayerState.STOPPED)
|
||||
this.stop();
|
||||
}
|
||||
});
|
||||
|
Reference in New Issue
Block a user