set window icon/title and use centralized logger (#30091)

This commit is contained in:
Davide Giacometti 2023-12-09 13:15:24 +01:00 коммит произвёл GitHub
Родитель 9e03386eb3
Коммит f30438b959
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
15 изменённых файлов: 29 добавлений и 96 удалений

Просмотреть файл

@ -1,78 +0,0 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using Scripting;
namespace Peek.Common.Helpers
{
public static class Logger
{
private static readonly IFileSystem _fileSystem = new FileSystemObject();
private static readonly string ApplicationLogPath = Path.Combine(interop.Constants.AppDataPath(), "Peek\\Logs");
static Logger()
{
if (!_fileSystem.FolderExists(ApplicationLogPath))
{
_fileSystem.CreateFolder(ApplicationLogPath);
}
// Using InvariantCulture since this is used for a log file name
var logFilePath = _fileSystem.BuildPath(ApplicationLogPath, "Log_" + DateTime.Now.ToString(@"yyyy-MM-dd", CultureInfo.InvariantCulture) + ".txt");
Trace.Listeners.Add(new TextWriterTraceListener(logFilePath));
Trace.AutoFlush = true;
}
public static void LogError(string message)
{
Log(message, "ERROR");
}
public static void LogError(string message, Exception ex)
{
Log(
message + Environment.NewLine +
ex?.Message + Environment.NewLine +
"Inner exception: " + Environment.NewLine +
ex?.InnerException?.Message + Environment.NewLine +
"Stack trace: " + Environment.NewLine +
ex?.StackTrace,
"ERROR");
}
public static void LogWarning(string message)
{
Log(message, "WARNING");
}
public static void LogInfo(string message)
{
Log(message, "INFO");
}
private static void Log(string message, string type)
{
Trace.WriteLine(type + ": " + DateTime.Now.TimeOfDay);
Trace.Indent();
Trace.WriteLine(GetCallerInfo());
Trace.WriteLine(message);
Trace.Unindent();
}
private static string GetCallerInfo()
{
StackTrace stackTrace = new StackTrace();
var methodName = stackTrace.GetFrame(3)?.GetMethod();
var className = methodName?.DeclaringType?.Name ?? string.Empty;
return "[Method]: " + methodName?.Name + " [Class]: " + className;
}
}
}

Просмотреть файл

@ -3,10 +3,8 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Peek.Common.Extensions;
using Peek.Common.Helpers;
using ManagedCommon;
using Windows.Storage;
#nullable enable

Просмотреть файл

@ -3,10 +3,8 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Peek.Common.Extensions;
using Peek.Common.Helpers;
using ManagedCommon;
using Windows.Storage;
#nullable enable

Просмотреть файл

@ -32,6 +32,7 @@
<ItemGroup>
<ProjectReference Include="..\..\..\common\interop\PowerToys.Interop.vcxproj" />
<ProjectReference Include="..\..\..\common\ManagedCommon\ManagedCommon.csproj" />
<ProjectReference Include="..\..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" />
</ItemGroup>
</Project>

Просмотреть файл

@ -4,12 +4,12 @@
using System;
using System.Threading.Tasks;
using ManagedCommon;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.Web.WebView2.Core;
using Peek.Common.Constants;
using Peek.Common.Helpers;
using Windows.ApplicationModel.DataTransfer;
using Windows.System;
using Windows.UI;

Просмотреть файл

@ -8,6 +8,7 @@ using System.Text;
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using ManagedCommon;
using Microsoft.PowerToys.Telemetry;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

Просмотреть файл

@ -8,9 +8,9 @@ using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using ManagedCommon;
using Microsoft.UI.Xaml.Media.Imaging;
using Peek.Common;
using Peek.Common.Helpers;
using Peek.Common.Models;
using Peek.FilePreviewer.Previewers.Helpers;

Просмотреть файл

@ -8,6 +8,7 @@ using System.IO;
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;
using ManagedCommon;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;

Просмотреть файл

@ -4,8 +4,7 @@
using System;
using System.IO;
using System.Runtime.CompilerServices;
using Peek.Common.Helpers;
using ManagedCommon;
using Peek.Common.Models;
namespace Peek.UI.Extensions

Просмотреть файл

@ -4,7 +4,7 @@
using System.Runtime.InteropServices;
using System.Text;
using Peek.Common.Helpers;
using ManagedCommon;
using Peek.Common.Models;
using Peek.UI.Native;
@ -30,7 +30,7 @@ namespace Peek.UI.Helpers
ret = NativeMethods.AssocQueryString(NativeMethods.AssocF.Verify, NativeMethods.AssocStr.FriendlyAppName, extension, null, sb, ref length);
if (ret != HResult.Ok)
{
Logger.LogError($"Error when getting accessString for {extension} file: {Marshal.GetExceptionForHR((int)ret)!.Message}" );
Logger.LogError($"Error when getting accessString for {extension} file: {Marshal.GetExceptionForHR((int)ret)!.Message}");
return appName;
}

Просмотреть файл

@ -5,6 +5,7 @@
using System;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;
using ManagedCommon;
using Microsoft.UI.Xaml;
using Peek.Common.Helpers;
using Peek.Common.Models;
@ -15,6 +16,7 @@ namespace Peek.UI
{
public partial class MainWindowViewModel : ObservableObject
{
private static readonly string _defaultWindowTitle = ResourceLoaderInstance.ResourceLoader.GetString("AppTitle/Title");
private const int NavigationThrottleDelayMs = 100;
[ObservableProperty]
@ -23,6 +25,16 @@ namespace Peek.UI
[ObservableProperty]
private IFileSystemItem? _currentItem;
partial void OnCurrentItemChanged(IFileSystemItem? value)
{
WindowTitle = value != null
? ReadableStringHelper.FormatResourceString("WindowTitle", value.Name)
: _defaultWindowTitle;
}
[ObservableProperty]
private string _windowTitle;
[ObservableProperty]
private NeighboringItems? _items;
@ -36,6 +48,7 @@ namespace Peek.UI
public MainWindowViewModel(NeighboringItemsQuery query)
{
NeighboringItemsQuery = query;
WindowTitle = _defaultWindowTitle;
NavigationThrottleTimer.Tick += NavigationThrottleTimer_Tick;
NavigationThrottleTimer.Interval = TimeSpan.FromMilliseconds(NavigationThrottleDelayMs);

Просмотреть файл

@ -10,6 +10,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="using:Peek.UI.Views"
xmlns:winuiex="using:WinUIEx"
Title="{x:Bind ViewModel.WindowTitle, Mode=OneWay}"
MinWidth="450"
MinHeight="400"
mc:Ignorable="d">

Просмотреть файл

@ -47,6 +47,7 @@ namespace Peek.UI
ViewModel = Application.Current.GetService<MainWindowViewModel>();
TitleBarControl.SetTitleBarToWindow(this);
AppWindow.SetIcon("Assets/Peek/Icon.ico");
AppWindow.Closing += AppWindow_Closing;
}

Просмотреть файл

@ -21,7 +21,6 @@ using Peek.UI.Telemetry.Events;
using Windows.Graphics;
using Windows.Storage;
using Windows.System;
using WinUIEx;
namespace Peek.UI.Views
{
@ -123,11 +122,6 @@ namespace Peek.UI.Views
var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
ThemeHelpers.SetImmersiveDarkMode(hWnd, ThemeHelpers.GetAppTheme() == AppTheme.Dark);
Visibility = Visibility.Collapsed;
// Set window icon
WindowId windowId = Win32Interop.GetWindowIdFromWindow(hWnd);
AppWindow appWindow = AppWindow.GetFromWindowId(windowId);
appWindow.SetIcon("Assets/Peek/Icon.ico");
}
}

Просмотреть файл

@ -261,4 +261,8 @@
<value> ({1:N0} byte)</value>
<comment>Displays unit byte. Don't localize the "{1:N0}" part.</comment>
</data>
<data name="WindowTitle" xml:space="preserve">
<value>{0} - Peek</value>
<comment>Title of the Peek window. {0} is the name of the currently previewed item."Peek" is the name of the utility.</comment>
</data>
</root>