Skip to content

Commit

Permalink
Widgets|libappfw: Restore focus after opening and closing a popup
Browse files Browse the repository at this point in the history
GuiRootWidget maintains a stack of widgets that were focused before
a popup was opened.
  • Loading branch information
skyjake committed Aug 2, 2016
1 parent fa60fcf commit e8a95f4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
8 changes: 8 additions & 0 deletions doomsday/sdk/libappfw/include/de/framework/guirootwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ class LIBAPPFW_PUBLIC GuiRootWidget : public RootWidget

FocusWidget &focusIndicator();

/**
* Pushes a pointer to the current focused widget to a stack. It can later be
* restored with popFocus().
*/
void pushFocus();

void popFocus();

// Events.
void update();
void draw();
Expand Down
5 changes: 3 additions & 2 deletions doomsday/sdk/libappfw/include/de/widgets/popupwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ class LIBAPPFW_PUBLIC PopupWidget : public PanelWidget
void glMakeGeometry(DefaultVertexBuf::Builder &verts) override;
void updateStyle() override;

virtual void preparePanelForOpening() override;
virtual void panelDismissed() override;
void preparePanelForOpening() override;
void panelClosing() override;
void panelDismissed() override;

private:
DENG2_PRIVATE(d)
Expand Down
24 changes: 24 additions & 0 deletions doomsday/sdk/libappfw/src/guirootwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ DENG2_PIMPL(GuiRootWidget)
TextureBank texBank; ///< Bank for the atlas contents.
FocusWidget *focusIndicator;
bool noFramesDrawnYet;
QList<SafeWidgetPtr<Widget> *> focusStack;

Impl(Public *i, CanvasWindow *win)
: Base(i)
Expand All @@ -139,6 +140,8 @@ DENG2_PIMPL(GuiRootWidget)

~Impl()
{
qDeleteAll(focusStack);

GuiWidget::recycleTrashedWidgets();

// Tell all widgets to release their resource allocations. The base
Expand Down Expand Up @@ -366,6 +369,27 @@ FocusWidget &GuiRootWidget::focusIndicator()
return *d->focusIndicator;
}

void GuiRootWidget::pushFocus()
{
if (!focus()) return;

d->focusStack.append(new SafeWidgetPtr<Widget>(focus()));
}

void GuiRootWidget::popFocus()
{
while (!d->focusStack.isEmpty())
{
std::unique_ptr<SafeWidgetPtr<Widget>> ptr(d->focusStack.takeLast());
if (*ptr)
{
setFocus(*ptr);
return;
}
}
setFocus(nullptr);
}

void GuiRootWidget::update()
{
if (window().canvas().isGLReady())
Expand Down
5 changes: 3 additions & 2 deletions doomsday/sdk/libappfw/src/guiwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ DENG2_PIMPL(GuiWidget)

if (self.isClipped())
{
int const CULL_SAFETY_WIDTH = 100; // avoid pop-in when scrolling
int const CULL_SAFETY_WIDTH = 50; // avoid pop-in when scrolling

// Clipped widgets are guaranteed to be within their rectangle.
return !visibleArea.overlaps(self.rule().recti().expanded(CULL_SAFETY_WIDTH));
return !visibleArea.overlaps(self.rule().recti().expanded(
GuiWidget::toDevicePixels(CULL_SAFETY_WIDTH)));
}
// Otherwise widgets may draw anywhere in the view.
return visibleArea.isNull();
Expand Down
8 changes: 8 additions & 0 deletions doomsday/sdk/libappfw/src/widgets/popupwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,9 +487,17 @@ void PopupWidget::preparePanelForOpening()

d->updateLayout();

root().pushFocus();
offerFocus();
}

void PopupWidget::panelClosing()
{
PanelWidget::panelClosing();

root().popFocus();
}

void PopupWidget::panelDismissed()
{
PanelWidget::panelDismissed();
Expand Down

0 comments on commit e8a95f4

Please sign in to comment.