The code below illustrates a problem I'm having with Gtk2::TreeView (or here). It just shows a list of random words, and adds a new one at the bottom once every second. I'd like to automatically scroll the new word into view if the one just before it was visible. That way, this automatic scrolling won't annoy someone looking elsewhere in the tree view.
Everything words fine until something causes the tree view to shrink. Making the window smaller is an example. The actual application I'm working on has other examples, e.g., a Gtk2::Expander (or here). If you resize the window to be smaller, you should see the problem. The "gravity" of the scroll bar, I guess, moves it off the bottom after the shrink, turning off the automatic scrolling.
How can I keep my automatic scrolling, even after these kinds of events?
use Glib qw(TRUE FALSE);
use Gtk2 -init;
# Creates the widgets in the application. Returns the main
# window and tree view.
$win->signal_connect(destroy => \&Gtk2::main_quit);
new Gtk2::CellRendererText(),text => 0);
addWord($model) for 0 .. 100;
# Called at regular intervals to add another random "word"
# to the bottom of the tree view. If the previous word was
# visible beforehand, scrolls the tree view so the new word
# is visible.
$lastVis->get_indices() == $numRows-1;
showLast($tree) if $mustScroll;
# Adds a random "word" to the bottom of the tree view.
my(@cons)=grep !/[aeiou]/,'a' .. 'z';
$cons[rand @cons] . 'oo');
# Scrolls the tree view so the last row is visible.