[nautilus/wip/coreyberla/app-picker-followups: 62/67] app-chooser: Replace Reset and Set as Default buttons with a Switch




commit 8fe25c418c9d3c8321385cd91f000e433e113147
Author: Corey Berla <corey berla me>
Date:   Fri Aug 5 12:48:59 2022 -0700

    app-chooser: Replace Reset and Set as Default buttons with a Switch
    
    Set the switch to active when the association is set as default
    and inactive when it is not set as default (also a way to remove
    the default association).  Add a label for the switch with a
    subtitle indicating the content description.

 src/nautilus-app-chooser.c               | 72 ++++++++++++++++++--------------
 src/resources/ui/nautilus-app-chooser.ui | 66 ++++++++++++++++++-----------
 2 files changed, 82 insertions(+), 56 deletions(-)
---
diff --git a/src/nautilus-app-chooser.c b/src/nautilus-app-chooser.c
index 599e1c38d..63a8b302a 100644
--- a/src/nautilus-app-chooser.c
+++ b/src/nautilus-app-chooser.c
@@ -20,8 +20,9 @@ struct _NautilusAppChooser
     gboolean single_content_type;
 
     GtkWidget *app_chooser_widget_box;
-    GtkWidget *reset_button;
-    GtkWidget *set_as_default_button;
+    GtkWidget *label_content_type_description;
+    GtkWidget *set_as_default_switch;
+    GtkWidget *set_default_box;
 
     GtkWidget *app_chooser_widget;
 };
@@ -37,30 +38,32 @@ enum
 };
 
 static void
-reset_clicked_cb (GtkButton *button,
-                  gpointer   user_data)
+open_button_clicked_cb (GtkButton          *button,
+                        NautilusAppChooser *self)
 {
-    NautilusAppChooser *self = NAUTILUS_APP_CHOOSER (user_data);
-
-    g_app_info_reset_type_associations (self->content_type);
-    gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->app_chooser_widget));
-    gtk_widget_set_sensitive (self->reset_button, FALSE);
-
-    g_signal_emit_by_name (nautilus_signaller_get_current (), "mime-data-changed");
-}
-
-static void
-set_as_default_clicked_cb (GtkButton *button,
-                           gpointer   user_data)
-{
-    NautilusAppChooser *self = NAUTILUS_APP_CHOOSER (user_data);
+    gboolean state;
     g_autoptr (GAppInfo) info = NULL;
     g_autoptr (GError) error = NULL;
 
-    info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->app_chooser_widget));
+    state = gtk_switch_get_active (GTK_SWITCH (self->set_as_default_switch));
+
+    if (!self->single_content_type)
+    {
+        /* Don't attempt to set an association with multiple content types */
+        return;
+    }
 
-    g_app_info_set_as_default_for_type (info, self->content_type,
-                                        &error);
+    if (state)
+    {
+        info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->app_chooser_widget));
+        g_app_info_set_as_default_for_type (info, self->content_type,
+                                            &error);
+    }
+    else
+    {
+        g_app_info_reset_type_associations (self->content_type);
+        gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->app_chooser_widget));
+    }
 
     if (error != NULL)
     {
@@ -77,7 +80,6 @@ set_as_default_clicked_cb (GtkButton *button,
     }
 
     gtk_app_chooser_refresh (GTK_APP_CHOOSER (self->app_chooser_widget));
-    gtk_widget_set_sensitive (self->reset_button, TRUE);
     g_signal_emit_by_name (nautilus_signaller_get_current (), "mime-data-changed");
 }
 
@@ -95,7 +97,7 @@ on_application_selected (GtkAppChooserWidget *widget,
     default_app = g_app_info_get_default_for_type (self->content_type, FALSE);
     is_default = default_app != NULL && g_app_info_equal (info, default_app);
 
-    gtk_widget_set_sensitive (self->set_as_default_button, !is_default);
+    gtk_switch_set_state (GTK_SWITCH (self->set_as_default_switch), is_default);
 }
 
 static void
@@ -141,6 +143,7 @@ nautilus_app_chooser_constructed (GObject *object)
 {
     NautilusAppChooser *self = NAUTILUS_APP_CHOOSER (object);
     g_autoptr (GAppInfo) info = NULL;
+    g_autofree gchar *content_type_description = NULL;
 
     G_OBJECT_CLASS (nautilus_app_chooser_parent_class)->constructed (object);
 
@@ -152,12 +155,6 @@ nautilus_app_chooser_constructed (GObject *object)
     gtk_app_chooser_widget_set_show_default (GTK_APP_CHOOSER_WIDGET (self->app_chooser_widget), TRUE);
     gtk_app_chooser_widget_set_show_fallback (GTK_APP_CHOOSER_WIDGET (self->app_chooser_widget), TRUE);
     gtk_app_chooser_widget_set_show_other (GTK_APP_CHOOSER_WIDGET (self->app_chooser_widget), TRUE);
-    g_signal_connect (self->reset_button, "clicked",
-                      G_CALLBACK (reset_clicked_cb),
-                      self);
-    g_signal_connect (self->set_as_default_button, "clicked",
-                      G_CALLBACK (set_as_default_clicked_cb),
-                      self);
 
     /* initialize sensitivity */
     info = gtk_app_chooser_get_app_info (GTK_APP_CHOOSER (self->app_chooser_widget));
@@ -175,6 +172,16 @@ nautilus_app_chooser_constructed (GObject *object)
     gtk_header_bar_set_title_widget (GTK_HEADER_BAR (gtk_dialog_get_header_bar (GTK_DIALOG (self))),
                                      adw_window_title_new (gtk_window_get_title (GTK_WINDOW (self)),
                                                            self->content_type));
+
+    if (self->single_content_type)
+    {
+        content_type_description = g_content_type_get_description (self->content_type);
+        gtk_label_set_label (GTK_LABEL (self->label_content_type_description), content_type_description);
+    }
+    else
+    {
+        gtk_widget_set_visible (self->set_default_box, FALSE);
+    }
 }
 
 static void
