diff --git a/src/panel/meson.build b/src/panel/meson.build index cb7d1fbb0..65c9793eb 100644 --- a/src/panel/meson.build +++ b/src/panel/meson.build @@ -38,7 +38,6 @@ deps = [ if libpulse.found() widget_sources += [ 'widgets/volume.cpp', - 'widgets/volume-level.cpp', ] deps += [libpulse, libgvc] else @@ -50,7 +49,6 @@ if wireplumber.found() 'widgets/wp-mixer/wp-mixer.cpp', 'widgets/wp-mixer/wf-wp-control.cpp', 'widgets/wp-mixer/wp-common.cpp', - 'widgets/volume-level.cpp', ] deps += [pipewire, wireplumber] else diff --git a/src/panel/widgets/volume-level.cpp b/src/panel/widgets/volume-level.cpp deleted file mode 100644 index 64f92be30..000000000 --- a/src/panel/widgets/volume-level.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include - -#include "volume-level.hpp" - -enum VolumeLevel -{ - VOLUME_LEVEL_MUTE = 0, - VOLUME_LEVEL_LOW, - VOLUME_LEVEL_MED, - VOLUME_LEVEL_HIGH, - VOLUME_LEVEL_OOR, /* Out of range */ -}; - -const std::map volume_icons = { - {VOLUME_LEVEL_MUTE, "audio-volume-muted"}, - {VOLUME_LEVEL_LOW, "audio-volume-low"}, - {VOLUME_LEVEL_MED, "audio-volume-medium"}, - {VOLUME_LEVEL_HIGH, "audio-volume-high"}, - {VOLUME_LEVEL_OOR, "emblem-unreadable"}, -}; - -// volume is expected to be from 0 to 1 -std::string volume_icon_for(double volume) -{ - double max = 1.0; - auto third = max / 3; - if (volume == 0) - { - return volume_icons.at(VOLUME_LEVEL_MUTE); - } else if ((volume > 0) && (volume <= third)) - { - return volume_icons.at(VOLUME_LEVEL_LOW); - } else if ((volume > third) && (volume <= (third * 2))) - { - return volume_icons.at(VOLUME_LEVEL_MED); - } else if ((volume > (third * 2)) && (volume <= max)) - { - return volume_icons.at(VOLUME_LEVEL_HIGH); - } - - return volume_icons.at(VOLUME_LEVEL_OOR); -} diff --git a/src/panel/widgets/volume-level.hpp b/src/panel/widgets/volume-level.hpp deleted file mode 100644 index 6647133c6..000000000 --- a/src/panel/widgets/volume-level.hpp +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -#include - -// helper in common for volume and wireplumber widgets icons handling -std::string volume_icon_for(double volume); diff --git a/src/panel/widgets/volume.cpp b/src/panel/widgets/volume.cpp index 601968bf4..1aca27247 100644 --- a/src/panel/widgets/volume.cpp +++ b/src/panel/widgets/volume.cpp @@ -1,18 +1,20 @@ #include #include + #include "volume.hpp" +#include "icon-select.hpp" -#include "volume-level.hpp" +#define ICON(volume) icon_from_range(volume_icons, volume) void WayfireVolume::update_icon() { if (gvc_stream && gvc_mixer_stream_get_is_muted(gvc_stream)) { - main_image.set_from_icon_name(volume_icon_for(0)); // mute + main_image.set_from_icon_name(ICON(0)); // mute return; } - main_image.set_from_icon_name(volume_icon_for(volume_scale.get_target_value() / (double)max_norm)); + main_image.set_from_icon_name(ICON(volume_scale.get_target_value() / (double)max_norm)); } bool WayfireVolume::on_popover_timeout(int timer) diff --git a/src/panel/widgets/wp-mixer/wf-wp-control.cpp b/src/panel/widgets/wp-mixer/wf-wp-control.cpp index ffaa25e09..02401509a 100644 --- a/src/panel/widgets/wp-mixer/wf-wp-control.cpp +++ b/src/panel/widgets/wp-mixer/wf-wp-control.cpp @@ -2,7 +2,9 @@ #include #include "wf-wp-control.hpp" -#include "../volume-level.hpp" +#include "icon-select.hpp" + +#define ICON(volume) icon_from_range(volume_icons, volume) WfWpControl::WfWpControl(WpPipewireObject *obj, WayfireWpMixer *parent_widget) { @@ -143,12 +145,12 @@ void WfWpControl::update_icon() if (button.get_active()) { add_css_class("muted"); - volume_icon.set_from_icon_name(volume_icon_for(0)); // mute + volume_icon.set_from_icon_name(ICON(0)); // mute return; } remove_css_class("muted"); - volume_icon.set_from_icon_name(volume_icon_for(get_scale_target_value())); + volume_icon.set_from_icon_name(ICON(get_scale_target_value())); } double WfWpControl::get_scale_target_value() diff --git a/src/panel/widgets/wp-mixer/wp-mixer.cpp b/src/panel/widgets/wp-mixer/wp-mixer.cpp index 8e0f39896..797cc7255 100644 --- a/src/panel/widgets/wp-mixer/wp-mixer.cpp +++ b/src/panel/widgets/wp-mixer/wp-mixer.cpp @@ -4,7 +4,9 @@ #include "wp-mixer.hpp" #include "wf-wp-control.hpp" -#include "../volume-level.hpp" +#include "icon-select.hpp" + +#define ICON(volume) icon_from_range(volume_icons, volume) bool WayfireWpMixer::on_popover_timeout(int timer) { @@ -332,17 +334,17 @@ void WayfireWpMixer::update_icon() // depends on quick_target widget if (!quick_target) { - main_image.set_from_icon_name(volume_icon_for(-1)); // OOR + main_image.set_from_icon_name(ICON(-1)); // OOR return; } if (quick_target->button.get_active()) { - main_image.set_from_icon_name(volume_icon_for(0)); // mute + main_image.set_from_icon_name(ICON(0)); // mute return; } - main_image.set_from_icon_name(volume_icon_for(quick_target->get_scale_target_value())); + main_image.set_from_icon_name(ICON(quick_target->get_scale_target_value())); } void WayfireWpMixer::set_quick_target_from(WfWpControl *from) diff --git a/src/util/icon-select.cpp b/src/util/icon-select.cpp new file mode 100644 index 000000000..678354a65 --- /dev/null +++ b/src/util/icon-select.cpp @@ -0,0 +1,14 @@ +#include "icon-select.hpp" + +std::string icon_from_range(std::map icons, double value) +{ + for (auto pair : icons) + { + if (value <= pair.first) + { + return pair.second; + } + } + + return ""; +} diff --git a/src/util/icon-select.hpp b/src/util/icon-select.hpp new file mode 100644 index 000000000..9b9b0269d --- /dev/null +++ b/src/util/icon-select.hpp @@ -0,0 +1,17 @@ +#include +#include +#include + +std::string icon_from_range(std::map icons, double value); + +// the number in the first term is the maximal value at which this icon will be shown +// selection values of the tables are expected to be ordered from least to greatest + +const std::map volume_icons = { + {std::numeric_limits::min(), "emblem-unreadable"}, + {0.0, "audio-volume-muted"}, + {1.0 / 3, "audio-volume-low"}, + {(1.0 / 3) * 2, "audio-volume-medium"}, + {1.0, "audio-volume-high"}, + {std::numeric_limits::max(), "audio-volume-high-danger-symbolic"} +}; diff --git a/src/util/meson.build b/src/util/meson.build index f50eb58f7..7c56112fb 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -2,6 +2,7 @@ util = static_library( 'util', [ 'gtk-utils.cpp', + 'icon-select.cpp', 'wf-shell-app.cpp', 'wf-autohide-window.cpp', 'wf-popover.cpp', @@ -9,6 +10,7 @@ util = static_library( 'wf-ipc.cpp', 'animated-scale.cpp', 'background-gl.cpp', + 'icon-select.cpp' ], dependencies: [wf_protos, gtklayershell, wayland_client, gtkmm, wfconfig, libinotify, json], )