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

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

View File

@@ -72,8 +72,9 @@ bool BluetoothService::getIsDiscovering() {
return discovering;
}
void BluetoothService::setPowerState(bool state) {
void BluetoothService::togglePowerState() {
GError *error = nullptr;
bool state = !this->powerState;
GDBusConnection *connection = g_dbus_proxy_get_connection(this->adapter_proxy);
GVariant *reply = g_dbus_connection_call_sync(
@@ -108,17 +109,11 @@ void BluetoothService::setPowerState(bool state) {
}
}
void BluetoothService::setIsDiscovering(bool state) {
const bool currently_discovering = this->getIsDiscovering();
this->isDiscovering = currently_discovering;
if (currently_discovering == state) {
return;
}
void BluetoothService::toggleIsDiscovering() {
bool newState = !this->isDiscovering;
GError *error = nullptr;
const char *method = state ? "StartDiscovery" : "StopDiscovery";
const char *method = newState ? "StartDiscovery" : "StopDiscovery";
GVariant *reply = g_dbus_proxy_call_sync(
this->adapter_proxy,
method,
@@ -140,7 +135,7 @@ void BluetoothService::setIsDiscovering(bool state) {
return;
}
this->isDiscovering = state;
this->isDiscovering = newState;
if (reply) {
g_variant_unref(reply);
@@ -157,8 +152,10 @@ void BluetoothService::onPropertyChanged(GDBusProxy *proxy,
if (g_variant_lookup(changed_properties, "Powered", "b", &is_powered)) {
this->setIsDiscovering(false);
isDiscoveringChangedSignal.emit(isDiscovering);
if (!is_powered) {
this->isDiscovering = is_discovering;
isDiscoveringChangedSignal.emit(isDiscovering);
}
this->powerState = is_powered;
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->container.append(this->statusArea);
this->toggleButton = Gtk::make_managed<Gtk::Button>("\ue1a8");
this->toggleButton->add_css_class("bluetooth-toggle-button");
this->toggleButton->set_tooltip_text("Turn Bluetooth Off");
this->toggleButton->signal_clicked().connect([this]() {
const bool newState = !isPowered;
setPowerState(newState);
powerStateChangedSignal.emit(newState);
});
this->toggleButton->add_css_class("toggle-button");
this->powerButton = Gtk::make_managed<Gtk::Button>("\ue1a8");
this->powerButton->set_tooltip_text("Turn Bluetooth Off");
this->powerButton->signal_clicked().connect(sigc::mem_fun(*this, &BluetoothWidget::onPowerButtonClicked));
this->powerButton->add_css_class("toggle-button");
this->scanButton = Gtk::make_managed<Gtk::Button>("\ue1aa");
this->scanButton->set_tooltip_text("Scan for Devices");
this->scanButton->add_css_class("bluetooth-scan-button");
this->scanButton->signal_clicked().connect([this]() {
const bool newState = !isDiscovering;
setIsDiscovering(newState);
isDiscoveringChangedSignal.emit(newState);
});
this->scanButton->add_css_class("toggle-button");
this->scanButton->signal_clicked().connect(sigc::mem_fun(*this, &BluetoothWidget::onScanButtonClicked));
this->statusArea.append(*this->toggleButton);
this->statusArea.append(*this->powerButton);
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) {
this->isPowered = state;
if (state) {
this->toggleButton->set_label("\ue1a8");
this->toggleButton->add_css_class("toggle-button-on");
this->toggleButton->remove_css_class("toggle-button-off");
this->scanButton->set_sensitive(state);
if (!state) {
this->scanButton->add_css_class("toggle-button-disabled");
this->add_css_class("disabled-popover-icon");
this->setIsDiscovering(false);
} else {
this->toggleButton->set_label("\ue1a9");
this->toggleButton->add_css_class("toggle-button-off");
this->toggleButton->remove_css_class("toggle-button-on");
this->scanButton->remove_css_class("toggle-button-disabled");
this->remove_css_class("disabled-popover-icon");
}
this->setIsDiscovering(false);
this->toggleButton(this->powerButton, state);
}
void BluetoothWidget::setIsDiscovering(bool state) {
this->isDiscovering = state;
if (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");
}
this->toggleButton(this->scanButton, state);
}
void BluetoothWidget::update() {