- if (_full_canvas_height - begin < _visible_canvas_height){
- begin = _full_canvas_height - _visible_canvas_height;
+
+ double const current_view_min_y = vertical_adjustment.get_value();
+ double const current_view_max_y = current_view_min_y + vertical_adjustment.get_page_size();
+
+ double const track_min_y = track.y_position ();
+ double const track_max_y = track.y_position () + track.effective_height ();
+
+ if (!at_top &&
+ (track_min_y >= current_view_min_y &&
+ track_max_y < current_view_max_y)) {
+ /* already visible, and caller did not ask to place it at the
+ * top of the track canvas
+ */
+ return;
+ }
+
+ double new_value;
+
+ if (at_top) {
+ new_value = track_min_y;
+ } else {
+ if (track_min_y < current_view_min_y) {
+ // Track is above the current view
+ new_value = track_min_y;
+ } else if (track_max_y > current_view_max_y) {
+ // Track is below the current view
+ new_value = track.y_position () + track.effective_height() - vertical_adjustment.get_page_size();
+ } else {
+ new_value = track_min_y;
+ }