Skip to content

Commit

Permalink
libappfw|ScrollAreaWidget: Scrolling to show a specific widget
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Apr 16, 2016
1 parent 6ca7f27 commit 276875b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
26 changes: 22 additions & 4 deletions doomsday/sdk/libappfw/include/de/widgets/scrollareawidget.h
Expand Up @@ -114,6 +114,8 @@ class LIBAPPFW_PUBLIC ScrollAreaWidget : public GuiWidget
void scrollX(int to, TimeDelta span = 0);
void scrollY(int to, TimeDelta span = 0);

bool isScrollable() const;

/**
* Determines if the history view is at the bottom, showing the latest entry.
*/
Expand Down Expand Up @@ -150,15 +152,31 @@ class LIBAPPFW_PUBLIC ScrollAreaWidget : public GuiWidget
bool handleEvent(Event const &event) override;

public slots:
void scrollToTop(TimeDelta span = .3f);
void scrollToTop(TimeDelta span = 0.3);

/**
* Moves the scroll offset of the widget to the bottom of the content.
*/
void scrollToBottom(TimeDelta span = .3f);
void scrollToBottom(TimeDelta span = 0.3);

void scrollToLeft(TimeDelta span = 0.3);
void scrollToRight(TimeDelta span = 0.3);

void scrollToLeft(TimeDelta span = .3f);
void scrollToRight(TimeDelta span = .3f);
/**
* Moves the scroll offset to center on the given widget.
*
* @param widget Widget to center on.
* @param span Animation duration.
*/
void scrollToWidget(GuiWidget const &widget, TimeDelta span = 0.3);

/**
* Finds the topmost scroll area that can be scrolled. May return this widget
* if there are no scrollable ancestors.
*
* @return
*/
ScrollAreaWidget &findTopmostScrollable();

protected:
void glInit() override;
Expand Down
1 change: 1 addition & 0 deletions doomsday/sdk/libappfw/src/widgets/menuwidget.cpp
Expand Up @@ -546,6 +546,7 @@ bool MenuWidget::handleEvent(Event const &event)
if(child->isVisible() && child->behavior().testFlag(Focusable))
{
root().setFocus(child);
findTopmostScrollable().scrollToWidget(child->as<GuiWidget>());
return true;
}
}
Expand Down
28 changes: 28 additions & 0 deletions doomsday/sdk/libappfw/src/widgets/scrollareawidget.cpp
Expand Up @@ -354,6 +354,11 @@ void ScrollAreaWidget::scrollY(int to, TimeDelta span)
d->restartScrollOpacityFade();
}

bool ScrollAreaWidget::isScrollable() const
{
return d->scrollingEnabled && maximumScrollY().valuei() > 0;
}

bool ScrollAreaWidget::isAtBottom() const
{
return d->origin == Bottom && d->y->animation().target() == 0;
Expand Down Expand Up @@ -485,6 +490,29 @@ void ScrollAreaWidget::scrollToRight(TimeDelta span)
scrollX(maximumScrollX().valuei(), span);
}

void ScrollAreaWidget::scrollToWidget(GuiWidget const &widget, TimeDelta span)
{
int off = widget.rule().midY().valuei() - contentRule().top().valuei() -
rule().height().valuei()/2;
qDebug() << "scroll off:" << off;
scrollY(off, span);
}

ScrollAreaWidget &ScrollAreaWidget::findTopmostScrollable()
{
for(Widget *parent = parentWidget(); parent; parent = parent->parent())
{
if(ScrollAreaWidget *scroll = parent->maybeAs<ScrollAreaWidget>())
{
if(scroll->isScrollable())
{
return *scroll;
}
}
}
return *this;
}

void ScrollAreaWidget::glInit()
{
d->glInit();
Expand Down

0 comments on commit 276875b

Please sign in to comment.