From 3c629bd1f37098888a9b8ecf7f06ba1447c6fc85 Mon Sep 17 00:00:00 2001 From: Andrew Whitechapel Date: Mon, 20 May 2024 10:47:06 -0700 Subject: [PATCH 1/3] Enable the user to unpin/pin an external tool on the bar --- tools/PI/DevHome.PI/BarWindow.xaml | 9 +- tools/PI/DevHome.PI/BarWindow.xaml.cs | 66 ++++--- tools/PI/DevHome.PI/Helpers/ExternalTool.cs | 20 +- .../DevHome.PI/Helpers/ExternalToolsHelper.cs | 174 +++++++++++++++--- tools/PI/DevHome.PI/Models/PerfCounters.cs | 20 +- .../DevHome.PI/SettingsUi/AddToolControl.xaml | 9 +- .../SettingsUi/AddToolControl.xaml.cs | 15 +- .../SettingsUi/EditToolsControl.xaml | 2 +- .../SettingsUi/EditToolsControl.xaml.cs | 2 +- .../DevHome.PI/Strings/en-us/Resources.resw | 18 +- 10 files changed, 254 insertions(+), 81 deletions(-) diff --git a/tools/PI/DevHome.PI/BarWindow.xaml b/tools/PI/DevHome.PI/BarWindow.xaml index 877679fbbe..5f8100509a 100644 --- a/tools/PI/DevHome.PI/BarWindow.xaml +++ b/tools/PI/DevHome.PI/BarWindow.xaml @@ -93,13 +93,10 @@ - + x:Name="PinUnpinMenuItem" + Click="PinUnpinMenuItem_Click"> @@ -131,7 +128,7 @@ diff --git a/tools/PI/DevHome.PI/BarWindow.xaml.cs b/tools/PI/DevHome.PI/BarWindow.xaml.cs index 4695246b89..27a8a08a17 100644 --- a/tools/PI/DevHome.PI/BarWindow.xaml.cs +++ b/tools/PI/DevHome.PI/BarWindow.xaml.cs @@ -294,7 +294,7 @@ private void CreateMenuItemFromTool(ExternalTool item) private void ExternalToolItem_PropertyChanged(object? sender, PropertyChangedEventArgs e) { - if (sender is ExternalTool item && e.PropertyName == nameof(ExternalTool.MenuIcon)) + if (sender is ExternalTool item && string.Equals(e.PropertyName, nameof(ExternalTool.MenuIcon), StringComparison.Ordinal)) { var menuItem = (MenuFlyoutItem?)ExternalToolsMenu.Items.FirstOrDefault(i => ((ExternalTool)i.Tag).ID == item.ID); if (menuItem is not null) diff --git a/tools/PI/DevHome.PI/Helpers/ExternalTool.cs b/tools/PI/DevHome.PI/Helpers/ExternalTool.cs index 0a189838b5..efcca40459 100644 --- a/tools/PI/DevHome.PI/Helpers/ExternalTool.cs +++ b/tools/PI/DevHome.PI/Helpers/ExternalTool.cs @@ -5,6 +5,7 @@ using System.ComponentModel; using System.Diagnostics; using System.Text.Json.Serialization; +using CommunityToolkit.Mvvm.ComponentModel; using Microsoft.UI.Xaml.Controls; using Microsoft.UI.Xaml.Media.Imaging; using Serilog; @@ -22,7 +23,7 @@ public enum ExternalToolArgType } // ExternalTool represents an imported tool -public class ExternalTool : INotifyPropertyChanged +public partial class ExternalTool : ObservableObject { private static readonly ILogger _log = Log.ForContext("SourceContext", nameof(ExternalTool)); @@ -45,48 +46,20 @@ public string OtherArgs get; private set; } - private bool isPinned; + [ObservableProperty] + private bool _isPinned; - public bool IsPinned - { - get => isPinned; - set - { - isPinned = value; - OnPropertyChanged(nameof(IsPinned)); - } - } - - [JsonIgnore] + // Note the additional "property:" syntax to ensure the JsonIgnore is propagated to the generated property. + [ObservableProperty] + [property: JsonIgnore] private SoftwareBitmapSource? _toolIcon; - [JsonIgnore] - public SoftwareBitmapSource? ToolIcon - { - get => _toolIcon; - private set - { - _toolIcon = value; - OnPropertyChanged(nameof(ToolIcon)); - } - } - - [JsonIgnore] + [ObservableProperty] + [property: JsonIgnore] private BitmapIcon? _menuIcon; [JsonIgnore] - public BitmapIcon? MenuIcon - { - get => _menuIcon; - private set - { - _menuIcon = value; - OnPropertyChanged(nameof(MenuIcon)); - } - } - - [JsonIgnore] - private SoftwareBitmap? softwareBitmap; + private SoftwareBitmap? _softwareBitmap; public ExternalTool( string name, @@ -116,10 +89,10 @@ private async void GetToolImage() { try { - softwareBitmap ??= GetSoftwareBitmapFromExecutable(Executable); - if (softwareBitmap is not null) + _softwareBitmap ??= GetSoftwareBitmapFromExecutable(Executable); + if (_softwareBitmap is not null) { - ToolIcon = await GetSoftwareBitmapSourceFromSoftwareBitmap(softwareBitmap); + ToolIcon = await GetSoftwareBitmapSourceFromSoftwareBitmap(_softwareBitmap); } } catch (Exception ex) @@ -132,10 +105,10 @@ private async void GetMenuIcon() { try { - softwareBitmap ??= GetSoftwareBitmapFromExecutable(Executable); - if (softwareBitmap is not null) + _softwareBitmap ??= GetSoftwareBitmapFromExecutable(Executable); + if (_softwareBitmap is not null) { - var bitmapUri = await SaveSoftwareBitmapToTempFile(softwareBitmap); + var bitmapUri = await SaveSoftwareBitmapToTempFile(_softwareBitmap); MenuIcon = new BitmapIcon { UriSource = bitmapUri, @@ -188,11 +161,4 @@ internal string CreateCommandLine(int? pid, HWND? hwnd) return null; } } - - public event PropertyChangedEventHandler? PropertyChanged; - - protected void OnPropertyChanged(string propertyName) - { - PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); - } } diff --git a/tools/PI/DevHome.PI/Helpers/ExternalToolsHelper.cs b/tools/PI/DevHome.PI/Helpers/ExternalToolsHelper.cs index 128caf2d85..9bcd096bde 100644 --- a/tools/PI/DevHome.PI/Helpers/ExternalToolsHelper.cs +++ b/tools/PI/DevHome.PI/Helpers/ExternalToolsHelper.cs @@ -69,7 +69,7 @@ private ExternalToolsHelper() } // The file should be in this location: - // %LocalAppData%\Packages\Microsoft.Windows.DevHome.Dev_8wekyb3d8bbwe\LocalState\externaltools.json + // %LocalAppData%\Packages\Microsoft.Windows.DevHome_8wekyb3d8bbwe\LocalState\externaltools.json toolInfoFileName = Path.Combine(localFolder, "externaltools.json"); AllExternalTools = new(allExternalTools); } @@ -93,7 +93,7 @@ internal void Init() { // TODO If we failed to parse the JSON file, we should rename it (using DateTime.Now), // create a new one, and report to the user. - _log.Error(ex, "Failed to parse {tool}", toolInfoFileName); + _log.Error(ex, $"Failed to parse {toolInfoFileName}"); } } } @@ -101,7 +101,7 @@ internal void Init() private void ToolItem_PropertyChanged(object? sender, PropertyChangedEventArgs e) { // The user can change the IsPinned property of a tool, to pin or unpin it on the bar. - if (sender is ExternalTool tool && e.PropertyName == nameof(ExternalTool.IsPinned)) + if (sender is ExternalTool tool && string.Equals(e.PropertyName, nameof(ExternalTool.IsPinned), StringComparison.Ordinal)) { if (tool.IsPinned) { diff --git a/tools/PI/DevHome.PI/SettingsUi/AddToolControl.xaml b/tools/PI/DevHome.PI/SettingsUi/AddToolControl.xaml index db614c1b8c..16be0d9db3 100644 --- a/tools/PI/DevHome.PI/SettingsUi/AddToolControl.xaml +++ b/tools/PI/DevHome.PI/SettingsUi/AddToolControl.xaml @@ -97,9 +97,9 @@ x:Uid="IsPinnedToggleSwitch" x:Name="IsPinnedToggleSwitch" Grid.Row="4" Margin="0,12,0,0" HorizontalAlignment="Left" IsOn="True"/> - -