2015-06-26 12:42:20 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2015-07-25 14:02:42 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2015-06-26 12:42:20 -04:00
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Forms;
|
2015-07-05 17:43:17 -04:00
|
|
|
|
using System.Windows.Media;
|
2015-06-26 12:42:20 -04:00
|
|
|
|
using System.Windows.Media.Imaging;
|
2015-07-04 12:28:34 -04:00
|
|
|
|
using Filtration.Common.Services;
|
2015-06-26 12:42:20 -04:00
|
|
|
|
using Filtration.Common.ViewModels;
|
|
|
|
|
using Filtration.Interface;
|
2015-07-06 08:41:46 -04:00
|
|
|
|
using Filtration.ObjectModel;
|
2015-07-05 17:43:17 -04:00
|
|
|
|
using Filtration.ObjectModel.Enums;
|
|
|
|
|
using Filtration.ObjectModel.ThemeEditor;
|
2015-07-06 08:41:46 -04:00
|
|
|
|
using Filtration.ThemeEditor.Messages;
|
2015-06-26 12:42:20 -04:00
|
|
|
|
using Filtration.ThemeEditor.Providers;
|
2015-07-05 17:43:17 -04:00
|
|
|
|
using GalaSoft.MvvmLight.CommandWpf;
|
2015-07-06 08:41:46 -04:00
|
|
|
|
using GalaSoft.MvvmLight.Messaging;
|
2015-07-03 17:16:17 -04:00
|
|
|
|
using NLog;
|
2015-06-26 12:42:20 -04:00
|
|
|
|
|
|
|
|
|
namespace Filtration.ThemeEditor.ViewModels
|
|
|
|
|
{
|
2015-07-05 17:43:17 -04:00
|
|
|
|
public interface IThemeEditorViewModel : IEditableDocument
|
2015-06-26 12:42:20 -04:00
|
|
|
|
{
|
2015-07-05 17:43:17 -04:00
|
|
|
|
RelayCommand<ThemeComponentType> AddThemeComponentCommand { get; }
|
|
|
|
|
RelayCommand<ThemeComponent> DeleteThemeComponentCommand { get; }
|
2015-07-06 08:41:46 -04:00
|
|
|
|
RelayCommand CloseCommand { get; }
|
2015-07-05 17:43:17 -04:00
|
|
|
|
|
2015-07-06 08:41:46 -04:00
|
|
|
|
void InitialiseForNewTheme(ThemeComponentCollection themeComponentCollection);
|
2017-06-17 08:50:44 -04:00
|
|
|
|
void InitialiseForMasterTheme(IItemFilterScript script);
|
2015-07-06 08:41:46 -04:00
|
|
|
|
bool IsMasterTheme { get; }
|
2017-06-17 08:50:44 -04:00
|
|
|
|
IItemFilterScript IsMasterThemeForScript { get; }
|
2015-06-26 12:42:20 -04:00
|
|
|
|
string Title { get; }
|
|
|
|
|
string FilePath { get; set; }
|
|
|
|
|
string Filename { get; }
|
|
|
|
|
string Name { get; set; }
|
2015-07-05 17:43:17 -04:00
|
|
|
|
ThemeComponentCollection Components { get; set; }
|
|
|
|
|
ThemeComponent SelectedThemeComponent { get; }
|
2015-06-26 12:42:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-05 17:43:17 -04:00
|
|
|
|
public class ThemeEditorViewModel : PaneViewModel, IThemeEditorViewModel
|
2015-06-26 12:42:20 -04:00
|
|
|
|
{
|
2015-12-13 15:17:15 -05:00
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
2015-07-03 17:16:17 -04:00
|
|
|
|
|
2015-06-26 12:42:20 -04:00
|
|
|
|
private readonly IThemeProvider _themeProvider;
|
2015-07-04 12:28:34 -04:00
|
|
|
|
private readonly IMessageBoxService _messageBoxService;
|
2015-06-26 12:42:20 -04:00
|
|
|
|
private bool _filenameIsFake;
|
|
|
|
|
private string _filePath;
|
2015-07-05 17:43:17 -04:00
|
|
|
|
private ThemeComponent _selectedThemeComponent;
|
2015-06-26 12:42:20 -04:00
|
|
|
|
|
2015-07-05 17:43:17 -04:00
|
|
|
|
public ThemeEditorViewModel(IThemeProvider themeProvider,
|
2015-07-04 12:28:34 -04:00
|
|
|
|
IMessageBoxService messageBoxService)
|
2015-06-26 12:42:20 -04:00
|
|
|
|
{
|
|
|
|
|
_themeProvider = themeProvider;
|
2015-07-04 12:28:34 -04:00
|
|
|
|
_messageBoxService = messageBoxService;
|
2015-06-26 12:42:20 -04:00
|
|
|
|
|
2015-07-06 08:41:46 -04:00
|
|
|
|
AddThemeComponentCommand = new RelayCommand<ThemeComponentType>(OnAddThemeComponentCommand, t => IsMasterTheme);
|
2015-07-05 17:43:17 -04:00
|
|
|
|
DeleteThemeComponentCommand = new RelayCommand<ThemeComponent>(OnDeleteThemeComponentCommand,
|
2015-07-06 08:41:46 -04:00
|
|
|
|
t => IsMasterTheme && SelectedThemeComponent != null);
|
2015-07-25 14:02:42 -04:00
|
|
|
|
CloseCommand = new RelayCommand(async () => await OnCloseCommand());
|
2015-06-26 12:42:20 -04:00
|
|
|
|
|
|
|
|
|
var icon = new BitmapImage();
|
|
|
|
|
icon.BeginInit();
|
2015-06-27 13:08:06 -04:00
|
|
|
|
icon.UriSource = new Uri("pack://application:,,,/Filtration;component/Resources/Icons/Theme.ico");
|
2015-06-26 12:42:20 -04:00
|
|
|
|
icon.EndInit();
|
|
|
|
|
IconSource = icon;
|
2015-07-06 08:41:46 -04:00
|
|
|
|
|
2015-06-26 12:42:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-13 15:17:15 -05:00
|
|
|
|
public RelayCommand<ThemeComponentType> AddThemeComponentCommand { get; }
|
|
|
|
|
public RelayCommand<ThemeComponent> DeleteThemeComponentCommand { get; }
|
|
|
|
|
public RelayCommand CloseCommand { get; }
|
2015-07-05 17:43:17 -04:00
|
|
|
|
|
2015-12-13 15:17:15 -05:00
|
|
|
|
public bool IsMasterTheme => Components.IsMasterCollection;
|
2015-07-05 17:43:17 -04:00
|
|
|
|
|
2017-06-17 08:50:44 -04:00
|
|
|
|
public IItemFilterScript IsMasterThemeForScript { get; private set; }
|
2015-07-06 08:41:46 -04:00
|
|
|
|
|
|
|
|
|
public void InitialiseForNewTheme(ThemeComponentCollection themeComponentCollection)
|
2015-06-26 12:42:20 -04:00
|
|
|
|
{
|
2015-07-05 17:43:17 -04:00
|
|
|
|
Components = themeComponentCollection;
|
2015-07-06 08:41:46 -04:00
|
|
|
|
_filenameIsFake = true;
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-17 08:50:44 -04:00
|
|
|
|
public void InitialiseForMasterTheme(IItemFilterScript script)
|
2015-07-06 08:41:46 -04:00
|
|
|
|
{
|
|
|
|
|
Components = script.ThemeComponents;
|
|
|
|
|
IsMasterThemeForScript = script;
|
|
|
|
|
_filenameIsFake = true;
|
|
|
|
|
|
2015-06-26 12:42:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-13 15:17:15 -05:00
|
|
|
|
public bool IsScript => false;
|
|
|
|
|
public bool IsTheme => true;
|
2015-06-26 12:42:20 -04:00
|
|
|
|
public bool IsDirty { get; private set; }
|
|
|
|
|
|
|
|
|
|
public string FilePath
|
|
|
|
|
{
|
|
|
|
|
get { return _filePath; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_filePath = value;
|
|
|
|
|
Title = Filename;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-13 15:17:15 -05:00
|
|
|
|
public string Filename => _filenameIsFake ? FilePath : Path.GetFileName(FilePath);
|
2015-06-26 12:42:20 -04:00
|
|
|
|
|
|
|
|
|
public string Name { get; set; }
|
|
|
|
|
|
2015-07-05 17:43:17 -04:00
|
|
|
|
public ThemeComponentCollection Components { get; set; }
|
|
|
|
|
|
|
|
|
|
public ThemeComponent SelectedThemeComponent
|
|
|
|
|
{
|
|
|
|
|
get { return _selectedThemeComponent; }
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
_selectedThemeComponent = value;
|
|
|
|
|
RaisePropertyChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-06-26 12:42:20 -04:00
|
|
|
|
|
2015-07-25 14:02:42 -04:00
|
|
|
|
public async Task SaveAsync()
|
2015-06-26 12:42:20 -04:00
|
|
|
|
{
|
2015-07-06 08:41:46 -04:00
|
|
|
|
if (IsMasterTheme) return;
|
|
|
|
|
|
2015-06-26 12:42:20 -04:00
|
|
|
|
if (_filenameIsFake)
|
|
|
|
|
{
|
2015-07-25 14:02:42 -04:00
|
|
|
|
await SaveAsAsync();
|
2015-06-26 12:42:20 -04:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2015-07-25 14:02:42 -04:00
|
|
|
|
await _themeProvider.SaveThemeAsync(this, FilePath);
|
2015-06-26 12:42:20 -04:00
|
|
|
|
//RemoveDirtyFlag();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2015-12-13 15:17:15 -05:00
|
|
|
|
if (Logger.IsErrorEnabled)
|
2015-07-03 17:16:17 -04:00
|
|
|
|
{
|
2015-12-13 15:17:15 -05:00
|
|
|
|
Logger.Error(e);
|
2015-07-03 17:16:17 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-04 12:28:34 -04:00
|
|
|
|
_messageBoxService.Show("Save Error", "Error saving filter theme - " + e.Message, MessageBoxButton.OK, MessageBoxImage.Error);
|
2015-06-26 12:42:20 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-25 14:02:42 -04:00
|
|
|
|
public async Task SaveAsAsync()
|
2015-06-26 12:42:20 -04:00
|
|
|
|
{
|
2015-07-06 08:41:46 -04:00
|
|
|
|
if (IsMasterTheme) return;
|
|
|
|
|
|
2015-06-26 12:42:20 -04:00
|
|
|
|
var saveDialog = new SaveFileDialog
|
|
|
|
|
{
|
|
|
|
|
DefaultExt = ".filter",
|
|
|
|
|
Filter = @"Filter Theme Files (*.filtertheme)|*.filtertheme|All Files (*.*)|*.*"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var result = saveDialog.ShowDialog();
|
|
|
|
|
|
|
|
|
|
if (result != DialogResult.OK) return;
|
|
|
|
|
|
|
|
|
|
var previousFilePath = FilePath;
|
2015-07-03 17:16:17 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2015-06-26 12:42:20 -04:00
|
|
|
|
FilePath = saveDialog.FileName;
|
2015-07-25 14:02:42 -04:00
|
|
|
|
await _themeProvider.SaveThemeAsync(this, FilePath);
|
2015-06-26 12:42:20 -04:00
|
|
|
|
_filenameIsFake = false;
|
2015-07-06 10:33:25 -04:00
|
|
|
|
Title = Filename;
|
2015-06-26 12:42:20 -04:00
|
|
|
|
//RemoveDirtyFlag();
|
2015-07-03 17:16:17 -04:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2015-12-13 15:17:15 -05:00
|
|
|
|
if (Logger.IsErrorEnabled)
|
2015-07-03 17:16:17 -04:00
|
|
|
|
{
|
2015-12-13 15:17:15 -05:00
|
|
|
|
Logger.Error(e);
|
2015-07-03 17:16:17 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-04 12:28:34 -04:00
|
|
|
|
_messageBoxService.Show("Save Error", "Error saving theme file - " + e.Message, MessageBoxButton.OK,
|
2015-07-03 17:16:17 -04:00
|
|
|
|
MessageBoxImage.Error);
|
|
|
|
|
FilePath = previousFilePath;
|
|
|
|
|
}
|
2015-06-26 12:42:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-25 14:02:42 -04:00
|
|
|
|
public async Task OnCloseCommand()
|
2015-06-26 12:42:20 -04:00
|
|
|
|
{
|
2015-07-25 14:02:42 -04:00
|
|
|
|
await Close();
|
2015-06-26 12:42:20 -04:00
|
|
|
|
}
|
2015-07-05 17:43:17 -04:00
|
|
|
|
|
2015-07-25 14:02:42 -04:00
|
|
|
|
#pragma warning disable 1998
|
|
|
|
|
public async Task Close()
|
|
|
|
|
#pragma warning restore 1998
|
2015-07-06 08:41:46 -04:00
|
|
|
|
{
|
2015-07-25 14:02:42 -04:00
|
|
|
|
Messenger.Default.Send(new ThemeClosedMessage {ClosedViewModel = this});
|
2015-07-06 08:41:46 -04:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-05 17:43:17 -04:00
|
|
|
|
private void OnAddThemeComponentCommand(ThemeComponentType themeComponentType)
|
|
|
|
|
{
|
2018-08-24 15:07:24 -04:00
|
|
|
|
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;
|
2018-08-25 08:52:16 -04:00
|
|
|
|
case ThemeComponentType.FontSize:
|
|
|
|
|
Components.Add(new IntegerThemeComponent(themeComponentType, "Untitled Component", 35));
|
|
|
|
|
break;
|
2018-08-26 13:24:13 -04:00
|
|
|
|
case ThemeComponentType.AlertSound:
|
|
|
|
|
Components.Add(new StrIntThemeComponent(themeComponentType, "Untitled Component", "1", 100));
|
|
|
|
|
break;
|
2018-08-24 15:07:24 -04:00
|
|
|
|
}
|
2015-07-05 17:43:17 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnDeleteThemeComponentCommand(ThemeComponent themeComponent)
|
|
|
|
|
{
|
|
|
|
|
if (themeComponent == null) return;
|
|
|
|
|
|
|
|
|
|
themeComponent.TerminateComponent();
|
|
|
|
|
Components.Remove(themeComponent);
|
|
|
|
|
}
|
2015-06-26 12:42:20 -04:00
|
|
|
|
}
|
|
|
|
|
}
|