Files
PowerToys/src/modules/Hosts/HostsUILib/HostsMainPage.xaml.cs

235 lines
8.2 KiB
C#
Raw Normal View History

// Copyright (c) Microsoft Corporation
2022-10-13 13:05:43 +02:00
// 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.Threading.Tasks;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;
using HostsUILib.Helpers;
using HostsUILib.Models;
using HostsUILib.ViewModels;
2022-10-13 13:05:43 +02:00
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
namespace HostsUILib.Views
2022-10-13 13:05:43 +02:00
{
public partial class HostsMainPage : Page
2022-10-13 13:05:43 +02:00
{
public MainViewModel ViewModel { get; private set; }
public ICommand NewDialogCommand => new AsyncRelayCommand(OpenNewDialogAsync);
public ICommand AdditionalLinesDialogCommand => new AsyncRelayCommand(OpenAdditionalLinesDialogAsync);
public ICommand AddCommand => new RelayCommand(Add);
public ICommand UpdateCommand => new RelayCommand(Update);
public ICommand DeleteCommand => new RelayCommand(Delete);
public ICommand UpdateAdditionalLinesCommand => new RelayCommand(UpdateAdditionalLines);
public ICommand ExitCommand => new RelayCommand(() => { Microsoft.UI.Dispatching.DispatcherQueue.GetForCurrentThread().TryEnqueue(Application.Current.Exit); });
public HostsMainPage(MainViewModel viewModel)
2022-10-13 13:05:43 +02:00
{
InitializeComponent();
ViewModel = viewModel;
2022-10-13 13:05:43 +02:00
DataContext = ViewModel;
}
private async Task OpenNewDialogAsync()
{
await ShowAddDialogAsync();
2022-10-13 13:05:43 +02:00
}
private async Task OpenAdditionalLinesDialogAsync()
{
AdditionalLines.Text = ViewModel.AdditionalLines;
await AdditionalLinesDialog.ShowAsync();
}
private async void Entries_ItemClick(object sender, ItemClickEventArgs e)
{
Entry entry = e.ClickedItem as Entry;
ViewModel.Selected = entry;
await ShowEditDialogAsync(entry);
}
public async Task ShowEditDialogAsync(Entry entry)
2022-10-13 13:05:43 +02:00
{
[General]Reduce installer size by flattening application paths (#27451) * Flatten everything and succeed build * Figure out Settings assets * Remove UseCommonOutputDirectory tag * Proper settings app path * [VCM] Fix assets location * Fix some runtime paths * [RegistryPreview]Use MRTCore specific pri file * [Hosts]Use MRTCore specific pri file * [Settings]Use MRTCore specific pri file * [Peek]Use MRTCore specific pri file * [FileLocksmith]Use MRTCore specific pri file * [ScreenRuler]Use MRTCore specific pri file * [PowerRename]Use MRTCore specific pri file * [Peek]Move assets to own folder * [FileLocksmith] Use own Assets path * [Hosts]Use own assets folder * [PowerRename]Use own assets dir * [MeasureTool] Use its own assets folder * [ImageResizer]Use its own assets path * Fix spellcheck * Fix tab instead of space in project files * Normalize target frameworks and platforms * Remove WINRT_NO_MAKE_DETECTION flag. No longer needed? * Fix AOT and Hosts locations * Fix Dll version differences on dependency * Add Common.UI.csproj as refernce to fix dll versions * Update ControlzEx to normalize dll versions * Update System.Management version to 7.0.2 * Add GPOWrapper to Registry Preview to fix dll versions * [PTRun]Reference Microsoft.Extensions.Hosting to fix dll versions * Fix remaining output paths / dll version conflicts * [KeyboardManager]Executables still on their own directories * Fix Monaco paths * WinAppSDK apps get to play outside again * Enable VCM settings again * Fix KBM Editor path again * [Monaco]Set to own Assets dir * Fix installer preamble; Fix publish. remove unneeded publishes * Remove Hardlink functions * Installer builds again (still needs work to work) * Readd Monaco to spellcheck excludes * Fix spellcheck and call publish.cmd again * [Installer] Remove components that don't need own dirs * [Installer] Refactor Power Launcher * [Installer] Refactor Color Picker * [Installer] Refactor Monaco assets * [Installer]Generate File script no longer needs to remove files * [Installer]Refactor FileLocksmith * [Installer] Refactor Hosts * [Installer]Refactor Image Resizer * [Installer]Refactor MouseUtils * [Installer]Refactor MWB * [Installer]Refactor MeasureTool * [Installer]Refactor Peek * [Installer]Refactor PowerRename and registry fixes * [Installer]Refactor RegistryPreview * [Installer]Refactor ShortcutGuide * [Installer]Refactor Settings * [Installer]Clean up some unused stuff * [Installer]Clean up stuff for user install * [Installer]Fix WinUi3Apps wxs * [Installer]Fix misplaced folders * [Installer]Move x86 VCM dll to right path * Fix monaco language list location * [Installer]Fix VCM directory reference * [CI]Fix signing * [Installer] Fix resources folder for release CI * [ci]Looks like we still ship NLog on PowerToys Run * [Settings]Add dependency to avoid dll collision with Experimentation * [RegistryPreview]Move XAML files to own path * [RegistryPreview]Fix app icon * [Hosts]Move XAML files to their own path * [FileLocksmith]Move XAML files to their own path * [Peek]Move XAML files to own path * [ScreenRuler]Move XAML files to its own path * [Settings]Move XAML to its own path * [ColorPicker]Move Resources to Assets * [ShortcutGuide]Move svgs to own Assets path * [Awake]Move images to assets path * [Runner]Remove debug checks for PowerToys Run assets * [PTRun]Move images to its own assets path * [ImageResizer]Icon for context menu on own assets path * [PowerRename]Move ico to its own path * Remove unneeded intermediary directories * Remove further int dirs * Move tests to its own output path * Fix spellcheck * spellcheck: remove warnings * [CppAnalyzers]Ignore rule in a tool * [CI]Check if all deps.json files reference same versions * fix spellcheck * [ci]Fix task identation * [ci]Add script to guard against asset conflicts * [ci]Add more deps.json audit steps in the release build * Add xbf to spellcheck expects * Fix typo in asset conflict check scripts * Fix some more dependency conflicts * Downgrade CsWinRT to have the same dll version as sdk * [ci]Do a recursive check for every deps.json * Fix spellcheck error inside comment * [ci]Fix asset script error * [ci]Name deps.json verify tasks a bit better * [ci]Improve deps json verify script output * [ci]Update WinRT version to the same running in CI * Also upgrade CsWinRT in NOTICE.MD * [PowerRename]Move XAML files to own path * [Common]Fix Settings executable path * [ci]Verify there's no xbf files in app directories * [installer]Fix firewall path * [Monaco]Move new files to their proper assets path * [Monaco]Fix paths for new files after merge * [Feedback]Removed unneeded build conditions * [Feedback]Clear vcxproj direct reference to frameworks * [Feedback]RunPlugins name to hold PTRun plugins * [Feedback]Remove unneeded foreach * [Feedback]Shortcut Guide svgs with ** in project file * [Feedback]Fix spellcheck
2023-07-20 00:12:46 +01:00
var resourceLoader = Helpers.ResourceLoaderInstance.ResourceLoader;
2022-10-13 13:05:43 +02:00
EntryDialog.Title = resourceLoader.GetString("UpdateEntry_Title");
EntryDialog.PrimaryButtonText = resourceLoader.GetString("UpdateBtn");
EntryDialog.PrimaryButtonCommand = UpdateCommand;
var clone = ViewModel.Selected.Clone();
EntryDialog.DataContext = clone;
await EntryDialog.ShowAsync();
}
private void Add()
{
ViewModel.Add(EntryDialog.DataContext as Entry);
}
private void Update()
{
ViewModel.Update(Entries.SelectedIndex, EntryDialog.DataContext as Entry);
}
private void Delete()
{
ViewModel.DeleteSelected();
}
private void UpdateAdditionalLines()
{
ViewModel.UpdateAdditionalLines(AdditionalLines.Text);
}
private async void Delete_Click(object sender, RoutedEventArgs e)
2022-10-13 13:05:43 +02:00
{
if (Entries.SelectedItem is Entry entry)
2022-10-13 13:05:43 +02:00
{
ViewModel.Selected = entry;
DeleteDialog.Title = entry.Address;
await DeleteDialog.ShowAsync();
2022-10-13 13:05:43 +02:00
}
}
private async void Edit_Click(object sender, RoutedEventArgs e)
2022-10-13 13:05:43 +02:00
{
if (Entries.SelectedItem is Entry entry)
2022-10-13 13:05:43 +02:00
{
await ShowEditDialogAsync(entry);
2022-10-13 13:05:43 +02:00
}
}
private async void Duplicate_Click(object sender, RoutedEventArgs e)
{
if (Entries.SelectedItem is Entry entry)
{
await ShowAddDialogAsync(entry);
}
}
2022-10-13 13:05:43 +02:00
private async void Ping_Click(object sender, RoutedEventArgs e)
{
if (Entries.SelectedItem is Entry entry)
2022-10-13 13:05:43 +02:00
{
ViewModel.Selected = entry;
await ViewModel.PingSelectedAsync();
2022-10-13 13:05:43 +02:00
}
}
private async void Page_Loaded(object sender, RoutedEventArgs e)
{
ViewModel.ReadHosts();
var userSettings = ViewModel.UserSettings;
2022-10-13 13:05:43 +02:00
if (userSettings.ShowStartupWarning)
{
[General]Reduce installer size by flattening application paths (#27451) * Flatten everything and succeed build * Figure out Settings assets * Remove UseCommonOutputDirectory tag * Proper settings app path * [VCM] Fix assets location * Fix some runtime paths * [RegistryPreview]Use MRTCore specific pri file * [Hosts]Use MRTCore specific pri file * [Settings]Use MRTCore specific pri file * [Peek]Use MRTCore specific pri file * [FileLocksmith]Use MRTCore specific pri file * [ScreenRuler]Use MRTCore specific pri file * [PowerRename]Use MRTCore specific pri file * [Peek]Move assets to own folder * [FileLocksmith] Use own Assets path * [Hosts]Use own assets folder * [PowerRename]Use own assets dir * [MeasureTool] Use its own assets folder * [ImageResizer]Use its own assets path * Fix spellcheck * Fix tab instead of space in project files * Normalize target frameworks and platforms * Remove WINRT_NO_MAKE_DETECTION flag. No longer needed? * Fix AOT and Hosts locations * Fix Dll version differences on dependency * Add Common.UI.csproj as refernce to fix dll versions * Update ControlzEx to normalize dll versions * Update System.Management version to 7.0.2 * Add GPOWrapper to Registry Preview to fix dll versions * [PTRun]Reference Microsoft.Extensions.Hosting to fix dll versions * Fix remaining output paths / dll version conflicts * [KeyboardManager]Executables still on their own directories * Fix Monaco paths * WinAppSDK apps get to play outside again * Enable VCM settings again * Fix KBM Editor path again * [Monaco]Set to own Assets dir * Fix installer preamble; Fix publish. remove unneeded publishes * Remove Hardlink functions * Installer builds again (still needs work to work) * Readd Monaco to spellcheck excludes * Fix spellcheck and call publish.cmd again * [Installer] Remove components that don't need own dirs * [Installer] Refactor Power Launcher * [Installer] Refactor Color Picker * [Installer] Refactor Monaco assets * [Installer]Generate File script no longer needs to remove files * [Installer]Refactor FileLocksmith * [Installer] Refactor Hosts * [Installer]Refactor Image Resizer * [Installer]Refactor MouseUtils * [Installer]Refactor MWB * [Installer]Refactor MeasureTool * [Installer]Refactor Peek * [Installer]Refactor PowerRename and registry fixes * [Installer]Refactor RegistryPreview * [Installer]Refactor ShortcutGuide * [Installer]Refactor Settings * [Installer]Clean up some unused stuff * [Installer]Clean up stuff for user install * [Installer]Fix WinUi3Apps wxs * [Installer]Fix misplaced folders * [Installer]Move x86 VCM dll to right path * Fix monaco language list location * [Installer]Fix VCM directory reference * [CI]Fix signing * [Installer] Fix resources folder for release CI * [ci]Looks like we still ship NLog on PowerToys Run * [Settings]Add dependency to avoid dll collision with Experimentation * [RegistryPreview]Move XAML files to own path * [RegistryPreview]Fix app icon * [Hosts]Move XAML files to their own path * [FileLocksmith]Move XAML files to their own path * [Peek]Move XAML files to own path * [ScreenRuler]Move XAML files to its own path * [Settings]Move XAML to its own path * [ColorPicker]Move Resources to Assets * [ShortcutGuide]Move svgs to own Assets path * [Awake]Move images to assets path * [Runner]Remove debug checks for PowerToys Run assets * [PTRun]Move images to its own assets path * [ImageResizer]Icon for context menu on own assets path * [PowerRename]Move ico to its own path * Remove unneeded intermediary directories * Remove further int dirs * Move tests to its own output path * Fix spellcheck * spellcheck: remove warnings * [CppAnalyzers]Ignore rule in a tool * [CI]Check if all deps.json files reference same versions * fix spellcheck * [ci]Fix task identation * [ci]Add script to guard against asset conflicts * [ci]Add more deps.json audit steps in the release build * Add xbf to spellcheck expects * Fix typo in asset conflict check scripts * Fix some more dependency conflicts * Downgrade CsWinRT to have the same dll version as sdk * [ci]Do a recursive check for every deps.json * Fix spellcheck error inside comment * [ci]Fix asset script error * [ci]Name deps.json verify tasks a bit better * [ci]Improve deps json verify script output * [ci]Update WinRT version to the same running in CI * Also upgrade CsWinRT in NOTICE.MD * [PowerRename]Move XAML files to own path * [Common]Fix Settings executable path * [ci]Verify there's no xbf files in app directories * [installer]Fix firewall path * [Monaco]Move new files to their proper assets path * [Monaco]Fix paths for new files after merge * [Feedback]Removed unneeded build conditions * [Feedback]Clear vcxproj direct reference to frameworks * [Feedback]RunPlugins name to hold PTRun plugins * [Feedback]Remove unneeded foreach * [Feedback]Shortcut Guide svgs with ** in project file * [Feedback]Fix spellcheck
2023-07-20 00:12:46 +01:00
var resourceLoader = Helpers.ResourceLoaderInstance.ResourceLoader;
2022-10-13 13:05:43 +02:00
var dialog = new ContentDialog();
dialog.XamlRoot = XamlRoot;
dialog.Style = Application.Current.Resources["DefaultContentDialogStyle"] as Style;
dialog.Title = resourceLoader.GetString("WarningDialog_Title");
dialog.Content = new TextBlock
{
Text = resourceLoader.GetString("WarningDialog_Text"),
TextWrapping = TextWrapping.Wrap,
};
dialog.PrimaryButtonText = resourceLoader.GetString("WarningDialog_AcceptBtn");
dialog.PrimaryButtonStyle = Application.Current.Resources["AccentButtonStyle"] as Style;
dialog.CloseButtonText = resourceLoader.GetString("WarningDialog_QuitBtn");
dialog.CloseButtonCommand = ExitCommand;
await dialog.ShowAsync();
}
}
private void ReorderButtonUp_Click(object sender, RoutedEventArgs e)
{
if (Entries.SelectedItem is Entry entry)
2022-10-13 13:05:43 +02:00
{
var index = ViewModel.Entries.IndexOf(entry);
if (index > 0)
{
ViewModel.Move(index, index - 1);
2022-10-13 13:05:43 +02:00
}
}
}
private void ReorderButtonDown_Click(object sender, RoutedEventArgs e)
{
if (Entries.SelectedItem is Entry entry)
2022-10-13 13:05:43 +02:00
{
var index = ViewModel.Entries.IndexOf(entry);
if (index < ViewModel.Entries.Count - 1)
{
ViewModel.Move(index, index + 1);
2022-10-13 13:05:43 +02:00
}
}
}
/// <summary>
/// Focus the first item when the list view gets the focus with keyboard
/// </summary>
private void Entries_GotFocus(object sender, RoutedEventArgs e)
{
var element = sender as FrameworkElement;
var entry = element.DataContext as Entry;
if (entry != null)
{
ViewModel.Selected = entry;
}
else if (Entries.SelectedItem == null && Entries.Items.Count > 0)
{
Entries.SelectedIndex = 0;
}
}
private void Entries_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
var entry = (e.OriginalSource as FrameworkElement).DataContext as Entry;
ViewModel.Selected = entry;
}
private async Task ShowAddDialogAsync(Entry template = null)
{
var resourceLoader = ResourceLoaderInstance.ResourceLoader;
EntryDialog.Title = resourceLoader.GetString("AddNewEntryDialog_Title");
EntryDialog.PrimaryButtonText = resourceLoader.GetString("AddBtn");
EntryDialog.PrimaryButtonCommand = AddCommand;
EntryDialog.DataContext = template == null
? new Entry(ViewModel.NextId, string.Empty, string.Empty, string.Empty, true)
: new Entry(ViewModel.NextId, template.Address, template.Hosts, template.Comment, template.Active);
await EntryDialog.ShowAsync();
}
private void ContentDialog_Loaded_ApplyMargin(object sender, RoutedEventArgs e)
{
try
{
// Based on the template from dev/CommonStyles/ContentDialog_themeresources.xaml in https://github.com/microsoft/microsoft-ui-xaml
var border = VisualTreeUtils.FindVisualChildByName(sender as ContentDialog, "BackgroundElement") as Border;
if (border is not null)
{
border.Margin = new Thickness(0, 32, 0, 0); // Should be the size reserved for the title bar as in MainWindow.
}
}
catch (Exception ex)
{
LoggerInstance.Logger.LogError("Couldn't set the margin for a content dialog. It will appear on top of the title bar.", ex);
}
}
2022-10-13 13:05:43 +02:00
}
}