diff --git a/Base/res/icons/audio-0.png b/Base/res/icons/audio-0.png new file mode 100644 index 0000000000..b5baca3a15 Binary files /dev/null and b/Base/res/icons/audio-0.png differ diff --git a/Base/res/icons/audio-1.png b/Base/res/icons/audio-1.png new file mode 100644 index 0000000000..839e2cb3a8 Binary files /dev/null and b/Base/res/icons/audio-1.png differ diff --git a/Base/res/icons/audio-unmuted.png b/Base/res/icons/audio-2.png similarity index 100% rename from Base/res/icons/audio-unmuted.png rename to Base/res/icons/audio-2.png diff --git a/Base/res/icons/audio-muted.png b/Base/res/icons/audio-muted.png index 6b4cde9546..9f6940a4b3 100644 Binary files a/Base/res/icons/audio-muted.png and b/Base/res/icons/audio-muted.png differ 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 #include #include +#include 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 bitmap; + }; + NonnullRefPtr m_audio_client; - RefPtr m_muted_bitmap; - RefPtr m_unmuted_bitmap; + Vector 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(); window->show();