/ src / settings-ui / Settings.UI / SettingsXAML / Views / ColorPickerPage.xaml.cs
ColorPickerPage.xaml.cs
  1  // Copyright (c) Microsoft Corporation
  2  // The Microsoft Corporation licenses this file to you under the MIT license.
  3  // See the LICENSE file in the project root for more information.
  4  
  5  using System;
  6  using System.Collections.Generic;
  7  using System.Windows.Input;
  8  
  9  using CommunityToolkit.WinUI.Controls;
 10  using Microsoft.PowerToys.Settings.UI.Helpers;
 11  using Microsoft.PowerToys.Settings.UI.Library;
 12  using Microsoft.PowerToys.Settings.UI.ViewModels;
 13  using Microsoft.UI.Xaml.Controls;
 14  using Microsoft.Windows.ApplicationModel.Resources;
 15  
 16  namespace Microsoft.PowerToys.Settings.UI.Views
 17  {
 18      public sealed partial class ColorPickerPage : NavigablePage, IRefreshablePage
 19      {
 20          public ColorPickerViewModel ViewModel { get; set; }
 21  
 22          public ICommand AddCommand => new RelayCommand(Add);
 23  
 24          public ICommand UpdateCommand => new RelayCommand(Update);
 25  
 26          private ResourceLoader resourceLoader = ResourceLoaderInstance.ResourceLoader;
 27  
 28          public ColorPickerPage()
 29          {
 30              var settingsUtils = SettingsUtils.Default;
 31              ViewModel = new ColorPickerViewModel(
 32                  settingsUtils,
 33                  SettingsRepository<GeneralSettings>.GetInstance(settingsUtils),
 34                  SettingsRepository<ColorPickerSettings>.GetInstance(settingsUtils),
 35                  ShellPage.SendDefaultIPCMessage);
 36              DataContext = ViewModel;
 37              InitializeComponent();
 38  
 39              Loaded += (s, e) => ViewModel.OnPageLoaded();
 40          }
 41  
 42          /// <summary>
 43          /// Event is called when the <see cref="ComboBox"/> is completely loaded, inclusive the ItemSource
 44          /// </summary>
 45          /// <param name="sender">The sender of this event</param>
 46          /// <param name="e">The arguments of this event</param>
 47          private void ColorPicker_ComboBox_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
 48          {
 49              /**
 50               * UWP hack
 51               * because UWP load the bound ItemSource of the ComboBox asynchronous,
 52               * so after InitializeComponent() the ItemSource is still empty and can't automatically select a entry.
 53               * Selection via SelectedItem and SelectedValue is still not working too
 54               */
 55              ViewModel.SetPreviewSelectedIndex();
 56          }
 57  
 58          private void ReorderButtonUp_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
 59          {
 60              ColorFormatModel color = ((MenuFlyoutItem)sender).DataContext as ColorFormatModel;
 61              if (color == null)
 62              {
 63                  return;
 64              }
 65  
 66              var index = ViewModel.ColorFormats.IndexOf(color);
 67              if (index > 0)
 68              {
 69                  ViewModel.ColorFormats.Move(index, --index);
 70                  SetColorFormatsFocus(index);
 71              }
 72          }
 73  
 74          private void ReorderButtonDown_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
 75          {
 76              ColorFormatModel color = ((MenuFlyoutItem)sender).DataContext as ColorFormatModel;
 77              if (color == null)
 78              {
 79                  return;
 80              }
 81  
 82              var index = ViewModel.ColorFormats.IndexOf(color);
 83              if (index < ViewModel.ColorFormats.Count - 1)
 84              {
 85                  ViewModel.ColorFormats.Move(index, ++index);
 86                  SetColorFormatsFocus(index);
 87              }
 88          }
 89  
 90          private async void RemoveButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
 91          {
 92              ColorFormatModel color = ((MenuFlyoutItem)sender).DataContext as ColorFormatModel;
 93  
 94              ContentDialog dialog = new ContentDialog();
 95              dialog.XamlRoot = RootPage.XamlRoot;
 96              dialog.Title = color.Name;
 97              dialog.PrimaryButtonText = resourceLoader.GetString("Yes");
 98              dialog.CloseButtonText = resourceLoader.GetString("No");
 99              dialog.DefaultButton = ContentDialogButton.Primary;
100              dialog.Content = new TextBlock() { Text = resourceLoader.GetString("Delete_Dialog_Description") };
101              dialog.PrimaryButtonClick += (s, args) =>
102              {
103                  var deleteIndex = ViewModel.DeleteModel(color);
104                  SetColorFormatsFocus(deleteIndex < ViewModel.ColorFormats.Count ? deleteIndex : ViewModel.ColorFormats.Count - 1);
105              };
106              var result = await dialog.ShowAsync();
107          }
108  
109          private void Add()
110          {
111              ColorFormatModel newColorFormat = ColorFormatDialog.DataContext as ColorFormatModel;
112              ViewModel.AddNewColorFormat(newColorFormat.Name, newColorFormat.Format, true);
113              ColorFormatDialog.Hide();
114              SetColorFormatsFocus(0);
115          }
116  
117          private void Update()
118          {
119              ColorFormatModel colorFormat = ColorFormatDialog.DataContext as ColorFormatModel;
120              string oldName = ((KeyValuePair<string, string>)ColorFormatDialog.Tag).Key;
121              ViewModel.UpdateColorFormat(oldName, colorFormat);
122          }
123  
124          private async void NewFormatClick(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
125          {
126              ColorFormatDialog.Title = resourceLoader.GetString("AddCustomColorFormat");
127              ColorFormatModel newColorFormatModel = ViewModel.GetNewColorFormatModel();
128              ColorFormatDialog.DataContext = newColorFormatModel;
129              ColorFormatDialog.Tag = string.Empty;
130  
131              ColorFormatDialog.PrimaryButtonText = resourceLoader.GetString("ColorFormatSave");
132              ColorFormatDialog.PrimaryButtonCommand = AddCommand;
133              await ColorFormatDialog.ShowAsync();
134          }
135  
136          private async void EditButton_Click(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
137          {
138              SettingsCard btn = sender as SettingsCard;
139              ColorFormatModel colorFormatModel = btn.DataContext as ColorFormatModel;
140              ColorFormatDialog.Title = resourceLoader.GetString("EditCustomColorFormat");
141              ColorFormatDialog.DataContext = colorFormatModel;
142              ColorFormatDialog.Tag = new KeyValuePair<string, string>(colorFormatModel.Name, colorFormatModel.Format);
143  
144              ColorFormatDialog.PrimaryButtonText = resourceLoader.GetString("ColorFormatUpdate");
145              ColorFormatDialog.PrimaryButtonCommand = UpdateCommand;
146              await ColorFormatDialog.ShowAsync();
147          }
148  
149          private void ColorFormatEditor_PropertyChanged(object sender, EventArgs e)
150          {
151              ColorFormatDialog.IsPrimaryButtonEnabled = ViewModel.SetValidity(ColorFormatDialog.DataContext as ColorFormatModel, ColorFormatDialog.Tag as string);
152          }
153  
154          public void RefreshEnabledState()
155          {
156              ViewModel.RefreshEnabledState();
157          }
158  
159          private void ColorFormatDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args)
160          {
161              if (args.Result != ContentDialogResult.Primary && ColorFormatDialog.Tag is KeyValuePair<string, string>)
162              {
163                  ColorFormatModel modifiedColorFormat = ColorFormatDialog.DataContext as ColorFormatModel;
164                  KeyValuePair<string, string> oldProperties = (KeyValuePair<string, string>)ColorFormatDialog.Tag;
165                  modifiedColorFormat.Name = oldProperties.Key;
166                  modifiedColorFormat.Format = oldProperties.Value;
167              }
168          }
169  
170          private void SetColorFormatsFocus(int index)
171          {
172              ColorFormats.UpdateLayout();
173              var colorFormat = ColorFormats.ContainerFromIndex(index) as ContentPresenter;
174              colorFormat.Focus(Microsoft.UI.Xaml.FocusState.Programmatic);
175          }
176      }
177  }