optimized the code, added some bugs :)
This commit is contained in:
@@ -17,15 +17,10 @@ VolumeWidget::VolumeWidget() : Gtk::Box(Gtk::Orientation::HORIZONTAL) {
|
||||
|
||||
append(label);
|
||||
|
||||
// Click toggles mute using wpctl
|
||||
click = Gtk::GestureClick::create();
|
||||
click->set_button(GDK_BUTTON_PRIMARY);
|
||||
// signal_released provides (int, double, double) — use lambda to ignore
|
||||
// args
|
||||
click->signal_released().connect([this](int /*n_press*/, double /*x*/,
|
||||
double /*y*/) {
|
||||
click->signal_released().connect([this](int, double, double) {
|
||||
try {
|
||||
// Toggle mute then refresh
|
||||
(void)SystemHelper::get_command_output(
|
||||
"wpctl set-mute @DEFAULT_SINK@ toggle");
|
||||
} catch (const std::exception &ex) {
|
||||
@@ -34,19 +29,17 @@ VolumeWidget::VolumeWidget() : Gtk::Box(Gtk::Orientation::HORIZONTAL) {
|
||||
}
|
||||
this->update();
|
||||
});
|
||||
add_controller(click);
|
||||
|
||||
// Initial read
|
||||
add_controller(click);
|
||||
update();
|
||||
|
||||
// Start polling every 1 second to keep the display up to date
|
||||
timeoutConn = Glib::signal_timeout().connect(
|
||||
this->timeoutConn = Glib::signal_timeout().connect(
|
||||
sigc::mem_fun(*this, &VolumeWidget::on_timeout), 100);
|
||||
}
|
||||
|
||||
VolumeWidget::~VolumeWidget() {
|
||||
if (timeoutConn.connected())
|
||||
timeoutConn.disconnect();
|
||||
if (this->timeoutConn.connected())
|
||||
this->timeoutConn.disconnect();
|
||||
}
|
||||
|
||||
void VolumeWidget::update() {
|
||||
@@ -54,7 +47,6 @@ void VolumeWidget::update() {
|
||||
const std::string out =
|
||||
SystemHelper::get_command_output("wpctl get-volume @DEFAULT_SINK@");
|
||||
|
||||
// Attempt to parse a number (percentage or fraction)
|
||||
std::smatch m;
|
||||
std::regex r_percent(R"((\d+(?:\.\d+)?)%)");
|
||||
std::regex r_number(R"((\d+(?:\.\d+)?))");
|
||||
@@ -65,7 +57,6 @@ void VolumeWidget::update() {
|
||||
if (std::regex_search(text, m, r_percent)) {
|
||||
percent = static_cast<int>(std::round(std::stod(m[1].str())));
|
||||
} else if (std::regex_search(text, m, r_number)) {
|
||||
// If number looks like 0.8 treat as fraction
|
||||
const double v = std::stod(m[1].str());
|
||||
if (v <= 1.0)
|
||||
percent = static_cast<int>(std::round(v * 100.0));
|
||||
@@ -76,7 +67,6 @@ void VolumeWidget::update() {
|
||||
if (percent >= 0) {
|
||||
label.set_text(std::to_string(percent) + "%");
|
||||
} else {
|
||||
// Fallback to raw output (trimmed)
|
||||
auto pos = text.find_first_not_of(" \t\n\r");
|
||||
if (pos != std::string::npos) {
|
||||
auto end = text.find_last_not_of(" \t\n\r");
|
||||
@@ -94,5 +84,6 @@ void VolumeWidget::update() {
|
||||
|
||||
bool VolumeWidget::on_timeout() {
|
||||
update();
|
||||
|
||||
return true; // keep timeout active
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user