From 2a6d3b20944082499ac033aa7172e87febf22afa Mon Sep 17 00:00:00 2001 From: Stefan Markovic Date: Tue, 12 Mar 2024 13:02:13 +0100 Subject: [PATCH] WIP Hosts - remove deps --- .../Hosts/Hosts.Tests/HostsServiceTest.cs | 1 - src/modules/Hosts/Hosts.sln | 31 +++++++++++++++++++ .../Hosts/Hosts/Helpers/HostsService.cs | 10 +++--- src/modules/Hosts/Hosts/Hosts.csproj | 8 ++--- src/modules/Hosts/Hosts/HostsXAML/App.xaml.cs | 18 +++++------ .../Hosts/Hosts/HostsXAML/MainWindow.xaml | 2 +- .../Hosts/Hosts/HostsXAML/MainWindow.xaml.cs | 3 +- .../{MainPage.xaml => HostsMainPage.xaml} | 2 +- ...MainPage.xaml.cs => HostsMainPage.xaml.cs} | 9 +++--- src/modules/Hosts/Hosts/Program.cs | 9 +++--- .../Settings/HostsAdditionalLinesPosition.cs | 12 +++++++ .../Hosts/Hosts/Settings/HostsEncoding.cs | 12 +++++++ .../Hosts/Hosts/Settings/IUserSettings.cs | 1 - .../Hosts/Hosts/Settings/UserSettings.cs | 29 ++++++++++------- .../Telemetry/HostsFileEditorOpenedEvent.cs | 16 ---------- .../Hosts/Hosts/ViewModels/MainViewModel.cs | 11 +++---- 16 files changed, 106 insertions(+), 68 deletions(-) create mode 100644 src/modules/Hosts/Hosts.sln rename src/modules/Hosts/Hosts/HostsXAML/Views/{MainPage.xaml => HostsMainPage.xaml} (99%) rename src/modules/Hosts/Hosts/HostsXAML/Views/{MainPage.xaml.cs => HostsMainPage.xaml.cs} (96%) create mode 100644 src/modules/Hosts/Hosts/Settings/HostsAdditionalLinesPosition.cs create mode 100644 src/modules/Hosts/Hosts/Settings/HostsEncoding.cs delete mode 100644 src/modules/Hosts/Hosts/Telemetry/HostsFileEditorOpenedEvent.cs diff --git a/src/modules/Hosts/Hosts.Tests/HostsServiceTest.cs b/src/modules/Hosts/Hosts.Tests/HostsServiceTest.cs index d76beebd60..c7a4ace0f0 100644 --- a/src/modules/Hosts/Hosts.Tests/HostsServiceTest.cs +++ b/src/modules/Hosts/Hosts.Tests/HostsServiceTest.cs @@ -12,7 +12,6 @@ using Hosts.Settings; using Hosts.Tests.Mocks; using Microsoft.VisualStudio.TestTools.UnitTesting; using Moq; -using Settings.UI.Library.Enumerations; namespace Hosts.Tests { diff --git a/src/modules/Hosts/Hosts.sln b/src/modules/Hosts/Hosts.sln new file mode 100644 index 0000000000..3d0c5ceb81 --- /dev/null +++ b/src/modules/Hosts/Hosts.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.002.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hosts", "Hosts\Hosts.csproj", "{7F7A8443-E3D9-42D9-9C3D-2FA433D5510E}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Hosts.Tests", "Hosts.Tests\Hosts.Tests.csproj", "{8486EE8B-C604-464C-A52D-7D7C9F3CEBCE}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {7F7A8443-E3D9-42D9-9C3D-2FA433D5510E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7F7A8443-E3D9-42D9-9C3D-2FA433D5510E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7F7A8443-E3D9-42D9-9C3D-2FA433D5510E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7F7A8443-E3D9-42D9-9C3D-2FA433D5510E}.Release|Any CPU.Build.0 = Release|Any CPU + {8486EE8B-C604-464C-A52D-7D7C9F3CEBCE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8486EE8B-C604-464C-A52D-7D7C9F3CEBCE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8486EE8B-C604-464C-A52D-7D7C9F3CEBCE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8486EE8B-C604-464C-A52D-7D7C9F3CEBCE}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {A6A11D66-5DBB-403B-92D6-17E1EB7479AA} + EndGlobalSection +EndGlobal diff --git a/src/modules/Hosts/Hosts/Helpers/HostsService.cs b/src/modules/Hosts/Hosts/Helpers/HostsService.cs index ec58c149de..b881ea51e9 100644 --- a/src/modules/Hosts/Hosts/Helpers/HostsService.cs +++ b/src/modules/Hosts/Hosts/Helpers/HostsService.cs @@ -16,9 +16,7 @@ using System.Threading.Tasks; using Hosts.Exceptions; using Hosts.Models; using Hosts.Settings; -using ManagedCommon; using Microsoft.Win32; -using Settings.UI.Library.Enumerations; namespace Hosts.Helpers { @@ -275,9 +273,9 @@ namespace Hosts.Helpers } } } - catch (Exception ex) + catch (Exception) { - Logger.LogError("Failed to open default editor", ex); + // Logger.LogError("Failed to open default editor", ex); notepadFallback = true; } @@ -287,9 +285,9 @@ namespace Hosts.Helpers { Process.Start(new ProcessStartInfo("notepad.exe", HostsFilePath)); } - catch (Exception ex) + catch (Exception) { - Logger.LogError("Failed to open notepad", ex); + // Logger.LogError("Failed to open notepad", ex); } } } diff --git a/src/modules/Hosts/Hosts/Hosts.csproj b/src/modules/Hosts/Hosts/Hosts.csproj index ba13f01b00..60efde46aa 100644 --- a/src/modules/Hosts/Hosts/Hosts.csproj +++ b/src/modules/Hosts/Hosts/Hosts.csproj @@ -74,10 +74,10 @@ - - - - + + + + diff --git a/src/modules/Hosts/Hosts/HostsXAML/App.xaml.cs b/src/modules/Hosts/Hosts/HostsXAML/App.xaml.cs index 34080ce130..40b0ab7c92 100644 --- a/src/modules/Hosts/Hosts/HostsXAML/App.xaml.cs +++ b/src/modules/Hosts/Hosts/HostsXAML/App.xaml.cs @@ -9,10 +9,8 @@ using Hosts.Helpers; using Hosts.Settings; using Hosts.ViewModels; using Hosts.Views; -using ManagedCommon; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; -using Microsoft.PowerToys.Telemetry; using Microsoft.UI.Dispatching; using Microsoft.UI.Xaml; @@ -50,11 +48,12 @@ namespace Hosts // Core Services services.AddSingleton(); services.AddSingleton(); + services.AddSingleton(); services.AddSingleton(); // Views and ViewModels - services.AddTransient(); + services.AddTransient(); services.AddTransient(); }). Build(); @@ -73,9 +72,9 @@ namespace Hosts { GetService().CleanupBackup(); } - catch (Exception ex) + catch (Exception) { - Logger.LogError("Failed to delete backup", ex); + // Logger.LogError("Failed to delete backup", ex); } }); @@ -90,6 +89,7 @@ namespace Hosts { if (int.TryParse(cmdArgs[cmdArgs.Length - 1], out int powerToysRunnerPid)) { + /* Logger.LogInfo($"Hosts started from the PowerToys Runner. Runner pid={powerToysRunnerPid}"); var dispatcher = DispatcherQueue.GetForCurrentThread(); @@ -98,22 +98,22 @@ namespace Hosts Logger.LogInfo("PowerToys Runner exited. Exiting Hosts"); dispatcher.TryEnqueue(App.Current.Exit); }); + */ } } else { - Logger.LogInfo($"Hosts started detached from PowerToys Runner."); + // Logger.LogInfo($"Hosts started detached from PowerToys Runner."); } - PowerToysTelemetry.Log.WriteEvent(new Hosts.Telemetry.HostsFileEditorOpenedEvent()); - + // PowerToysTelemetry.Log.WriteEvent(new Hosts.Telemetry.HostsFileEditorOpenedEvent()); _window = new MainWindow(); _window.Activate(); } private void App_UnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) { - Logger.LogError("Unhandled exception", e.Exception); + // Logger.LogError("Unhandled exception", e.Exception); } } } diff --git a/src/modules/Hosts/Hosts/HostsXAML/MainWindow.xaml b/src/modules/Hosts/Hosts/HostsXAML/MainWindow.xaml index 37c43fa4fc..9d81833624 100644 --- a/src/modules/Hosts/Hosts/HostsXAML/MainWindow.xaml +++ b/src/modules/Hosts/Hosts/HostsXAML/MainWindow.xaml @@ -43,6 +43,6 @@ Style="{StaticResource CaptionTextBlockStyle}" /> - + diff --git a/src/modules/Hosts/Hosts/HostsXAML/MainWindow.xaml.cs b/src/modules/Hosts/Hosts/HostsXAML/MainWindow.xaml.cs index d6a474b841..9deb4fef15 100644 --- a/src/modules/Hosts/Hosts/HostsXAML/MainWindow.xaml.cs +++ b/src/modules/Hosts/Hosts/HostsXAML/MainWindow.xaml.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using Hosts.Helpers; -using ManagedCommon; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Media; using WinUIEx; @@ -26,8 +25,8 @@ namespace Hosts AppTitleTextBlock.Text = title; var handle = this.GetWindowHandle(); - WindowHelpers.BringToForeground(handle); + // WindowHelpers.BringToForeground(handle); Activated += MainWindow_Activated; } diff --git a/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml b/src/modules/Hosts/Hosts/HostsXAML/Views/HostsMainPage.xaml similarity index 99% rename from src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml rename to src/modules/Hosts/Hosts/HostsXAML/Views/HostsMainPage.xaml index 8c5b744fb5..ab40e65ec6 100644 --- a/src/modules/Hosts/Hosts/HostsXAML/Views/MainPage.xaml +++ b/src/modules/Hosts/Hosts/HostsXAML/Views/HostsMainPage.xaml @@ -1,5 +1,5 @@  new RelayCommand(() => { Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread().TryEnqueue(Application.Current.Exit); }); - public MainPage() + public HostsMainPage() { InitializeComponent(); ViewModel = App.GetService(); @@ -221,9 +220,9 @@ namespace Hosts.Views var border = VisualTreeUtils.FindVisualChildByName(sender as ContentDialog, "BackgroundElement") as Border; border.Margin = new Thickness(0, 32, 0, 0); // Should be the size reserved for the title bar as in MainWindow.xaml } - catch (Exception ex) + catch (Exception) { - Logger.LogError("Couldn't set the margin for a content dialog. It will appear on top of the title bar.", ex); + // Logger.LogError("Couldn't set the margin for a content dialog. It will appear on top of the title bar.", ex); } } } diff --git a/src/modules/Hosts/Hosts/Program.cs b/src/modules/Hosts/Hosts/Program.cs index 4b41f3c71a..37c77a1c1f 100644 --- a/src/modules/Hosts/Hosts/Program.cs +++ b/src/modules/Hosts/Hosts/Program.cs @@ -4,7 +4,6 @@ using System; using System.Threading; -using ManagedCommon; using Microsoft.UI.Dispatching; using Microsoft.Windows.AppLifecycle; @@ -15,15 +14,17 @@ namespace Hosts [STAThread] public static void Main(string[] args) { - Logger.InitializeLogger("\\Hosts\\Logs"); - + // Logger.InitializeLogger("\\Hosts\\Logs"); WinRT.ComWrappersSupport.InitializeComWrappers(); +// Removed GPO dep +/* if (PowerToys.GPOWrapper.GPOWrapper.GetConfiguredHostsFileEditorEnabledValue() == PowerToys.GPOWrapper.GpoRuleConfigured.Disabled) { Logger.LogWarning("Tried to start with a GPO policy setting the utility to always be disabled. Please contact your systems administrator."); return; } +*/ var instanceKey = AppInstance.FindOrRegisterForKey("PowerToys_Hosts_Instance"); @@ -38,7 +39,7 @@ namespace Hosts } else { - Logger.LogWarning("Another instance of Hosts running. Exiting Hosts"); + // Logger.LogWarning("Another instance of Hosts running. Exiting Hosts"); } return; diff --git a/src/modules/Hosts/Hosts/Settings/HostsAdditionalLinesPosition.cs b/src/modules/Hosts/Hosts/Settings/HostsAdditionalLinesPosition.cs new file mode 100644 index 0000000000..d19fb4b77b --- /dev/null +++ b/src/modules/Hosts/Hosts/Settings/HostsAdditionalLinesPosition.cs @@ -0,0 +1,12 @@ +// 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. + +namespace Hosts.Settings +{ + public enum HostsAdditionalLinesPosition + { + Top = 0, + Bottom = 1, + } +} diff --git a/src/modules/Hosts/Hosts/Settings/HostsEncoding.cs b/src/modules/Hosts/Hosts/Settings/HostsEncoding.cs new file mode 100644 index 0000000000..543ca962d0 --- /dev/null +++ b/src/modules/Hosts/Hosts/Settings/HostsEncoding.cs @@ -0,0 +1,12 @@ +// 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. + +namespace Hosts.Settings +{ + public enum HostsEncoding + { + Utf8 = 0, + Utf8Bom = 1, + } +} diff --git a/src/modules/Hosts/Hosts/Settings/IUserSettings.cs b/src/modules/Hosts/Hosts/Settings/IUserSettings.cs index fabea87e62..d91f9d9a27 100644 --- a/src/modules/Hosts/Hosts/Settings/IUserSettings.cs +++ b/src/modules/Hosts/Hosts/Settings/IUserSettings.cs @@ -3,7 +3,6 @@ // See the LICENSE file in the project root for more information. using System; -using Settings.UI.Library.Enumerations; namespace Hosts.Settings { diff --git a/src/modules/Hosts/Hosts/Settings/UserSettings.cs b/src/modules/Hosts/Hosts/Settings/UserSettings.cs index 5c73dd3a17..14bf8f04b4 100644 --- a/src/modules/Hosts/Hosts/Settings/UserSettings.cs +++ b/src/modules/Hosts/Hosts/Settings/UserSettings.cs @@ -3,12 +3,7 @@ // See the LICENSE file in the project root for more information. using System; -using System.IO.Abstractions; using System.Threading; -using ManagedCommon; -using Microsoft.PowerToys.Settings.UI.Library; -using Microsoft.PowerToys.Settings.UI.Library.Utilities; -using Settings.UI.Library.Enumerations; namespace Hosts.Settings { @@ -17,8 +12,9 @@ namespace Hosts.Settings private const string HostsModuleName = "Hosts"; private const int MaxNumberOfRetry = 5; - private readonly SettingsUtils _settingsUtils; - private readonly IFileSystemWatcher _watcher; + // SettingsUtils is in Settings.UI.Library + // private readonly SettingsUtils _settingsUtils; + // private readonly IFileSystemWatcher _watcher; private readonly object _loadingSettingsLock = new object(); public bool ShowStartupWarning { get; private set; } @@ -38,13 +34,16 @@ namespace Hosts.Settings } } + // Moved from Settings.UI.Library public HostsAdditionalLinesPosition AdditionalLinesPosition { get; private set; } + // Moved from Settings.UI.Library public HostsEncoding Encoding { get; set; } public UserSettings() { - _settingsUtils = new SettingsUtils(); + // SettingsUtils is in Settings.UI.Library + // _settingsUtils = new SettingsUtils(); ShowStartupWarning = true; LoopbackDuplicates = false; AdditionalLinesPosition = HostsAdditionalLinesPosition.Top; @@ -52,7 +51,8 @@ namespace Hosts.Settings LoadSettingsFromJson(); - _watcher = Helper.GetFileWatcher(HostsModuleName, "settings.json", () => LoadSettingsFromJson()); + // Watcher is in Settings.UI.Library + // _watcher = Helper.GetFileWatcher(HostsModuleName, "settings.json", () => LoadSettingsFromJson()); } public event EventHandler LoopbackDuplicatesChanged; @@ -70,9 +70,12 @@ namespace Hosts.Settings { retryCount++; + // SettingsUtils is in Settings.UI.Library + /* if (!_settingsUtils.SettingsExists(HostsModuleName)) { - Logger.LogInfo("Hosts settings.json was missing, creating a new one"); + // Logger needs to be abstracted + // Logger.LogInfo("Hosts settings.json was missing, creating a new one"); var defaultSettings = new HostsSettings(); defaultSettings.Save(_settingsUtils); } @@ -85,17 +88,19 @@ namespace Hosts.Settings Encoding = settings.Properties.Encoding; LoopbackDuplicates = settings.Properties.LoopbackDuplicates; } + */ retry = false; } - catch (Exception ex) + catch (Exception) { if (retryCount > MaxNumberOfRetry) { retry = false; } - Logger.LogError("Failed to read changed settings", ex); + // Logger needs to be abstracted + // Logger.LogError("Failed to read changed settings", ex); Thread.Sleep(500); } } diff --git a/src/modules/Hosts/Hosts/Telemetry/HostsFileEditorOpenedEvent.cs b/src/modules/Hosts/Hosts/Telemetry/HostsFileEditorOpenedEvent.cs deleted file mode 100644 index fee4748db5..0000000000 --- a/src/modules/Hosts/Hosts/Telemetry/HostsFileEditorOpenedEvent.cs +++ /dev/null @@ -1,16 +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.Diagnostics.Tracing; -using Microsoft.PowerToys.Telemetry; -using Microsoft.PowerToys.Telemetry.Events; - -namespace Hosts.Telemetry -{ - [EventData] - public class HostsFileEditorOpenedEvent : EventBase, IEvent - { - public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage; - } -} diff --git a/src/modules/Hosts/Hosts/ViewModels/MainViewModel.cs b/src/modules/Hosts/Hosts/ViewModels/MainViewModel.cs index 414fb8fce2..73d06a7c39 100644 --- a/src/modules/Hosts/Hosts/ViewModels/MainViewModel.cs +++ b/src/modules/Hosts/Hosts/ViewModels/MainViewModel.cs @@ -10,7 +10,6 @@ using System.Linq; using System.Linq.Expressions; using System.Threading; using System.Threading.Tasks; -using Common.UI; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using CommunityToolkit.WinUI; @@ -19,7 +18,6 @@ using Hosts.Exceptions; using Hosts.Helpers; using Hosts.Models; using Hosts.Settings; -using ManagedCommon; using Microsoft.UI.Dispatching; namespace Hosts.ViewModels @@ -270,7 +268,8 @@ namespace Hosts.ViewModels [RelayCommand] public void OpenSettings() { - SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.Hosts, true); + // Removed Common.UI dep + // SettingsDeepLink.OpenSettings(SettingsDeepLink.SettingsWindow.Hosts, true); } [RelayCommand] @@ -335,7 +334,7 @@ namespace Hosts.ViewModels } catch (OperationCanceledException) { - Logger.LogInfo("FindDuplicates cancelled"); + // Logger.LogInfo("FindDuplicates cancelled"); return; } } @@ -422,9 +421,9 @@ namespace Hosts.ViewModels var resourceLoader = ResourceLoaderInstance.ResourceLoader; errorMessage = resourceLoader.GetString("FileSaveError_FileInUse"); } - catch (Exception ex) + catch (Exception) { - Logger.LogError("Failed to save hosts file", ex); + // Logger.LogError("Failed to save hosts file", ex); var resourceLoader = ResourceLoaderInstance.ResourceLoader; errorMessage = resourceLoader.GetString("FileSaveError_Generic"); }