"Fossies" - the Fresh Open Source Software Archive

Member "tdesktop-4.8.3/Telegram/SourceFiles/media/player/media_player_volume_controller.cpp" (1 Jun 2023, 3431 Bytes) of package /linux/misc/tdesktop-4.8.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "media_player_volume_controller.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2 This file is part of Telegram Desktop,
    3 the official desktop application for the Telegram messaging service.
    4 
    5 For license and copyright information please follow this link:
    6 https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
    7 */
    8 #include "media/player/media_player_volume_controller.h"
    9 
   10 #include "media/audio/media_audio.h"
   11 #include "media/player/media_player_dropdown.h"
   12 #include "ui/widgets/buttons.h"
   13 #include "ui/widgets/shadow.h"
   14 #include "ui/widgets/continuous_sliders.h"
   15 #include "ui/ui_utility.h"
   16 #include "ui/cached_round_corners.h"
   17 #include "mainwindow.h"
   18 #include "main/main_session.h"
   19 #include "window/window_session_controller.h"
   20 #include "core/application.h"
   21 #include "core/core_settings.h"
   22 #include "styles/style_media_player.h"
   23 #include "styles/style_widgets.h"
   24 
   25 #include <QtGui/QGuiApplication>
   26 
   27 namespace Media::Player {
   28 
   29 VolumeController::VolumeController(
   30     QWidget *parent,
   31     not_null<Window::SessionController*> controller)
   32 : RpWidget(parent)
   33 , _slider(this, st::mediaPlayerPanelPlayback) {
   34     _slider->setMoveByWheel(true);
   35     _slider->setChangeProgressCallback([=](float64 volume) {
   36         applyVolumeChange(volume);
   37     });
   38     _slider->setChangeFinishedCallback([=](float64 volume) {
   39         if (volume > 0) {
   40             Core::App().settings().setRememberedSongVolume(volume);
   41         }
   42         applyVolumeChange(volume);
   43         Core::App().saveSettingsDelayed();
   44     });
   45     Core::App().settings().songVolumeChanges(
   46     ) | rpl::start_with_next([=](float64 volume) {
   47         if (!_slider->isChanging()) {
   48             _slider->setValue(volume);
   49         }
   50     }, lifetime());
   51     setVolume(Core::App().settings().songVolume());
   52 
   53     resize(st::mediaPlayerPanelVolumeWidth, 2 * st::mediaPlayerPanelPlaybackPadding + st::mediaPlayerPanelPlayback.width);
   54 }
   55 
   56 void VolumeController::setIsVertical(bool vertical) {
   57     using Direction = Ui::MediaSlider::Direction;
   58     _slider->setDirection(vertical ? Direction::Vertical : Direction::Horizontal);
   59     _slider->setAlwaysDisplayMarker(vertical);
   60 }
   61 
   62 void VolumeController::outerWheelEvent(not_null<QWheelEvent*> e) {
   63     QGuiApplication::sendEvent(_slider.data(), e);
   64 }
   65 
   66 void VolumeController::resizeEvent(QResizeEvent *e) {
   67     _slider->setGeometry(rect());
   68 }
   69 
   70 void VolumeController::setVolume(float64 volume) {
   71     _slider->setValue(volume);
   72     if (volume > 0) {
   73         Core::App().settings().setRememberedSongVolume(volume);
   74     }
   75     applyVolumeChange(volume);
   76 }
   77 
   78 void VolumeController::applyVolumeChange(float64 volume) {
   79     if (volume != Core::App().settings().songVolume()) {
   80         mixer()->setSongVolume(volume);
   81         Core::App().settings().setSongVolume(volume);
   82     }
   83 }
   84 
   85 void PrepareVolumeDropdown(
   86         not_null<Dropdown*> dropdown,
   87         not_null<Window::SessionController*> controller,
   88         rpl::producer<not_null<QWheelEvent*>> outerWheelEvents) {
   89     const auto volume = Ui::CreateChild<VolumeController>(
   90         dropdown.get(),
   91         controller);
   92     volume->show();
   93     volume->setIsVertical(true);
   94 
   95     dropdown->sizeValue(
   96     ) | rpl::start_with_next([=](QSize size) {
   97         const auto rect = QRect(QPoint(), size);
   98         const auto inner = rect.marginsRemoved(dropdown->getMargin());
   99         volume->setGeometry(
  100             inner.x(),
  101             inner.y() - st::lineWidth,
  102             inner.width(),
  103             (inner.height()
  104                 + st::lineWidth
  105                 - ((st::mediaPlayerVolumeSize.width()
  106                     - st::mediaPlayerPanelPlayback.width) / 2)));
  107     }, volume->lifetime());
  108 
  109     std::move(
  110         outerWheelEvents
  111     ) | rpl::start_with_next([=](not_null<QWheelEvent*> e) {
  112         volume->outerWheelEvent(e);
  113     }, volume->lifetime());
  114 }
  115 
  116 } // namespace Media::Player