Refactor theme code to support different types

This commit is contained in:
azakhi 2018-08-24 22:07:24 +03:00
parent d0bc0b6864
commit 8ba3433dcf
17 changed files with 122 additions and 64 deletions

View File

@ -63,7 +63,7 @@ namespace Filtration.ObjectModel.BlockItemBaseTypes
private void OnThemeComponentUpdated(object sender, EventArgs e)
{
Color = ((ThemeComponent) sender).Color;
Color = ((ColorThemeComponent) sender).Color;
}
private void OnThemeComponentDeleted(object sender, EventArgs e)

View File

@ -131,6 +131,7 @@
<Compile Include="Socket.cs" />
<Compile Include="SocketGroup.cs" />
<Compile Include="ThemeEditor\Theme.cs" />
<Compile Include="ThemeEditor\ColorThemeComponent.cs" />
<Compile Include="ThemeEditor\ThemeComponent.cs" />
<Compile Include="ThemeEditor\ThemeComponentCollection.cs" />
<Compile Include="WindsorInstallers\CommandsInstaller.cs" />

View File

@ -0,0 +1,39 @@
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Media;
using Filtration.ObjectModel.Annotations;
using Filtration.ObjectModel.Enums;
namespace Filtration.ObjectModel.ThemeEditor
{
[Serializable]
public class ColorThemeComponent : ThemeComponent
{
private Color _color;
private readonly object _eventLock = new object();
public ColorThemeComponent(ThemeComponentType componentType, string componentName, Color componentColor)
{
if (componentName == null || componentColor == null)
{
throw new ArgumentException("Null parameters not allowed in ColorThemeComponent constructor");
}
ComponentType = componentType;
Color = componentColor;
ComponentName = componentName;
}
public Color Color
{
get { return _color; }
set
{
_color = value;
OnPropertyChanged();
_themeComponentUpdatedEventHandler?.Invoke(this, EventArgs.Empty);
}
}
}
}

View File

@ -32,7 +32,7 @@ namespace Filtration.ObjectModel.ThemeEditor
public void AddComponent(ThemeComponentType componentType, string componentName, Color componentColor)
{
_components.Add(new ThemeComponent(componentType, componentName, componentColor));
_components.Add(new ColorThemeComponent(componentType, componentName, componentColor));
}
}
}

View File

