Skip to content

Commit

Permalink
Remember last volume from slider
Browse files Browse the repository at this point in the history
  • Loading branch information
kraxarn committed Jun 14, 2020
1 parent 9127982 commit 361c5b7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ void Settings::load()
general.style = g["style"].toString();
general.lastPlaylist = g["last_playlist"].toString();
general.lastVersion = g["last_version"].toString();
general.lastVolume = g["last_volume"].toInt();
general.pulseVolume = g["pulse_volume"].toBool(false);
general.mediaController = g["media_controller"].toBool(hasMediaControllerSupport());
general.spotifyPlaybackOrder = g["spotify_playback_order"].toBool(false);
Expand Down Expand Up @@ -102,6 +103,7 @@ void Settings::save()
{"hidden_song_headers", jsonHiddenSongHeaders},
{"last_playlist", general.lastPlaylist},
{"last_version", general.lastVersion},
{"last_volume", general.lastVolume},
{"media_controller", general.mediaController},
{"pulse_volume", general.pulseVolume},
{"refresh_interval", general.refreshInterval},
Expand Down
2 changes: 1 addition & 1 deletion src/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Settings
showChangelog, fallbackIcons, fixedWidthTime, trayAlbumArt;
Palette stylePalette;
QVector<int> hiddenSongHeaders;
int songHeaderResizeMode, songHeaderSortBy, refreshInterval;
int songHeaderResizeMode, songHeaderSortBy, refreshInterval, lastVolume;
} General;

typedef struct {
Expand Down
11 changes: 9 additions & 2 deletions src/volumebutton.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include "volumebutton.hpp"

VolumeButton::VolumeButton(const Settings &settings, spt::Spotify &spotify, QWidget *parent)
VolumeButton::VolumeButton(Settings &settings, spt::Spotify &spotify, QWidget *parent)
: settings(settings), spotify(spotify), volume(new QSlider(this)), QToolButton(parent)
{
// Volume slider
volume->setOrientation(Qt::Orientation::Vertical);
volume->setFixedHeight(100);
volume->setMinimum(0);
volume->setMaximum(20);
volume->setValue(spt::ClientHandler::getVolume() * 20);
volume->setValue(settings.general.lastVolume > 0
? settings.general.lastVolume : spt::ClientHandler::getVolume() * 20);

// Layout for volume slider
auto volumeMenu = new QMenu(this);
Expand Down Expand Up @@ -48,6 +49,12 @@ VolumeButton::VolumeButton(const Settings &settings, spt::Spotify &spotify, QWid
}
}

VolumeButton::~VolumeButton()
{
settings.general.lastVolume = volume->value();
settings.save();
}

void VolumeButton::wheelEvent(QWheelEvent *event)
{
volume->setValue(volume->value() + (event->angleDelta().y() < 0 ? -1 : 1));
Expand Down
6 changes: 4 additions & 2 deletions src/volumebutton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ class VolumeButton : public QToolButton
Q_OBJECT

public:
VolumeButton(const Settings &settings, spt::Spotify &spotify, QWidget *parent);
VolumeButton(Settings &settings, spt::Spotify &spotify, QWidget *parent);
~VolumeButton();

void updateIcon();

protected:
Expand All @@ -26,6 +28,6 @@ class VolumeButton : public QToolButton

private:
QSlider *volume;
const Settings &settings;
Settings &settings;
spt::Spotify &spotify;
};

0 comments on commit 361c5b7

Please sign in to comment.