diff --git a/include/services/bluetooth.hpp b/include/services/bluetooth.hpp index ace3100..3a6c361 100644 --- a/include/services/bluetooth.hpp +++ b/include/services/bluetooth.hpp @@ -13,10 +13,11 @@ class BluetoothService { sigc::signal isDiscoveringChangedSignal; bool getPowerState(); - void setPowerState(bool state); bool getIsDiscovering(); - void setIsDiscovering(bool state); + + void togglePowerState(); + void toggleIsDiscovering(); private: GDBusProxy *adapter_proxy = nullptr; diff --git a/include/widgets/bluetooth.hpp b/include/widgets/bluetooth.hpp index c1416ba..ce5049d 100644 --- a/include/widgets/bluetooth.hpp +++ b/include/widgets/bluetooth.hpp @@ -13,9 +13,9 @@ class BluetoothWidget : public Popover { void setPowerState(bool state); void setIsDiscovering(bool state); - sigc::signal powerStateChangedSignal; - sigc::signal isDiscoveringChangedSignal; - + sigc::signal onPowerStateButtonClickedSignal; + sigc::signal onIsDiscoveringButtonClickedSignal; + void update(); private: bool isPowered = false; @@ -27,5 +27,11 @@ class BluetoothWidget : public Popover { Gtk::Box *deviceList = nullptr; Gtk::Button *scanButton = nullptr; - Gtk::Button *toggleButton = nullptr; + Gtk::Button *powerButton = nullptr; + + + void onPowerButtonClicked(); + void onScanButtonClicked(); + + void toggleButton(Gtk::Button *button, bool state); }; \ No newline at end of file diff --git a/resources/bar.css b/resources/bar.css index cdcea46..a28273b 100644 --- a/resources/bar.css +++ b/resources/bar.css @@ -135,4 +135,14 @@ tooltip { .toggle-button-off { background-color: rgba(244, 67, 54, 0.2); 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; } \ No newline at end of file diff --git a/src/bar/bar.cpp b/src/bar/bar.cpp index d5e3342..136081b 100644 --- a/src/bar/bar.cpp +++ b/src/bar/bar.cpp @@ -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(); diff --git a/src/services/bluetooth.cpp b/src/services/bluetooth.cpp index 5eda130..0cedc8b 100644 --- a/src/services/bluetooth.cpp +++ b/src/services/bluetooth.cpp @@ -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); diff --git a/src/widgets/bluetooth.cpp b/src/widgets/bluetooth.cpp index 9636e52..510177f 100644 --- a/src/widgets/bluetooth.cpp +++ b/src/widgets/bluetooth.cpp @@ -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("\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("\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("\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() {