[epiphany] EphyEncodingDialog: Use a GtkSwitch.



commit ddc54f46926924e7a2ef5856e0d198ffd6001ee8
Author: Arnaud Bonatti <arnaud bonatti gmail com>
Date:   Tue Dec 8 12:21:12 2015 +0100

    EphyEncodingDialog: Use a GtkSwitch.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=759164

 src/ephy-encoding-dialog.c       |  101 ++++++++++++++++++++++----------------
 src/resources/encoding-dialog.ui |   75 ++++++++--------------------
 2 files changed, 79 insertions(+), 97 deletions(-)
---
diff --git a/src/ephy-encoding-dialog.c b/src/ephy-encoding-dialog.c
index 61b5e12..8348b24 100644
--- a/src/ephy-encoding-dialog.c
+++ b/src/ephy-encoding-dialog.c
@@ -47,8 +47,7 @@ struct _EphyEncodingDialog
        char *selected_encoding;
 
        /* from the UI file */
-       GtkRadioButton    *automatic_button;
-       GtkRadioButton    *manual_button;
+       GtkSwitch         *default_switch;
        GtkScrolledWindow *scrolled_window;
 };
 
@@ -106,13 +105,9 @@ select_encoding_row (GtkTreeView *view, EphyEncoding *encoding)
 static void
 sync_encoding_against_embed (EphyEncodingDialog *dialog)
 {
-       GtkTreeSelection *selection;
-       GtkTreeModel *model;
-       GList *rows;
        const char *encoding;
        gboolean is_automatic = FALSE;
        WebKitWebView *view;
-       EphyEncoding *node;
 
        dialog->update_tag = TRUE;
 
@@ -120,33 +115,42 @@ sync_encoding_against_embed (EphyEncodingDialog *dialog)
        view = EPHY_GET_WEBKIT_WEB_VIEW_FROM_EMBED (dialog->embed);
 
        encoding = webkit_web_view_get_custom_charset (view);
-       if (encoding == NULL) goto out;
+       is_automatic = encoding == NULL;
+
+       if (!is_automatic)
+       {
+               EphyEncoding *node;
+               GtkTreeSelection *selection;
+               GtkTreeModel *model;
+               GList *rows;
 
-       node = ephy_encodings_get_encoding (dialog->encodings, encoding, TRUE);
-       g_assert (EPHY_IS_ENCODING (node));
+               node = ephy_encodings_get_encoding (dialog->encodings, encoding, TRUE);
+               g_assert (EPHY_IS_ENCODING (node));
 
-       /* Select the current encoding in the list view. */
-       select_encoding_row (GTK_TREE_VIEW (dialog->enc_view), node);
+               /* Select the current encoding in the list view. */
+               select_encoding_row (GTK_TREE_VIEW (dialog->enc_view), node);
 
-       /* scroll the view so the active encoding is visible */
-        selection = gtk_tree_view_get_selection
-                (GTK_TREE_VIEW (dialog->enc_view));
-       model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->enc_view));
-        rows = gtk_tree_selection_get_selected_rows (selection, &model);
-        if (rows != NULL)
-       {
-               gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (dialog->enc_view),
-                                             (GtkTreePath *) rows->data,
-                                             NULL, /* column */
-                                             TRUE,
-                                             0.5,
-                                             0.0);
-               g_list_foreach (rows, (GFunc)gtk_tree_path_free, NULL);
-               g_list_free (rows);
+               /* scroll the view so the active encoding is visible */
+               selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->enc_view));
+               model = gtk_tree_view_get_model (GTK_TREE_VIEW (dialog->enc_view));
+               rows = gtk_tree_selection_get_selected_rows (selection, &model);
+
+               if (rows != NULL)
+               {
+                       gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (dialog->enc_view),
+                                                     (GtkTreePath *) rows->data,
+                                                     NULL, /* column */
+                                                     TRUE,
+                                                     0.5,
+                                                     0.0);
+                       g_list_foreach (rows, (GFunc)gtk_tree_path_free, NULL);
+                       g_list_free (rows);
+               }
        }
