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




commit 6ff6efc6fcc96916712039d03234b80895a8a8c5
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               | 69 ++++++++++++++++++--------------
 src/resources/ui/nautilus-app-chooser.ui | 34 ++++++++++------
 2 files changed, 60 insertions(+), 43 deletions(-)
---
diff --git a/src/nautilus-app-chooser.c b/src/nautilus-app-chooser.c
index f9704d689..24a006aa7 100644
--- a/src/nautilus-app-chooser.c
+++ b/src/nautilus-app-chooser.c
@@ -18,8 +18,9 @@ struct _NautilusAppChooser
     gchar *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;
 };
@@ -33,31 +34,26 @@ enum
     LAST_PROP
 };
 
-static void
-reset_clicked_cb (GtkButton *button,
-                  gpointer   user_data)
-{
-    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)
+static gboolean
+set_as_default_state_set_cb (GtkSwitch *default_switch,
+                             gboolean   state,
+                             gpointer   user_data)
 {
     NautilusAppChooser *self = NAUTILUS_APP_CHOOSER (user_data);
     g_autoptr (GAppInfo) info = NULL;
     g_autoptr (GError) error = NULL;
 
-    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);
+    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)
     {
@@ -74,8 +70,8 @@ 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");
+    return FALSE;
 }
 
 static void
@@ -92,7 +88,9 @@ 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);
+    g_signal_handlers_block_by_func (self->set_as_default_switch, G_CALLBACK (set_as_default_state_set_cb), 
self);
+    gtk_switch_set_state (GTK_SWITCH (self->set_as_default_switch), is_default);
+    g_signal_handlers_unblock_by_func (self->set_as_default_switch, G_CALLBACK 
(set_as_default_state_set_cb), self);
 }
 
 static void
@@ -132,6 +130,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);
 
@@ -143,11 +142,8 @@ 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),
+    g_signal_connect (self->set_as_default_switch, "state-set",
+                      G_CALLBACK (set_as_default_state_set_cb),
                       self);
 
     /* initialize sensitivity */
@@ -166,6 +162,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
@@ -191,8 +197,9 @@ 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);
 
     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..fc7cc4671 100644
--- a/src/resources/ui/nautilus-app-chooser.ui
+++ b/src/resources/ui/nautilus-app-chooser.ui
@@ -41,22 +41,32 @@
                               </object>
                             </child>
                             <child>
-                              <object class="GtkBox">
-                                <property name="spacing">6</property>
+                              <object class="GtkBox" id="set_default_box">
                                 <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 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="GtkButton" id="set_as_default_button">
-                                    <property name="label" translatable="yes">Set as _Default</property>
-                                    <property name="use-underline">True</property>
+                                  <object class="GtkSwitch" id="set_as_default_switch">
+                                    <property name="halign">end</property>
+                                    <property name="valign">center</property>
                                   </object>
                                 </child>
                               </object>


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