@ -10,8 +10,7 @@ namespace Filtration.ObjectModel.ThemeEditor
[Serializable]
public class ThemeComponent : INotifyPropertyChanged
{
private Color _color;
private EventHandler _themeComponentUpdatedEventHandler;
protected EventHandler _themeComponentUpdatedEventHandler;
private readonly object _eventLock = new object();
public ThemeComponent()
@ -19,18 +18,6 @@ namespace Filtration.ObjectModel.ThemeEditor
}
public ThemeComponent(ThemeComponentType componentType, string componentName, Color componentColor)
{
if (componentName == null || componentColor == null)
{
throw new ArgumentException("Null parameters not allowed in ThemeComponent constructor");
}
ComponentType = componentType;
Color = componentColor;
ComponentName = componentName;
}
// By implementing a custom event accessor here we can keep the UsageCount up to date.
public event EventHandler ThemeComponentUpdated
{
@ -58,17 +45,6 @@ namespace Filtration.ObjectModel.ThemeEditor
public string ComponentName { get; set; }
public ThemeComponentType ComponentType{ get; set; }
public Color Color
{
get { return _color; }
set
{
_color = value;
OnPropertyChanged();
_themeComponentUpdatedEventHandler?.Invoke(this, EventArgs.Empty);
}
}
public int UsageCount
{
get

View File

@ -16,7 +16,15 @@ namespace Filtration.ObjectModel.ThemeEditor
return Items.FirstOrDefault(t => t.ComponentName == componentName && t.ComponentType == componentType);
}
var component = new ThemeComponent(componentType, componentName, componentColor);
ThemeComponent component = null;
switch(componentType)
{
case ThemeComponentType.BackgroundColor:
case ThemeComponentType.BorderColor:
case ThemeComponentType.TextColor:
component = new ColorThemeComponent(componentType, componentName, componentColor);
break;
}
Items.Add(component);
return component;

View File

@ -773,7 +773,7 @@ namespace Filtration.Parser.Tests.Services
// Arrange
var inputString = "Show" + Environment.NewLine +
" SetTextColor 255 20 100 # Rare Item Text";
var testComponent = new ThemeComponent(ThemeComponentType.TextColor, "Rare Item Text", new Color { R = 255, G = 20, B = 100});
var testComponent = new ColorThemeComponent(ThemeComponentType.TextColor, "Rare Item Text", new Color { R = 255, G = 20, B = 100});
var testInputThemeComponentCollection = new ThemeComponentCollection { testComponent };
// Act
@ -1751,7 +1751,7 @@ namespace Filtration.Parser.Tests.Services
var blockItem = new TextColorBlockItem(new Color {A = 255, R = 54, G = 102, B = 255})
{
ThemeComponent = new ThemeComponent(ThemeComponentType.TextColor, "Test Theme Component", new Color())
ThemeComponent = new ColorThemeComponent(ThemeComponentType.TextColor, "Test Theme Component", new Color())
};
_testUtility.TestBlock.BlockItems.Add(blockItem);

View File

@ -26,7 +26,7 @@ namespace Filtration.ThemeEditor.Tests.Services
var testInputTheme = new Theme();
var testInputThemeComponentColor = new Color{ R = 255, G = 0, B = 1 };
var testInputThemeComponent = new ThemeComponent(ThemeComponentType.TextColor, "Test Component 1", testInputThemeComponentColor);
var testInputThemeComponent = new ColorThemeComponent(ThemeComponentType.TextColor, "Test Component 1", testInputThemeComponentColor);
testInputTheme.Components.Add(testInputThemeComponent);
testInputBlockItem.ThemeComponent = testInputThemeComponent;
var mockMessageBoxService = new Mock<IMessageBoxService>();
@ -53,8 +53,8 @@ namespace Filtration.ThemeEditor.Tests.Services
var testInputTheme = new Theme();
var testInputThemeComponentColor = new Color { R = 255, G = 0, B = 1 };
var testInputThemeComponent = new ThemeComponent(ThemeComponentType.TextColor, "Test Component 1", testInputThemeComponentColor);
var testInputBlockItemThemeComponent = new ThemeComponent(ThemeComponentType.TextColor, "Different Component", testInputThemeComponentColor);
var testInputThemeComponent = new ColorThemeComponent(ThemeComponentType.TextColor, "Test Component 1", testInputThemeComponentColor);
var testInputBlockItemThemeComponent = new ColorThemeComponent(ThemeComponentType.TextColor, "Different Component", testInputThemeComponentColor);
testInputTheme.Components.Add(testInputThemeComponent);
testInputBlockItem.ThemeComponent = testInputBlockItemThemeComponent;

View File

@ -108,6 +108,7 @@
<Compile Include="Providers\ThemeProvider.cs" />
<Compile Include="Services\ThemePersistenceService.cs" />
<Compile Include="Services\ThemeService.cs" />
<Compile Include="ViewModels\ColorThemeComponentViewModel.cs" />
<Compile Include="ViewModels\IThemeViewModelFactory.cs" />
<Compile Include="ViewModels\ThemeComponentViewModel.cs" />
<Compile Include="ViewModels\ThemeEditorViewModel.cs" />

View File

@ -3,6 +3,7 @@ using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Filtration.ObjectModel;
using Filtration.ObjectModel.Enums;
using Filtration.ObjectModel.ThemeEditor;
using Filtration.ThemeEditor.Services;
using Filtration.ThemeEditor.ViewModels;
@ -34,7 +35,14 @@ namespace Filtration.ThemeEditor.Providers
var themeComponentCollection = script.ThemeComponents.Aggregate(new ThemeComponentCollection(),
(c, component) =>
{
c.Add(new ThemeComponent(component.ComponentType, component.ComponentName, component.Color));
switch(component.ComponentType)
{
case ThemeComponentType.BackgroundColor:
case ThemeComponentType.BorderColor:
case ThemeComponentType.TextColor:
c.Add(new ColorThemeComponent(component.ComponentType, component.ComponentName, ((ColorThemeComponent)component).Color));
break;
}
return c;
});

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Filtration.Common.Services;
@ -29,39 +30,19 @@ namespace Filtration.ThemeEditor.Services
var mismatchedComponents = false;
foreach (var component in theme.Components)
{
var componentMatched = false;
Type targetType = null;
var blocks = script.ItemFilterBlocks.OfType<ItemFilterBlock>();
switch (component.ComponentType)
{
case ThemeComponentType.BackgroundColor:
targetType = typeof (BackgroundColorBlockItem);
mismatchedComponents = ApplyColorTheme(blocks, typeof(BackgroundColorBlockItem), component);
break;
case ThemeComponentType.TextColor:
targetType = typeof (TextColorBlockItem);
mismatchedComponents = ApplyColorTheme(blocks, typeof(TextColorBlockItem), component);
break;
case ThemeComponentType.BorderColor:
targetType = typeof (BorderColorBlockItem);
mismatchedComponents = ApplyColorTheme(blocks, typeof(BorderColorBlockItem), component);
break;
}
foreach (var block in script.ItemFilterBlocks.OfType<ItemFilterBlock>())
{
foreach (var blockItem in block.BlockItems.Where(i => i.GetType() == targetType))
{
var colorBlockItem = (ColorBlockItem) blockItem;
if (colorBlockItem.ThemeComponent != null &&
colorBlockItem.ThemeComponent.ComponentName == component.ComponentName)
{
colorBlockItem.Color = component.Color;
componentMatched = true;
}
}
}
if (!componentMatched)
{
mismatchedComponents = true;
}
}
if (mismatchedComponents)
@ -71,5 +52,25 @@ namespace Filtration.ThemeEditor.Services
MessageBoxButton.OK, MessageBoxImage.Exclamation);
}
}
private bool ApplyColorTheme(IEnumerable<ItemFilterBlock> blocks, Type type, ThemeComponent component)
{
var componentMatched = false;
foreach (var block in blocks)
{
foreach (var blockItem in block.BlockItems.Where(i => i.GetType() == type))
{
var colorBlockItem = (ColorBlockItem)blockItem;
if (colorBlockItem.ThemeComponent != null &&
colorBlockItem.ThemeComponent.ComponentName == component.ComponentName)
{
colorBlockItem.Color = ((ColorThemeComponent)component).Color;
componentMatched = true;
}
}
}
return !componentMatched;
}
}
}

