Cleanup namespaces and hide icon to image conversion from XAML

This commit is contained in:
Peter Kirmeier 2022-11-13 19:58:54 +01:00
parent 384e2e1d74
commit 8001a5324a
8 changed files with 36 additions and 50 deletions

View file

@ -8,10 +8,8 @@ namespace SystemTrayMenu.Business
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
@ -21,14 +19,8 @@ namespace SystemTrayMenu.Business
using SystemTrayMenu.Handler;
using SystemTrayMenu.Helper;
using SystemTrayMenu.Helpers;
using SystemTrayMenu.UserInterface;
using SystemTrayMenu.Utilities;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static SystemTrayMenu.Utilities.IconReader;
using ListView = System.Windows.Controls.ListView;
using Menu = SystemTrayMenu.UserInterface.Menu;
using MessageBox = System.Windows.MessageBox;
using Point = System.Drawing.Point;
internal class Menus : IDisposable
{
@ -650,7 +642,7 @@ namespace SystemTrayMenu.Business
rowData.RowIndex = items.Count; // Index
items.Add(new(
rowData.HiddenEntry ? AddIconOverlay(rowData.Icon, Properties.Resources.White50Percentage) : rowData.Icon,
(rowData.HiddenEntry ? IconReader.AddIconOverlay(rowData.Icon, Properties.Resources.White50Percentage) : rowData.Icon).ToImageSource(),
rowData.Text,
rowData,
rowData.IsAddionalItem && Properties.Settings.Default.ShowOnlyAsSearchResult ? 99 : 0));
@ -1200,7 +1192,7 @@ namespace SystemTrayMenu.Business
private void AdjustMenusSizeAndLocation()
{
Rectangle screenBounds;
Rect screenBounds;
bool isCustomLocationOutsideOfScreen = false;
if (Properties.Settings.Default.AppearAtMouseLocation)
@ -1209,7 +1201,7 @@ namespace SystemTrayMenu.Business
}
else if (Properties.Settings.Default.UseCustomLocation)
{
screenBounds = NativeMethods.Screen.FromPoint(new Point(
screenBounds = NativeMethods.Screen.FromPoint(new (
Properties.Settings.Default.CustomLocationX,
Properties.Settings.Default.CustomLocationY));

View file

@ -94,6 +94,7 @@ namespace SystemTrayMenu
public static Color DarkModeIcons { get; set; }
// TODO: WPF: Remove by not using system drawing colors
public static SolidColorBrush ToSolidColorBrush(this Color color)
{
return new SolidColorBrush(System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B));

View file

@ -7,7 +7,7 @@ namespace SystemTrayMenu.Helper
using System;
using System.Windows.Threading;
public class Fading : IDisposable
public class Fading
{
private const int Interval100FPS = 10; // 100fps=>1s/100fps=~10ms
@ -46,25 +46,11 @@ namespace SystemTrayMenu.Helper
internal bool IsHiding => state == FadingState.Hide;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
internal void Fade(FadingState state)
{
StartStopTimer(state);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
timer.Stop();
}
}
private void StartStopTimer(FadingState newState)
{
if (newState == FadingState.Idle)
@ -168,4 +154,4 @@ namespace SystemTrayMenu.Helper
}
}
}
}
}

View file

