Skip to content

Commit

Permalink
Merge pull request #812 from dremin/invert-custom-icons
Browse files Browse the repository at this point in the history
Ability to configure which tray icons get inverted
  • Loading branch information
dremin authored Apr 28, 2024
2 parents 4f91054 + 7bcea51 commit 5d67ae0
Show file tree
Hide file tree
Showing 11 changed files with 167 additions and 20 deletions.
14 changes: 3 additions & 11 deletions RetroBar/Controls/NotifyIcon.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using ManagedShell.Common.Helpers;
using ManagedShell.Interop;
using ManagedShell.WindowsTray;
using RetroBar.Extensions;
using RetroBar.Utilities;

namespace RetroBar.Controls
Expand Down Expand Up @@ -33,21 +34,12 @@ public NotifyIcon()

private void applyEffects()
{
if ((!EnvironmentHelper.IsWindows10OrBetter && Settings.Instance.InvertIconsMode == Settings.InvertIconsOption.WhenNeededByTheme) || TrayIcon == null || Settings.Instance.InvertIconsMode == Settings.InvertIconsOption.Never)
if (TrayIcon == null || Settings.Instance.InvertIconsMode == Settings.InvertIconsOption.Never)
{
return;
}

string iconGuid = TrayIcon.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))
if (!TrayIcon.CanInvert())
{
return;
}
Expand Down
4 changes: 2 additions & 2 deletions RetroBar/Controls/NotifyIconList.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ private void Settings_PropertyChanged(object sender, PropertyChangedEventArgs e)
NotifyIcons.ItemsSource = allNotifyIconsSource.View;
}
}
else if (e.PropertyName == nameof(Settings.InvertIconsMode))
else if (e.PropertyName == nameof(Settings.InvertIconsMode) || e.PropertyName == nameof(Settings.InvertNotifyIcons))
{
// Reload icons
NotifyIcons.ItemsSource = null;

if (Settings.Instance.CollapseNotifyIcons)
if (Settings.Instance.CollapseNotifyIcons && NotifyIconToggleButton.IsChecked != true)
{
NotifyIcons.ItemsSource = pinnedNotifyIconsSource.View;
}
Expand Down
7 changes: 6 additions & 1 deletion RetroBar/Converters/BoolToIntConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ public object Convert(object value, Type targetType, object parameter, System.Gl
{
if (value is bool boolValue)
{
return boolValue ? 1 : 0;
var multiplier = 1;
if (parameter is string strValue)
{
multiplier = System.Convert.ToInt32(strValue);
}
return boolValue ? 1 * multiplier : 0;
}

return Binding.DoNothing;
Expand Down
26 changes: 26 additions & 0 deletions RetroBar/Converters/NotifyIconCanInvertConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using ManagedShell.WindowsTray;
using RetroBar.Extensions;
using System;
using System.Windows.Data;

namespace RetroBar.Converters
{
[ValueConversion(typeof(NotifyIcon), typeof(bool))]
public class NotifyIconCanInvertConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is NotifyIcon icon)
{
return icon.CanInvert();
}

return Binding.DoNothing;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Binding.DoNothing;
}
}
}
41 changes: 41 additions & 0 deletions RetroBar/Extensions/NotifyIconExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using ManagedShell.WindowsTray;
using RetroBar.Utilities;
using System.Collections.Generic;

namespace RetroBar.Extensions
{
public static class NotifyIconExtensions
{
public static string GetInvertIdentifier(this NotifyIcon icon)
{
if (icon.GUID != default) return icon.GUID.ToString();
else return icon.Path + ":" + icon.UID.ToString();
}

public static bool CanInvert(this NotifyIcon icon) {
return Settings.Instance.InvertNotifyIcons.Contains(icon.GetInvertIdentifier());
}

public static void SetCanInvert(this NotifyIcon icon, bool canInvert)
{
var identifier = icon.GetInvertIdentifier();
var settings = new List<string>(Settings.Instance.InvertNotifyIcons);
var changed = false;

if (!canInvert)
{
changed = settings.Remove(identifier);
}
else if (!settings.Contains(identifier))
{
settings.Add(identifier);
changed = true;
}

if (changed)
{
Settings.Instance.InvertNotifyIcons = settings;
}
}
}
}
1 change: 1 addition & 0 deletions RetroBar/Languages/English.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<s:String x:Key="always_show">Always show</s:String>
<s:String x:Key="name_heading">Name</s:String>
<s:String x:Key="behavior_heading">Behavior</s:String>
<s:String x:Key="invert_heading">Invert</s:String>

