From 88f2e63eeb0b0b9912133ab14ad1f24a452b1c2b Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Tue, 18 Oct 2022 02:53:11 -0400 Subject: custom draw capture volume with gate indicator --- src/components/volumemeter.cpp | 119 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/components/volumemeter.cpp (limited to 'src/components/volumemeter.cpp') 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 + +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 &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; +} -- cgit v1.2.3 From 848e75f5774d56ebb1a67ba4326a96c8ea5ac3f1 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Thu, 20 Oct 2022 02:17:50 -0400 Subject: use new volume meter for other users --- src/components/volumemeter.cpp | 14 ++++++++++---- src/components/volumemeter.hpp | 2 ++ src/windows/voicewindow.cpp | 6 ++++-- 3 files changed, 16 insertions(+), 6 deletions(-) (limited to 'src/components/volumemeter.cpp') diff --git a/src/components/volumemeter.cpp b/src/components/volumemeter.cpp index 82f47a5..380af55 100644 --- a/src/components/volumemeter.cpp +++ b/src/components/volumemeter.cpp @@ -17,6 +17,10 @@ void VolumeMeter::SetTick(double fraction) { queue_draw(); } +void VolumeMeter::SetShowTick(bool show) { + m_show_tick = show; +} + Gtk::SizeRequestMode VolumeMeter::get_request_mode_vfunc() const { return Gtk::Widget::get_request_mode_vfunc(); } @@ -109,11 +113,13 @@ bool VolumeMeter::on_draw(const Cairo::RefPtr &cr) { cr->rectangle(0.0, 0.0, draw_low, height); cr->fill(); - const double tick_base = width * m_tick; + if (m_show_tick) { + 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(); + 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 index e9656b6..83e0861 100644 --- a/src/components/volumemeter.hpp +++ b/src/components/volumemeter.hpp @@ -7,6 +7,7 @@ public: void SetVolume(double fraction); void SetTick(double fraction); + void SetShowTick(bool show); protected: Gtk::SizeRequestMode get_request_mode_vfunc() const override; @@ -26,4 +27,5 @@ private: double m_fraction = 0.0; double m_tick = 0.0; + bool m_show_tick = false; }; diff --git a/src/windows/voicewindow.cpp b/src/windows/voicewindow.cpp index 69eb969..80a388e 100644 --- a/src/windows/voicewindow.cpp +++ b/src/windows/voicewindow.cpp @@ -50,7 +50,7 @@ public: } void SetVolumeMeter(double frac) { - m_meter.set_fraction(frac); + m_meter.SetVolume(frac); } private: @@ -60,7 +60,7 @@ private: Gtk::Label m_name; Gtk::CheckButton m_mute; Gtk::Scale m_volume; - Gtk::ProgressBar m_meter; + VolumeMeter m_meter; public: using type_signal_mute_cs = sigc::signal; @@ -101,6 +101,8 @@ VoiceWindow::VoiceWindow(Snowflake channel_id) m_scroll.set_hexpand(true); m_scroll.set_vexpand(true); + m_capture_volume.SetShowTick(true); + m_capture_gate.set_range(0.0, 100.0); m_capture_gate.set_value_pos(Gtk::POS_LEFT); m_capture_gate.set_value(0.0); -- cgit v1.2.3