Skip to content

Commit

Permalink
Add GtkCustomFilter constructor and filtered list view example
Browse files Browse the repository at this point in the history
  • Loading branch information
nHackel committed Aug 10, 2023
1 parent 1059964 commit 9a10c76
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
45 changes: 45 additions & 0 deletions examples/filteredlistview.jl
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions src/lists.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9a10c76

Please sign in to comment.