Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion KeyStats.Windows/KeyStats/Services/StatsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,8 @@ private void NotifyClickThresholdIfNeeded()

public string FormatNumber(int number)
{
if (number >= 1_000_000)
// 999,950+ would round to "1000.0k", so switch to M before that.
if (number >= 999_950)
return $"{number / 1_000_000.0:F1}M";
if (number >= 1_000)
return $"{number / 1_000.0:F1}k";
Expand Down
18 changes: 9 additions & 9 deletions KeyStats.Windows/KeyStats/Views/FloatingStatsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="clr-namespace:KeyStats.Properties"
Title="{x:Static p:Strings.FloatingStats_WindowTitle}"
Width="35"
SizeToContent="Width"
Height="41"
WindowStyle="None"
AllowsTransparency="True"
Expand All @@ -24,11 +24,15 @@
SnapsToDevicePixels="True"
MouseLeftButtonDown="RootBorder_MouseLeftButtonDown">
<Grid>
<Grid x:Name="SingleRowLayout" Margin="2,1" Visibility="Collapsed">
<Grid x:Name="SingleRowLayout"
Margin="4,1"
HorizontalAlignment="Center"
Grid.IsSharedSizeScope="True"
Visibility="Collapsed">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="Value"/>
<ColumnDefinition Width="3"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="Value"/>
</Grid.ColumnDefinitions>

<TextBlock x:Name="SinglePrimaryValueTextBlock"
Expand All @@ -38,7 +42,6 @@
FontWeight="Medium"
Foreground="{DynamicResource TextPrimaryBrush}"
TextAlignment="Center"
TextTrimming="CharacterEllipsis"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
ToolTip="{Binding PrimaryFullValue}"/>
Expand All @@ -57,13 +60,12 @@
FontWeight="Medium"
Foreground="{DynamicResource TextPrimaryBrush}"
TextAlignment="Center"
TextTrimming="CharacterEllipsis"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
ToolTip="{Binding SecondaryFullValue}"/>
</Grid>

<Grid x:Name="DoubleRowLayout" Margin="0,1">
<Grid x:Name="DoubleRowLayout" Margin="1,1">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="3"/>
Expand All @@ -77,7 +79,6 @@
FontWeight="Medium"
Foreground="{DynamicResource TextPrimaryBrush}"
TextAlignment="Center"
TextTrimming="CharacterEllipsis"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
ToolTip="{Binding PrimaryFullValue}"/>
Expand All @@ -96,7 +97,6 @@
FontWeight="Medium"
Foreground="{DynamicResource TextPrimaryBrush}"
TextAlignment="Center"
TextTrimming="CharacterEllipsis"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
ToolTip="{Binding SecondaryFullValue}"/>
Expand Down
47 changes: 41 additions & 6 deletions KeyStats.Windows/KeyStats/Views/FloatingStatsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public FloatingStatsWindow()
Loaded += OnLoaded;
Closed += OnClosed;
LocationChanged += OnLocationChanged;
SizeChanged += OnSizeChanged;
ThemeManager.Instance.ThemeChanged += OnThemeChanged;
SystemEvents.DisplaySettingsChanged += OnDisplaySettingsChanged;
}
Expand Down Expand Up @@ -235,15 +236,49 @@ private bool ApplyLayoutSettings()
var baseHeight = useDoubleRow ? DoubleRowHeight : SingleRowHeight;
var targetWidth = Math.Round(baseWidth * layoutScale, MidpointRounding.AwayFromZero);
var targetHeight = Math.Round(baseHeight * layoutScale, MidpointRounding.AwayFromZero);
var sizeChanged = !Width.Equals(targetWidth) || !Height.Equals(targetHeight);
var sizeChanged = !MinWidth.Equals(targetWidth) || !Height.Equals(targetHeight);

SingleRowLayout.Visibility = useDoubleRow ? Visibility.Collapsed : Visibility.Visible;
DoubleRowLayout.Visibility = useDoubleRow ? Visibility.Visible : Visibility.Collapsed;
Width = targetWidth;
// Width follows content (SizeToContent) so large values are never truncated.
MinWidth = targetWidth;
Height = targetHeight;
return sizeChanged;
}

private void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
if (!_isLoaded || !e.WidthChanged)
{
return;
}

// Keep the right edge fixed when the window sits on the right half of its screen,
// so it grows toward the screen center instead of jumping when the value widens.
var widthDelta = e.NewSize.Width - e.PreviousSize.Width;
var workingAreas = GetWorkingAreasInDips();
var previousBounds = new Rect(Left, Top, e.PreviousSize.Width, Height);
var area = FindWorkingAreaByDeviceName(GetCurrentMonitorDeviceName(), workingAreas)
?? FindBestWorkingArea(previousBounds, workingAreas);
var areaCenterX = area != null
? area.Bounds.Left + area.Bounds.Width / 2
: SystemParameters.WorkArea.Left + SystemParameters.WorkArea.Width / 2;
if (previousBounds.Left + previousBounds.Width / 2 > areaCenterX)
{
_isRestoringPosition = true;
try
{
Left -= widthDelta;
}
finally
{
_isRestoringPosition = false;
}
}

EnsureVisiblePosition();
}

private void OnLocationChanged(object? sender, EventArgs e)
{
if (!_isLoaded || _isRestoringPosition)
Expand Down Expand Up @@ -287,11 +322,11 @@ private void RestorePosition()
workingAreas);
var preferredArea = savedArea ?? primaryArea;
var requestedBounds = settings.FloatingStatsLeft.HasValue && settings.FloatingStatsTop.HasValue
? new Rect(settings.FloatingStatsLeft.Value, settings.FloatingStatsTop.Value, Width, Height)
? new Rect(settings.FloatingStatsLeft.Value, settings.FloatingStatsTop.Value, ActualWidth, Height)
: new Rect(
preferredArea.Bounds.Right - Width - EdgeMargin,
preferredArea.Bounds.Right - ActualWidth - EdgeMargin,
preferredArea.Bounds.Top + EdgeMargin,
Width,
ActualWidth,
Height);

var targetArea = savedArea ?? FindBestWorkingArea(requestedBounds, workingAreas) ?? preferredArea;
Expand Down Expand Up @@ -327,7 +362,7 @@ private void ClampCurrentPositionToWorkingArea()
var preferredArea = workingAreas.Count > 0
? workingAreas[0]
: new WorkingAreaInfo(string.Empty, SystemParameters.WorkArea);
var bounds = new Rect(Left, Top, Width, Height);
var bounds = new Rect(Left, Top, ActualWidth, Height);
var currentArea = FindWorkingAreaByDeviceName(GetCurrentMonitorDeviceName(), workingAreas);
var targetArea = currentArea ?? FindBestWorkingArea(bounds, workingAreas) ?? preferredArea;
var clamped = ClampToArea(bounds, targetArea.Bounds);
Expand Down
Loading