From a597de5481841832839d1ca5404452739533c6e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20Dzi=C4=99giel?= Date: Wed, 10 Feb 2021 23:11:46 +0100 Subject: [PATCH] Swipe when fullscreen to seek or adjust volume --- src/player.js | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/src/player.js b/src/player.js index 86a95c02..7f8086dd 100644 --- a/src/player.js +++ b/src/player.js @@ -44,6 +44,13 @@ class ClapperPlayer extends PlayerBase dragGesture.connect('drag-update', this._onWidgetDragUpdate.bind(this)); this.widget.add_controller(dragGesture); + const swipeGesture = new Gtk.GestureSwipe({ + touch_only: true, + }); + swipeGesture.connect('swipe', this._onWidgetSwipe.bind(this)); + swipeGesture.connect('update', this._onWidgetSwipeUpdate.bind(this)); + this.widget.add_controller(swipeGesture); + const keyController = new Gtk.EventControllerKey(); keyController.connect('key-pressed', this._onWidgetKeyPressed.bind(this)); keyController.connect('key-released', this._onWidgetKeyReleased.bind(this)); @@ -259,10 +266,12 @@ class ClapperPlayer extends PlayerBase controls.positionScale.set_value(positionSeconds); } - adjust_volume(isIncrease) + adjust_volume(isIncrease, offset) { + offset = offset || 0.05; + const { controls } = this.widget.get_ancestor(Gtk.Grid); - const value = (isIncrease) ? 0.05 : -0.05; + const value = (isIncrease) ? offset : -offset; const volume = controls.volumeScale.get_value() + value; controls.volumeScale.set_value(volume); @@ -292,6 +301,21 @@ class ClapperPlayer extends PlayerBase } } + getIsSwipeOk(velocity, otherVelocity) + { + if(!velocity) + return false; + + const absVel = Math.abs(velocity); + + if(absVel < 20 || Math.abs(otherVelocity) * 1.5 >= absVel) + return false; + + const clapperWidget = this.widget.get_ancestor(Gtk.Grid); + + return clapperWidget.fullscreenMode; + } + _setHideCursorTimeout() { this._clearTimeout('hideCursor'); @@ -723,6 +747,27 @@ class ClapperPlayer extends PlayerBase } } + _onWidgetSwipe(gesture, velocityX, velocityY) + { + if(!this.getIsSwipeOk(velocityX, velocityY)) + return; + + this._onScroll(gesture, -velocityX, 0); + } + + _onWidgetSwipeUpdate(gesture, sequence) + { + const [isCalc, velocityX, velocityY] = gesture.get_velocity(); + if(!isCalc) return; + + if(!this.getIsSwipeOk(velocityY, velocityX)) + return; + + const isIncrease = velocityY < 0; + + this.adjust_volume(isIncrease, 0.01); + } + _onScroll(controller, dx, dy) { const isHorizontal = (Math.abs(dx) >= Math.abs(dy));