Skip to content

Commit

Permalink
Fixed|Widgets|libappfw: Problems with button colors and hovering
Browse files Browse the repository at this point in the history
It was possible that a button would forget its correct color under
specific circumstances.

ChildWidgetOrganizer now first notifies the creation observers and then
updates the widget instead of doing this the other way around.
  • Loading branch information
skyjake committed Apr 16, 2017
1 parent 9ed627c commit 29e9607
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 27 deletions.
2 changes: 1 addition & 1 deletion doomsday/sdk/libappfw/include/de/framework/guiwidget.h
Expand Up @@ -305,7 +305,7 @@ class LIBAPPFW_PUBLIC GuiWidget : public QObject, public Widget
Rectanglef normalizedContentRect() const;

void setFont(DotPath const &id);
void setTextColor(DotPath const &id);
virtual void setTextColor(DotPath const &id);
void set(Background const &bg);
void setSaturation(float saturation);

Expand Down
7 changes: 4 additions & 3 deletions doomsday/sdk/libappfw/include/de/widgets/buttonwidget.h
Expand Up @@ -76,6 +76,8 @@ class LIBAPPFW_PUBLIC ButtonWidget : public LabelWidget
void setColorTheme(ColorTheme theme);
ColorTheme colorTheme() const;

void setTextColor(DotPath const &colorId) override;