<s:String x:Key="start_text">Start</s:String>
<s:String x:Key="start_text_xp">start</s:String>
Expand Down
22 changes: 19 additions & 3 deletions RetroBar/NotificationPropertiesWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:converters="clr-namespace:RetroBar.Converters"
xmlns:Settings="clr-namespace:RetroBar.Utilities"
Title="{DynamicResource customize_notifications}"
Height="406"
Width="364"
Expand All @@ -12,7 +13,9 @@
<Window.Resources>
<ResourceDictionary>
<converters:BoolToIntConverter x:Key="boolToIntConverter" />
<converters:BoolToVisibilityConverter x:Key="boolToVisibilityConverter" />
<converters:NewLineToSpaceConverter x:Key="newLineToSpaceConverter" />
<converters:NotifyIconCanInvertConverter x:Key="notifyIconCanInvertConverter" />
<Style TargetType="{x:Type ComboBox}">
<Setter Property="HorizontalAlignment"
Value="Right" />
Expand Down Expand Up @@ -54,23 +57,26 @@
</Grid.RowDefinitions>
<DockPanel Grid.Row="0">
<TextBlock Text="{DynamicResource customize_notifications_info}"
Visibility="{Binding Source={x:Static Settings:Settings.Instance}, Path=CollapseNotifyIcons, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource boolToVisibilityConverter}}"
TextWrapping="Wrap"
Margin="0,0,0,20"
DockPanel.Dock="Top" />
<TextBlock Text="{DynamicResource customize_notifications_instruction}"
TextWrapping="Wrap"
Margin="0,20,0,3"
Margin="0,0,0,3"
DockPanel.Dock="Top" />
<ListView VerticalAlignment="Stretch"
ItemsSource="{Binding AllIcons}"
DockPanel.Dock="Top">
<ListView.View>
<GridView>
<GridViewColumn Header="{DynamicResource name_heading}"
Width="180">
Width="145">
<GridViewColumn.CellTemplate>
<DataTemplate>
<DockPanel>
<Image Source="{Binding Icon}"
Loaded="Icon_Loaded"
Width="16"
Height="16"
Margin="0,0,3,0"
Expand All @@ -85,7 +91,7 @@
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="{DynamicResource behavior_heading}"
Width="120">
Width="{Binding Source={x:Static Settings:Settings.Instance}, Path=CollapseNotifyIcons, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource boolToIntConverter}, ConverterParameter=120}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
Expand Down Expand Up @@ -128,6 +134,16 @@
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="{DynamicResource invert_heading}"
Width="Auto">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding Mode=OneWay, Converter={StaticResource notifyIconCanInvertConverter}}"
Checked="InvertCheckBox_Checked"
Unchecked="InvertCheckBox_Unchecked" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Expand Down
56 changes: 55 additions & 1 deletion RetroBar/NotificationPropertiesWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using ManagedShell.WindowsTray;
using RetroBar.Extensions;
using RetroBar.Utilities;
using System.Windows;
using System.Windows.Controls;

namespace RetroBar
{
Expand All @@ -20,6 +22,17 @@ private NotificationPropertiesWindow(NotificationArea notificationArea)
InitializeComponent();

DataContext = _notificationArea;
Settings.Instance.PropertyChanged += Settings_PropertyChanged;
}

private void Settings_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "InvertNotifyIcons")
{
// Reload icons
DataContext = null;
DataContext = _notificationArea;
}
}

