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

View File

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