From 8001a5324aaa4ae98da56642b31c4ab1a6f7fffb Mon Sep 17 00:00:00 2001 From: Peter Kirmeier Date: Sun, 13 Nov 2022 19:58:54 +0100 Subject: [PATCH] Cleanup namespaces and hide icon to image conversion from XAML --- Business/Menus.cs | 14 +++----------- Config/AppColors.cs | 1 + Helpers/Fading.cs | 18 ++---------------- Helpers/ImagingHelper.cs | 1 - NativeDllImport/Screen.cs | 14 +++++++------- UserInterface/Menu.xaml | 2 +- UserInterface/Menu.xaml.cs | 19 ++++++++----------- Utilities/WPFExtensions.cs | 17 ++++++++++++++--- 8 files changed, 36 insertions(+), 50 deletions(-) diff --git a/Business/Menus.cs b/Business/Menus.cs index e562b63..ddf1350 100644 --- a/Business/Menus.cs +++ b/Business/Menus.cs @@ -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)); diff --git a/Config/AppColors.cs b/Config/AppColors.cs index aa082df..ce4e896 100644 --- a/Config/AppColors.cs +++ b/Config/AppColors.cs @@ -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)); diff --git a/Helpers/Fading.cs b/Helpers/Fading.cs index 6a54840..c987524 100644 --- a/Helpers/Fading.cs +++ b/Helpers/Fading.cs @@ -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 } } } -} \ No newline at end of file +} diff --git a/Helpers/ImagingHelper.cs b/Helpers/ImagingHelper.cs index ec70f96..5df7398 100644 --- a/Helpers/ImagingHelper.cs +++ b/Helpers/ImagingHelper.cs @@ -5,7 +5,6 @@ namespace SystemTrayMenu.Helper { using System.Drawing; - using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; diff --git a/NativeDllImport/Screen.cs b/NativeDllImport/Screen.cs index 51dcf2a..c662391 100644 --- a/NativeDllImport/Screen.cs +++ b/NativeDllImport/Screen.cs @@ -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; /// /// 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? screens; + private static List? screens; - public static List Screens + public static List 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)) { diff --git a/UserInterface/Menu.xaml b/UserInterface/Menu.xaml index df067c2..ea2c557 100644 --- a/UserInterface/Menu.xaml +++ b/UserInterface/Menu.xaml @@ -144,7 +144,7 @@ - + diff --git a/UserInterface/Menu.xaml.cs b/UserInterface/Menu.xaml.cs index ba6d8fa..1c44bd7 100644 --- a/UserInterface/Menu.xaml.cs +++ b/UserInterface/Menu.xaml.cs @@ -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; /// /// 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 /// Defines where the first menu is drawn (when no predecessor is set). /// isCustomLocationOutsideOfScreen. 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 /// 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; } diff --git a/Utilities/WPFExtensions.cs b/Utilities/WPFExtensions.cs index d084fef..ff8d648 100644 --- a/Utilities/WPFExtensions.cs +++ b/Utilities/WPFExtensions.cs @@ -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); } } }