[gnome-control-center] bluetooth: Move all UI into the GtkTemplate



commit af1ffd76e11721a36ef8d5345a76eafedfe99432
Author: Robert Ancell <robert ancell canonical com>
Date:   Fri Nov 9 11:34:20 2018 +1300

    bluetooth: Move all UI into the GtkTemplate

 panels/bluetooth/cc-bluetooth-panel.c  | 149 ++++++---------------------
 panels/bluetooth/cc-bluetooth-panel.ui | 182 ++++++++++++++++++++++++++++++++-
 po/POTFILES.in                         |   1 -
 3 files changed, 213 insertions(+), 119 deletions(-)
---
diff --git a/panels/bluetooth/cc-bluetooth-panel.c b/panels/bluetooth/cc-bluetooth-panel.c
index 28f5a8c54..d654b2d9c 100644
--- a/panels/bluetooth/cc-bluetooth-panel.c
+++ b/panels/bluetooth/cc-bluetooth-panel.c
@@ -22,7 +22,6 @@
 #include <config.h>
 #endif
 
-#include <glib/gi18n-lib.h>
 #include <shell/cc-shell.h>
 #include <shell/cc-object-storage.h>
 #include <bluetooth-settings-widget.h>
@@ -30,14 +29,13 @@
 #include "cc-bluetooth-panel.h"
 #include "cc-bluetooth-resources.h"
 
-#define WID(s) GTK_WIDGET (gtk_builder_get_object (self->builder, s))
-
 struct _CcBluetoothPanel {
        CcPanel                  parent_instance;
 
        GtkBox                  *airplane_box;
-       GtkBuilder              *builder;
        GtkBox                  *disabled_box;
+       GtkSwitch               *enable_switch;
+       GtkBox                  *header_box;
        GtkBox                  *hw_airplane_box;
        GtkBox                  *no_devices_box;
        BluetoothSettingsWidget *settings_widget;
@@ -46,7 +44,6 @@ struct _CcBluetoothPanel {
        GCancellable            *cancellable;
 
        /* Killswitch */
-       GtkWidget               *kill_switch_header;
        GDBusProxy              *rfkill;
        GDBusProxy              *properties;
        gboolean                 airplane_mode;
@@ -75,7 +72,6 @@ cc_bluetooth_panel_finalize (GObject *object)
 
        g_clear_object (&self->properties);
        g_clear_object (&self->rfkill);
-       g_clear_object (&self->kill_switch_header);
 
        G_OBJECT_CLASS (cc_bluetooth_panel_parent_class)->finalize (object);
 }
@@ -88,18 +84,16 @@ cc_bluetooth_panel_constructed (GObject *object)
        G_OBJECT_CLASS (cc_bluetooth_panel_parent_class)->constructed (object);
 
        /* add kill switch widgets  */
-       self->kill_switch_header = g_object_ref (WID ("box_power"));
        cc_shell_embed_widget_in_header (cc_panel_get_shell (CC_PANEL (self)),
-                                        self->kill_switch_header, GTK_POS_RIGHT);
-       gtk_widget_show (self->kill_switch_header);
+                                        GTK_WIDGET (self->header_box), GTK_POS_RIGHT);
 }
 
 static void