@@ -200,8 +207,11 @@ nautilus_app_chooser_class_init (NautilusAppChooserClass *klass)
     gtk_widget_class_set_template_from_resource (widget_class, 
"/org/gnome/nautilus/ui/nautilus-app-chooser.ui");
 
     gtk_widget_class_bind_template_child (widget_class, NautilusAppChooser, app_chooser_widget_box);
-    gtk_widget_class_bind_template_child (widget_class, NautilusAppChooser, reset_button);
-    gtk_widget_class_bind_template_child (widget_class, NautilusAppChooser, set_as_default_button);
+    gtk_widget_class_bind_template_child (widget_class, NautilusAppChooser, set_as_default_switch);
+    gtk_widget_class_bind_template_child (widget_class, NautilusAppChooser, label_content_type_description);
+    gtk_widget_class_bind_template_child (widget_class, NautilusAppChooser, set_default_box);
+
+    gtk_widget_class_bind_template_callback (widget_class, open_button_clicked_cb);
 
     g_object_class_install_property (object_class,
                                      PROP_CONTENT_TYPE,
diff --git a/src/resources/ui/nautilus-app-chooser.ui b/src/resources/ui/nautilus-app-chooser.ui
index 5c2eb95f5..b99623822 100644
--- a/src/resources/ui/nautilus-app-chooser.ui
+++ b/src/resources/ui/nautilus-app-chooser.ui
@@ -19,14 +19,14 @@
                 <property name="name">list</property>
                 <property name="child">
                   <object class="GtkScrolledWindow">
+                    <property name="margin-top">18</property>
+                    <property name="margin-bottom">18</property>
+                    <property name="margin-start">18</property>
+                    <property name="margin-end">18</property>
                     <property name="hscrollbar-policy">never</property>
                     <property name="vexpand">true</property>
                     <property name="child">
                       <object class="AdwClamp">
-                        <property name="margin-top">12</property>
-                        <property name="margin-bottom">12</property>
-                        <property name="margin-start">12</property>
-                        <property name="margin-end">12</property>
                         <style>
                           <class name="background"/>
                         </style>
@@ -36,31 +36,12 @@
                             <property name="spacing">12</property>
                             <child>
                               <object class="GtkBox" id="app_chooser_widget_box">
+                                <property name="margin-start">18</property>
+                                <property name="margin-end">18</property>
                                 <property name="vexpand">True</property>
                                 <property name="orientation">vertical</property>
                               </object>
                             </child>
-                            <child>
-                              <object class="GtkBox">
-                                <property name="spacing">6</property>
-                                <child>
-                                  <object class="GtkButton" id="reset_button">
-                                    <property name="label" translatable="yes">_Reset</property>
-                                    <property name="use-underline">True</property>
-                                    <property name="focusable">True</property>
-                                    <property name="receives_default">True</property>
-                                    <property name="hexpand">True</property>
-                                    <property name="halign">start</property>
-                                  </object>
-                                </child>
-                                <child>
-                                  <object class="GtkButton" id="set_as_default_button">
-                                    <property name="label" translatable="yes">Set as _Default</property>
-                                    <property name="use-underline">True</property>
-                                  </object>
-                                </child>
-                              </object>
-                            </child>
                           </object>
                         </property>
                       </object>
@@ -71,6 +52,40 @@
             </child>
           </object>
         </child>
+        <child>
+          <object class="GtkBox" id="set_default_box">
+            <property name="margin-bottom">18</property>
+            <property name="margin-start">18</property>
+            <property name="margin-end">18</property>
+            <child>
+              <object class="GtkBox">
+                <property name="orientation">vertical</property>
+                <child>
+                  <object class="GtkLabel">
+                    <property name="halign">start</property>
+                    <property name="hexpand">True</property>
+                    <property name="label" translatable="yes">Always use for this file type</property>
+                  </object>
+                </child>
+                <child>
+                  <object class="GtkLabel" id="label_content_type_description">
+                    <property name="halign">start</property>
+                    <style>
+                      <class name="dim-label"/>
+                      <class name="caption"/>
+                    </style>
+                  </object>
+                </child>
+              </object>
+            </child>
+            <child>
+              <object class="GtkSwitch" id="set_as_default_switch">
+                <property name="halign">end</property>
+                <property name="valign">center</property>
+              </object>
+            </child>
+          </object>
+        </child>
       </object>
     </child>
     <child type="action">
@@ -84,6 +99,7 @@
         <property name="label" translatable="yes">_Open</property>
         <property name="use-underline">True</property>
         <property name="sensitive">False</property>
+        <signal name="clicked" object="NautilusAppChooser" handler="open_button_clicked_cb" swapped="no"/>
       </object>
     </child>
     <action-widgets>


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