Commit 83d11b3a authored by Jens Georg's avatar Jens Georg

Extract function to scroll to item

Code was copied into three places. Time to make it a function
parent d71e1c94
......@@ -509,25 +509,8 @@ private class MapWidget : Gtk.Bin {
CheckerboardItem item = m.view as CheckerboardItem;
if (!did_adjust_view) {
// if first item is in any way out of view, scroll to it
Gtk.Adjustment vadj = page.get_vadjustment();
if (!(get_adjustment_relation(vadj, item.allocation.y) == AdjustmentRelation.IN_RANGE
&& (get_adjustment_relation(vadj, item.allocation.y + item.allocation.height) == AdjustmentRelation.IN_RANGE))) {
// scroll to see the new item
int top = 0;
if (item.allocation.y < vadj.get_value()) {
top = item.allocation.y;
top -= CheckerboardLayout.ROW_GUTTER_PADDING / 2;
} else {
top = item.allocation.y + item.allocation.height - (int) vadj.get_page_size();
top += CheckerboardLayout.ROW_GUTTER_PADDING / 2;
}
vadj.set_value(top);
}
if (!did_adjust_view && page is CheckerboardPage) {
(page as CheckerboardPage).scroll_to_item(item);
did_adjust_view = true;
}
item.brighten();
......
......@@ -1384,6 +1384,26 @@ public abstract class CheckerboardPage : Page {
base.switching_from();
}
public void scroll_to_item(CheckerboardItem item) {
Gtk.Adjustment vadj = get_vadjustment();
if (!(get_adjustment_relation(vadj, item.allocation.y) == AdjustmentRelation.IN_RANGE
&& (get_adjustment_relation(vadj, item.allocation.y + item.allocation.height) == AdjustmentRelation.IN_RANGE))) {
// scroll to see the new item
int top = 0;
if (item.allocation.y < vadj.get_value()) {
top = item.allocation.y;
top -= CheckerboardLayout.ROW_GUTTER_PADDING / 2;
} else {
top = item.allocation.y + item.allocation.height - (int) vadj.get_page_size();
top += CheckerboardLayout.ROW_GUTTER_PADDING / 2;
}
vadj.set_value(top);
}
}
public override void switched_to() {
layout.set_in_view(true);
......@@ -1394,23 +1414,7 @@ public abstract class CheckerboardPage : Page {
CheckerboardItem? item = (CheckerboardItem?) get_view().get_selected_at(0);
// if item is in any way out of view, scroll to it
Gtk.Adjustment vadj = get_vadjustment();
if (!(get_adjustment_relation(vadj, item.allocation.y) == AdjustmentRelation.IN_RANGE
&& (get_adjustment_relation(vadj, item.allocation.y + item.allocation.height) == AdjustmentRelation.IN_RANGE))) {
// scroll to see the new item
int top = 0;
if (item.allocation.y < vadj.get_value()) {
top = item.allocation.y;
top -= CheckerboardLayout.ROW_GUTTER_PADDING / 2;
} else {
top = item.allocation.y + item.allocation.height - (int) vadj.get_page_size();
top += CheckerboardLayout.ROW_GUTTER_PADDING / 2;
}
vadj.set_value(top);
}
scroll_to_item(item);
}
base.switched_to();
......@@ -1891,24 +1895,7 @@ public abstract class CheckerboardPage : Page {
get_view().select_marked(marker);
}
layout.set_cursor(item);
// if item is in any way out of view, scroll to it
Gtk.Adjustment vadj = get_vadjustment();
if (get_adjustment_relation(vadj, item.allocation.y) == AdjustmentRelation.IN_RANGE
&& (get_adjustment_relation(vadj, item.allocation.y + item.allocation.height) == AdjustmentRelation.IN_RANGE))
return;
// scroll to see the new item
int top = 0;
if (item.allocation.y < vadj.get_value()) {
top = item.allocation.y;
top -= CheckerboardLayout.ROW_GUTTER_PADDING / 2;
} else {
top = item.allocation.y + item.allocation.height - (int) vadj.get_page_size();
top += CheckerboardLayout.ROW_GUTTER_PADDING / 2;
}
vadj.set_value(top);
scroll_to_item(item);
}
public void move_cursor(CompassPoint point) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment