From 40871c7e962e6e3114f3bad3b85163a03a9780a8 Mon Sep 17 00:00:00 2001 From: Peter Kirmeier Date: Sun, 23 Apr 2023 23:53:20 +0200 Subject: [PATCH] Fix and improve fading and transparency --- Business/Menus.cs | 36 +++++++++++++++++++++------------- UserInterface/Menu.xaml | 14 +++++++++++-- UserInterface/Menu.xaml.cs | 40 +++++++++++++++++++------------------- 3 files changed, 55 insertions(+), 35 deletions(-) diff --git a/Business/Menus.cs b/Business/Menus.cs index 145b06d..d14fbb1 100644 --- a/Business/Menus.cs +++ b/Business/Menus.cs @@ -444,7 +444,11 @@ namespace SystemTrayMenu.Business IconReader.IsPreloading = false; if (showMenuAfterMainPreload) { - AsEnumerable.ToList().ForEach(m => { m.ShowWithFade(); }); + Menu? menu = menus[0]; + if (menu != null) + { + menu.ShowWithFade(); + } } } else @@ -579,8 +583,14 @@ namespace SystemTrayMenu.Business { if (IsActive() && IsMainUsable) { - AsList.ForEach(m => m.ShowWithFade()); timerStillActiveCheck.Stop(); + + // Bring transparent menus back + foreach (Menu? menu in menus.Where(m => m != null && m.Opacity != 1D)) + { + menu!.ActivateWithFade(); + } + timerStillActiveCheck.Start(); } } @@ -621,7 +631,7 @@ namespace SystemTrayMenu.Business { HideOldMenu(menu, true); menus[menu.Level] = menu; - menu.ShowWithFadeOrTransparent(IsActive()); + menu.ShowWithFade(!IsActive()); } } @@ -917,12 +927,12 @@ namespace SystemTrayMenu.Business { if (!keyboardInput.InUse) { - AsList.ForEach(menu => menu.ShowTransparent()); + AsList.ForEach(menu => menu.ShowWithFade(true)); } } else if (Config.AlwaysOpenByPin) { - AsList.ForEach(menu => menu.ShowTransparent()); + AsList.ForEach(menu => menu.ShowWithFade(true)); } else { @@ -949,7 +959,7 @@ namespace SystemTrayMenu.Business joystickHelper.Disable(); } - private void GetScreenBounds(out Rect screenBounds, out bool useCustomLocation, out Menu.StartLocation startLocation) + private void GetScreenBounds(out Rect screenBounds, out bool useCustomLocation, out StartLocation startLocation) { if (Settings.Default.AppearAtMouseLocation) { @@ -985,33 +995,33 @@ namespace SystemTrayMenu.Business case TaskbarPosition.Left: screenBounds.X += taskbar.Size.Width; screenBounds.Width -= taskbar.Size.Width; - startLocation = Menu.StartLocation.BottomLeft; + startLocation = StartLocation.BottomLeft; break; case TaskbarPosition.Right: screenBounds.Width -= taskbar.Size.Width; - startLocation = Menu.StartLocation.BottomRight; + startLocation = StartLocation.BottomRight; break; case TaskbarPosition.Top: screenBounds.Y += taskbar.Size.Height; screenBounds.Height -= taskbar.Size.Height; - startLocation = Menu.StartLocation.TopRight; + startLocation = StartLocation.TopRight; break; case TaskbarPosition.Bottom: default: screenBounds.Height -= taskbar.Size.Height; - startLocation = Menu.StartLocation.BottomRight; + startLocation = StartLocation.BottomRight; break; } if (Settings.Default.AppearAtTheBottomLeft) { - startLocation = Menu.StartLocation.BottomLeft; + startLocation = StartLocation.BottomLeft; } } private void AdjustMenusSizeAndLocation(int startLevel) { - GetScreenBounds(out Rect screenBounds, out bool useCustomLocation, out Menu.StartLocation startLocation); + GetScreenBounds(out Rect screenBounds, out bool useCustomLocation, out StartLocation startLocation); Menu menu; Menu? menuPredecessor = null; @@ -1035,7 +1045,7 @@ namespace SystemTrayMenu.Business !Settings.Default.UseCustomLocation && i == 0) { - const int overlapTolerance = 4; + const double overlapTolerance = 4D; // Remember width of the initial menu as we don't want to overlap with it if (taskbarPosition == TaskbarPosition.Left) diff --git a/UserInterface/Menu.xaml b/UserInterface/Menu.xaml index e9ba0ce..3dbb683 100644 --- a/UserInterface/Menu.xaml +++ b/UserInterface/Menu.xaml @@ -16,12 +16,22 @@ + + + + + + + @@ -31,7 +41,7 @@ diff --git a/UserInterface/Menu.xaml.cs b/UserInterface/Menu.xaml.cs index 7a0640f..d0b31e5 100644 --- a/UserInterface/Menu.xaml.cs +++ b/UserInterface/Menu.xaml.cs @@ -28,6 +28,9 @@ namespace SystemTrayMenu.UserInterface { private const int CornerRadius = 10; + private static readonly RoutedEvent FadeToTransparentEvent = EventManager.RegisterRoutedEvent( + nameof(FadeToTransparent), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Menu)); + private static readonly RoutedEvent FadeInEvent = EventManager.RegisterRoutedEvent( nameof(FadeIn), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Menu)); @@ -305,6 +308,12 @@ namespace SystemTrayMenu.UserInterface internal event Action? CellMouseClick; + internal event RoutedEventHandler FadeToTransparent + { + add { AddHandler(FadeToTransparentEvent, value); } + remove { RemoveHandler(FadeToTransparentEvent, value); } + } + internal event RoutedEventHandler FadeIn { add { AddHandler(FadeInEvent, value); } @@ -460,31 +469,25 @@ namespace SystemTrayMenu.UserInterface SetCounts(foldersCount, filesCount); } - internal void ShowWithFadeOrTransparent(bool formActiveFormIsMenu) + internal void ActivateWithFade() { - if (formActiveFormIsMenu) + if (Settings.Default.UseFading) { - ShowWithFade(); + isFading = true; + RaiseEvent(new(routedEvent: FadeInEvent)); } else { - ShowTransparent(); + Opacity = 1D; + FadeIn_Completed(this, new()); } } - internal void ShowWithFade() => Fading_Show(false); - - internal void ShowTransparent() => Fading_Show(true); - - internal void Fading_Show(bool transparency) + internal void ShowWithFade(bool transparency = false) { timerUpdateIcons.Start(); - if (Level == 0) - { - Activate(); - } - else + if (Level > 0) { ShowActivated = false; } @@ -497,8 +500,7 @@ namespace SystemTrayMenu.UserInterface isFading = true; if (transparency) { - // TODO: FADING: Instead setting of opacity 100% only go up to 80% (Temporarily go to 100% as well) - RaiseEvent(new(routedEvent: FadeInEvent)); + RaiseEvent(new(routedEvent: FadeToTransparentEvent)); } else { @@ -517,8 +519,6 @@ namespace SystemTrayMenu.UserInterface if (Settings.Default.UseFading) { isFading = true; - - // TODO: FADING: Instead starting at opacity 100% it should start with 80% due to transparency setting RaiseEvent(new(routedEvent: FadeOutEvent)); } else @@ -545,7 +545,7 @@ namespace SystemTrayMenu.UserInterface StartLocation startLocation, bool useCustomLocation) { - Point originLocation = new(0.0D, 0.0D); + Point originLocation = new(0D, 0D); // Update the height and width AdjustDataGridViewHeight(menuPredecessor, bounds.Height); @@ -599,7 +599,7 @@ namespace SystemTrayMenu.UserInterface void AdjustWindowPositionInternal(in Point originLocation) { double scaling = Math.Round(Scaling.Factor, 0, MidpointRounding.AwayFromZero); - double overlappingOffset = 0.0D; + double overlappingOffset = 0D; // Make sure we have latest values of own window size UpdateLayout();