[gjs: 1/2] Add overrides for Gio.ListStore.insert_sorted and Gio.ListStore.sort
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs: 1/2] Add overrides for Gio.ListStore.insert_sorted and Gio.ListStore.sort
- Date: Thu, 6 May 2021 15:58:11 +0000 (UTC)
commit af9d297f90169ba9a3fcab03cb7eaf9b421b3d0c
Author: veena <veenanitks gmail com>
Date: Mon Apr 26 12:29:53 2021 +0530
Add overrides for Gio.ListStore.insert_sorted and Gio.ListStore.sort
Closes: #326
installed-tests/js/testGio.js | 31 +++++++++++++++++++++++++++++++
libgjs-private/gjs-util.c | 37 +++++++++++++++++++++++++++++++++++++
libgjs-private/gjs-util.h | 28 ++++++++++++++++++++++++++++
modules/core/overrides/Gio.js | 6 ++++++
4 files changed, 102 insertions(+)
---
diff --git a/installed-tests/js/testGio.js b/installed-tests/js/testGio.js
index 865b0b78..4260b202 100644
--- a/installed-tests/js/testGio.js
+++ b/installed-tests/js/testGio.js
@@ -32,6 +32,37 @@ describe('ListStore iterator', function () {
});
});
+function compareFunc(a, b) {
+ return a.value - b.value;
+}
+
+describe('Sorting in ListStore', function () {
+ let list;
+
+ beforeEach(function () {
+ list = new Gio.ListStore({
+ item_type: Foo,
+ });
+ });
+
+ it('test insert_sorted', function () {
+ for (let i = 10; i > 0; i--)
+ list.insert_sorted(new Foo(i), compareFunc);
+ let i = 1;
+ for (let f of list)
+ expect(f.value).toBe(i++);
+ });
+
+ it('test sort', function () {
+ for (let i = 10; i > 0; i--)
+ list.append(new Foo(i));
+ list.sort(compareFunc);
+ let i = 1;
+ for (let f of list)
+ expect(f.value).toBe(i++);
+ });
+});
+
describe('Gio.Settings overrides', function () {
it("doesn't crash when forgetting to specify a schema ID", function () {
expect(() => new Gio.Settings()).toThrowError(/schema/);
diff --git a/libgjs-private/gjs-util.c b/libgjs-private/gjs-util.c
index 9dbe5f6f..15060950 100644
--- a/libgjs-private/gjs-util.c
+++ b/libgjs-private/gjs-util.c
@@ -176,3 +176,40 @@ out:
g_clear_pointer(&base_info, g_base_info_unref);
g_clear_pointer(&child_set_property_fun, g_base_info_unref);
}
+
+/**
+ * gjs_list_store_insert_sorted:
+ * @store: a #GListStore
+ * @item: the new item
+ * @compare_func: (scope call): pairwise comparison function for sorting
+ * @user_data: (closure): user data for @compare_func
+ *
+ * Inserts @item into @store at a position to be determined by the
+ * @compare_func.
+ *
+ * The list must already be sorted before calling this function or the
+ * result is undefined. Usually you would approach this by only ever
+ * inserting items by way of this function.
+ *
+ * This function takes a ref on @item.
+ *
+ * Returns: the position at which @item was inserted
+ */
+unsigned int gjs_list_store_insert_sorted(GListStore *store, GObject *item,
+ GjsCompareDataFunc compare_func,
+ void *user_data) {
+ return g_list_store_insert_sorted(store, item, (GCompareDataFunc)compare_func, user_data);
+}
+
+/**
+ * gjs_list_store_sort:
+ * @store: a #GListStore
+ * @compare_func: (scope call): pairwise comparison function for sorting
+ * @user_data: (closure): user data for @compare_func
+ *
+ * Sort the items in @store according to @compare_func.
+ */
+void gjs_list_store_sort(GListStore *store, GjsCompareDataFunc compare_func,
+ void *user_data) {
+ g_list_store_sort(store, (GCompareDataFunc)compare_func, user_data);
+}
diff --git a/libgjs-private/gjs-util.h b/libgjs-private/gjs-util.h
index c24f2359..320337c5 100644
--- a/libgjs-private/gjs-util.h
+++ b/libgjs-private/gjs-util.h
@@ -9,6 +9,8 @@
#include <locale.h>
+#include <gio/gio.h>
+
#include <glib-object.h>
#include <glib.h>
@@ -20,6 +22,32 @@ G_BEGIN_DECLS
GJS_EXPORT
char * gjs_format_int_alternative_output (int n);
+/**
+ * GjsCompareDataFunc:
+ * @a: a value
+ * @b: a value to compare with
+ * @user_data: user data
+ *
+ * Specifies the type of a comparison function used to compare two
+ * values. The function should return a negative integer if the first
+ * value comes before the second, 0 if they are equal, or a positive
+ * integer if the first value comes after the second.
+ *
+ * Returns: negative value if @a < @b; zero if @a = @b; positive
+ * value if @a > @b
+ */
+typedef int (*GjsCompareDataFunc)(const GObject *a, const GObject *b,
+ void *user_data);
+
+GJS_EXPORT
+unsigned gjs_list_store_insert_sorted(GListStore *store, GObject *item,
+ GjsCompareDataFunc compare_func,
+ void *user_data);
+
+GJS_EXPORT
+void gjs_list_store_sort(GListStore *store, GjsCompareDataFunc compare_func,
+ void *user_data);
+
/* For imports.gettext */
typedef enum
{
diff --git a/modules/core/overrides/Gio.js b/modules/core/overrides/Gio.js
index d51738e0..c1fc54c0 100644
--- a/modules/core/overrides/Gio.js
+++ b/modules/core/overrides/Gio.js
@@ -490,6 +490,12 @@ function _init() {
// ListStore
Gio.ListStore.prototype[Symbol.iterator] = _listModelIterator;
+ Gio.ListStore.prototype.insert_sorted = function (item, compareFunc) {
+ return GjsPrivate.list_store_insert_sorted(this, item, compareFunc);
+ };
+ Gio.ListStore.prototype.sort = function (compareFunc) {
+ return GjsPrivate.list_store_sort(this, compareFunc);
+ };
// Promisify
Gio._promisify = _promisify;
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]