filechooser: Set date and time after cell is a child of filechooserwidget
authorCorey Berla <corey@berla.me>
Tue, 11 Apr 2023 00:58:09 +0000 (17:58 -0700)
committerCorey Berla <corey@berla.me>
Tue, 11 Apr 2023 01:02:35 +0000 (18:02 -0700)
The date/time column relies on the filechooserwidget to format the date
properly.  During bind, the filechoosercell, get the filechooserwidget
ancestor, but now due to changes in the listview, the cell isn't a
child of the filechooserwidget at that point.  Since this is deeply
ingrained into the filechooserwidget, let's keep the same behavior,
but move it to filechoosercell in realize.  Alternatively, we could have
used a signal factory (with the file chooser widget as the user data),
but that would have been a major overhaul.

gtk/gtkfilechoosercell.c
gtk/gtkfilechooserwidget.c
gtk/gtkfilechooserwidgetprivate.h
gtk/ui/gtkfilechooserwidget.ui

index 090d17d1ee2f81228a7a69e7b79bfc197cbe912b..3098d7a2e9a95741b55b00f60d4b07e385017037 100644 (file)
@@ -28,6 +28,7 @@
 #include "gtkgestureclick.h"
 #include "gtkgesturelongpress.h"
 #include "gtkicontheme.h"
+#include "gtklabel.h"
 #include "gtkselectionmodel.h"
 #include "gtkfilechooserutils.h"
 #include "gtkfilechooserwidgetprivate.h"
@@ -39,6 +40,7 @@ struct _GtkFileChooserCell
   GFileInfo *item;
   GtkColumnViewCell *list_item;
 
+  gboolean date_column;
   guint type_format;
 
   gboolean show_time;
@@ -53,7 +55,8 @@ G_DEFINE_TYPE (GtkFileChooserCell, gtk_file_chooser_cell, GTK_TYPE_WIDGET)
 
 enum
 {
-  PROP_POSITION = 1,
+  PROP_DATE_COLUMN = 1,
+  PROP_POSITION,
   PROP_ITEM,
   PROP_SHOW_TIME,
   PROP_LIST_ITEM,
@@ -168,6 +171,23 @@ gtk_file_chooser_cell_realize (GtkWidget *widget)
                                                            GTK_TYPE_FILE_CHOOSER_WIDGET));
 
   g_object_bind_property (impl, "show-time", self, "show-time", G_BINDING_SYNC_CREATE);
+
+  if (self->date_column)
+    {
+      GtkWidget *box;
+      GtkWidget *label;
+      char *text;
+
+      box = gtk_widget_get_first_child (GTK_WIDGET (self));
+      label = gtk_widget_get_first_child (box);
+      text = gtk_file_chooser_widget_get_file_date (self->list_item, self->item);
+      gtk_label_set_text (GTK_LABEL (label), text);
+      g_free (text);
+      label = gtk_widget_get_last_child (box);
+      text = gtk_file_chooser_widget_get_file_time (self->list_item, self->item);
+      gtk_label_set_text (GTK_LABEL (label), text);
+      g_free (text);
+    }
 }
 
 static void
