From 4916cfa3a3e010531abd528499b3eb95c96ee1ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Lormeau?= Date: Tue, 21 Jul 2020 03:17:23 +0200 Subject: [PATCH] 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 :^) --- Base/res/icons/audio-0.png | Bin 0 -> 163 bytes Base/res/icons/audio-1.png | Bin 0 -> 180 bytes .../icons/{audio-unmuted.png => audio-2.png} | Bin Base/res/icons/audio-muted.png | Bin 165 -> 240 bytes MenuApplets/Audio/main.cpp | 52 ++++++++++++++++-- 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 Base/res/icons/audio-0.png create mode 100644 Base/res/icons/audio-1.png rename Base/res/icons/{audio-unmuted.png => audio-2.png} (100%) diff --git a/Base/res/icons/audio-0.png b/Base/res/icons/audio-0.png new file mode 100644 index 0000000000000000000000000000000000000000..b5baca3a1557022d32be32e854a65af8ca5718fa GIT binary patch literal 163 zcmeAS@N?(olHy`uVBq!ia0y~yVBle3U=ZM7V_;xVN}R{ez`&5@>EaloaXvXgLgRz{ zbQWQ=qm^AUJV&yA>|dwBu>Pvf3%?(KKdZ;_7D$UNw=jO9S!D3%*UxSX-u(;Igx5Ya z4^h16*EI9b>*M;14*#`hWEaloaXvXgLgRz{ zbQWQ=qm^AUJV&yA>|dwBu>Pu#hu@F$2M!$g@cX&{9>3<>8!V2_RP>+N{P6nu_!FOc zUd(zRRp;pIcDdm;|Nezs7w5D@GoN`9Z(k=U`|iJ_gv7t^JF@?t4}S8)|HS2<0G{vM mlPnT%PsmwdlX*dofnlPQLAKoC+HeL21_n=8KbLh*2~7YNgGo*R literal 0 HcmV?d00001 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 6b4cde9546a91a3608bbb5637c556a7ea27dbc66..9f6940a4b3f77fb735c63573c71bd0a455eafcfc 100644 GIT binary patch delta 212 zcmZ3=_oHnp6t$EHUmH)tMP_V9nx zCW|>;z480YQyhz!n3Z?=R4DyZGVtb$^Y&-HNX+ z%d_Ql1)Bc(bN0Z20|ycc9-8?b&5%-1`~Uy5`0^wX-nQ!VhJXI~e(Qet|NefS(?22# z++>8WG_$KjM(`OJ7#O^nBz8#pvd@Wy4?q7iPJX_w_{ZgAk%0&Qo|tvvaqB+@h9!Ld Wb(^>Sd&$7Sz~JfX=d#Wzp$P!)b!TG$ delta 137 zcmeysxRh~%N_~!}i(`n!`Q!u%jSuqES%l4wew_7)=SbF%{R?#X)?d|m;rHY3XZ1MV z0_kOGEJjDNyNv(*`q^#4yT9&l%cUb(KYmJk39o%99w;^8@Lzjo=EMJ^;!pqoIO`G1 u<3_ #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();