~comcloudway/melon

640c266bbe540d2903992a4a486bc303a24ec6f6 — Jakob Meier a month ago 4243143
Remember volume and brightness when switching videos
1 files changed, 28 insertions(+), 19 deletions(-)

M melon/widgets/player.py
M melon/widgets/player.py => melon/widgets/player.py +28 -19
@@ 99,10 99,12 @@ class VolumeDisplay(OverlayDispay):
        return f"{int(value * 100)}%"


# remember the values for as long as the app is running
volume = 1.0
# inverse opacity of overlay
brightness = 1.0

class VideoPlayerBase(Gtk.Overlay):
    volume = 1.0
    # inverse opacity of overlay
    brightness = 1.0

    def __init__(self, streams: list[Stream], with_gl=True, *args, **kwargs):
        super().__init__(*args, **kwargs)


@@ 117,6 119,9 @@ class VideoPlayerBase(Gtk.Overlay):
        self.toggle_fullscreen = None
        self.toggle_popout = None

        global brightness
        global volume

        overlay = Gtk.Overlay()
        self.set_child(overlay)



@@ 160,10 165,10 @@ class VideoPlayerBase(Gtk.Overlay):
        self.brightness_overlay.add_css_class("osd")
        overlay.add_overlay(self.brightness_overlay)

        self.brightness_display = BrightnessDisplay(self.brightness)
        self.brightness_display = BrightnessDisplay(brightness)
        overlay.add_overlay(self.brightness_display)
        self.brightness_display.hide()
        self.volume_display = VolumeDisplay(self.volume)
        self.volume_display = VolumeDisplay(volume)
        overlay.add_overlay(self.volume_display)
        self.volume_display.hide()



@@ 267,8 272,8 @@ class VideoPlayerBase(Gtk.Overlay):
                "audio-volume-medium-symbolic",
            ],
        )
        # self.volume uses the [0,1] interval, but the slider uses [0,100]
        self.volume_ctr.set_value(self.volume * 100)
        # volume uses the [0,1] interval, but the slider uses [0,100]
        self.volume_ctr.set_value(volume * 100)
        self.volume_ctr.connect(
            "value-changed", lambda w, val: self.change_volume(val / 100)
        )


@@ 301,8 306,8 @@ class VideoPlayerBase(Gtk.Overlay):
        self.click_ctr.connect("pressed", lambda e, n, x, y: self._onclick(n, x, y))

        # initialize volume & brightness
        self.change_brightness(self.brightness)
        self.change_volume(self.volume)
        self.change_brightness(brightness)
        self.change_volume(volume)

    def reg_action(self, name, func, variant=None):
        self.install_action(name, variant, func)


@@ 355,6 360,8 @@ class VideoPlayerBase(Gtk.Overlay):
        GLib.timeout_add(timeout * 1000, self._hide_controls)

    def _update_swipe(self, e, s):
        global volume
        global brightness
        bounds = self.swipe_ctr.get_bounding_box_center()
        if not bounds[0]:
            return


@@ 377,14 384,14 @@ class VideoPlayerBase(Gtk.Overlay):
            # volume control
            # update the value before showing the overlay
            # invert the value so it goes in the right direction
            self.change_volume(self.volume - fact)
            self.change_volume(volume - fact)
            # make sure the display is shown
            self.volume_display.show()
        elif bounds[1] <= left_bound:
            # brightness controll
            # update the value before showing the overlay
            # invert the value so it goes in the right direction
            self.change_brightness(self.brightness - fact)
            self.change_brightness(brightness - fact)
            self.brightness_display.show()
        else:
            # mixed controls


@@ 393,18 400,20 @@ class VideoPlayerBase(Gtk.Overlay):
            self.volume_display.hide()

    def change_volume(self, vol):
        self.volume = clamp(0.0, vol, 1.0)
        self.volume_display.update(self.volume)
        global volume
        volume = clamp(0.0, vol, 1.0)
        self.volume_display.update(volume)
        # update player volume
        self.source.set_property("volume", self.volume)
        self.source.set_property("volume", volume)
        # update volume button value (once implemented)
        # self.volume uses the [0,1] interval, but the slider uses [0,100]
        self.volume_ctr.set_value(self.volume * 100)
        # volume uses the [0,1] interval, but the slider uses [0,100]
        self.volume_ctr.set_value(volume * 100)

    def change_brightness(self, bright):
        self.brightness = clamp(0.0, bright, 1.0)
        self.brightness_display.update(self.brightness)
        self.brightness_overlay.set_opacity(1.0 - self.brightness)
        global brightness
        brightness = clamp(0.0, bright, 1.0)
        self.brightness_display.update(brightness)
        self.brightness_overlay.set_opacity(1.0 - brightness)

    def _end_swipe(self, e, vx, vy):
        # hide overlay