Skip to content

Commit

Permalink
update Settings Color Combo
Browse files Browse the repository at this point in the history
Modified : Source/AutoCommitMessage.csproj
Modified : Source/ToolWindows/SettingsControl.xaml
Modified : Source/ToolWindows/SettingsControl.xaml.cs
  • Loading branch information
samanazadi1996 committed Sep 19, 2024
1 parent 1dd6850 commit 9fc224f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 105 deletions.
3 changes: 3 additions & 0 deletions Source/AutoCommitMessage.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@
<ItemGroup>
<PackageReference Include="Community.VisualStudio.VSCT" Version="16.0.29.6" PrivateAssets="all" />
<PackageReference Include="Community.VisualStudio.Toolkit.17" Version="17.0.507" ExcludeAssets="Runtime" />
<PackageReference Include="Extended.Wpf.Toolkit">
<Version>4.6.1</Version>
</PackageReference>
<PackageReference Include="Microsoft.VSSDK.BuildTools" Version="17.9.3168" />
<PackageReference Include="System.Text.Json">
<Version>8.0.4</Version>
Expand Down
18 changes: 5 additions & 13 deletions Source/ToolWindows/SettingsControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
xmlns:util="clr-namespace:Microsoft.VisualStudio.PlatformUI;assembly=Microsoft.VisualStudio.Utilities"
xmlns:catalog="clr-namespace:Microsoft.VisualStudio.Imaging;assembly=Microsoft.VisualStudio.ImageCatalog"
xmlns:toolkit="clr-namespace:Community.VisualStudio.Toolkit;assembly=Community.VisualStudio.Toolkit"
xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
toolkit:Themes.UseVsTheme="True"
mc:Ignorable="d"
d:DesignHeight="300"
Expand All @@ -19,32 +20,23 @@
<ToolBar DockPanel.Dock="Top" Background="Transparent">
<!-- Buttons with simple text -->
<Button Content="Back" Click="Back_OnClick" ToolTip="Back" Margin="3" Padding="3" Background="Gray"/>
<Button Content="Refresh" Click="Refresh_OnClick" ToolTip="Reload and update the file change list" Margin="3" Padding="3" Background="Gray"/>

</ToolBar>

<Grid>
<ScrollViewer VerticalScrollBarVisibility="Auto">
<StackPanel Orientation="Vertical" Margin="10">

<!-- Input for Message -->
<StackPanel Margin="0 0 0 10">
<Label Content="Select Staged File Color:" FontWeight="Bold"/>
<ComboBox x:Name="StagedColorComboBox"
FontWeight="Normal"
SelectedValuePath="Tag" Margin="5">
<ComboBoxItem Content="LimeGreen" Tag="LimeGreen"/>
<ComboBoxItem Content="Green" Tag="Green"/>
<ComboBoxItem Content="Blue" Tag="Blue"/>
</ComboBox>
<xctk:ColorPicker x:Name="StagedColorPicker" SelectedColor="LimeGreen" Margin="5"/>
</StackPanel>

<!-- Input for Unstaged File Color -->
<StackPanel Margin="0 0 0 10">
<Label Content="Select UnStaged File Color:" FontWeight="Bold"/>
<ComboBox x:Name="UnStagedColorComboBox" SelectedValuePath="Tag" Margin="5" FontWeight="Normal" >
<ComboBoxItem Content="OrangeRed" Tag="OrangeRed"/>
<ComboBoxItem Content="Red" Tag="Red"/>
<ComboBoxItem Content="Yellow" Tag="Yellow"/>
</ComboBox>
<xctk:ColorPicker x:Name="UnstagedColorPicker" SelectedColor="OrangeRed" Margin="5"/>
</StackPanel>

<StackPanel Margin="0 0 0 10">
Expand Down
115 changes: 23 additions & 92 deletions Source/ToolWindows/SettingsControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Linq;
using System.Reflection;
using AutoCommitMessage.Helper;
using AutoCommitMessage.Models;
using System.Windows;
Expand All @@ -9,109 +7,24 @@
namespace AutoCommitMessage;

