[Peek]Update FilePreviewer to prevent tooltips from obscuring title bar controls (#34718)

* Update FilePreviewer to prevent tooltips from obscuring title bar controls. Fixes #34496

* Small tidy to pointer move handler and StringBuilder setup.
This commit is contained in:
Dave Rayment 2024-09-23 16:00:28 +01:00 committed by GitHub
parent 035d70dd04
commit a70aafb3b8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 6 deletions

View File

@ -29,18 +29,25 @@
x:Name="ImagePreview"
MaxWidth="{x:Bind ImagePreviewer.MaxImageSize.Width, Mode=OneWay}"
MaxHeight="{x:Bind ImagePreviewer.MaxImageSize.Height, Mode=OneWay}"
PointerMoved="ToolTipParentControl_PointerMoved"
Source="{x:Bind ImagePreviewer.Preview, Mode=OneWay}"
ToolTipService.ToolTip="{x:Bind InfoTooltip, Mode=OneWay}"
Visibility="{x:Bind IsPreviewVisible(ImagePreviewer, Previewer.State), Mode=OneWay}" />
Visibility="{x:Bind IsPreviewVisible(ImagePreviewer, Previewer.State), Mode=OneWay}">
<ToolTipService.ToolTip>
<ToolTip Content="{x:Bind InfoTooltip, Mode=OneWay}" />
</ToolTipService.ToolTip>
</Image>
<MediaPlayerElement
x:Name="VideoPreview"
AreTransportControlsEnabled="True"
AutoPlay="True"
FlowDirection="LeftToRight"
PointerMoved="ToolTipParentControl_PointerMoved"
Source="{x:Bind VideoPreviewer.Preview, Mode=OneWay}"
ToolTipService.ToolTip="{x:Bind InfoTooltip, Mode=OneWay}"
Visibility="{x:Bind IsPreviewVisible(VideoPreviewer, Previewer.State), Mode=OneWay}">
<ToolTipService.ToolTip>
<ToolTip Content="{x:Bind InfoTooltip, Mode=OneWay}" />
</ToolTipService.ToolTip>
<MediaPlayerElement.KeyboardAccelerators>
<KeyboardAccelerator Key="Space" Invoked="KeyboardAccelerator_Space_Invoked" />
</MediaPlayerElement.KeyboardAccelerators>

View File

@ -13,6 +13,7 @@ using ManagedCommon;
using Microsoft.PowerToys.Telemetry;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Input;
using Microsoft.Web.WebView2.Core;
using Peek.Common.Extensions;
@ -340,10 +341,8 @@ namespace Peek.FilePreviewer
}
// Fetch and format available file properties
var sb = new StringBuilder();
string fileNameFormatted = ReadableStringHelper.FormatResourceString("PreviewTooltip_FileName", Item.Name);
sb.Append(fileNameFormatted);
var sb = new StringBuilder(fileNameFormatted, 256);
cancellationToken.ThrowIfCancellationRequested();
string fileType = await Task.Run(Item.GetContentTypeAsync);
@ -360,5 +359,23 @@ namespace Peek.FilePreviewer
InfoTooltip = sb.ToString();
}
/// <summary>
/// Set the placement of the tooltip for those previewers supporting the feature, ensuring it does not obscure the Main Window's title bar.
/// </summary>
private void ToolTipParentControl_PointerMoved(object sender, PointerRoutedEventArgs e)
{
var previewControl = sender as FrameworkElement;
if (previewControl != null)
{
var toolTip = ToolTipService.GetToolTip(previewControl) as ToolTip;
if (toolTip != null)
{
var pos = e.GetCurrentPoint(previewControl).Position;
toolTip.Placement = pos.Y < previewControl.ActualHeight / 2 ?
PlacementMode.Bottom : PlacementMode.Top;
}
}
}
}
}