Skip to content

Commit

Permalink
DYN-5782-PrePopulate-CustomColorPicker (#13903)
Browse files Browse the repository at this point in the history
* DYN-5782-PrePopulate-CustomColorPicker

This fix will be pre-populating the custom colors in CustomColorPicker with the colors added in the Preferences->GroupStyles section (just for custom Styles).
This fix is not applicable for the Color Palette node.

* DYN-5782-PrePopulate-CustomColorPicker

This fix will be pre-populating the custom colors in CustomColorPicker (when opened from the Color Palette node) with the colors added in the Preferences->GroupStyles section.
Also I've added a unit test that will be validating that the custom colors list used by ColorPicker is populated correctly with the color loaded from Group Styles
  • Loading branch information
RobertGlobant20 authored Apr 18, 2023
1 parent 5f8b9c5 commit 66bf680
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 4 deletions.
13 changes: 12 additions & 1 deletion src/DynamoCoreWpf/Views/Menu/PreferencesView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public partial class PreferencesView : Window
private IDisposable managePackageCommandEvent;

//This list will be passed everytime that we create a new GroupStyle so the custom colors can remain
private ObservableCollection<CustomColorItem> stylesCustomColors;
internal ObservableCollection<CustomColorItem> stylesCustomColors;

/// <summary>
/// Storing the original custom styles before the user could update them
Expand Down Expand Up @@ -265,6 +265,17 @@ private void RemoveStyle_Click(object sender, RoutedEventArgs e)
private void ButtonColorPicker_Click(object sender, RoutedEventArgs e)
{
var colorPicker = new CustomColorPicker();

//This section populate the CustomColorPicker custom colors with the colors defined in the custom GroupStyles
var customStylesColorsList = viewModel.StyleItemsList.Where(style => style.IsDefault == false);
foreach (var styleItem in customStylesColorsList)
{
Color color = (Color)ColorConverter.ConvertFromString("#"+styleItem.HexColorString);
var customColorItem = new CustomColorItem(color, string.Format("#{0},{1},{2}", color.R, color.G, color.B));
if (!stylesCustomColors.Contains(customColorItem))
stylesCustomColors.Add(customColorItem);
}

//This will set the custom colors list so the custom colors will remain the same for the Preferences panel (no matter if preferences is closed the list will remain).
colorPicker.SetCustomColors(stylesCustomColors);
if (colorPicker == null) return;
Expand Down
19 changes: 18 additions & 1 deletion src/Libraries/CoreNodeModelsWpf/Controls/ColorPalette.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
Expand Down Expand Up @@ -33,15 +34,17 @@ public void OnColorPickerSelectedColor()
}

private ColorPaletteViewModel viewModel = null;
private DynamoViewModel dynViewModel = null;

public ColorPaletteUI()
public ColorPaletteUI(DynamoViewModel dynViewM)
{
InitializeComponent();
if(viewModel == null)
{
viewModel = new ColorPaletteViewModel();
}
this.DataContext = viewModel;
dynViewModel = dynViewM;

//By default the ToggleButton will contain the Black color
viewModel.SelectedColor = new SolidColorBrush(Colors.Black);
Expand All @@ -64,6 +67,20 @@ private void ColorPickerPopup_Closed(object sender, System.EventArgs e)
private void ColorToggleButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
var colorPickerPopup = new CustomColorPicker();

if (dynViewModel != null)
{
//This section populate the CustomColorPicker custom colors with the colors defined in the custom GroupStyles
var customStylesColorsList = dynViewModel.PreferencesViewModel.StyleItemsList.Where(style => style.IsDefault == false);
foreach (var styleItem in customStylesColorsList)
{
Color color = (Color)ColorConverter.ConvertFromString("#" + styleItem.HexColorString);
var customColorItem = new CustomColorItem(color, string.Format("#{0},{1},{2}", color.R, color.G, color.B));
if (!nodeCustomColors.Contains(customColorItem))
nodeCustomColors.Add(customColorItem);
}
}

//This will set the custom colors list so the custom colors will remain the same just for this node.
colorPickerPopup.SetCustomColors(nodeCustomColors);
colorPickerPopup.Placement = PlacementMode.Bottom;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public void CustomizeView(ColorPalette model, NodeView nodeView)
viewNode = nodeView;
colorPaletteNode = model;
converter = new Converters.MediatoDSColorConverter();
ColorPaletteUINode = new ColorPaletteUI();

ColorPaletteUINode = new ColorPaletteUI(viewNode.ViewModel.DynamoViewModel);
colorPaletteViewModel = ColorPaletteUINode.DataContext as ColorPaletteViewModel;
if(colorPaletteNode == null ) return;

Expand Down
57 changes: 56 additions & 1 deletion test/DynamoCoreWpfTests/GroupStylesTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -120,5 +120,60 @@ public void TestAddGroupStyle_ContextMenu()
Assert.AreEqual(annotationViewModel.GroupStyleList.OfType<GroupStyleItem>().Count(), currentGroupStylesCounter);

}

[Test]
public void CustomColorPicker_PrePopulateDefaultColors()
{
Open(@"UI\GroupTest.dyn");
var tabName = "Visual Settings";
var expanderName = "Group Styles";

var preferencesSettings = (View.DataContext as DynamoViewModel).PreferenceSettings;

//Creates the Preferences dialog
var preferencesWindow = new PreferencesView(View);

//Validate the 4 default existing styles list
Assert.AreEqual(preferencesSettings.GroupStyleItemsList.Count, 4);

//Adds two Custom Group Styles to the PreferencesView
preferencesSettings.GroupStyleItemsList.Add(new GroupStyleItem { Name = "Custom 1", HexColorString = "FFFF00", IsDefault = false });
preferencesSettings.GroupStyleItemsList.Add(new GroupStyleItem { Name = "Custom 2", HexColorString = "FF00FF", IsDefault = false });

//Finds the Visual Settings tab and open it
var tabControl = preferencesWindow.preferencesTabControl;
if (tabControl == null) return;
var preferencesTab = (from TabItem tabItem in tabControl.Items
where tabItem.Header.ToString().Equals(tabName)
select tabItem).FirstOrDefault();
if (preferencesTab == null) return;
tabControl.SelectedItem = preferencesTab;

//Finds the Group Styles section and open it
var listExpanders = WpfUtilities.ChildrenOfType<Expander>(preferencesTab.Content as Grid);
var tabExpander = (from expander in listExpanders
where expander.Header.ToString().Equals(expanderName)
select expander).FirstOrDefault();
if (tabExpander == null) return;
tabExpander.IsExpanded = true;


preferencesWindow.Show();
DispatcherUtil.DoEvents();

//Click the AddStyle button
preferencesWindow.AddStyleButton.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
DispatcherUtil.DoEvents();

//The custom Colors list used by ColorPicker is empty
Assert.AreEqual(preferencesWindow.stylesCustomColors.Count, 0);

//Clicks the color button, this will populate the custom Colors list with the style colors added in Group Styles
preferencesWindow.buttonColorPicker.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
DispatcherUtil.DoEvents();

//Validates that the 3 added custom colors are present in the custom Colors list
Assert.AreEqual(preferencesWindow.stylesCustomColors.Count, 2);
}
}
}

0 comments on commit 66bf680

Please sign in to comment.