public static void Open(NotificationArea notificationArea, Point position)
Expand All @@ -44,12 +57,53 @@ private void OK_OnClick(object sender, RoutedEventArgs e)

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Settings.Instance.PropertyChanged -= Settings_PropertyChanged;
_instance = null;
}

private void BehaviorComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
private void BehaviorComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Settings.Instance.PinnedNotifyIcons = _notificationArea.PinnedNotifyIcons;
}

private void InvertCheckBox_Checked(object sender, RoutedEventArgs e)
{
var checkBox = sender as CheckBox;
var icon = checkBox.DataContext as NotifyIcon;
icon.SetCanInvert(true);
}

private void InvertCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
var checkBox = sender as CheckBox;
var icon = checkBox.DataContext as NotifyIcon;
icon.SetCanInvert(false);
}

private void Icon_Loaded(object sender, RoutedEventArgs e)
{
var image = sender as Image;
var notifyIcon = image.DataContext as NotifyIcon;
applyEffects(image, notifyIcon);
}

private void applyEffects(Image NotifyIconImage, NotifyIcon TrayIcon)
{
if (TrayIcon == null)
{
return;
}

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

if (NotifyIconImage.Effect == null)
{
NotifyIconImage.Effect = new InvertEffect();
}
}
}
}
3 changes: 3 additions & 0 deletions RetroBar/PropertiesWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@
SelectedIndex="{Binding Source={x:Static Settings:Settings.Instance}, Path=InvertIconsMode, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource enumConverter}}"
SelectionChanged="cboInvertIconsMode_SelectionChanged" />
</DockPanel>
<Button HorizontalAlignment="Right"
Content="{DynamicResource customize}"
Click="CustomizeNotifications_OnClick" />
<DockPanel>
<Label VerticalAlignment="Center"
Target="{Binding ElementName=cboMiddleMouseAction}">
Expand Down
2 changes: 1 addition & 1 deletion RetroBar/RetroBar.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@

<ItemGroup>
<PackageReference Include="gong-wpf-dragdrop" Version="3.1.1" />
<PackageReference Include="ManagedShell" Version="0.0.240" />
<PackageReference Include="ManagedShell" Version="0.0.245" />
<PackageReference Include="System.Net.Http.Json" Version="6.0.0" />
</ItemGroup>

Expand Down
11 changes: 10 additions & 1 deletion RetroBar/Utilities/Settings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using ManagedShell.AppBar;
using ManagedShell.Common.Helpers;
using ManagedShell.WindowsTray;
using System;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -165,6 +167,13 @@ public bool CollapseNotifyIcons
set => Set(ref _collapseNotifyIcons, value);
}

private List<string> _invertNotifyIcons = new List<string> { NotificationArea.HARDWARE_GUID, NotificationArea.UPDATE_GUID, NotificationArea.MICROPHONE_GUID, NotificationArea.LOCATION_GUID, NotificationArea.MEETNOW_GUID, NotificationArea.NETWORK_GUID, NotificationArea.POWER_GUID, NotificationArea.VOLUME_GUID };
public List<string> InvertNotifyIcons
{
get => _invertNotifyIcons;
set => Set(ref _invertNotifyIcons, value);
}

private string[] _pinnedNotifyIcons = ["7820ae76-23e3-4229-82c1-e41cb67d5b9c", "7820ae75-23e3-4229-82c1-e41cb67d5b9c", "7820ae74-23e3-4229-82c1-e41cb67d5b9c", "7820ae73-23e3-4229-82c1-e41cb67d5b9c"];
public string[] PinnedNotifyIcons
{
Expand Down Expand Up @@ -242,7 +251,7 @@ public bool LockTaskbar
set => Set(ref _lockTaskbar, value);
}

private InvertIconsOption _invertIconsMode = InvertIconsOption.WhenNeededByTheme;
private InvertIconsOption _invertIconsMode = EnvironmentHelper.IsWindows10OrBetter ? InvertIconsOption.WhenNeededByTheme : InvertIconsOption.Never;
public InvertIconsOption InvertIconsMode
{
get => _invertIconsMode;
Expand Down

0 comments on commit 5d67ae0

Please sign in to comment.