+       gtk_switch_set_active (dialog->default_switch, is_automatic);
+       gtk_switch_set_state (dialog->default_switch, is_automatic);
+       gtk_widget_set_sensitive (GTK_WIDGET (dialog->scrolled_window), !is_automatic);
 
-       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->automatic_button), is_automatic);
-out:
        dialog->update_tag = FALSE;
 }
 
@@ -205,16 +209,12 @@ ephy_encoding_dialog_sync_embed (EphyWindow *window, GParamSpec *pspec, EphyEnco
 static void
 activate_choice (EphyEncodingDialog *dialog)
 {
-       gboolean is_automatic;
        WebKitWebView *view;
 
        g_return_if_fail (EPHY_IS_EMBED (dialog->embed));
-
-       is_automatic = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->automatic_button));
-
        view = EPHY_GET_WEBKIT_WEB_VIEW_FROM_EMBED (dialog->embed);
 
-       if (is_automatic)
+       if (gtk_switch_get_active (dialog->default_switch))
        {
                webkit_web_view_set_custom_charset (view, NULL);
        }
@@ -261,19 +261,36 @@ view_row_activated_cb (GtkTreeView *treeview,
        if (dialog->update_tag)
                return;
 
-       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->manual_button), TRUE);
-
        activate_choice (dialog);
 }
 
-static void
-automatic_toggled_cb (GtkToggleButton *button, EphyEncodingDialog *dialog)
+static gboolean
+default_switch_toggled_cb (GtkSwitch          *default_switch,
+                           gboolean            state,
+                           EphyEncodingDialog *dialog)
 {
-       if (gtk_toggle_button_get_active (button)
-           && dialog->update_tag == FALSE)
+       if (dialog->update_tag)
        {
+               gtk_switch_set_state (default_switch, !state);
+       }
+       else if (state)
+       {
+               gtk_switch_set_state (default_switch, TRUE);
+
+               if (dialog->selected_encoding != NULL)
+               {
+                       g_free (dialog->selected_encoding);
+                       dialog->selected_encoding = NULL;
+               }
                activate_choice (dialog);
        }
+       else
+       {
+               gtk_switch_set_state (default_switch, FALSE);
+
+               // TODO select safe default in list, or find another solution
+       }
+       return TRUE;
 }
 
 static void
@@ -288,6 +305,7 @@ ephy_encoding_dialog_init (EphyEncodingDialog *dialog)
 
        gtk_widget_init_template (GTK_WIDGET (dialog));
 
+       dialog->selected_encoding = NULL;
        dialog->encodings =
                EPHY_ENCODINGS (ephy_embed_shell_get_encodings
                                (EPHY_EMBED_SHELL (ephy_shell_get_default ())));
@@ -437,11 +455,10 @@ ephy_encoding_dialog_class_init (EphyEncodingDialogClass *klass)
        /* load from UI file */
        gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/epiphany/encoding-dialog.ui");
 
-       gtk_widget_class_bind_template_child (widget_class, EphyEncodingDialog, automatic_button);
-       gtk_widget_class_bind_template_child (widget_class, EphyEncodingDialog, manual_button);
+       gtk_widget_class_bind_template_child (widget_class, EphyEncodingDialog, default_switch);
        gtk_widget_class_bind_template_child (widget_class, EphyEncodingDialog, scrolled_window);
 
-       gtk_widget_class_bind_template_callback (widget_class, automatic_toggled_cb);
+       gtk_widget_class_bind_template_callback (widget_class, default_switch_toggled_cb);
        gtk_widget_class_bind_template_callback (widget_class, ephy_encoding_dialog_response_cb);
 }
 
diff --git a/src/resources/encoding-dialog.ui b/src/resources/encoding-dialog.ui
index 7f2fca7..5dcfb74 100644
--- a/src/resources/encoding-dialog.ui
+++ b/src/resources/encoding-dialog.ui
@@ -2,11 +2,11 @@
 <interface>
   <!-- interface-requires gtk+ 3.0 -->
   <template class="EphyEncodingDialog" parent="GtkDialog">