/**
* Text color to use in the Hover state. The default is to use the normal text
* color of the button (label).
Expand Down Expand Up @@ -111,11 +113,10 @@ class LIBAPPFW_PUBLIC ButtonWidget : public LabelWidget
Action const *action() const;

State state() const;

void setShortcutKey(String const &key);
void setState(State state);

String shortcutKey() const;

void setShortcutKey(String const &key);
bool handleShortcut(KeyEvent const &keyEvent);

// Events.
Expand Down
5 changes: 3 additions & 2 deletions doomsday/sdk/libappfw/src/childwidgetorganizer.cpp
Expand Up @@ -149,9 +149,7 @@ DENG2_PIMPL(ChildWidgetOrganizer)
}
if (!w) return nullptr; // Unpresentable.

// Update the widget immediately.
mapping.insert(&item, w);
itemChanged(item);

if (behavior.testFlag(AlwaysAppend) || pos == dataItems->size() - 1)
{
Expand Down Expand Up @@ -179,6 +177,9 @@ DENG2_PIMPL(ChildWidgetOrganizer)
i->widgetCreatedForItem(*w, item);
}

// Update the widget immediately.
itemChanged(item);

// Observe.
w->audienceForDeletion() += this; // in case it's manually deleted
item.audienceForChange() += this;
Expand Down
42 changes: 26 additions & 16 deletions doomsday/sdk/libappfw/src/widgets/buttonwidget.cpp
Expand Up @@ -58,13 +58,6 @@ DENG2_OBSERVES(Action, Triggered)
{
if (state == st) return;

if (st == Hover && state == Up)
{
// Remember the original text color.
originalTextColor = self().textColorId();
originalTextModColor = self().textModulationColorf();
}

State const prev = state;
state = st;
animating = true;
Expand All @@ -84,7 +77,7 @@ DENG2_OBSERVES(Action, Triggered)
self().setTextModulationColorf(originalTextModColor);
break;
case ReplaceColor:
self().setTextColor(originalTextColor);
setTemporaryTextColor(originalTextColor);
break;
}
}
Expand All @@ -100,7 +93,7 @@ DENG2_OBSERVES(Action, Triggered)
self().setTextModulationColorf(style().colors().colorf(hoverTextColor));
break;
case ReplaceColor:
self().setTextColor(hoverTextColor);
setTemporaryTextColor(hoverTextColor);
break;
}
}
Expand Down Expand Up @@ -182,6 +175,13 @@ DENG2_OBSERVES(Action, Triggered)
}
}

void setTemporaryTextColor(DotPath const &id)
{
DotPath const original = originalTextColor;
self().setTextColor(id); // originalTextColor changes...
originalTextColor = original;
}

DENG2_PIMPL_AUDIENCE(StateChange)
DENG2_PIMPL_AUDIENCE(Press)
DENG2_PIMPL_AUDIENCE(Triggered)
Expand All @@ -194,6 +194,7 @@ DENG2_AUDIENCE_METHOD(ButtonWidget, Triggered)
ButtonWidget::ButtonWidget(String const &name) : LabelWidget(name), d(new Impl(this))
{
setBehavior(Focusable);
setColorTheme(Normal);
}

void ButtonWidget::useInfoStyle(bool yes)
Expand All @@ -211,30 +212,28 @@ void ButtonWidget::setColorTheme(ColorTheme theme)
auto bg = background();

d->colorTheme = theme;
setTextModulationColorf(Vector4f(1, 1, 1, 1));
d->originalTextModColor = textModulationColorf();
if (theme == Inverted)
{
d->bgType = Background::GradientFrameWithRoundedFill;
if (bg.type == Background::GradientFrame) bg.type = d->bgType;
d->originalTextColor = "inverted.text";
setTextColor("inverted.text");
setHoverTextColor("inverted.text", ReplaceColor);
setBorderColor("inverted.text");
setBackgroundColor("inverted.background");
setImageColor(style().colors().colorf("inverted.text"));
}
else
{
d->bgType = Background::GradientFrame;
if (bg.type == Background::GradientFrameWithRoundedFill) bg.type = d->bgType;
d->originalTextColor = "text";
setTextColor("text");
setHoverTextColor("text", ReplaceColor);
setBorderColor("text");
setBackgroundColor("background");
setImageColor(style().colors().colorf("text"));
}
set(bg);
setTextColor(d->originalTextColor);
d->originalTextModColor = Vector4f(1, 1, 1, 1);
setTextModulationColorf(d->originalTextModColor);
setImageColor(textColorf());
updateStyle();
}

Expand All @@ -243,6 +242,12 @@ GuiWidget::ColorTheme ButtonWidget::colorTheme() const
return d->colorTheme;
}

void ButtonWidget::setTextColor(DotPath const &colorId)
{
LabelWidget::setTextColor(colorId);
d->originalTextColor = colorId;
}

void ButtonWidget::setHoverTextColor(DotPath const &hoverTextId, HoverColorMode mode)
{
d->hoverTextColor = hoverTextId;
Expand Down Expand Up @@ -311,6 +316,11 @@ ButtonWidget::State ButtonWidget::state() const
return d->state;
}

void ButtonWidget::setState(State state)
{
d->setState(state);
}

void ButtonWidget::setShortcutKey(String const &key)
{
d->shortcut = key;
Expand Down
8 changes: 6 additions & 2 deletions doomsday/sdk/libappfw/src/widgets/dialogwidget.cpp
Expand Up @@ -366,12 +366,16 @@ DENG_GUI_PIMPL(DialogWidget)
// Highlight the default button(s).
if (i->role().testFlag(Default))
{
but.setTextColor("dialog.default");
but.setTextColor(self().colorTheme() == Normal? "dialog.default" : "inverted.text");
if (self().colorTheme() == Normal)
{
but.setHoverTextColor("dialog.default", ButtonWidget::ReplaceColor);
}
but.setText(_E(b) + but.text());
}
else
{
but.setTextColor("text");
but.setTextColor(self().colorTheme() == Normal? "text" : "inverted.text");
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions doomsday/sdk/libappfw/src/widgets/popupmenuwidget.cpp
Expand Up @@ -211,10 +211,13 @@ DENG_GUI_PIMPL(PopupMenuWidget)

void setButtonColors(ButtonWidget &button)
{
bool const hovering = (hover == &button);
/*bool const hovering = (hover == &button);
button.setTextColor(!hovering ^ (colorTheme == Inverted)? "text" : "inverted.text");
button.setHoverTextColor(!hovering ^ (colorTheme == Inverted)? "inverted.text" : "text",
ButtonWidget::ReplaceColor);
ButtonWidget::ReplaceColor);*/

button.setTextColor (colorTheme == Normal? "text" : "inverted.text" );
button.setHoverTextColor(colorTheme == Normal? "inverted.text" : "text", ButtonWidget::ReplaceColor);
}

void updateItemHitRules()
Expand Down Expand Up @@ -487,7 +490,8 @@ void PopupMenuWidget::panelClosing()
{
auto &btn = *const_cast<ButtonWidget *>(d->hover);
d->hover = 0;
d->setButtonColors(btn);
btn.setState(ButtonWidget::Up);
//d->setButtonColors(btn);
d->updateImageColor(btn);
//btn.setImageColor(style().colors().colorf(!d->infoStyle? "text" : "inverted.text"));
requestGeometry();
Expand Down

0 comments on commit 29e9607

Please sign in to comment.