-power_callback (CcBluetoothPanel *self)
+enable_switch_changed_cb (CcBluetoothPanel *self)
 {
        gboolean state;
 
-       state = gtk_switch_get_active (GTK_SWITCH (WID ("switch_bluetooth")));
+       state = gtk_switch_get_active (self->enable_switch);
        g_debug ("Power switched to %s", state ? "on" : "off");
        g_dbus_proxy_call (self->properties,
                           "Set",
@@ -112,10 +106,9 @@ power_callback (CcBluetoothPanel *self)
 }
 
 static void
-cc_bluetooth_panel_update_power (CcBluetoothPanel *self)
+adapter_status_changed_cb (CcBluetoothPanel *self)
 {
        GtkAlign valign;
-       GObject *toggle;
        gboolean sensitive, powered, change_powered;
        GtkWidget *page;
 
@@ -155,13 +148,12 @@ cc_bluetooth_panel_update_power (CcBluetoothPanel *self)
        }
 
        gtk_widget_set_valign (GTK_WIDGET (self->stack), valign);
-       gtk_widget_set_sensitive (WID ("box_power") , sensitive);
+       gtk_widget_set_sensitive (GTK_WIDGET (self->header_box), sensitive);
 
-       toggle = G_OBJECT (WID ("switch_bluetooth"));
        if (change_powered) {
-               g_signal_handlers_block_by_func (toggle, power_callback, self);
-               gtk_switch_set_active (GTK_SWITCH (toggle), powered);
-               g_signal_handlers_unblock_by_func (toggle, power_callback, self);
+               g_signal_handlers_block_by_func (self->enable_switch, enable_switch_changed_cb, self);
+               gtk_switch_set_active (self->enable_switch, powered);
+               g_signal_handlers_unblock_by_func (self->enable_switch, enable_switch_changed_cb, self);
        }
 
        gtk_stack_set_visible_child (self->stack, page);
@@ -187,11 +179,11 @@ airplane_mode_changed (CcBluetoothPanel *self)
        bluetooth_has_airplane_mode = g_dbus_proxy_get_cached_property (self->rfkill, 
"BluetoothHasAirplaneMode");
        self->has_airplane_mode = g_variant_get_boolean (bluetooth_has_airplane_mode);
 
-       cc_bluetooth_panel_update_power (self);
+       adapter_status_changed_cb (self);
 }
 
 static void
-on_airplane_mode_off_clicked (CcBluetoothPanel *self)
+airplane_mode_off_button_clicked_cb (CcBluetoothPanel *self)
 {
        g_debug ("Airplane Mode Off clicked, disabling airplane mode");
        g_dbus_proxy_call (self->rfkill,
@@ -205,47 +197,9 @@ on_airplane_mode_off_clicked (CcBluetoothPanel *self)
                           NULL, NULL);
 }
 
-static GtkBox *
-add_stack_page (CcBluetoothPanel *self,
-               const char       *icon_name,
-               const char       *message,
-               const char       *explanation)
-{
-       GtkWidget *label, *image, *box;
-       char *str;
-
-       box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
-       gtk_widget_show (box);
-       g_object_set (G_OBJECT (box), "margin-top", 64, "margin-bottom", 64, NULL);
-       g_object_set (G_OBJECT (box), "margin-start", 12, "margin-end", 12, NULL);
-
-       image = gtk_image_new_from_icon_name (icon_name,
-                                             GTK_ICON_SIZE_DIALOG);
-       gtk_widget_show (image);
-       gtk_image_set_pixel_size (GTK_IMAGE (image), 192);
-       gtk_style_context_add_class (gtk_widget_get_style_context (image), "dim-label");
-       gtk_box_pack_start (GTK_BOX (box), image, FALSE, FALSE, 24);
-
-       str = g_strdup_printf ("<span size=\"larger\" weight=\"bold\">%s</span>", message);
-       label = gtk_label_new ("");
-       gtk_widget_show (label);
-       gtk_label_set_markup (GTK_LABEL (label), str);
-       g_free (str);
-       gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
-
-       label = gtk_label_new (explanation);
-       gtk_widget_show (label);
-       gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
-       gtk_box_pack_start (GTK_BOX (box), label, FALSE, FALSE, 0);
-
-       gtk_container_add (GTK_CONTAINER (self->stack), box);
-
-       return GTK_BOX (box);
-}
-
 static void
