[gnome-music/wip/jfelder/volume-slider: 3/5] playertoolbar: Add a volume slider




commit 2edbd64b93c3fa803b77dbe3ef7cc86d8632db1a
Author: Jean Felder <jfelder src gnome org>
Date:   Fri Oct 1 15:28:04 2021 +0200

    playertoolbar: Add a volume slider

 data/ui/PlayerToolbar.ui            | 13 +++++++++++++
 gnomemusic/widgets/playertoolbar.py | 36 +++++++++++++++++++++++++++++++++++-
 2 files changed, 48 insertions(+), 1 deletion(-)
---
diff --git a/data/ui/PlayerToolbar.ui b/data/ui/PlayerToolbar.ui
index 39616b80d..2005405d1 100644
--- a/data/ui/PlayerToolbar.ui
+++ b/data/ui/PlayerToolbar.ui
@@ -197,7 +197,20 @@
             </child>
           </object>
         </child>
+        <child>
+          <object class="GtkVolumeButton" id="_volume_button">
+            <property name="focusable">True</property>
+            <!-- <property name="adjustment">_volume_adjustment</property> -->
+          </object>
+        </child>
       </object>
     </child>
   </template>
+  <!-- <object class="GtkAdjustment" id="_volume_adjustment"> -->
+  <!--   <property name="upper">1</property> -->
+  <!--   <property name="step-increment">0.1</property> -->
+  <!--   <property name="page-increment">1</property> -->
+  <!--   <property name="value">1</property> -->
+  <!--   <signal name="value-changed" handler="_volume_adjustment_changed" swapped="no"/> -->
+  <!-- </object> -->
 </interface>
diff --git a/gnomemusic/widgets/playertoolbar.py b/gnomemusic/widgets/playertoolbar.py
index 71a094d1e..8215a091c 100644
--- a/gnomemusic/widgets/playertoolbar.py
+++ b/gnomemusic/widgets/playertoolbar.py
@@ -23,7 +23,11 @@
 # delete this exception statement from your version.
 
 from gettext import gettext as _
-from gi.repository import Gio, GLib, GObject, Gtk
+from math import isclose
+
+import gi
+gi.require_version("GstAudio", "1.0")
+from gi.repository import Gio, GLib, GObject, GstAudio, Gtk
 
 from gnomemusic.coverpaintable import CoverPaintable
 from gnomemusic.gstplayer import Playback
@@ -56,6 +60,8 @@ class PlayerToolbar(Gtk.ActionBar):
     _repeat_image = Gtk.Template.Child()
     _song_info_box = Gtk.Template.Child()
     _title_label = Gtk.Template.Child()
+    # _volume_adjustment = Gtk.Template.Child()
+    _volume_button = Gtk.Template.Child()
 
     def __init__(self):
         super().__init__()
@@ -122,6 +128,34 @@ class PlayerToolbar(Gtk.ActionBar):
 
         self._sync_repeat_image()
 
+        self._player.connect("notify::volume", self._player_volume_changed)
+        self._volume_button.connect(
+            "notify::value", self._volume_button_changed)
+
+    def _volume_button_changed(
+            self, widget: Gtk.VolumeButton,
+            val: GObject.ParamSpecDouble) -> None:
+        value_linear = GstAudio.StreamVolume.convert_volume(
+            GstAudio.StreamVolumeFormat.CUBIC,
+            GstAudio.StreamVolumeFormat.LINEAR,
+            self._volume_button.props.value)
+        print("toolbar, volume value changed", value_linear, self._player.props.volume)
+        if isclose(value_linear, self._player.props.volume):
+            return
+
+        print("toolbar, on ajuste cote player")
+        self._player.props.volume = value_linear
+
+    def _player_volume_changed(
+            self, player: Player, val: GObject.ParamSpecDouble) -> None:
+        print("on ajuste le volume dans la toolbar")
+        print("la valeur linear", self._player.props.volume)
+        volume_cubic = GstAudio.StreamVolume.convert_volume(
+            GstAudio.StreamVolumeFormat.LINEAR,
+            GstAudio.StreamVolumeFormat.CUBIC, self._player.props.volume)
+        print("la valeur cubic", volume_cubic)
+        self._volume_button.props.value = volume_cubic
+
     def _repeat_menu_changed(
             self, action: Gio.SimpleAction, new_state: GLib.Variant) -> None:
         self._repeat_action.set_state(new_state)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]