Skip to content

Commit

Permalink
Auto-scroll to selected dataref. Great for keyboard operation.
Browse files Browse the repository at this point in the history
  • Loading branch information
leecbaker committed Apr 3, 2021
1 parent 7132f04 commit e24765e
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/lb_xplane_ui/container/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,22 @@ class WidgetsContainer : public WidgetContainer {
return widgets.crend();
}

widget_container_type::reference front() {
return widgets.front();
}

widget_container_type::const_reference front() const {
return widgets.front();
}

widget_container_type::reference back() {
return widgets.back();
}

widget_container_type::const_reference back() const {
return widgets.back();
}

size_t widget_count() const
{
return widgets.size();
Expand Down
22 changes: 22 additions & 0 deletions lib/lb_xplane_ui/container/scroll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,25 @@ std::shared_ptr<Widget11> ScrollContainer::mouseClick(Point point, XPLMMouseStat

return nullptr;
}

void ScrollContainer::scrollIntoView(Rect rect) {
const Rect translated_rect = rect.translated(0, scroll_distance);
const Rect bounds = getBounds();

// rect is already in the viewport, no need to scroll
if(bounds.top >= translated_rect.top && bounds.bottom <= translated_rect.bottom) {
return;
}


int scroll_distance_top_aligned = bounds.top - rect.top;
int scroll_distance_bottom_aligned = bounds.bottom - rect.bottom;

// if desired target is taller than the scroll viewport, just set the viewport to the top of the target
if(rect.height() >= bounds.height()) {
scroll_distance = scroll_distance_top_aligned;
} else {
// center the target within the viewport
scroll_distance = (scroll_distance_top_aligned + scroll_distance_bottom_aligned) / 2;
}
}
2 changes: 2 additions & 0 deletions lib/lb_xplane_ui/container/scroll.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@ class ScrollContainer : public WidgetContainer {


virtual XPLMCursorStatus handleCursor(Point point, bool mouse_inside) override { return object_->handleCursor(point.translated(0, -scroll_distance), mouse_inside); }

void scrollIntoView(Rect rect);
};
1 change: 1 addition & 0 deletions src/plugin/ui/search_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ SearchWindow::SearchWindow(RefRecords & refs)
setSelectionAvailable(nullptr);
} else {
setSelectionAvailable(new_selection->getRecord());
list_scroll_container->scrollIntoView(new_selection->getBounds());
}
});

Expand Down
4 changes: 4 additions & 0 deletions src/plugin/ui/selectable_list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,10 @@ void ResultsList::update() {
SingleAxisLayoutContainer::addNoLayout(std::move(line_widget), false, true);
}

if(begin() != end()) {
selected_element = front();
}

recomputeLayout();
}

Expand Down

0 comments on commit e24765e

Please sign in to comment.