-    <property name="border_width">5</property>
+    <property name="border_width">2</property>
     <property name="role">epiphany-encoding-chooser</property>
     <property name="modal">True</property>
     <property name="resizable">False</property>
-    <property name="width_request">400</property>
+    <property name="width_request">500</property>
     <property name="height_request">420</property>
     <property name="type_hint">dialog</property>
     <property name="use_header_bar">1</property>
@@ -14,75 +14,40 @@
     <signal name="response" handler="ephy_encoding_dialog_response_cb"/>
     <child internal-child="vbox">
       <object class="GtkBox">
-        <property name="spacing">18</property>
+        <property name="margin">4</property>
+        <property name="spacing">6</property>
         <child>
-          <object class="GtkBox">
+          <object class="GtkGrid">
             <property name="visible">True</property>
-            <property name="orientation">vertical</property>
-            <property name="spacing">6</property>
+            <property name="hexpand">True</property>
+            <property name="orientation">horizontal</property>
             <child>
-              <object class="GtkRadioButton" id="automatic_button">
+              <object class="GtkLabel">
                 <property name="visible">True</property>
-                <property name="use_underline">True</property>
+                <property name="hexpand">True</property>
                 <property name="halign">start</property>
-                <signal name="toggled" handler="automatic_toggled_cb"/>
-                <child>
-                  <object class="GtkLabel">
-                    <property name="visible">True</property>
-                    <property name="label" translatable="yes">_Automatic</property>
-                    <property name="use_underline">True</property>
-                    <attributes>
-                      <attribute name="weight" value="bold"/>
-                    </attributes>
-                  </object>
-                </child>
+                <property name="label" translatable="yes">Use the encoding specified by the 
document</property> <!-- TODO document default fallback -->
+                <property name="margin-left">6</property>
               </object>
             </child>
             <child>
-              <object class="GtkLabel">
+              <object class="GtkSwitch" id="default_switch">
                 <property name="visible">True</property>
-                <property name="halign">start</property>
-                <property name="margin_start">5</property>
-                <property name="label" translatable="yes">Use the encoding specified by the 
document</property>
-                <property name="wrap">True</property>
+                <signal name="state-set" handler="default_switch_toggled_cb"/>
               </object>
             </child>
           </object>
         </child>
         <child>
-          <object class="GtkBox">
+          <object class="GtkScrolledWindow" id="scrolled_window">
             <property name="visible">True</property>
-            <property name="orientation">vertical</property>
-            <property name="spacing">6</property>
-            <child>
-              <object class="GtkRadioButton" id="manual_button">
-                <property name="visible">True</property>
-                <property name="use_underline">True</property>
-                <property name="halign">start</property>
-                <property name="group">automatic_button</property>
-                <child>
-                  <object class="GtkLabel">
-                    <property name="visible">True</property>
-                    <property name="label" translatable="yes">_Use a different encoding:</property>
-                    <property name="use_underline">True</property>
-                    <attributes>
-                      <attribute name="weight" value="bold"/>
-                    </attributes>
-                  </object>
-                </child>
-              </object>
-            </child>
+            <property name="expand">True</property>
+            <property name="hscrollbar_policy">never</property>
+            <property name="shadow_type">in</property>
+            <property name="min_content_height">300</property>
+            <property name="sensitive" bind-source="default_switch" bind-property="active" 
bind-flags="invert-boolean"/>
             <child>
-              <object class="GtkScrolledWindow" id="scrolled_window">
-                <property name="visible">True</property>
-                <property name="expand">True</property>
-                <property name="hscrollbar_policy">never</property>
-                <property name="shadow_type">in</property>
-                <property name="min_content_height">300</property>
-                <child>
-                  <placeholder/>
-                </child>
-              </object>
+              <placeholder/>
             </child>
           </object>
         </child>


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