[nautilus/wip/antoniof/new-list-view-without-expanders: 3/20] view-model: Use GtkSorter instead of custom struct
- From: António Fernandes <antoniof src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [nautilus/wip/antoniof/new-list-view-without-expanders: 3/20] view-model: Use GtkSorter instead of custom struct
- Date: Fri, 10 Jun 2022 19:05:41 +0000 (UTC)
commit c60a4977aac9f4db2ce036365cc8c6e0089cd1f8
Author: António Fernandes <antoniof gnome org>
Date: Thu Feb 10 13:59:50 2022 +0000
view-model: Use GtkSorter instead of custom struct
This prepares for GtkColumnView sorting.
src/nautilus-view-icon-controller.c | 44 ++++++++++++++++-------
src/nautilus-view-model.c | 72 +++++++++++++++++++------------------
src/nautilus-view-model.h | 12 ++-----
3 files changed, 71 insertions(+), 57 deletions(-)
---
diff --git a/src/nautilus-view-icon-controller.c b/src/nautilus-view-icon-controller.c
index 43c710ece..f755c384d 100644
--- a/src/nautilus-view-icon-controller.c
+++ b/src/nautilus-view-icon-controller.c
@@ -31,6 +31,10 @@ struct _NautilusViewIconController
guint scroll_to_file_handle_id;
guint prioritize_thumbnailing_handle_id;
GtkAdjustment *vadjustment;
+
+ NautilusFileSortType sort_type;
+ gboolean directories_first;
+ gboolean reversed;
};
G_DEFINE_TYPE (NautilusViewIconController, nautilus_view_icon_controller, NAUTILUS_TYPE_FILES_VIEW)
@@ -236,6 +240,24 @@ update_sort_order_from_metadata_and_preferences (NautilusViewIconController *sel
g_variant_new_string (get_sorts_constants_from_sort_type
(default_directory_sort->sort_type, default_directory_sort->reversed)->action_target_name));
}
+static gint
+nautilus_view_icon_controller_sort (gconstpointer a,
+ gconstpointer b,
+ gpointer user_data)
+{
+ NautilusViewIconController *self = user_data;
+ NautilusFile *file_a;
+ NautilusFile *file_b;
+
+ file_a = nautilus_view_item_model_get_file (NAUTILUS_VIEW_ITEM_MODEL ((gpointer) a));
+ file_b = nautilus_view_item_model_get_file (NAUTILUS_VIEW_ITEM_MODEL ((gpointer) b));
+
+ return nautilus_file_compare_for_sort (file_a, file_b,
+ self->sort_type,
+ self->directories_first,
+ self->reversed);
+}
+
static void
real_begin_loading (NautilusFilesView *files_view)
{
@@ -1149,17 +1171,14 @@ real_scroll_to_file (NautilusFilesView *files_view,
static void
real_sort_directories_first_changed (NautilusFilesView *files_view)
{
- NautilusViewModelSortData sort_data;
- NautilusViewModelSortData *current_sort_data;
NautilusViewIconController *self;
+ g_autoptr (GtkCustomSorter) sorter = NULL;
self = NAUTILUS_VIEW_ICON_CONTROLLER (files_view);
- current_sort_data = nautilus_view_model_get_sort_type (self->model);
- sort_data.sort_type = current_sort_data->sort_type;
- sort_data.reversed = current_sort_data->reversed;
- sort_data.directories_first = nautilus_files_view_should_sort_directories_first (NAUTILUS_FILES_VIEW
(self));
+ self->directories_first = nautilus_files_view_should_sort_directories_first (NAUTILUS_FILES_VIEW (self));
- nautilus_view_model_set_sort_type (self->model, &sort_data);
+ sorter = gtk_custom_sorter_new (nautilus_view_icon_controller_sort, self, NULL);
+ nautilus_view_model_set_sorter (self->model, GTK_SORTER (sorter));
}
static void
@@ -1169,8 +1188,8 @@ action_sort_order_changed (GSimpleAction *action,
{
const gchar *target_name;
const SortConstants *sort_constants;
- NautilusViewModelSortData sort_data;
NautilusViewIconController *self;
+ g_autoptr (GtkCustomSorter) sorter = NULL;
/* Don't resort if the action is in the same state as before */
if (g_strcmp0 (g_variant_get_string (value, NULL), g_variant_get_string (g_action_get_state (G_ACTION
(action)), NULL)) == 0)
@@ -1181,11 +1200,12 @@ action_sort_order_changed (GSimpleAction *action,
self = NAUTILUS_VIEW_ICON_CONTROLLER (user_data);
target_name = g_variant_get_string (value, NULL);
sort_constants = get_sorts_constants_from_action_target_name (target_name);
- sort_data.sort_type = sort_constants->sort_type;
- sort_data.reversed = sort_constants->reversed;
- sort_data.directories_first = nautilus_files_view_should_sort_directories_first (NAUTILUS_FILES_VIEW
(self));
+ self->sort_type = sort_constants->sort_type;
+ self->reversed = sort_constants->reversed;
+ self->directories_first = nautilus_files_view_should_sort_directories_first (NAUTILUS_FILES_VIEW (self));
- nautilus_view_model_set_sort_type (self->model, &sort_data);
+ sorter = gtk_custom_sorter_new (nautilus_view_icon_controller_sort, self, NULL);
+ nautilus_view_model_set_sorter (self->model, GTK_SORTER (sorter));
set_directory_sort_metadata (nautilus_files_view_get_directory_as_file (NAUTILUS_FILES_VIEW (self)),
sort_constants);
diff --git a/src/nautilus-view-model.c b/src/nautilus-view-model.c
index 69cdeb0f8..2d4deee02 100644
--- a/src/nautilus-view-model.c
+++ b/src/nautilus-view-model.c
@@ -9,7 +9,8 @@ struct _NautilusViewModel
GHashTable *map_files_to_model;
GListStore *internal_model;
GtkMultiSelection *selection_model;
- NautilusViewModelSortData *sort_data;
+ GtkSorter *sorter;
+ gulong sorter_changed_handler_id;
};
static GType
@@ -116,7 +117,7 @@ G_DEFINE_TYPE_WITH_CODE (NautilusViewModel, nautilus_view_model, G_TYPE_OBJECT,
enum
{
PROP_0,
- PROP_SORT_TYPE,
+ PROP_SORTER,
N_PROPS
};
@@ -143,6 +144,8 @@ dispose (GObject *object)
self->internal_model = NULL;
}
+ g_clear_signal_handler (&self->sorter_changed_handler_id, self->sorter);
+
G_OBJECT_CLASS (nautilus_view_model_parent_class)->dispose (object);
}
@@ -154,10 +157,7 @@ finalize (GObject *object)
G_OBJECT_CLASS (nautilus_view_model_parent_class)->finalize (object);
g_hash_table_destroy (self->map_files_to_model);
- if (self->sort_data)
- {
- g_free (self->sort_data);
- }
+ g_clear_object (&self->sorter);
}
static void
@@ -170,9 +170,9 @@ get_property (GObject *object,
switch (prop_id)
{
- case PROP_SORT_TYPE:
+ case PROP_SORTER:
{
- g_value_set_object (value, self->sort_data);
+ g_value_set_object (value, self->sorter);
}
break;
@@ -193,9 +193,9 @@ set_property (GObject *object,
switch (prop_id)
{
- case PROP_SORT_TYPE:
+ case PROP_SORTER:
{
- nautilus_view_model_set_sort_type (self, g_value_get_object (value));
+ nautilus_view_model_set_sorter (self, g_value_get_object (value));
}
break;
@@ -233,6 +233,13 @@ nautilus_view_model_class_init (NautilusViewModelClass *klass)
object_class->get_property = get_property;
object_class->set_property = set_property;
object_class->constructed = constructed;
+
+ g_object_class_install_property (object_class,
+ PROP_SORTER,
+ g_param_spec_object ("sorter",
+ NULL, NULL,
+ GTK_TYPE_SORTER,
+ G_PARAM_READWRITE));
}
static void
@@ -246,16 +253,20 @@ compare_data_func (gconstpointer a,
gpointer user_data)
{
NautilusViewModel *self = NAUTILUS_VIEW_MODEL (user_data);
- NautilusFile *file_a;
- NautilusFile *file_b;
- file_a = nautilus_view_item_model_get_file (NAUTILUS_VIEW_ITEM_MODEL ((gpointer) a));
- file_b = nautilus_view_item_model_get_file (NAUTILUS_VIEW_ITEM_MODEL ((gpointer) b));
+ g_return_val_if_fail (self->sorter != NULL, GTK_ORDERING_EQUAL);
- return nautilus_file_compare_for_sort (file_a, file_b,
- self->sort_data->sort_type,
- self->sort_data->directories_first,
- self->sort_data->reversed);
+ return gtk_sorter_compare (self->sorter, (gpointer) a, (gpointer) b);
+}
+
+static void
+on_sorter_changed (GtkSorter *sorter,
+ GtkSorterChange change,
+ gpointer user_data)
+{
+ NautilusViewModel *self = NAUTILUS_VIEW_MODEL (user_data);
+
+ g_list_store_sort (self->internal_model, compare_data_func, self);
}
NautilusViewModel *
@@ -265,28 +276,19 @@ nautilus_view_model_new ()
}
void
-nautilus_view_model_set_sort_type (NautilusViewModel *self,
- NautilusViewModelSortData *sort_data)
+nautilus_view_model_set_sorter (NautilusViewModel *self,
+ GtkSorter *sorter)
{
- if (self->sort_data)
- {
- g_free (self->sort_data);
- }
-
- self->sort_data = g_new (NautilusViewModelSortData, 1);
- self->sort_data->sort_type = sort_data->sort_type;
- self->sort_data->reversed = sort_data->reversed;
- self->sort_data->directories_first = sort_data->directories_first;
+ g_clear_signal_handler (&self->sorter_changed_handler_id, self->sorter);
+ g_set_object (&self->sorter, sorter);
+ self->sorter_changed_handler_id = g_signal_connect (self->sorter,
+ "changed",
+ G_CALLBACK (on_sorter_changed),
+ self);
g_list_store_sort (self->internal_model, compare_data_func, self);
}
-NautilusViewModelSortData *
-nautilus_view_model_get_sort_type (NautilusViewModel *self)
-{
- return self->sort_data;
-}
-
GQueue *
nautilus_view_model_get_items_from_files (NautilusViewModel *self,
GQueue *files)
diff --git a/src/nautilus-view-model.h b/src/nautilus-view-model.h
index c32e2656e..1771cd6e2 100644
--- a/src/nautilus-view-model.h
+++ b/src/nautilus-view-model.h
@@ -10,18 +10,10 @@ G_BEGIN_DECLS
G_DECLARE_FINAL_TYPE (NautilusViewModel, nautilus_view_model, NAUTILUS, VIEW_MODEL, GObject)
-typedef struct
-{
- NautilusFileSortType sort_type;
- gboolean reversed;
- gboolean directories_first;
-} NautilusViewModelSortData;
-
NautilusViewModel * nautilus_view_model_new (void);
-void nautilus_view_model_set_sort_type (NautilusViewModel *self,
- NautilusViewModelSortData *sort_data);
-NautilusViewModelSortData * nautilus_view_model_get_sort_type (NautilusViewModel *self);
+void nautilus_view_model_set_sorter (NautilusViewModel *self,
+ GtkSorter *sorter);
NautilusViewItemModel * nautilus_view_model_get_item_from_file (NautilusViewModel *self,
NautilusFile *file);
GQueue * nautilus_view_model_get_items_from_files (NautilusViewModel *self,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]