nicer button interactivities

This commit is contained in:
2025-12-22 02:32:18 +01:00
parent 5a429a3b8b
commit 8613024f8d
6 changed files with 73 additions and 55 deletions

View File

@@ -13,10 +13,11 @@ class BluetoothService {
sigc::signal<void(bool)> isDiscoveringChangedSignal; sigc::signal<void(bool)> isDiscoveringChangedSignal;
bool getPowerState(); bool getPowerState();
void setPowerState(bool state);
bool getIsDiscovering(); bool getIsDiscovering();
void setIsDiscovering(bool state);
void togglePowerState();
void toggleIsDiscovering();
private: private:
GDBusProxy *adapter_proxy = nullptr; GDBusProxy *adapter_proxy = nullptr;

View File

@@ -13,8 +13,8 @@ class BluetoothWidget : public Popover {
void setPowerState(bool state); void setPowerState(bool state);
void setIsDiscovering(bool state); void setIsDiscovering(bool state);
sigc::signal<void(bool)> powerStateChangedSignal; sigc::signal<void()> onPowerStateButtonClickedSignal;
sigc::signal<void(bool)> isDiscoveringChangedSignal; sigc::signal<void()> onIsDiscoveringButtonClickedSignal;
void update(); void update();
private: private:
@@ -27,5 +27,11 @@ class BluetoothWidget : public Popover {
Gtk::Box *deviceList = nullptr; Gtk::Box *deviceList = nullptr;
Gtk::Button *scanButton = nullptr; Gtk::Button *scanButton = nullptr;
Gtk::Button *toggleButton = nullptr; Gtk::Button *powerButton = nullptr;
void onPowerButtonClicked();
void onScanButtonClicked();
void toggleButton(Gtk::Button *button, bool state);
}; };

View File

@@ -136,3 +136,13 @@ tooltip {
background-color: rgba(244, 67, 54, 0.2); background-color: rgba(244, 67, 54, 0.2);
border: 1px solid #f44336; border: 1px solid #f44336;
} }
.toggle-button-disabled {
background-color: rgba(100, 100, 100, 0.2);
border: 1px solid #666666;
color: #888888;
}
.disabled-popover-icon {
color: #888888;
}

View File

