diff options
author | Benoît Lormeau <blormeau@outlook.com> | 2020-07-21 03:17:23 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-07-21 19:02:25 +0200 |
commit | 4916cfa3a3e010531abd528499b3eb95c96ee1ee (patch) | |
tree | fac34b0bfd1f15a06a6f22c1e3e229f3dd6e84cb /MenuApplets/Audio/main.cpp | |
parent | 60a7187db27f711c92243a437123e120a8ea9c5e (diff) | |
download | serenity-4916cfa3a3e010531abd528499b3eb95c96ee1ee.zip |
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 :^)
Diffstat (limited to 'MenuApplets/Audio/main.cpp')
-rw-r--r-- | MenuApplets/Audio/main.cpp | 52 |
1 files changed, 46 insertions, 6 deletions
diff --git a/MenuApplets/Audio/main.cpp b/MenuApplets/Audio/main.cpp index df6f859e41..1a88978e97 100644 --- a/MenuApplets/Audio/main.cpp +++ b/MenuApplets/Audio/main.cpp @@ -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(); |