-panel_changed (CcBluetoothPanel *self,
-              const char       *panel)
+panel_changed_cb (CcBluetoothPanel *self,
+                  const char       *panel)
 {
        CcShell *shell;
        g_autoptr(GError) error = NULL;
@@ -259,32 +213,38 @@ static void
 cc_bluetooth_panel_class_init (CcBluetoothPanelClass *klass)
 {
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
        CcPanelClass *panel_class = CC_PANEL_CLASS (klass);
 
        object_class->constructed = cc_bluetooth_panel_constructed;
        object_class->finalize = cc_bluetooth_panel_finalize;
 
        panel_class->get_help_uri = cc_bluetooth_panel_get_help_uri;
+
+       gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/control-center/bluetooth/cc-bluetooth-panel.ui");
+
+       gtk_widget_class_bind_template_child (widget_class, CcBluetoothPanel, airplane_box);
+       gtk_widget_class_bind_template_child (widget_class, CcBluetoothPanel, disabled_box);
+       gtk_widget_class_bind_template_child (widget_class, CcBluetoothPanel, enable_switch);
+       gtk_widget_class_bind_template_child (widget_class, CcBluetoothPanel, header_box);
+       gtk_widget_class_bind_template_child (widget_class, CcBluetoothPanel, no_devices_box);
+       gtk_widget_class_bind_template_child (widget_class, CcBluetoothPanel, hw_airplane_box);
+       gtk_widget_class_bind_template_child (widget_class, CcBluetoothPanel, settings_widget);
+       gtk_widget_class_bind_template_child (widget_class, CcBluetoothPanel, stack);
+
+       gtk_widget_class_bind_template_callback (widget_class, adapter_status_changed_cb);
+       gtk_widget_class_bind_template_callback (widget_class, airplane_mode_off_button_clicked_cb);
+       gtk_widget_class_bind_template_callback (widget_class, enable_switch_changed_cb);
+       gtk_widget_class_bind_template_callback (widget_class, panel_changed_cb);
 }
 
 static void
 cc_bluetooth_panel_init (CcBluetoothPanel *self)
 {
-       GError *error = NULL;
-       GtkWidget *button;
-
+       bluetooth_settings_widget_get_type ();
        g_resources_register (cc_bluetooth_get_resource ());
 
-       self->builder = gtk_builder_new ();
-       gtk_builder_set_translation_domain (self->builder, GETTEXT_PACKAGE);
-       gtk_builder_add_from_resource (self->builder,
-                                       "/org/gnome/control-center/bluetooth/cc-bluetooth-panel.ui",
-                                       &error);
-       if (error != NULL) {
-               g_warning ("Could not load ui: %s", error->message);
-               g_error_free (error);
-               return;
-       }
+       gtk_widget_init_template (GTK_WIDGET (self));
 
        self->cancellable = g_cancellable_new ();
 
@@ -302,48 +262,7 @@ cc_bluetooth_panel_init (CcBluetoothPanel *self)
                                                                     "org.freedesktop.DBus.Properties",
                                                                     NULL, NULL);
 
-       self->stack = GTK_STACK (gtk_stack_new ());
-       gtk_stack_set_homogeneous (self->stack, TRUE);
-       self->no_devices_box = add_stack_page (self,
-                                              "bluetooth-active-symbolic",
-                                              _("No Bluetooth Found"),
-                                              _("Plug in a dongle to use Bluetooth."));
-       self->disabled_box = add_stack_page (self,
-                                            "bluetooth-active-symbolic",
-                                            _("Bluetooth Turned Off"),
-                                            _("Turn on to connect devices and receive file transfers."));
-       self->airplane_box = add_stack_page (self,
-                                            "airplane-mode-symbolic",
-                                            _("Airplane Mode is on"),
-                                            _("Bluetooth is disabled when airplane mode is on."));
-       self->hw_airplane_box = add_stack_page (self,
-                                               "airplane-mode-symbolic",
-                                               _("Hardware Airplane Mode is on"),
-                                               _("Turn off the Airplane mode switch to enable Bluetooth."));
-
-       button = gtk_button_new_with_label (_("Turn Off Airplane Mode"));
-       gtk_widget_show (button);
-       gtk_widget_set_valign (button, GTK_ALIGN_CENTER);
-       gtk_widget_set_halign (button, GTK_ALIGN_CENTER);
-       g_signal_connect_swapped (G_OBJECT (button), "clicked",
-                                 G_CALLBACK (on_airplane_mode_off_clicked), self);
-       gtk_box_pack_start (self->airplane_box, button, FALSE, FALSE, 24);
-
-       self->settings_widget = BLUETOOTH_SETTINGS_WIDGET (bluetooth_settings_widget_new ());
-       g_signal_connect_swapped (G_OBJECT (self->settings_widget), "panel-changed",
-                                 G_CALLBACK (panel_changed), self);
-       gtk_container_add (GTK_CONTAINER (self->stack), GTK_WIDGET (self->settings_widget));
-       gtk_widget_show (GTK_WIDGET (self->settings_widget));
-       gtk_widget_show (GTK_WIDGET (self->stack));
-
-       gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->stack));
-
        airplane_mode_changed (self);
        g_signal_connect_object (self->rfkill, "g-properties-changed",
-                                 G_CALLBACK (airplane_mode_changed), self, G_CONNECT_SWAPPED);
-       g_signal_connect_object (G_OBJECT (self->settings_widget), "adapter-status-changed",
-                                 G_CALLBACK (cc_bluetooth_panel_update_power), self, G_CONNECT_SWAPPED);
-
-       g_signal_connect_swapped (G_OBJECT (WID ("switch_bluetooth")), "notify::active",
-                                 G_CALLBACK (power_callback), self);
+                                G_CALLBACK (airplane_mode_changed), self, G_CONNECT_SWAPPED);
 }
