[gnome-bluetooth] settings-widget: Fix connection button state out of sync after disconnect



commit bc0b9faab492f5de75f8df06deb106c95f20d713
Author: Benjamin Berg <bberg redhat com>
Date:   Thu Dec 14 19:57:05 2017 +0100

    settings-widget: Fix connection button state out of sync after disconnect
    
    When disconnecting the switch showing the connected state could get out
    of sync. To fix this, use GtkSwitch feature to keep the state always in
    sync with the connected state and send the message to connect/disconnect
    when "active" is out of sync with "state".
    
    https://bugzilla.gnome.org/show_bug.cgi?id=791726

 lib/bluetooth-settings-widget.c |   31 +++++++++++++++++--------------
 1 files changed, 17 insertions(+), 14 deletions(-)
---
diff --git a/lib/bluetooth-settings-widget.c b/lib/bluetooth-settings-widget.c
index 8695d97..7880418 100644
--- a/lib/bluetooth-settings-widget.c
+++ b/lib/bluetooth-settings-widget.c
@@ -189,10 +189,12 @@ connect_done (GObject      *source_object,
                GtkSwitch *button;
 
                button = GTK_SWITCH (WID ("switch_connection"));
-               /* Reset the switch if it failed */
+
+               /* Ensure the switch position is in the correct place. */
+               gtk_switch_set_active (button, gtk_switch_get_state (button));
+
                if (success == FALSE) {
                        g_debug ("Connection failed to %s: %s", data->bdaddr, error->message);
-                       gtk_switch_set_active (button, !gtk_switch_get_active (button));
                }
                set_connecting_page (self, CONNECTING_NOTEBOOK_PAGE_SWITCH);
        }
@@ -1029,16 +1031,19 @@ start_pairing (BluetoothSettingsWidget *self,
        g_object_unref (proxy);
 }
 
-static void
-switch_connected_active_changed (GtkSwitch               *button,
-                                GParamSpec              *spec,
-                                BluetoothSettingsWidget *self)
+static gboolean
+switch_connected_state_set (GtkSwitch               *button,
+                           gboolean                 state,
+                           BluetoothSettingsWidget *self)
 {
        ConnectData *data;
        BluetoothSettingsWidgetPrivate *priv = BLUETOOTH_SETTINGS_WIDGET_GET_PRIVATE (self);
 
+       if (gtk_switch_get_state (button) == state)
+               return TRUE;
+
        if (is_connecting (self, priv->selected_bdaddr))
-               return;
+               return TRUE;
 
        data = g_new0 (ConnectData, 1);
        data->bdaddr = g_strdup (priv->selected_bdaddr);
@@ -1053,6 +1058,8 @@ switch_connected_active_changed (GtkSwitch               *button,
 
        add_connecting (self, data->bdaddr);
        set_connecting_page (self, CONNECTING_NOTEBOOK_PAGE_SPINNER);
+
+       return TRUE;
 }
 
 static void
@@ -1126,18 +1133,14 @@ update_properties (BluetoothSettingsWidget *self,
 
        /* Connection */
        button = GTK_SWITCH (WID ("switch_connection"));
-       g_signal_handlers_block_by_func (button, switch_connected_active_changed, self);
 
+       gtk_switch_set_state (button, connected);
        if (is_connecting (self, bdaddr)) {
-               gtk_switch_set_active (button, TRUE);
                set_connecting_page (self, CONNECTING_NOTEBOOK_PAGE_SPINNER);
        } else {
-               gtk_switch_set_active (button, connected);
                set_connecting_page (self, CONNECTING_NOTEBOOK_PAGE_SWITCH);
        }
 
-       g_signal_handlers_unblock_by_func (button, switch_connected_active_changed, self);
-
        /* Paired */
        gtk_label_set_text (GTK_LABEL (WID ("paired_label")),
                            paired ? _("Yes") : _("No"));
@@ -1779,8 +1782,8 @@ setup_properties_dialog (BluetoothSettingsWidget *self)
                          G_CALLBACK (sound_callback), self);
        g_signal_connect (G_OBJECT (WID ("send_button")), "clicked",
                          G_CALLBACK (send_callback), self);
-       g_signal_connect (G_OBJECT (WID ("switch_connection")), "notify::active",
-                         G_CALLBACK (switch_connected_active_changed), self);
+       g_signal_connect (G_OBJECT (WID ("switch_connection")), "state-set",
+                         G_CALLBACK (switch_connected_state_set), self);
 
        /* Styling */
        gtk_image_set_pixel_size (GTK_IMAGE (WID ("image")), ICON_SIZE);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]