public partial class SettingsControl : UserControl
{
public string[] AvailableColors { get; set; }
{
public SettingsControl()
{
InitializeComponent();

LoadColors();

PopulateColorDropdowns();

LoadButtonstates();
}

private void LoadButtonstates()
{
CommitButton.IsChecked = SettingsModel.Data.CommitButton;
GenerateMessageButton.IsChecked = SettingsModel.Data.GenerateMessageButton;
PullButton.IsChecked = SettingsModel.Data.PullButton;
PushButton.IsChecked = SettingsModel.Data.PushButton;
RefreshButton.IsChecked = SettingsModel.Data.RefreshButton;
StageAllButton.IsChecked = SettingsModel.Data.StageAllButton;
}
private void LoadColors()
{
AvailableColors = typeof(Colors).GetProperties(BindingFlags.Static | BindingFlags.Public)
.Where(p => p.PropertyType == typeof(Color))
.Select(p => p.Name)
.ToArray();
LoadSettings();
}

private void PopulateColorDropdowns()
{
// Clear any existing items
StagedColorComboBox.Items.Clear();
UnStagedColorComboBox.Items.Clear();

// Add available colors to both ComboBoxes using the helper method
foreach (var color in AvailableColors)
{
AddColorToComboBox(StagedColorComboBox, color);
AddColorToComboBox(UnStagedColorComboBox, color);
}

// Set default values for both ComboBoxes
SetDefaultColor(StagedColorComboBox, SettingsModel.Data.StagedFileColor);
SetDefaultColor(UnStagedColorComboBox, SettingsModel.Data.UnStagedFileColor);
void SetDefaultColor(ComboBox comboBox, Color defaultColor)
{
var defaultColorName = ColorToName(defaultColor);

var defaultItem = comboBox.Items
.Cast<ComboBoxItem>()
.FirstOrDefault(item => item.Tag.ToString() == defaultColorName);

if (defaultItem != null)
{
comboBox.SelectedItem = defaultItem;
}
return;

string ColorToName(Color color)
{
var colorProperty = typeof(Colors).GetProperties(BindingFlags.Static | BindingFlags.Public)
.FirstOrDefault(p => ((Color)p.GetValue(null)).Equals(color));

return colorProperty?.Name ?? color.ToString();
}
}
void AddColorToComboBox(ComboBox comboBox, string color)
{
var colorPanel = new StackPanel { Orientation = Orientation.Horizontal };

var colorRectangle = new System.Windows.Shapes.Rectangle
{
Width = 16,
Height = 16,
Fill = new SolidColorBrush((Color)ColorConverter.ConvertFromString(color)),
Margin = new Thickness(0, 0, 5, 0) // Add some space between the rectangle and the text
};

var colorName = new TextBlock { Text = color };

colorPanel.Children.Add(colorRectangle);
colorPanel.Children.Add(colorName);

comboBox.Items.Add(new ComboBoxItem { Content = colorPanel, Tag = color });
}
}
private async void Back_OnClick(object sender, RoutedEventArgs e)
{
await AutoCommitMessage.Settings.HideAsync();
}
private async void Save_OnClick(object sender, RoutedEventArgs e)
{
// Parse the selected colors
Color stagedFileColor = (Color)ColorConverter.ConvertFromString((string)((ComboBoxItem)StagedColorComboBox.SelectedItem).Tag);
Color unStagedFileColor = (Color)ColorConverter.ConvertFromString((string)((ComboBoxItem)UnStagedColorComboBox.SelectedItem).Tag);

// Create model and save
var model = new SettingsModel.Model
{
StagedFileColor = stagedFileColor,
UnStagedFileColor = unStagedFileColor,
StagedFileColor = StagedColorPicker.SelectedColor ?? Colors.LimeGreen,
UnStagedFileColor = UnstagedColorPicker.SelectedColor ?? Colors.OrangeRed,
CommitButton = CommitButton.IsChecked == true,
GenerateMessageButton = GenerateMessageButton.IsChecked == true,
PullButton = PullButton.IsChecked == true,
Expand All @@ -122,8 +35,26 @@ private async void Save_OnClick(object sender, RoutedEventArgs e)

SettingsModel.Save(ApplicationContext.GetOpenedFolder(), model);
await AutoCommitMessage.Settings.HideAsync();
}

private void Refresh_OnClick(object sender, RoutedEventArgs e)
{
LoadSettings();
}
private void LoadSettings()
{
SettingsModel.Init(ApplicationContext.GetOpenedFolder());

if (SettingsModel.Data is null) return;

}
StagedColorPicker.SelectedColor = SettingsModel.Data.StagedFileColor;
UnstagedColorPicker.SelectedColor = SettingsModel.Data.UnStagedFileColor;

CommitButton.IsChecked = SettingsModel.Data.CommitButton;
GenerateMessageButton.IsChecked = SettingsModel.Data.GenerateMessageButton;
PullButton.IsChecked = SettingsModel.Data.PullButton;
PushButton.IsChecked = SettingsModel.Data.PushButton;
RefreshButton.IsChecked = SettingsModel.Data.RefreshButton;
StageAllButton.IsChecked = SettingsModel.Data.StageAllButton;
}
}

0 comments on commit 9fc224f

Please sign in to comment.