WIP Hosts - remove deps

This commit is contained in:
Stefan Markovic
2024-03-12 13:02:13 +01:00
parent fdc723b047
commit 2a6d3b2094
16 changed files with 106 additions and 68 deletions

View File

@@ -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
{

View File

@@ -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

View File

@@ -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);
}
}
}

View File

@@ -74,10 +74,10 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\common\GPOWrapper\GPOWrapper.vcxproj" />
<ProjectReference Include="..\..\..\common\interop\PowerToys.Interop.vcxproj" />
<ProjectReference Include="..\..\..\common\Common.UI\Common.UI.csproj" />
<ProjectReference Include="..\..\..\settings-ui\Settings.UI.Library\Settings.UI.Library.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Telemetry\" />
</ItemGroup>
</Project>

View File

@@ -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<IFileSystem, FileSystem>();
services.AddSingleton<IHostsService, HostsService>();
services.AddSingleton<IUserSettings, UserSettings>();
services.AddSingleton<IElevationHelper, ElevationHelper>();
// Views and ViewModels
services.AddTransient<MainPage>();
services.AddTransient<HostsMainPage>();
services.AddTransient<MainViewModel>();
}).
Build();
@@ -73,9 +72,9 @@ namespace Hosts
{
GetService<IHostsService>().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);
}
}
}

View File

@@ -43,6 +43,6 @@
Style="{StaticResource CaptionTextBlockStyle}" />
</Grid>
<views:MainPage Grid.Row="1" />
<views:HostsMainPage Grid.Row="1" />
</Grid>
</winuiex:WindowEx>

View File

@@ -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;
}

View File

@@ -1,5 +1,5 @@
<Page
x:Class="Hosts.Views.MainPage"
x:Class="Hosts.Views.HostsMainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

View File

@@ -10,14 +10,13 @@ using Hosts.Helpers;
using Hosts.Models;
using Hosts.Settings;
using Hosts.ViewModels;
using ManagedCommon;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
namespace Hosts.Views
{
public sealed partial class MainPage : Page
public sealed partial class HostsMainPage : Page
{
public MainViewModel ViewModel { get; private set; }
@@ -35,7 +34,7 @@ namespace Hosts.Views
public ICommand ExitCommand => new RelayCommand(() => { Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread().TryEnqueue(Application.Current.Exit); });
public MainPage()
public HostsMainPage()
{
InitializeComponent();
ViewModel = App.GetService<MainViewModel>();
@@ -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);
}
}
}

View File

@@ -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;

View File

@@ -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,
}
}

View File

@@ -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,
}
}

View File

@@ -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
{

View File

@@ -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);
}
}

View File

@@ -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;
}
}

View File

@@ -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");
}