Let the framework calculate text width of list entries

This will improve performance as less objects will be rendered for calculation.
Will also fix "flickering" of sub menu at wrong (not yet calculate) window position.
This commit is contained in:
Peter Kirmeier 2023-08-21 20:05:31 +02:00
parent 892b89e0e2
commit 0f1f1748a4
3 changed files with 4 additions and 55 deletions

View file

@ -355,18 +355,8 @@ namespace SystemTrayMenu.Business
if (menuData.DirectoryState != MenuDataDirectoryState.Undefined)
{
// Sub Menu (completed)
// As we need to render it during calculations but we don't want to show flickering windows,
// we just hide it for just a moment and show it again after updates have been applied.
// In order to hide it via Opacity we have to cancel any Fade animations.
menu.StopFade();
menu.Opacity = 0D;
menu.AddItemsToMenu(menuData.RowDatas, menuData.DirectoryState);
AdjustMenusSizeAndLocation(menu.Level);
menu.Opacity = 1D;
// Maybe this can be improved: It may happen that before stop event takes action,
// the previous FadeIn overwrites the opacity, so for safety we respawn a new FadeIn.
menu.StartFadeIn();
}
else
{

View file

@ -46,11 +46,6 @@
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="local:Menu.FadeStop">
<StopStoryboard BeginStoryboardName="FadeToTransparentStoryboard" />
<StopStoryboard BeginStoryboardName="FadeInStoryboard" />
<StopStoryboard BeginStoryboardName="FadeOutStoryboard"/>
</EventTrigger>
</Window.Triggers>
<Window.Resources>
@ -100,7 +95,7 @@
</DrawingImage>
<sys:Double x:Key="RowHeight">20</sys:Double>
<sys:Double x:Key="ColumnIconWidth">20</sys:Double>
<sys:Double x:Key="ColumnTextWidth">60</sys:Double>
<sys:Double x:Key="ColumnTextMaxWidth">80</sys:Double>
</Window.Resources>
<!-- Remember to adjust the windows's shadow effect accordingly to the size of the margin -->
@ -194,7 +189,7 @@
<Border Height="{DynamicResource RowHeight}" BorderBrush="{Binding BorderBrush}" Background="{Binding BackgroundBrush}" BorderThickness="1">
<StackPanel Orientation="Horizontal">
<Image Width="{DynamicResource ColumnIconWidth}" Margin="0" Source="{Binding ColumnIcon}" />
<TextBlock Width="{DynamicResource ColumnTextWidth}" Margin="5,0,2,0" Padding="0" VerticalAlignment="Center" TextTrimming="CharacterEllipsis"
<TextBlock MaxWidth="{DynamicResource ColumnTextMaxWidth}" Margin="5,0,2,0" Padding="0" VerticalAlignment="Center" TextTrimming="CharacterEllipsis"
Text="{Binding ColumnText}" Foreground="{x:Static stm:MenuDefines.ColorForeground}" />
<!-- TODO: Optional or only for trimmed entries: ToolTip="{Binding ColumnText}" -->
</StackPanel>

View file

@ -7,11 +7,9 @@ namespace SystemTrayMenu.UserInterface
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
@ -37,9 +35,6 @@ namespace SystemTrayMenu.UserInterface
private static readonly RoutedEvent FadeOutEvent = EventManager.RegisterRoutedEvent(
nameof(FadeOut), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Menu));
private static readonly RoutedEvent FadeStopEvent = EventManager.RegisterRoutedEvent(
nameof(FadeStop), RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(Menu));
private readonly string folderPath;
private bool isShellContextMenuOpen;
@ -246,12 +241,6 @@ namespace SystemTrayMenu.UserInterface
remove { RemoveHandler(FadeOutEvent, value); }
}
internal event RoutedEventHandler FadeStop
{
add { AddHandler(FadeStopEvent, value); }
remove { RemoveHandler(FadeStopEvent, value); }
}
internal enum StartLocation
{
Point,
@ -528,14 +517,6 @@ namespace SystemTrayMenu.UserInterface
}
}
internal void StopFade()
{
if (Settings.Default.UseFading)
{
RaiseEvent(new(routedEvent: FadeStopEvent));
}
}
/// <summary>
/// Update the position and size of the menu.
/// </summary>
@ -978,26 +959,9 @@ namespace SystemTrayMenu.UserInterface
double icoWidth = 16 * Scaling.FactorByDpi;
Resources["ColumnIconWidth"] = Math.Ceiling(icoWidth * factorIconSizeInPercent * Scaling.Factor);
double renderedMaxWidth = 0D;
foreach (RowData item in dgv.Items)
{
double renderedWidth = new FormattedText(
item.ColumnText,
CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
new Typeface(dgv.FontFamily, dgv.FontStyle, dgv.FontWeight, dgv.FontStretch),
dgv.FontSize,
dgv.Foreground,
VisualTreeHelper.GetDpi(this).PixelsPerDip).Width;
if (renderedWidth > renderedMaxWidth)
{
renderedMaxWidth = renderedWidth;
}
}
// Margin of the windowFrame is allowed to exceed the boundaries, so we just add them afterwards
Resources["ColumnTextWidth"] = Math.Ceiling(
Math.Min(renderedMaxWidth, (double)(Scaling.Factor * Scaling.FactorByDpi * 400f * (Settings.Default.WidthMaxInPercent / 100f)))
Resources["ColumnTextMaxWidth"] = Math.Ceiling(
((double)Scaling.Factor * Scaling.FactorByDpi * 400D * (Settings.Default.WidthMaxInPercent / 100D))
+ windowFrame.Margin.Left + windowFrame.Margin.Right);
}