Skip to content

Commit

Permalink
AudioApplet: Scrolling the Audio applet will adjust the main mix volume
Browse files Browse the repository at this point in the history
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 :^)
  • Loading branch information
benit8 authored and awesomekling committed Jul 21, 2020
1 parent 60a7187 commit 4916cfa
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
Binary file added Base/res/icons/audio-0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Base/res/icons/audio-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
Binary file modified Base/res/icons/audio-muted.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 46 additions & 6 deletions MenuApplets/Audio/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -44,8 +45,17 @@ class AudioWidget final : public GUI::Widget {
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 {}
Expand All @@ -59,19 +69,49 @@ class AudioWidget final : public GUI::Widget {
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)
Expand All @@ -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();
Expand Down

0 comments on commit 4916cfa

Please sign in to comment.