diff options
-rw-r--r-- | src/components/statusindicator.hpp | 4 | ||||
-rw-r--r-- | src/components/volumemeter.cpp | 119 | ||||
-rw-r--r-- | src/components/volumemeter.hpp | 29 | ||||
-rw-r--r-- | src/windows/voicewindow.cpp | 7 | ||||
-rw-r--r-- | src/windows/voicewindow.hpp | 3 |
5 files changed, 157 insertions, 5 deletions
diff --git a/src/components/statusindicator.hpp b/src/components/statusindicator.hpp index ad9122b..ce86d93 100644 --- a/src/components/statusindicator.hpp +++ b/src/components/statusindicator.hpp @@ -11,9 +11,9 @@ public: protected: Gtk::SizeRequestMode get_request_mode_vfunc() const override; void get_preferred_width_vfunc(int &minimum_width, int &natural_width) const override; - void get_preferred_height_for_width_vfunc(int width, int &minimum_height, int &natural_height) const override; - void get_preferred_height_vfunc(int &minimum_height, int &natural_height) const override; void get_preferred_width_for_height_vfunc(int height, int &minimum_width, int &natural_width) const override; + void get_preferred_height_vfunc(int &minimum_height, int &natural_height) const override; + void get_preferred_height_for_width_vfunc(int width, int &minimum_height, int &natural_height) const override; void on_size_allocate(Gtk::Allocation &allocation) override; void on_map() override; void on_unmap() override; diff --git a/src/components/volumemeter.cpp b/src/components/volumemeter.cpp new file mode 100644 index 0000000..82f47a5 --- /dev/null +++ b/src/components/volumemeter.cpp @@ -0,0 +1,119 @@ +#include "volumemeter.hpp" +#include <cstring> + +VolumeMeter::VolumeMeter() + : Glib::ObjectBase("volumemeter") + , Gtk::Widget() { + set_has_window(true); +} + +void VolumeMeter::SetVolume(double fraction) { + m_fraction = fraction; + queue_draw(); +} + +void VolumeMeter::SetTick(double fraction) { + m_tick = fraction; + queue_draw(); +} + +Gtk::SizeRequestMode VolumeMeter::get_request_mode_vfunc() const { + return Gtk::Widget::get_request_mode_vfunc(); +} + +void VolumeMeter::get_preferred_width_vfunc(int &minimum_width, int &natural_width) const { + const int width = get_allocated_width(); + minimum_width = natural_width = width; +} + +void VolumeMeter::get_preferred_width_for_height_vfunc(int height, int &minimum_width, int &natural_width) const { + get_preferred_width_vfunc(minimum_width, natural_width); +} + +void VolumeMeter::get_preferred_height_vfunc(int &minimum_height, int &natural_height) const { + // blehhh :PPP + const int height = get_allocated_height(); + minimum_height = natural_height = 4; +} + +void VolumeMeter::get_preferred_height_for_width_vfunc(int width, int &minimum_height, int &natural_height) const { + get_preferred_height_vfunc(minimum_height, natural_height); +} + +void VolumeMeter::on_size_allocate(Gtk::Allocation &allocation) { + set_allocation(allocation); + + if (m_window) + m_window->move_resize(allocation.get_x(), allocation.get_y(), allocation.get_width(), allocation.get_height()); +} + +void VolumeMeter::on_map() { + Gtk::Widget::on_map(); +} + +void VolumeMeter::on_unmap() { + Gtk::Widget::on_unmap(); +} + +void VolumeMeter::on_realize() { + set_realized(true); + + if (!m_window) { + GdkWindowAttr attributes; + std::memset(&attributes, 0, sizeof(attributes)); + + auto allocation = get_allocation(); + + attributes.x = allocation.get_x(); + attributes.y = allocation.get_y(); + attributes.width = allocation.get_width(); + attributes.height = allocation.get_height(); + + attributes.event_mask = get_events() | Gdk::EXPOSURE_MASK; + attributes.window_type = GDK_WINDOW_CHILD; + attributes.wclass = GDK_INPUT_OUTPUT; + + m_window = Gdk::Window::create(get_parent_window(), &attributes, GDK_WA_X | GDK_WA_Y); + set_window(m_window); + + m_window->set_user_data(gobj()); + } +} + +void VolumeMeter::on_unrealize() { + m_window.reset(); + + Gtk::Widget::on_unrealize(); +} + +bool VolumeMeter::on_draw(const Cairo::RefPtr<Cairo::Context> &cr) { + const auto allocation = get_allocation(); + const auto width = allocation.get_width(); + const auto height = allocation.get_height(); + + const double LOW_MAX = 0.7 * width; + const double MID_MAX = 0.85 * width; + const double desired_width = width * m_fraction; + + const double draw_low = std::min(desired_width, LOW_MAX); + const double draw_mid = std::min(desired_width, MID_MAX); + const double draw_hi = desired_width; + + cr->set_source_rgb(1.0, 0.0, 0.0); + cr->rectangle(0.0, 0.0, draw_hi, height); + cr->fill(); + cr->set_source_rgb(1.0, 0.5, 0.0); + cr->rectangle(0.0, 0.0, draw_mid, height); + cr->fill(); + cr->set_source_rgb(.0, 1.0, 0.0); + cr->rectangle(0.0, 0.0, draw_low, height); + cr->fill(); + + const double tick_base = width * m_tick; + + cr->set_source_rgb(0.8, 0.8, 0.8); + cr->rectangle(tick_base, 0, 4, height); + cr->fill(); + + return true; +} diff --git a/src/components/volumemeter.hpp b/src/components/volumemeter.hpp new file mode 100644 index 0000000..e9656b6 --- /dev/null +++ b/src/components/volumemeter.hpp @@ -0,0 +1,29 @@ +#pragma once +#include <gtkmm/widget.h> + +class VolumeMeter : public Gtk::Widget { +public: + VolumeMeter(); + + void SetVolume(double fraction); + void SetTick(double fraction); + +protected: + Gtk::SizeRequestMode get_request_mode_vfunc() const override; + void get_preferred_width_vfunc(int &minimum_width, int &natural_width) const override; + void get_preferred_width_for_height_vfunc(int height, int &minimum_width, int &natural_width) const override; + void get_preferred_height_vfunc(int &minimum_height, int &natural_height) const override; + void get_preferred_height_for_width_vfunc(int width, int &minimum_height, int &natural_height) const override; + void on_size_allocate(Gtk::Allocation &allocation) override; + void on_map() override; + void on_unmap() override; + void on_realize() override; + void on_unrealize() override; + bool on_draw(const Cairo::RefPtr<Cairo::Context> &cr) override; + +private: + Glib::RefPtr<Gdk::Window> m_window; + + double m_fraction = 0.0; + double m_tick = 0.0; +}; diff --git a/src/windows/voicewindow.cpp b/src/windows/voicewindow.cpp index 596f502..69eb969 100644 --- a/src/windows/voicewindow.cpp +++ b/src/windows/voicewindow.cpp @@ -105,7 +105,10 @@ VoiceWindow::VoiceWindow(Snowflake channel_id) m_capture_gate.set_value_pos(Gtk::POS_LEFT); m_capture_gate.set_value(0.0); m_capture_gate.signal_value_changed().connect([this]() { - m_signal_gate.emit(m_capture_gate.get_value()); + // todo this should probably emit 0-1 i dont think the mgr should be responsible for scaling down + const double val = m_capture_gate.get_value(); + m_signal_gate.emit(val); + m_capture_volume.SetTick(val / 100.0); }); m_scroll.add(m_user_list); @@ -149,7 +152,7 @@ void VoiceWindow::OnDeafenChanged() { } bool VoiceWindow::UpdateVoiceMeters() { - m_capture_volume.set_fraction(Abaddon::Get().GetAudio().GetCaptureVolumeLevel()); + m_capture_volume.SetVolume(Abaddon::Get().GetAudio().GetCaptureVolumeLevel()); for (auto [id, row] : m_rows) { const auto ssrc = Abaddon::Get().GetDiscordClient().GetSSRCOfUser(id); if (ssrc.has_value()) { diff --git a/src/windows/voicewindow.hpp b/src/windows/voicewindow.hpp index 5f5436b..86cedb8 100644 --- a/src/windows/voicewindow.hpp +++ b/src/windows/voicewindow.hpp @@ -2,6 +2,7 @@ #ifdef WITH_VOICE // clang-format off +#include "components/volumemeter.hpp" #include "discord/snowflake.hpp" #include <gtkmm/box.h> #include <gtkmm/checkbutton.h> @@ -40,7 +41,7 @@ private: Gtk::ScrolledWindow m_scroll; Gtk::ListBox m_user_list; - Gtk::ProgressBar m_capture_volume; + VolumeMeter m_capture_volume; Gtk::Scale m_capture_gate; Snowflake m_channel_id; |