From 9a10c76b711a259b993594f68b9fec11a6d8e97d Mon Sep 17 00:00:00 2001 From: nHackel Date: Thu, 10 Aug 2023 11:20:33 +0000 Subject: [PATCH] Add GtkCustomFilter constructor and filtered list view example --- examples/filteredlistview.jl | 45 ++++++++++++++++++++++++++++++++++++ src/lists.jl | 8 +++++++ 2 files changed, 53 insertions(+) create mode 100644 examples/filteredlistview.jl diff --git a/examples/filteredlistview.jl b/examples/filteredlistview.jl new file mode 100644 index 00000000..f789f6ab --- /dev/null +++ b/examples/filteredlistview.jl @@ -0,0 +1,45 @@ +using Gtk4, Gtk4.GLib + +win = GtkWindow("Listview demo with filter") +box = GtkBox(:v) +entry = GtkSearchEntry() +sw = GtkScrolledWindow() +push!(box, entry) +push!(box, sw) +push!(win, box) + +modelValues = string.(names(Gtk4)) +model = GtkStringList(modelValues) +factory = GtkSignalListItemFactory() + +function setup_cb(f, li) + set_child(li,GtkLabel("")) +end + +function bind_cb(f, li) + text = li[].string + label = get_child(li) + label.label = text +end + +function match(list_item, user_data) + itemLeaf = Gtk4.GLib.find_leaf_type(list_item) + item = convert(itemLeaf, list_item) + result = startswith(item.string, entry.text) + return result ? Cint(1) : Cint(0) +end + +filter = GtkCustomFilter((li, ud) -> match(li, ud)) +filteredModel = GtkFilterListModel(GLib.GListModel(model), filter) + +list = GtkListView(GtkSelectionModel(GtkSingleSelection(GLib.GListModel(filteredModel))), factory) +list.vexpand = true + +signal_connect(setup_cb, factory, "setup") +signal_connect(bind_cb, factory, "bind") + +signal_connect(entry, :search_changed) do w + @idle_add Gtk4.G_.changed(filter, Gtk4.FilterChange_DIFFERENT) +end + +sw[] = list diff --git a/src/lists.jl b/src/lists.jl index ba911b74..7bab43eb 100644 --- a/src/lists.jl +++ b/src/lists.jl @@ -73,3 +73,11 @@ function GtkTreeListModel(root::GListModel, passthrough, autoexpand, create_func ret = ccall(("gtk_tree_list_model_new", libgtk4), Ptr{GObject}, (Ptr{GObject}, Cint, Cint, Ptr{Nothing}, Ptr{Nothing}, Ptr{Nothing}), root, passthrough, autoexpand, create_cfunc, C_NULL, C_NULL) convert(GtkTreeListModel, ret, true) end + +## GtkCustomFilter + +function GtkCustomFilter(match::Function) + create_cfunc = @cfunction($match, Cint, (Ptr{GObject}, Ptr{Nothing})) + ret = ccall(("gtk_custom_filter_new", libgtk4), Ptr{GObject}, (Ptr{Nothing}, Ptr{Nothing}, Ptr{Nothing}), create_cfunc, C_NULL, C_NULL) + convert(GtkCustomFilter, ret, true) +end \ No newline at end of file