/ src / modules / cmdpal / Microsoft.CmdPal.UI / Controls / ColorPalette.xaml.cs
ColorPalette.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.Collections.ObjectModel;
 6  using Microsoft.UI.Xaml;
 7  using Microsoft.UI.Xaml.Controls;
 8  using Windows.UI;
 9  
10  namespace Microsoft.CmdPal.UI.Controls;
11  
12  public sealed partial class ColorPalette : UserControl
13  {
14      public static readonly DependencyProperty PaletteColorsProperty = DependencyProperty.Register(nameof(PaletteColors), typeof(ObservableCollection<Color>), typeof(ColorPalette), null!)!;
15  
16      public static readonly DependencyProperty CustomPaletteColumnCountProperty = DependencyProperty.Register(nameof(CustomPaletteColumnCount), typeof(int), typeof(ColorPalette), new PropertyMetadata(10))!;
17  
18      public static readonly DependencyProperty SelectedColorProperty = DependencyProperty.Register(nameof(SelectedColor), typeof(Color), typeof(ColorPalette), new PropertyMetadata(null!))!;
19  
20      public event EventHandler<Color?>? SelectedColorChanged;
21  
22      private Color? _selectedColor;
23  
24      public Color? SelectedColor
25      {
26          get => _selectedColor;
27  
28          set
29          {
30              if (_selectedColor != value)
31              {
32                  _selectedColor = value;
33                  if (value is not null)
34                  {
35                      SetValue(SelectedColorProperty, value);
36                  }
37                  else
38                  {
39                      ClearValue(SelectedColorProperty);
40                  }
41              }
42          }
43      }
44  
45      public ObservableCollection<Color> PaletteColors
46      {
47          get => (ObservableCollection<Color>)GetValue(PaletteColorsProperty)!;
48          set => SetValue(PaletteColorsProperty, value);
49      }
50  
51      public int CustomPaletteColumnCount
52      {
53          get => (int)GetValue(CustomPaletteColumnCountProperty);
54          set => SetValue(CustomPaletteColumnCountProperty, value);
55      }
56  
57      public ColorPalette()
58      {
59          PaletteColors = [];
60          InitializeComponent();
61      }
62  
63      private void ListViewBase_OnItemClick(object sender, ItemClickEventArgs e)
64      {
65          if (e.ClickedItem is Color color)
66          {
67              SelectedColor = color;
68              SelectedColorChanged?.Invoke(this, color);
69          }
70      }
71  }