Add tooltip to File

This commit is contained in:
Jojo Zhou 2022-12-07 23:44:29 -05:00
parent 8d9cb0f16d
commit c05d3730ba
4 changed files with 72 additions and 7 deletions

View File

@ -45,5 +45,13 @@ namespace Peek.Common.Helpers
return formattedString;
}
public static string FormatResourceString(string resourceId, object? args0, object? args1)
{
var formatString = ResourceLoader.GetForViewIndependentUse().GetString(resourceId);
var formattedString = string.Format(formatString, args0, args1);
return formattedString;
}
}
}

View File

@ -5,11 +5,11 @@
x:Class="Peek.FilePreviewer.FilePreview"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Peek.FilePreviewer.Controls"
xmlns:conv="using:Peek.Common.Converters"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:Peek.FilePreviewer"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:Peek.FilePreviewer.Controls"
mc:Ignorable="d">
<Grid>
@ -21,14 +21,16 @@
<Image
x:Name="PreviewImage"
Source="{x:Bind BitmapPreviewer.Preview, Mode=OneWay}"
ToolTipService.ToolTip="{x:Bind ImageInfoTooltip, Mode=OneWay}"
Visibility="{x:Bind IsImageVisible, Mode=OneWay}" />
<controls:BrowserControl x:Name="PreviewBrowser"
x:Load="True"
Source="{x:Bind BrowserPreviewer.Preview, Mode=OneWay}"
IsNavigationCompleted="{x:Bind BrowserPreviewer.IsPreviewLoaded, Mode=TwoWay}"
Visibility="{x:Bind IsBrowserVisible, Mode=OneWay, FallbackValue=Collapsed}"
NavigationCompleted="PreviewBrowser_NavigationCompleted"/>
<controls:BrowserControl
x:Name="PreviewBrowser"
x:Load="True"
IsNavigationCompleted="{x:Bind BrowserPreviewer.IsPreviewLoaded, Mode=TwoWay}"
NavigationCompleted="PreviewBrowser_NavigationCompleted"
Source="{x:Bind BrowserPreviewer.Preview, Mode=OneWay}"
Visibility="{x:Bind IsBrowserVisible, Mode=OneWay, FallbackValue=Collapsed}" />
<local:UnsupportedFilePreview
DateModified="{x:Bind UnsupportedFilePreviewer.DateModified, Mode=OneWay}"

View File

@ -10,6 +10,7 @@ namespace Peek.FilePreviewer
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Imaging;
using Peek.Common.Helpers;
using Peek.Common.Models;
using Peek.FilePreviewer.Models;
using Peek.FilePreviewer.Previewers;
@ -36,7 +37,9 @@ namespace Peek.FilePreviewer
[NotifyPropertyChangedFor(nameof(IsUnsupportedPreviewVisible))]
[NotifyPropertyChangedFor(nameof(BrowserPreviewer))]
[NotifyPropertyChangedFor(nameof(IsBrowserVisible))]
[NotifyPropertyChangedFor(nameof(ImageInfoTooltip))]
private IPreviewer? previewer;
private string imageTooltip = "No file yet";
public FilePreview()
{
@ -49,6 +52,8 @@ namespace Peek.FilePreviewer
public bool IsImageVisible => BitmapPreviewer != null;
public string ImageInfoTooltip => imageTooltip;
public IUnsupportedFilePreviewer? UnsupportedFilePreviewer => Previewer as IUnsupportedFilePreviewer;
public bool IsUnsupportedPreviewVisible => UnsupportedFilePreviewer != null;
@ -90,6 +95,8 @@ namespace Peek.FilePreviewer
PreviewSizeChanged?.Invoke(this, new PreviewSizeChangedArgs(size));
await Previewer.LoadPreviewAsync();
}
await UpdateImageTooltipAsync();
}
private void PreviewBrowser_NavigationCompleted(WebView2 sender, Microsoft.Web.WebView2.Core.CoreWebView2NavigationCompletedEventArgs args)
@ -97,5 +104,33 @@ namespace Peek.FilePreviewer
// Once browser has completed navigation it is ready to be visible
OnPropertyChanged(nameof(IsBrowserVisible));
}
private async Task UpdateImageTooltipAsync()
{
if (File == null)
{
return;
}
imageTooltip = string.Empty;
// Fetch and format available file properties
imageTooltip += ReadableStringHelper.FormatResourceString("PreviewTooltip_FileName", File.FileName);
string fileType = await PropertyHelper.GetFileType(File.Path);
imageTooltip += string.IsNullOrEmpty(fileType) ? string.Empty : "\n" + ReadableStringHelper.FormatResourceString("PreviewTooltip_FileType", fileType);
string dateModified = File.DateModified.ToString();
imageTooltip += string.IsNullOrEmpty(dateModified) ? string.Empty : "\n" + ReadableStringHelper.FormatResourceString("PreviewTooltip_DateModified", dateModified);
Size dimensions = await PropertyHelper.GetImageSize(File.Path);
imageTooltip += dimensions.IsEmpty ? string.Empty : "\n" + ReadableStringHelper.FormatResourceString("PreviewTooltip_Dimensions", dimensions.Width, dimensions.Height);
ulong bytes = await PropertyHelper.GetFileSizeInBytes(File.Path);
string fileSize = ReadableStringHelper.BytesToReadableString(bytes);
imageTooltip += string.IsNullOrEmpty(fileSize) ? string.Empty : "\n" + ReadableStringHelper.FormatResourceString("PreviewTooltip_FileSize", fileSize);
OnPropertyChanged(nameof(ImageInfoTooltip));
}
}
}

View File

@ -181,4 +181,24 @@
<value>{0} EB</value>
<comment>Abbrivation for the size unit exabyte.</comment>
</data>
<data name="PreviewTooltip_FileName" xml:space="preserve">
<value>Filename: {0}</value>
<comment>Filename for the tooltip of preview. {0} is the name.</comment>
</data>
<data name="PreviewTooltip_FileType" xml:space="preserve">
<value>Item Type: {0}</value>
<comment>Item Type for the tooltip of preview. {0} is the type.</comment>
</data>
<data name="PreviewTooltip_DateModified" xml:space="preserve">
<value>Date Modified: {0}</value>
<comment>Date Modified label for the tooltip of preview. {0} is the date.</comment>
</data>
<data name="PreviewTooltip_Dimensions" xml:space="preserve">
<value>Dimensions: {0} x {1}</value>
<comment>Dimensions label for the tooltip of preview. {0} is the width, {1} is the height.</comment>
</data>
<data name="PreviewTooltip_FileSize" xml:space="preserve">
<value>Size: {0}</value>
<comment>File Size label for the tooltip of preview. {0} is the size.</comment>
</data>
</root>