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




commit df3724417b2e85a4c4b711ce98d90a3e64135c2d
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               | 60 +++++++++++++++-----------------
 src/resources/ui/nautilus-app-chooser.ui | 32 +++++++++++------
 2 files changed, 50 insertions(+), 42 deletions(-)
---
diff --git a/src/nautilus-app-chooser.c b/src/nautilus-app-chooser.c
index f9704d689..2a0d9bb9a 100644
--- a/src/nautilus-app-chooser.c
+++ b/src/nautilus-app-chooser.c
@@ -18,8 +18,8 @@ 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 *app_chooser_widget;
 };
@@ -33,31 +33,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 +69,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 +87,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 +129,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 +141,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 +161,9 @@ 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));
+
+    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);
 }
 
 static void
@@ -191,8 +189,8 @@ 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);
 
     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..ef1c648f4 100644
--- a/src/resources/ui/nautilus-app-chooser.ui
+++ b/src/resources/ui/nautilus-app-chooser.ui
@@ -42,21 +42,31 @@
                             </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 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]