Skip to content

Commit

Permalink
Fix using colorized icons
Browse files Browse the repository at this point in the history
If icon-normal is in icon theme, icon color won't change with session
and on iconColor().

Setting empty string to iconColor() resets the color to session color.
  • Loading branch information
hluk committed May 12, 2018
1 parent 9d94036 commit c0328f5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
4 changes: 3 additions & 1 deletion docs/scripting-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,9 @@ omitted.

Get or set current tray and window icon color name.

Throws exception is the color name is invalid.
Resets color if color name is empty string.

Throws exception is the color name is not empty and invalid.

.. code-block:: js
Expand Down
9 changes: 7 additions & 2 deletions docs/sessions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,15 @@ Default session has empty name but it can be overridden by setting
``COPYQ_SESSION_NAME`` environment variable.

Icon for each session is bit different. The color is generated from session
name and can be changed using ``COPYQ_SESSION_COLOR`` environment variable (the
color to override can be set with ``COPYQ_APP_COLOR`` environment variable).
name and can be changed using ``COPYQ_SESSION_COLOR`` environment variable.

::

COPYQ_SESSION_COLOR="yellow" copyq
COPYQ_SESSION_COLOR="#f90" copyq

.. note::

On Linux, changing icon color won't work if current icon theme contains
icon named "copyq-normal" or doesn't contain "copyq-mask" (and
"copyq-busy-mask").
36 changes: 17 additions & 19 deletions src/gui/iconfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ const int lightThreshold = 100;

QPointer<QObject> activePaintDevice;

bool hasNormalIcon()
{
return QIcon::hasThemeIcon(COPYQ_ICON_NAME "-normal");
}

QString sessionName()
{
return qApp->property("CopyQ_session_name").toString();
Expand All @@ -69,22 +74,10 @@ QColor colorFromEnv(const char *envVaribleName)
return QColor( textFromEnv(envVaribleName) );
}

QColor appIconColorHelper()
{
const auto color = colorFromEnv("COPYQ_APP_COLOR");
return color.isValid() ? QColor(color) : QColor(0x7f, 0xca, 0x9b);
}

QColor appIconColor()
{
static const QColor color = appIconColorHelper();
return color;
}

QColor sessionNameToColor(const QString &name)
{
if (name.isEmpty())
return appIconColor();
return QColor();

int r = 0;
int g = 0;
Expand All @@ -111,7 +104,7 @@ QColor sessionNameToColor(const QString &name)
QColor sessionIconColorHelper()
{
const auto color = colorFromEnv("COPYQ_SESSION_COLOR");
return color.isValid() ? QColor(color) : sessionNameToColor( sessionName() );
return color.isValid() ? color : sessionNameToColor( sessionName() );
}

QColor &sessionIconColorVariable()
Expand Down Expand Up @@ -172,6 +165,9 @@ QString iconPath(const QString &iconSuffix)

QPixmap appPixmap(const QString &iconSuffix, QSize size)
{
if ( iconSuffix.isEmpty() && hasNormalIcon() )
return appPixmap("-normal", size);

const auto icon = QIcon::fromTheme(COPYQ_ICON_NAME + iconSuffix);

QPixmap pix;
Expand Down Expand Up @@ -481,11 +477,13 @@ class AppIconEngine : public BaseIconEngine
const auto suffix = running ? QLatin1String("-busy") : QLatin1String("");

auto pix = appPixmap(suffix, size);
const auto sessionColor = sessionIconColor();
const auto appColor = appIconColor();

if (sessionColor != appColor)
replaceColor(&pix, suffix, sessionColor);
// If copyq-normal icon exist in theme, omit changing color.
if ( !hasNormalIcon() ) {
const auto sessionColor = sessionIconColor();
if ( sessionColor.isValid() )
replaceColor(&pix, suffix, sessionColor);
}

return pix;
}
Expand Down Expand Up @@ -615,7 +613,7 @@ unsigned short toIconId(const QString &fileNameOrId)

void setSessionIconColor(QColor color)
{
sessionIconColorVariable() = color;
sessionIconColorVariable() = color.isValid() ? color : sessionIconColorHelper();
}

void setSessionIconTag(const QString &tag)
Expand Down
6 changes: 4 additions & 2 deletions src/scriptable/scriptableproxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1621,14 +1621,16 @@ QString ScriptableProxy::translationsPath()
QString ScriptableProxy::iconColor()
{
INVOKE(iconColor, ());
return m_wnd->sessionIconColor().name();
const auto color = m_wnd->sessionIconColor();
return color.isValid() ? color.name() : QString();
}

bool ScriptableProxy::setIconColor(const QString &colorName)
{
INVOKE(setIconColor, (colorName));

QColor color(colorName);
if ( !color.isValid() )
if ( !colorName.isEmpty() && !color.isValid() )
return false;

m_wnd->setSessionIconColor(color);
Expand Down
3 changes: 3 additions & 0 deletions src/tests/tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,9 @@ void Tests::commandIcon()
RUN_EXPECT_ERROR("iconColor" << "BAD_COLOR_NAME", CommandException);
RUN("iconColor", "#ff0000\n");

RUN("iconColor" << "", "");
RUN("iconColor", QByteArray(defaultSessionColor) + "\n");

RUN("iconColor" << defaultSessionColor, "");
RUN("iconColor", QByteArray(defaultSessionColor) + "\n");
}
Expand Down

0 comments on commit c0328f5

Please sign in to comment.