Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GtkCustomFilter constructor and filtered list view example #36

Merged
merged 1 commit into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading