gtk: avoid duplicated g_sort_array fallback code

The previous fallback duplicated the full sort call at each use site,
which was unnecessarily noisy and harder to maintain.

Add a small internal _gtk_sort_array macro instead, using g_sort_array()
when available and g_qsort_with_data() on older GLib versions.
This commit is contained in:
Daemonratte 2026-05-04 08:08:20 +02:00
commit 0675b80da2
3 changed files with 19 additions and 26 deletions

View file

@ -32,6 +32,7 @@
#include "gtktreednd.h"
#include "gtktreemodel.h"
#include "gtkalias.h"
#include "gtkprivate.h"
/*** Structure: how GtkFileSystemModel works
*
@ -801,19 +802,11 @@ gtk_file_system_model_sort (GtkFileSystemModel *model)
n_visible_rows = node_get_tree_row (model, model->files->len - 1) + 1;
model->n_nodes_valid = 0;
g_hash_table_remove_all (model->file_lookup);
#if GLIB_CHECK_VERSION(2,82,0)
g_sort_array (get_node (model, 1), /* start at index 1; don't sort the editable row */
model->files->len - 1,
model->node_size,
compare_array_element,
&data);
#else
g_qsort_with_data (get_node (model, 1), /* start at index 1; don't sort the editable row */
model->files->len - 1,
model->node_size,
compare_array_element,
&data);
#endif
_gtk_sort_array (get_node (model, 1), /* start at index 1; don't sort the editable row */
model->files->len - 1,
model->node_size,
compare_array_element,
&data);
g_assert (model->n_nodes_valid == 0);
g_assert (g_hash_table_size (model->file_lookup) == 0);
if (n_visible_rows)

View file

@ -29,6 +29,13 @@
#include <gtk/gtkwidget.h>
/* Use g_sort_array() when available, otherwise keep the old GTK2 fallback. */
#if GLIB_CHECK_VERSION (2, 82, 0)
#define _gtk_sort_array g_sort_array
#else
#define _gtk_sort_array(a,n,s,c,d) g_qsort_with_data ((a), (gint) (n), (s), (c), (d))
#endif
G_BEGIN_DECLS
/* The private flags that are used in the private_flags member of GtkWidget.

View file

@ -27,6 +27,7 @@
#include "gtkbuildable.h"
#include "gtkintl.h"
#include "gtkalias.h"
#include "gtkprivate.h"
#define G_NODE(node) ((GNode *)node)
#define GTK_TREE_STORE_IS_SORTED(tree) (((GtkTreeStore*)(tree))->sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
@ -2193,19 +2194,11 @@ gtk_tree_store_reorder (GtkTreeStore *tree_store,
node = node->next;
}
#if GLIB_CHECK_VERSION(2,82,0)
g_sort_array (sort_array,
length,
sizeof (SortTuple),
gtk_tree_store_reorder_func,
NULL);
#else
g_qsort_with_data (sort_array,
length,
sizeof (SortTuple),
gtk_tree_store_reorder_func,
NULL);
#endif
_gtk_sort_array (sort_array,
length,
sizeof (SortTuple),
gtk_tree_store_reorder_func,
NULL);
/* fix up level */
for (i = 0; i < length - 1; i++)