View File

@ -0,0 +1,10 @@
using System.Windows.Media;
using Filtration.ObjectModel.Enums;
namespace Filtration.ThemeEditor.ViewModels
{
public class ColorThemeComponentViewModel : ThemeComponentViewModel
{
public Color Color { get; set; }
}
}

View File

@ -7,13 +7,11 @@ namespace Filtration.ThemeEditor.ViewModels
{
string ComponentName { get; set; }
ThemeComponentType ComponentType { get; set; }
Color Color { get; set; }
}
public class ThemeComponentViewModel : IThemeComponentViewModel
{
public string ComponentName { get; set; }
public ThemeComponentType ComponentType { get; set; }
public Color Color { get; set; }
}
}

View File

@ -194,8 +194,15 @@ namespace Filtration.ThemeEditor.ViewModels
private void OnAddThemeComponentCommand(ThemeComponentType themeComponentType)
{
Components.Add(new ThemeComponent(themeComponentType, "Untitled Component",
new Color {A = 255, R = 255, G = 255, B = 255}));
switch (themeComponentType)
{
case ThemeComponentType.BackgroundColor:
case ThemeComponentType.BorderColor:
case ThemeComponentType.TextColor:
Components.Add(new ColorThemeComponent(themeComponentType, "Untitled Component",
new Color { A = 255, R = 255, G = 255, B = 255 }));
break;
}
}
private void OnDeleteThemeComponentCommand(ThemeComponent themeComponent)

View File

@ -58,6 +58,13 @@
</Style>
</ContentControl.Style>
</ContentControl>
<xctk:ColorPicker Grid.Row="2" SelectedColor="{Binding Color}" />
<ContentControl Grid.Row="2" Content="{Binding Mode=OneWay}">
<ContentControl.Resources>
<!-- Color Theme Template -->
<DataTemplate DataType="{x:Type themeEditor:ColorThemeComponent}">
<xctk:ColorPicker SelectedColor="{Binding Color}" />
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</Grid>
</UserControl>

View File

@ -46,6 +46,7 @@ namespace Filtration
cfg.ConstructServicesUsing(_container.Resolve);
cfg.CreateMap<Theme, IThemeEditorViewModel>().ConstructUsingServiceLocator();
cfg.CreateMap<ThemeComponent, ThemeComponentViewModel>().ReverseMap();
cfg.CreateMap<ColorThemeComponent, ColorThemeComponentViewModel>().ReverseMap();
cfg.CreateMap<IThemeEditorViewModel, Theme>();
});

View File

@ -6,6 +6,7 @@ using System.Windows;
using Filtration.Annotations;
using Filtration.ObjectModel;
using Filtration.ObjectModel.BlockItemBaseTypes;
using Filtration.ObjectModel.ThemeEditor;
using Filtration.Views;
using GalaSoft.MvvmLight.CommandWpf;
using Xceed.Wpf.Toolkit;
@ -97,7 +98,7 @@ namespace Filtration.UserControls
var blockItem = BlockItem as ColorBlockItem;
if (blockItem?.ThemeComponent == null) return;
blockItem.Color = blockItem.ThemeComponent.Color;
blockItem.Color = ((ColorThemeComponent)blockItem.ThemeComponent).Color;
}
public event PropertyChangedEventHandler PropertyChanged;