I’m using GTK4 through JavaGI binding, but I think my issue is not specific to the language.
What I want to achieve
I’m creating an app that has a sidebar with a files tree. The user can create / delete files there.
What I created
I used a ListView wrapped in a ScrolledWindow. I used a TreeListModel wrapped in a SingleSelectionModel. It all works like a charm to populate the ListView, expand / collapse folder.
What the issue is
have a tree large enough to require scrollars
scroll down
remove an item
=> the scroll position gets reset to the top: this is not very user-friendly.
What I tried
looked at other projects, namely Gnome Builder and Cambalache: they exhibit the same behavior as my app
restoring the store position:
get ScrolledWindow.vadjustment.value
ListStore.remove(item)
restore ScrolledWindow.vadjustment.value (in a GLib.idleAdd() running once)
=> doesn’t have any effect
restoring the store position:
get ScrolledWindow.vadjustment.value
ListStore.remove(item)
wait a bit (50-100ms in my test) to leave the time to the ListView to update
restore ScrolledWindow.vadjustment.value (in a GLib.idleAdd() running once)
=> works, but with a noticeable glitch (jumps up and down quickly)
plugging the scroll restoration on SelectionModelitems-changed signal:
=> not better
Question
I there a way to achieve what I’m trying to achieve using GTK4?
My instinct tells me that you shouldn’t try to scroll manually, but rather use something like Gtk.ListView.scroll_to. Although if other apps are also not working around this, I’m not sure if there’s a nice way to do it in the first place, besides changing something in GTK’s source code.
Ok, so a key difference in Gnome Files is that they are using a ColumnView, not a ListView.
Interestingly, I replaced the ListView with a ColumnView in my app, and indeed the issue is not present anymore. So I think this is a real ListView implementation issue
In my usecase, I don’t need several columns or headers, though.
That sounds like something that should just work, so perhaps you’re running into a bug in Gtk.ListView. It should be fixed in ListView, rather than working around it by messing with adjustments and manually calling scroll_to in an idle and things like that.