diff --git a/panels/bluetooth/cc-bluetooth-panel.ui b/panels/bluetooth/cc-bluetooth-panel.ui
index 8ed2156e7..c4f36cecb 100644
--- a/panels/bluetooth/cc-bluetooth-panel.ui
+++ b/panels/bluetooth/cc-bluetooth-panel.ui
@@ -1,16 +1,192 @@
 <?xml version="1.0" encoding="UTF-8"?>
-<!-- Generated with glade 3.16.0 on Fri Nov 29 16:46:49 2013 -->
 <interface>
   <!-- interface-requires gtk+ 3.0 -->
-  <object class="GtkBox" id="box_power">
+  <template class="CcBluetoothPanel" parent="CcPanel">
+    <property name="visible">True</property>
+    <child>
+      <object class="GtkStack" id="stack">
+        <property name="visible">True</property>
+        <child>
+          <object class="GtkBox" id="no_devices_box">
+            <property name="visible">True</property>
+            <property name="orientation">vertical</property>
+            <property name="margin_top">64</property>
+            <property name="margin_bottom">64</property>
+            <property name="margin_start">12</property>
+            <property name="margin_end">12</property>
+            <property name="spacing">24</property>
+            <child>
+              <object class="GtkImage">
+                <property name="visible">True</property>
+                <property name="icon_name">bluetooth-active-symbolic</property>
+                <property name="pixel_size">192</property>
+                <style>
+                  <class name="dim-label"/>
+                </style>
+              </object>
+            </child>
+            <child>
+              <object class="GtkLabel">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">No Bluetooth Found</property>
+                <attributes>
+                  <attribute name="weight" value="bold"/>
+                  <attribute name="scale" value="1.2"/>
+                </attributes>
+              </object>
+            </child>
+            <child>
+              <object class="GtkLabel">
+                <property name="visible">True</property>
+                <property name="wrap">True</property>
+                <property name="label" translatable="yes">Plug in a dongle to use Bluetooth.</property>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkBox" id="disabled_box">
+            <property name="visible">True</property>
+            <property name="orientation">vertical</property>
+            <property name="margin_top">64</property>
+            <property name="margin_bottom">64</property>
+            <property name="margin_start">12</property>
+            <property name="margin_end">12</property>
+            <property name="spacing">24</property>
+            <child>
+              <object class="GtkImage">
+                <property name="visible">True</property>
+                <property name="icon_name">bluetooth-active-symbolic</property>
+                <property name="pixel_size">192</property>
+                <style>
+                  <class name="dim-label"/>
+                </style>
+              </object>
+            </child>
+            <child>
+              <object class="GtkLabel">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">Bluetooth Turned Off</property>
+                <attributes>
+                  <attribute name="weight" value="bold"/>
+                  <attribute name="scale" value="1.2"/>
+                </attributes>
+              </object>
+            </child>
+            <child>
+              <object class="GtkLabel">
+                <property name="visible">True</property>
+                <property name="wrap">True</property>
+                <property name="label" translatable="yes">Turn on to connect devices and receive file 
transfers.</property>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkBox" id="airplane_box">
+            <property name="visible">True</property>
+            <property name="orientation">vertical</property>
+            <property name="margin_top">64</property>
+            <property name="margin_bottom">64</property>
+            <property name="margin_start">12</property>
+            <property name="margin_end">12</property>
+            <property name="spacing">24</property>
+            <child>
+              <object class="GtkImage">
+                <property name="visible">True</property>
+                <property name="icon_name">airplane-mode-symbolic</property>
+                <property name="pixel_size">192</property>
+                <style>
+                  <class name="dim-label"/>
+                </style>
+              </object>
+            </child>
+            <child>
+              <object class="GtkLabel">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">Airplane Mode is on</property>
+                <attributes>
+                  <attribute name="weight" value="bold"/>
+                  <attribute name="scale" value="1.2"/>
+                </attributes>
+              </object>
+            </child>
+            <child>
+              <object class="GtkLabel">
+                <property name="visible">True</property>
+                <property name="wrap">True</property>
+                <property name="label" translatable="yes">Bluetooth is disabled when airplane mode is 
on.</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkButton">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">Turn Off Airplane Mode</property>
+                <property name="halign">center</property>
+                <property name="valign">center</property>
+                <signal name="clicked" handler="airplane_mode_off_button_clicked_cb" 
object="CcBluetoothPanel" swapped="yes"/>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkBox" id="hw_airplane_box">
+            <property name="visible">True</property>
+            <property name="orientation">vertical</property>
+            <property name="margin_top">64</property>
+            <property name="margin_bottom">64</property>
+            <property name="margin_start">12</property>
+            <property name="margin_end">12</property>
+            <property name="spacing">24</property>
+            <child>
+              <object class="GtkImage">
+                <property name="visible">True</property>
+                <property name="icon_name">airplane-mode-symbolic</property>
+                <property name="pixel_size">192</property>
+                <style>
+                  <class name="dim-label"/>
+                </style>
+              </object>
+            </child>
+            <child>
+              <object class="GtkLabel">
+                <property name="visible">True</property>
+                <property name="label" translatable="yes">Hardware Airplane Mode is on</property>
+                <attributes>
+                  <attribute name="weight" value="bold"/>
+                  <attribute name="scale" value="1.2"/>
+                </attributes>
+              </object>
+            </child>
+            <child>
+              <object class="GtkLabel">
+                <property name="visible">True</property>
+                <property name="wrap">True</property>
+                <property name="label" translatable="yes">Turn off the Airplane mode switch to enable 
Bluetooth.</property>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="BluetoothSettingsWidget" id="settings_widget">
+            <property name="visible">True</property>
+            <signal name="panel-changed" handler="panel_changed_cb" object="CcBluetoothPanel" swapped="yes"/>
+            <signal name="adapter-status-changed" handler="adapter_status_changed_cb" 
object="CcBluetoothPanel" swapped="yes"/>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+  <object class="GtkBox" id="header_box">
     <property name="visible">True</property>
     <property name="can_focus">False</property>
     <child>
-      <object class="GtkSwitch" id="switch_bluetooth">
+      <object class="GtkSwitch" id="enable_switch">
         <property name="use_action_appearance">False</property>
         <property name="visible">True</property>
         <property name="can_focus">True</property>
         <property name="valign">center</property>
+        <signal name="notify::active" handler="enable_switch_changed_cb" object="CcBluetoothPanel" 
swapped="yes"/>
       </object>
       <packing>
         <property name="expand">False</property>
diff --git a/po/POTFILES.in b/po/POTFILES.in
index 806afb4a4..4a75e2d0d 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -11,7 +11,6 @@ panels/background/cc-background-panel.c
 panels/background/cc-background-panel.ui
 panels/background/cc-background-preview.ui
 panels/background/gnome-background-panel.desktop.in.in
-panels/bluetooth/cc-bluetooth-panel.c
 panels/bluetooth/cc-bluetooth-panel.ui
 panels/bluetooth/gnome-bluetooth-panel.desktop.in.in
 panels/color/cc-color-calibrate.c


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