@@ -227,6 +247,10 @@ gtk_file_chooser_cell_set_property (GObject      *object,
 
   switch (prop_id)
     {
+    case PROP_DATE_COLUMN:
+      self->date_column = g_value_get_boolean (value);
+      break;
+
     case PROP_ITEM:
       self->item = g_value_get_object (value);
 
@@ -261,6 +285,10 @@ gtk_file_chooser_cell_get_property (GObject    *object,
 
   switch (prop_id)
     {
+    case PROP_DATE_COLUMN:
+      g_value_set_boolean (value, self->date_column);
+      break;
+
     case PROP_ITEM:
       g_value_set_object (value, self->item);
       break;
@@ -286,6 +314,12 @@ gtk_file_chooser_cell_class_init (GtkFileChooserCellClass *klass)
   object_class->set_property = gtk_file_chooser_cell_set_property;
   object_class->get_property = gtk_file_chooser_cell_get_property;
 
+  g_object_class_install_property (object_class, PROP_DATE_COLUMN,
+                                   g_param_spec_boolean ("date-column", NULL, NULL,
+                                                        FALSE,
+                                                        GTK_PARAM_READWRITE));
+
+
   g_object_class_install_property (object_class, PROP_ITEM,
                                    g_param_spec_object ("item", NULL, NULL,
                                                         G_TYPE_FILE_INFO,
index 92ec6c7ad23df6fd7a50303de5b1f8dbbe82e076..51607dfe873b34540407cdd6c57d48604868b8a9 100644 (file)
@@ -1945,9 +1945,9 @@ files_list_restrict_key_presses (GtkEventControllerKey *controller,
   return GDK_EVENT_PROPAGATE;
 }
 
-static char *
-column_view_get_file_date (GtkColumnViewCell *cell,
-                           GFileInfo        *info)
+char *
+gtk_file_chooser_widget_get_file_date (GtkColumnViewCell *cell,
+                                     GFileInfo        *info)
 {
   GtkFileChooserWidget *impl;
   glong time;
@@ -1981,9 +1981,9 @@ column_view_get_file_display_name (GtkColumnViewCell *cell,
     return NULL;
 }
 
-static char *
-column_view_get_file_time (GtkColumnViewCell *cell,
-                           GFileInfo        *info)
+char *
+gtk_file_chooser_widget_get_file_time (GtkColumnViewCell *cell,
+                                     GFileInfo        *info)
 {
   GtkFileChooserWidget *impl;
   glong time;
@@ -6847,9 +6847,7 @@ gtk_file_chooser_widget_class_init (GtkFileChooserWidgetClass *class)
   gtk_widget_class_bind_template_callback (widget_class, rename_file_name_changed);
   gtk_widget_class_bind_template_callback (widget_class, rename_file_rename_clicked);
   gtk_widget_class_bind_template_callback (widget_class, rename_file_end);
-  gtk_widget_class_bind_template_callback (widget_class, column_view_get_file_date);
   gtk_widget_class_bind_template_callback (widget_class, column_view_get_file_display_name);
-  gtk_widget_class_bind_template_callback (widget_class, column_view_get_file_time);
   gtk_widget_class_bind_template_callback (widget_class, column_view_get_file_type);
   gtk_widget_class_bind_template_callback (widget_class, column_view_get_location);
   gtk_widget_class_bind_template_callback (widget_class, column_view_get_size);
index 319209c58d6c7a98da736a711fa3e62493b863c6..e99e60cbdd6a53fb16ae77af8e45b810d0a8928a 100644 (file)
@@ -22,6 +22,7 @@
 
 #include <glib.h>
 #include "deprecated/gtkfilechooserwidget.h"
+#include "gtkcolumnviewcell.h"
 #include "gtkselectionmodel.h"
 
 G_BEGIN_DECLS
@@ -36,6 +37,14 @@ gtk_file_chooser_widget_should_respond (GtkFileChooserWidget *chooser);
 void
 gtk_file_chooser_widget_initial_focus  (GtkFileChooserWidget *chooser);
 
+char *
+gtk_file_chooser_widget_get_file_date   (GtkColumnViewCell *cell,
+                                       GFileInfo         *info);
+
+char *
+gtk_file_chooser_widget_get_file_time   (GtkColumnViewCell *cell,
+                                       GFileInfo        *info);
+
 GSList *
 gtk_file_chooser_widget_get_selected_files (GtkFileChooserWidget *impl);
 
index c3f193973b166a96f2de83a00939f9f42069cec0..acc3b57e94a6b557f4acdeeb04b850a1519fa84a 100644 (file)
           <lookup name="item">GtkColumnViewCell</lookup>
         </binding>
         <property name="list-item">GtkColumnViewCell</property>
+        <property name="date-column">true</property>
         <child>
           <object class="GtkBox">
             <property name="spacing">6</property>
             </binding>
             <child>
               <object class="GtkLabel">
-                <binding name="label">
-                  <closure type="gchararray" function="column_view_get_file_date">
-                    <lookup name="item">GtkColumnViewCell</lookup>
-                  </closure>
-                </binding>
               </object>
             </child>
             <child>
               <object class="GtkLabel">
                 <property name="visible" bind-source="file_chooser_cell" bind-property="show-time" bind-flags="sync-create"/>
-                <binding name="label">
-                  <closure type="gchararray" function="column_view_get_file_time">
-                    <lookup name="item">GtkColumnViewCell</lookup>
-                  </closure>
-                </binding>
               </object>
             </child>
           </object>