AudioApplet: Scrolling the Audio applet will adjust the main mix volume

The Audio applet now dislays the main mix volume next to the speaker
icon. A click on the applet still mutes the global mixer. By scrolling
the mouse wheel while on the applet, you can decrease/increase the mixer
volume. Different icons will be painted depending on the volume and the
mute state.

Happy listening :^)
This commit is contained in:
Benoît Lormeau 2020-07-21 03:17:23 +02:00 committed by Andreas Kling
parent 60a7187db2
commit 4916cfa3a3
Notes: sideshowbarker 2024-07-19 04:41:32 +09:00
5 changed files with 46 additions and 6 deletions

BIN
Base/res/icons/audio-0.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 163 B

BIN
Base/res/icons/audio-1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 180 B

View file

Before

Width:  |  Height:  |  Size: 191 B

After

Width:  |  Height:  |  Size: 191 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 165 B

After

Width:  |  Height:  |  Size: 240 B

View file

@ -31,6 +31,7 @@
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Font.h>
#include <LibGfx/Palette.h>
class AudioWidget final : public GUI::Widget {
C_OBJECT(AudioWidget)
@ -44,8 +45,17 @@ public:
m_audio_muted = muted;
update();
};
m_unmuted_bitmap = Gfx::Bitmap::load_from_file("/res/icons/audio-unmuted.png");
m_muted_bitmap = Gfx::Bitmap::load_from_file("/res/icons/audio-muted.png");
m_audio_client->on_main_mix_volume_change = [this](int volume) {
m_audio_volume = volume;
if (!m_audio_muted)
update();
};
m_volume_level_bitmaps.append({66, Gfx::Bitmap::load_from_file("/res/icons/audio-2.png")});
m_volume_level_bitmaps.append({33, Gfx::Bitmap::load_from_file("/res/icons/audio-1.png")});
m_volume_level_bitmaps.append({1, Gfx::Bitmap::load_from_file("/res/icons/audio-0.png")});
m_volume_level_bitmaps.append({0, Gfx::Bitmap::load_from_file("/res/icons/audio-muted.png")});
}
virtual ~AudioWidget() override {}
@ -59,19 +69,49 @@ private:
update();
}
virtual void mousewheel_event(GUI::MouseEvent& event) override
{
if (m_audio_muted)
return;
int volume = clamp(m_audio_volume - event.wheel_delta() * 5, 0, 100);
m_audio_client->set_main_mix_volume(volume);
update();
}
virtual void paint_event(GUI::PaintEvent& event) override
{
GUI::Painter painter(*this);
painter.add_clip_rect(event.rect());
painter.clear_rect(event.rect(), Color::from_rgba(0));
auto& audio_bitmap = m_audio_muted ? *m_muted_bitmap : *m_unmuted_bitmap;
auto& audio_bitmap = choose_bitmap_from_volume();
painter.blit({}, audio_bitmap, audio_bitmap.rect());
auto volume_text = m_audio_muted ? "Mut" : String::format("%d%%", m_audio_volume);
painter.draw_text({16, 3, 24, 16}, volume_text, Gfx::Font::default_font(), Gfx::TextAlignment::TopLeft, palette().window_text());
}
Gfx::Bitmap& choose_bitmap_from_volume()
{
if (m_audio_muted)
return *m_volume_level_bitmaps.last().bitmap;
for (auto& pair : m_volume_level_bitmaps) {
if (m_audio_volume >= pair.volume_threshold)
return *pair.bitmap;
}
ASSERT_NOT_REACHED();
}
struct VolumeBitmapPair {
int volume_threshold { 0 };
RefPtr<Gfx::Bitmap> bitmap;
};
NonnullRefPtr<Audio::ClientConnection> m_audio_client;
RefPtr<Gfx::Bitmap> m_muted_bitmap;
RefPtr<Gfx::Bitmap> m_unmuted_bitmap;
Vector<VolumeBitmapPair, 4> m_volume_level_bitmaps;
bool m_audio_muted { false };
int m_audio_volume { 100 };
};
int main(int argc, char** argv)
@ -92,7 +132,7 @@ int main(int argc, char** argv)
window->set_has_alpha_channel(true);
window->set_title("Audio");
window->set_window_type(GUI::WindowType::MenuApplet);
window->resize(12, 16);
window->resize(42, 16);
window->set_main_widget<AudioWidget>();
window->show();