@ -5,7 +5,6 @@
namespace SystemTrayMenu.Helper
{
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;

View file

@ -10,9 +10,9 @@ namespace SystemTrayMenu.DllImports
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Windows;
/// <summary>
/// wraps the methodcalls to native windows dll's.
@ -23,9 +23,9 @@ namespace SystemTrayMenu.DllImports
{
private static Point LastCursorPosition = new Point(0, 0);
private static List<Rectangle>? screens;
private static List<Rect>? screens;
public static List<Rectangle> Screens
public static List<Rect> Screens
{
get
{
@ -38,7 +38,7 @@ namespace SystemTrayMenu.DllImports
{
return new()
{
new Rectangle(0, 0, 800, 600),
new (0, 0, 800, 600),
};
}
@ -46,7 +46,7 @@ namespace SystemTrayMenu.DllImports
}
}
public static Rectangle PrimaryScreen => Screens[0];
public static Rect PrimaryScreen => Screens[0];
public static Point CursorPosition
{
@ -62,9 +62,9 @@ namespace SystemTrayMenu.DllImports
}
}
public static Rectangle FromPoint(Point pt)
public static Rect FromPoint(Point pt)
{
foreach (Rectangle screen in Screens)
foreach (Rect screen in Screens)
{
if (screen.Contains(pt))
{

View file

@ -144,7 +144,7 @@
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Height="{DynamicResource RowHeight}">
<Image Width="{DynamicResource ColumnIconWidth}" Margin="0,2,0,3" Source="{Binding ColumnIcon, Converter={utils:IconToImageSourceConverter}}" />
<Image Width="{DynamicResource ColumnIconWidth}" Margin="0,2,0,3" Source="{Binding ColumnIcon}" />
<Label Width="{DynamicResource ColumnTextWidth}" Margin="3,0" Padding="0" VerticalContentAlignment="Center">
<TextBlock TextTrimming="CharacterEllipsis" Text="{Binding ColumnText}"/>
</Label>

View file

@ -8,7 +8,6 @@ namespace SystemTrayMenu.UserInterface
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Reflection;
using System.Windows;
@ -21,8 +20,6 @@ namespace SystemTrayMenu.UserInterface
using SystemTrayMenu.DllImports;
using SystemTrayMenu.Helper;
using SystemTrayMenu.Utilities;
using Color = System.Drawing.Color;
using Point = System.Drawing.Point;
/// <summary>
/// Logic of Menu window.
@ -51,6 +48,7 @@ namespace SystemTrayMenu.UserInterface
timerUpdateIcons.Tick += TimerUpdateIcons_Tick;
Closed += (_, _) =>
{
fading.Fade(Fading.FadingState.Idle);
timerUpdateIcons.Stop();
isClosed = true; // TODO WPF Replace Forms wrapper
};
@ -147,14 +145,14 @@ namespace SystemTrayMenu.UserInterface
customScrollbar.Name = "customScrollbar";
customScrollbar.Size = new Size(Scaling.Scale(15), 40);
#endif
SolidColorBrush foreColor = Color.Black.ToSolidColorBrush();
SolidColorBrush foreColor = new(Colors.Black);
SolidColorBrush backColor = AppColors.Background.ToSolidColorBrush();
SolidColorBrush backColorSearch = AppColors.SearchField.ToSolidColorBrush();
SolidColorBrush backgroundBorder = AppColors.BackgroundBorder.ToSolidColorBrush();
if (Config.IsDarkMode())
{
foreColor = Color.White.ToSolidColorBrush();
foreColor = new (Colors.White);
backColor = AppColors.DarkModeBackground.ToSolidColorBrush();
backColorSearch = AppColors.DarkModeSearchField.ToSolidColorBrush();
backgroundBorder = AppColors.DarkModeBackgroundBorder.ToSolidColorBrush();
@ -436,8 +434,7 @@ namespace SystemTrayMenu.UserInterface
buttonOpenFolder.Visibility = Visibility.Collapsed;
// Todo: use embedded resources that we can assign image in XAML already
pictureBoxLoading.Source = (ImageSource)new IconToImageSourceConverter().Convert(
SystemTrayMenu.Resources.StaticResources.LoadingIcon, typeof(ImageSource), null, CultureInfo.InvariantCulture);
pictureBoxLoading.Source = SystemTrayMenu.Resources.StaticResources.LoadingIcon.ToImageSource();
pictureBoxLoading.Visibility = Visibility.Visible;
break;
default:
@ -571,7 +568,7 @@ namespace SystemTrayMenu.UserInterface
/// <param name="startLocation">Defines where the first menu is drawn (when no predecessor is set).</param>
/// <param name="isCustomLocationOutsideOfScreen">isCustomLocationOutsideOfScreen.</param>
internal void AdjustSizeAndLocation(
Rectangle bounds,
Rect bounds,
Menu menuPredecessor,
StartLocation startLocation,
bool isCustomLocationOutsideOfScreen)
@ -1282,7 +1279,7 @@ namespace SystemTrayMenu.UserInterface
if (rowData.IconLoading)
{
iconsToUpdate++;
row.ColumnIcon = rowData.ReadIcon(false);
row.ColumnIcon = rowData.ReadIcon(false).ToImageSource();
}
}
@ -1369,7 +1366,7 @@ namespace SystemTrayMenu.UserInterface
/// </summary>
internal class ListViewItemData
{
public ListViewItemData(Icon columnIcon, string columnText, RowData rowData, int sortIndex)
public ListViewItemData(ImageSource columnIcon, string columnText, RowData rowData, int sortIndex)
{
ColumnIcon = columnIcon;
ColumnText = columnText;
@ -1377,7 +1374,7 @@ namespace SystemTrayMenu.UserInterface
SortIndex = sortIndex;
}
public Icon ColumnIcon { get; set; }
public ImageSource ColumnIcon { get; set; }
public string ColumnText { get; set; }

View file

@ -8,10 +8,12 @@
namespace SystemTrayMenu.Utilities
{
using System;
using System.Drawing;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Point = System.Windows.Point;
internal static class WPFExtensions
{
@ -58,8 +60,17 @@ namespace SystemTrayMenu.Utilities
internal static Point GetRelativeChildPositionTo(this Visual parent, Visual child)
{
var pt = child.TransformToAncestor(parent).Transform(new(0, 0));
return new (pt.X, pt.Y);
return child.TransformToAncestor(parent).Transform(new(0, 0));
}
// TODO: Find and remove any unnecessary convertions
internal static ImageSource ToImageSource(this Icon icon)
{
return (ImageSource)new IconToImageSourceConverter().Convert(
icon,
typeof(ImageSource),
null,
CultureInfo.InvariantCulture);
}
}
}