Skip to content

Commit

Permalink
Merge pull request #709 from cairoshell/invert-tray-icons
Browse files Browse the repository at this point in the history
  • Loading branch information
josuave committed Sep 3, 2022
2 parents 1087708 + a42fb20 commit d2b5c37
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cairo Desktop/Cairo Desktop/Cairo Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="ManagedShell" Version="0.0.186" />
<PackageReference Include="ManagedShell" Version="0.0.188" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
Expand Down
2 changes: 2 additions & 0 deletions Cairo Desktop/Cairo Desktop/Themes/Cairo.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<system:Double x:Key="MediumFontSize">12</system:Double>
<system:Double x:Key="SmallFontSize">10</system:Double>

<!-- Theme configuration resources -->
<system:Boolean x:Key="EnableDarkMode">True</system:Boolean>
<system:Boolean x:Key="EnableInvertSystemNotifyIcons">False</system:Boolean>

<!-- Menu resources -->
<system:Boolean x:Key="MenuBarWindowAllowsTransparency">True</system:Boolean>
Expand Down
2 changes: 2 additions & 0 deletions Cairo Desktop/Cairo Desktop/Themes/White.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/netfx/2009/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<!-- Theme configuration resources -->
<system:Boolean x:Key="EnableDarkMode">False</system:Boolean>
<system:Boolean x:Key="EnableInvertSystemNotifyIcons">True</system:Boolean>

<!-- Menu resources -->
<SolidColorBrush Color="#FF000000" x:Key="MenuHeaderForeground" />
Expand Down
7 changes: 6 additions & 1 deletion Cairo Desktop/CairoDesktop.Common/CairoDesktop.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="ManagedShell" Version="0.0.186" />
<None Remove="Resources\InvertShader\shader_invert.ps" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="ManagedShell" Version="0.0.188" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="6.0.1" />
</ItemGroup>
Expand All @@ -27,6 +31,7 @@
<Resource Include="Resources\dialogIconLogOff.png" />
<Resource Include="Resources\dialogIconRestart.png" />
<Resource Include="Resources\dialogIconShutDown.png" />
<Resource Include="Resources\InvertShader\shader_invert.ps" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions Cairo Desktop/CairoDesktop.Common/InvertEffect.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using ManagedShell.Common.Logging;
using System;
using System.Windows.Media.Effects;
using System.Windows;
using System.Windows.Media;

namespace CairoDesktop.Common
{
// Courtesy of https://stackoverflow.com/a/45096471
public class InvertEffect : ShaderEffect
{
private static readonly PixelShader _shader =
new PixelShader { UriSource = new Uri("pack://application:,,,/CairoDesktop.Common;component/Resources/InvertShader/shader_invert.ps") };

public InvertEffect()
{
PixelShader = _shader;
PixelShader.InvalidPixelShaderEncountered += PixelShader_InvalidPixelShaderEncountered;
UpdateShaderValue(InputProperty);
}

private void PixelShader_InvalidPixelShaderEncountered(object sender, EventArgs e)
{
ShellLogger.Error("InvertEffect: The given pixel shader is not valid.");
}

public Brush Input
{
get { return (Brush)GetValue(InputProperty); }
set { SetValue(InputProperty, value); }
}

public static readonly DependencyProperty InputProperty =
RegisterPixelShaderSamplerProperty("Input", typeof(InvertEffect), 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\x64\fxc.exe" /T ps_3_0 /E main /Foshader_invert.ps shader_invert.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
sampler2D input : register(s0);

float4 main(float2 uv : TEXCOORD) : COLOR
{
float4 color = tex2D(input, uv);
float alpha = color.a;

if (!(color.r == color.g && color.g == color.b))
{
return color;
}

color.rgb /= alpha;
color.rgb = 1 - color.rgb;
color.rgb *= alpha;

return color;
}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
ToolTip="{Binding Path=Title}"
ToolTipService.Placement="Bottom">
<Image Source="{Binding Path=Icon, Mode=OneWay}"
Name="NotifyIconImage"
Style="{DynamicResource NotifyIconImage}" />
</Border>
<!-- The width of this border animates to a constant value 0..1 via its style -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using ManagedShell.Common.Helpers;
using CairoDesktop.Common;
using CairoDesktop.Configuration;
using ManagedShell.Common.Helpers;
using ManagedShell.Interop;
using ManagedShell.WindowsTray;
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -53,6 +56,9 @@ private void UserControl_Loaded(object sender, RoutedEventArgs e)
return;
}

applyEffects();

Settings.Instance.PropertyChanged += Settings_PropertyChanged;
_notifyIcon.NotificationBalloonShown += TrayIcon_NotificationBalloonShown;

// If a notification was received before we started listening, it will be here. Show the first one that is not expired.
Expand All @@ -73,10 +79,62 @@ private void UserControl_Unloaded(object sender, RoutedEventArgs e)
{
_notifyIcon.NotificationBalloonShown -= TrayIcon_NotificationBalloonShown;
}
Settings.Instance.PropertyChanged -= Settings_PropertyChanged;

_isLoaded = false;
}

private void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e == null || string.IsNullOrWhiteSpace(e.PropertyName))
{
return;
}

if (e.PropertyName == "CairoTheme")
{
applyEffects();
}
}

private void applyEffects()
{
if (!EnvironmentHelper.IsWindows10OrBetter || _notifyIcon == null)
{
return;
}

string iconGuid = _notifyIcon.GUID.ToString();

if (!(iconGuid == NotificationArea.HARDWARE_GUID ||
iconGuid == NotificationArea.UPDATE_GUID ||
iconGuid == NotificationArea.MICROPHONE_GUID ||
iconGuid == NotificationArea.LOCATION_GUID ||
iconGuid == NotificationArea.MEETNOW_GUID ||
iconGuid == NotificationArea.NETWORK_GUID ||
iconGuid == NotificationArea.POWER_GUID ||
iconGuid == NotificationArea.VOLUME_GUID))
{
return;
}

bool invertByTheme = FindResource("EnableInvertSystemNotifyIcons") as bool? ?? false;

if (NotifyIconImage.Effect == null != invertByTheme)
{
return;
}

if (invertByTheme)
{
NotifyIconImage.Effect = new InvertEffect();
}
else
{
NotifyIconImage.Effect = null;
}
}

#region Notify icon image mouse events
private void Image_MouseDown(object sender, MouseButtonEventArgs e)
{
Expand Down

0 comments on commit d2b5c37

Please sign in to comment.