@@ -44,14 +44,14 @@ Bar::Bar(GdkMonitor *monitor, HyprlandService &hyprlandService,
bluetoothService.powerStateChangedSignal.connect( bluetoothService.powerStateChangedSignal.connect(
sigc::mem_fun(*this->bluetoothWidget, &BluetoothWidget::setPowerState)); sigc::mem_fun(*this->bluetoothWidget, &BluetoothWidget::setPowerState));
bluetoothWidget->powerStateChangedSignal.connect( bluetoothWidget->onPowerStateButtonClickedSignal.connect(
sigc::mem_fun(bluetoothService, &BluetoothService::setPowerState)); sigc::mem_fun(bluetoothService, &BluetoothService::togglePowerState));
bluetoothWidget->setPowerState(bluetoothService.getPowerState()); bluetoothWidget->setPowerState(bluetoothService.getPowerState());
bluetoothService.isDiscoveringChangedSignal.connect( bluetoothService.isDiscoveringChangedSignal.connect(
sigc::mem_fun(*this->bluetoothWidget, &BluetoothWidget::setIsDiscovering)); sigc::mem_fun(*this->bluetoothWidget, &BluetoothWidget::setIsDiscovering));
bluetoothWidget->isDiscoveringChangedSignal.connect( bluetoothWidget->onIsDiscoveringButtonClickedSignal.connect(
sigc::mem_fun(bluetoothService, &BluetoothService::setIsDiscovering)); sigc::mem_fun(bluetoothService, &BluetoothService::toggleIsDiscovering));
bluetoothWidget->setIsDiscovering(bluetoothService.getIsDiscovering()); bluetoothWidget->setIsDiscovering(bluetoothService.getIsDiscovering());
load_css(); load_css();

View File

@@ -72,8 +72,9 @@ bool BluetoothService::getIsDiscovering() {
return discovering; return discovering;
} }
void BluetoothService::setPowerState(bool state) { void BluetoothService::togglePowerState() {
GError *error = nullptr; GError *error = nullptr;
bool state = !this->powerState;
GDBusConnection *connection = g_dbus_proxy_get_connection(this->adapter_proxy); GDBusConnection *connection = g_dbus_proxy_get_connection(this->adapter_proxy);
GVariant *reply = g_dbus_connection_call_sync( GVariant *reply = g_dbus_connection_call_sync(
@@ -108,17 +109,11 @@ void BluetoothService::setPowerState(bool state) {
} }
} }
void BluetoothService::setIsDiscovering(bool state) { void BluetoothService::toggleIsDiscovering() {
const bool currently_discovering = this->getIsDiscovering(); bool newState = !this->isDiscovering;
this->isDiscovering = currently_discovering;
if (currently_discovering == state) {
return;
}
GError *error = nullptr; GError *error = nullptr;
const char *method = state ? "StartDiscovery" : "StopDiscovery"; const char *method = newState ? "StartDiscovery" : "StopDiscovery";
GVariant *reply = g_dbus_proxy_call_sync( GVariant *reply = g_dbus_proxy_call_sync(
this->adapter_proxy, this->adapter_proxy,
method, method,
@@ -140,7 +135,7 @@ void BluetoothService::setIsDiscovering(bool state) {
return; return;
} }
this->isDiscovering = state; this->isDiscovering = newState;
if (reply) { if (reply) {
g_variant_unref(reply); g_variant_unref(reply);
@@ -157,8 +152,10 @@ void BluetoothService::onPropertyChanged(GDBusProxy *proxy,
if (g_variant_lookup(changed_properties, "Powered", "b", &is_powered)) { if (g_variant_lookup(changed_properties, "Powered", "b", &is_powered)) {
this->setIsDiscovering(false); if (!is_powered) {
this->isDiscovering = is_discovering;
isDiscoveringChangedSignal.emit(isDiscovering); isDiscoveringChangedSignal.emit(isDiscovering);
}
this->powerState = is_powered; this->powerState = is_powered;
powerStateChangedSignal.emit(powerState); powerStateChangedSignal.emit(powerState);

View File

@@ -13,55 +13,59 @@ BluetoothWidget::BluetoothWidget(std::string icon, std::string title) : Popover(
this->statusArea.set_halign(Gtk::Align::FILL); this->statusArea.set_halign(Gtk::Align::FILL);
this->container.append(this->statusArea); this->container.append(this->statusArea);
this->toggleButton = Gtk::make_managed<Gtk::Button>("\ue1a8"); this->powerButton = Gtk::make_managed<Gtk::Button>("\ue1a8");
this->toggleButton->add_css_class("bluetooth-toggle-button"); this->powerButton->set_tooltip_text("Turn Bluetooth Off");
this->toggleButton->set_tooltip_text("Turn Bluetooth Off"); this->powerButton->signal_clicked().connect(sigc::mem_fun(*this, &BluetoothWidget::onPowerButtonClicked));
this->toggleButton->signal_clicked().connect([this]() { this->powerButton->add_css_class("toggle-button");
const bool newState = !isPowered;
setPowerState(newState);
powerStateChangedSignal.emit(newState);
});
this->toggleButton->add_css_class("toggle-button");
this->scanButton = Gtk::make_managed<Gtk::Button>("\ue1aa"); this->scanButton = Gtk::make_managed<Gtk::Button>("\ue1aa");
this->scanButton->set_tooltip_text("Scan for Devices"); this->scanButton->set_tooltip_text("Scan for Devices");
this->scanButton->add_css_class("bluetooth-scan-button"); this->scanButton->add_css_class("toggle-button");
this->scanButton->signal_clicked().connect([this]() { this->scanButton->signal_clicked().connect(sigc::mem_fun(*this, &BluetoothWidget::onScanButtonClicked));
const bool newState = !isDiscovering;
setIsDiscovering(newState);
isDiscoveringChangedSignal.emit(newState);
});
this->statusArea.append(*this->toggleButton); this->statusArea.append(*this->powerButton);
this->statusArea.append(*this->scanButton); this->statusArea.append(*this->scanButton);
} }
void BluetoothWidget::onPowerButtonClicked() {
onPowerStateButtonClickedSignal.emit();
}
void BluetoothWidget::onScanButtonClicked() {
onIsDiscoveringButtonClickedSignal.emit();
}
void BluetoothWidget::toggleButton(Gtk::Button *button, bool state) {
if (state) {
button->add_css_class("toggle-button-on");
button->remove_css_class("toggle-button-off");
} else {
button->add_css_class("toggle-button-off");
button->remove_css_class("toggle-button-on");
}
}
void BluetoothWidget::setPowerState(bool state) { void BluetoothWidget::setPowerState(bool state) {
this->isPowered = state; this->isPowered = state;
if (state) { this->scanButton->set_sensitive(state);
this->toggleButton->set_label("\ue1a8");
this->toggleButton->add_css_class("toggle-button-on"); if (!state) {
this->toggleButton->remove_css_class("toggle-button-off"); this->scanButton->add_css_class("toggle-button-disabled");
this->add_css_class("disabled-popover-icon");
this->setIsDiscovering(false);
} else { } else {
this->toggleButton->set_label("\ue1a9"); this->scanButton->remove_css_class("toggle-button-disabled");
this->toggleButton->add_css_class("toggle-button-off"); this->remove_css_class("disabled-popover-icon");
this->toggleButton->remove_css_class("toggle-button-on");
} }
this->setIsDiscovering(false); this->toggleButton(this->powerButton, state);
} }
void BluetoothWidget::setIsDiscovering(bool state) { void BluetoothWidget::setIsDiscovering(bool state) {
this->isDiscovering = state; this->isDiscovering = state;
if (state) { this->toggleButton(this->scanButton, state);
this->scanButton->add_css_class("toggle-button-on");
this->scanButton->remove_css_class("toggle-button-off");
} else {
this->scanButton->add_css_class("toggle-button-off");
this->scanButton->remove_css_class("toggle-button-on");
}
} }
void BluetoothWidget::update() { void BluetoothWidget::update() {