Skip to content

Commit

Permalink
Fixed|libappfw: Handling notifications owned by other widgets
Browse files Browse the repository at this point in the history
If a notification is owned by a widget prior to showing,
NotificationWidget is supposed to only temporarily take ownership
of the notification. This requires observing the original parents
and seeing if they get deleted.

Todo: Add a utility class for this.
  • Loading branch information
skyjake committed Apr 14, 2014
1 parent 9946567 commit 9888106
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
7 changes: 7 additions & 0 deletions doomsday/libappfw/include/de/widgets/notificationwidget.h
Expand Up @@ -42,6 +42,13 @@ class LIBAPPFW_PUBLIC NotificationWidget : public GuiWidget
public:
NotificationWidget(String const &name = "");

/**
* Places the notification widget in the top right corner of @a area.
*
* @param area Reference area.
*/
void useDefaultPlacement(RuleRectangle const &area);

Rule const &shift();

/**
Expand Down
48 changes: 42 additions & 6 deletions doomsday/libappfw/src/widgets/notificationwidget.cpp
Expand Up @@ -30,9 +30,10 @@ namespace de {

static TimeDelta const ANIM_SPAN = .5;

DENG_GUI_PIMPL(NotificationWidget),
DENG2_OBSERVES(Widget, ChildAddition),
DENG2_OBSERVES(Widget, ChildRemoval)
DENG_GUI_PIMPL(NotificationWidget)
, DENG2_OBSERVES(Widget, ChildAddition)
, DENG2_OBSERVES(Widget, ChildRemoval)
, DENG2_OBSERVES(Widget, Deletion)
{
ScalarRule *shift;

Expand Down Expand Up @@ -65,9 +66,35 @@ DENG2_OBSERVES(Widget, ChildRemoval)

~Instance()
{
// Give current notifications back to their old parents.
DENG2_FOR_EACH_CONST(OldParents, i, oldParents)
{
dismissChild(*i.key());
i.value()->audienceForDeletion() -= this;
}

releaseRef(shift);
}

void widgetBeingDeleted(Widget &widget)
{
// Is this one of the old parents?
QMutableMapIterator<GuiWidget *, Widget *> iter(oldParents);
while(iter.hasNext())
{
iter.next();
if(iter.value() == &widget)
{
GuiWidget *notif = iter.key();
iter.remove();

// The old parent is gone, so its notifications should also be deleted.
dismissChild(*notif);
notif->guiDeleteLater();
}
}
}

void updateStyle()
{
//self.
Expand Down Expand Up @@ -145,8 +172,10 @@ DENG2_OBSERVES(Widget, ChildRemoval)

if(oldParents.contains(&notif))
{
oldParents[&notif]->add(&notif);
oldParents.remove(&notif);
Widget *oldParent = oldParents[&notif];
oldParent->add(&notif);
oldParent->audienceForDeletion() -= this;
oldParents.remove(&notif);
}
}

Expand Down Expand Up @@ -192,6 +221,12 @@ NotificationWidget::NotificationWidget(String const &name)
hide();
}

void NotificationWidget::useDefaultPlacement(RuleRectangle const &area)
{
rule().setInput(Rule::Top, area.top() + style().rules().rule("gap") - shift())
.setInput(Rule::Right, area.right() - style().rules().rule("gap"));
}

Rule const &NotificationWidget::shift()
{
return *d->shift;
Expand All @@ -213,7 +248,8 @@ void NotificationWidget::showChild(GuiWidget *notif)
if(notif->parentWidget())
{
d->oldParents.insert(notif, notif->parentWidget());
/// @todo Should observe if the old parent is destroyed.
notif->parentWidget()->audienceForDeletion() += d;
notif->parentWidget()->remove(*notif);
}
add(notif);
notif->show();
Expand Down

0 comments on commit 9888106

Please sign in to comment.