VolumeView: Change volume immediately

This commit is contained in:
Jaidyn Ann 2022-06-05 13:12:37 -05:00
parent c38c84b296
commit 5e8a753ef3
2 changed files with 29 additions and 16 deletions

View File

@ -15,6 +15,8 @@
// The same color MediaPlayer uses, interface/VolumeSlider
static const rgb_color kVolumeGreen = (rgb_color){ 116, 224, 0, 255 };
static const uint32 VOLUME_CHANGED = 'vvch';
VolumeView::VolumeView(BRect frame)
:
@ -24,6 +26,7 @@ VolumeView::VolumeView(BRect frame)
fSlider = new BSlider(frame, "volumeSlider", NULL, NULL, 0, 1000, B_HORIZONTAL, B_BLOCK_THUMB,
B_FOLLOW_LEFT_RIGHT);
fSlider->SetModificationMessage(new BMessage(VOLUME_CHANGED));
fSlider->UseFillColor(true, &kVolumeGreen);
fSlider->SetViewColor(B_TRANSPARENT_COLOR);
AddChild(fSlider);
@ -37,6 +40,8 @@ VolumeView::VolumeView(BMessage* data)
ReplicantView(data)
{
fSlider = dynamic_cast<BSlider*>(FindView("volumeSlider"));
if (fSlider->IsHidden())
fSlider->Show();
_Init();
}
@ -61,18 +66,31 @@ VolumeView::Instantiate(BMessage* data)
}
void
VolumeView::AttachedToWindow()
{
fSlider->SetTarget(this);
}
void
VolumeView::MessageReceived(BMessage* msg)
{
if (msg->what != B_MOUSE_WHEEL_CHANGED || !fSlider->IsEnabled()) {
ReplicantView::MessageReceived(msg);
return;
}
float scroll = 0.0f;
if ((msg->FindFloat("be:wheel_delta_x", &scroll) == B_OK && scroll != 0.0f)
|| (msg->FindFloat("be:wheel_delta_y", &scroll) == B_OK && scroll != 0.0f)) {
fSlider->SetPosition(fSlider->Position() + scroll * -0.03);
switch (msg->what) {
case VOLUME_CHANGED:
fMediaPlayer->SetVolume(_PositionToVolume(fSlider->Position()));
break;
case B_MOUSE_WHEEL_CHANGED:
{
float scroll = 0.0f;
if ((msg->FindFloat("be:wheel_delta_x", &scroll) == B_OK && scroll != 0.0f)
|| (msg->FindFloat("be:wheel_delta_y", &scroll) == B_OK && scroll != 0.0f))
fSlider->SetPosition(fSlider->Position() + scroll * -0.03);
fMediaPlayer->SetVolume(_PositionToVolume(fSlider->Position()));
break;
}
default:
ReplicantView::MessageReceived(msg);
}
}
@ -80,17 +98,12 @@ VolumeView::MessageReceived(BMessage* msg)
void
VolumeView::Pulse()
{
if (fSlider->Position() != fLastPos)
fMediaPlayer->SetVolume(_PositionToVolume(fSlider->Position()));
float volume = fMediaPlayer->Volume();
if (volume > 0) {
SetInactive(false);
fSlider->SetPosition(_VolumeToPosition(volume));
} else
SetInactive(true);
fLastPos = fSlider->Position();
}
@ -115,7 +128,6 @@ VolumeView::_Init()
fSlider->GetPreferredSize(NULL, &height);
SetExplicitMaxSize(BSize(B_SIZE_UNSET, height + 16));
fLastPos = 0;
Pulse();
}

View File

@ -18,6 +18,8 @@ public:
virtual status_t Archive(BMessage* data, bool deep = true) const;
static VolumeView* Instantiate(BMessage* data);
void AttachedToWindow();
virtual void MessageReceived(BMessage* msg);
virtual void Pulse();
@ -30,7 +32,6 @@ private:
float _PositionToVolume(float position);
BSlider* fSlider;
float fLastPos;
};
#endif // VOLUMEVIEW_H