PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/MarkPad/Settings/UI/SettingsViewModel.cs

https://github.com/bcott/DownmarkerWPF
C# | 214 lines | 175 code | 39 blank | 0 comment | 11 complexity | 390ecda1877b737eebe902daa90cf8b5 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Windows.Media;
  6. using Caliburn.Micro;
  7. using MarkPad.Document.SpellCheck;
  8. using MarkPad.DocumentSources.MetaWeblog;
  9. using MarkPad.Events;
  10. using MarkPad.Framework;
  11. using MarkPad.Plugins;
  12. using MarkPad.Settings.Models;
  13. namespace MarkPad.Settings.UI
  14. {
  15. public class SettingsViewModel : Screen
  16. {
  17. public const string FontSizeSettingsKey = "Font";
  18. public const string FontFamilySettingsKey = "FontFamily";
  19. private readonly ISettingsProvider settingsProvider;
  20. private readonly IEventAggregator eventAggregator;
  21. private readonly Func<IPlugin, PluginViewModel> pluginViewModelCreator;
  22. private readonly ISpellingService spellingService;
  23. private readonly IList<IPlugin> plugins;
  24. private readonly IBlogService blogService;
  25. private readonly IMarkpadRegistryEditor markpadRegistryEditor;
  26. private BlogSetting currentBlog;
  27. public IEnumerable<ExtensionViewModel> Extensions { get; set; }
  28. public IEnumerable<FontSizes> FontSizes { get; set; }
  29. public IEnumerable<FontFamily> FontFamilies { get; set; }
  30. public ObservableCollection<BlogSetting> Blogs { get; set; }
  31. public IEnumerable<SpellingLanguages> Languages { get; set; }
  32. public SpellingLanguages SelectedLanguage { get; set; }
  33. public FontSizes SelectedFontSize { get; set; }
  34. public FontFamily SelectedFontFamily { get; set; }
  35. public bool EnableFloatingToolBar { get; set; }
  36. public PluginViewModel SelectedPlugin { get; set; }
  37. public IEnumerable<PluginViewModel> Plugins { get; private set; }
  38. public IndentType IndentType { get; set; }
  39. public bool EnableMarkdownExtra { get; set; }
  40. public bool CanEditBlog { get { return currentBlog != null; } }
  41. public bool CanRemoveBlog { get { return currentBlog != null; } }
  42. public bool IsColorsInverted { get; set; }
  43. public BlogSetting CurrentBlog
  44. {
  45. get { return currentBlog; }
  46. set
  47. {
  48. currentBlog = value;
  49. NotifyOfPropertyChange(() => CanEditBlog);
  50. NotifyOfPropertyChange(() => CanRemoveBlog);
  51. }
  52. }
  53. public int SelectedActualFontSize
  54. {
  55. get
  56. {
  57. return Constants.FONT_SIZE_ENUM_ADJUSTMENT + (int)SelectedFontSize;
  58. }
  59. }
  60. public string EditorFontPreviewLabel
  61. {
  62. get
  63. {
  64. return string.Format(
  65. "Editor font ({0}, {1} pt)",
  66. SelectedFontFamily.Source,
  67. SelectedActualFontSize);
  68. }
  69. }
  70. public override string DisplayName
  71. {
  72. get { return "Settings"; }
  73. set { }
  74. }
  75. public ObservableCollection<IndentType> IndentTypes
  76. {
  77. get { return new ObservableCollection<IndentType> { IndentType.Tabs, IndentType.Spaces }; }
  78. }
  79. public SettingsViewModel(
  80. ISettingsProvider settingsProvider,
  81. IEventAggregator eventAggregator,
  82. Func<IPlugin, PluginViewModel> pluginViewModelCreator,
  83. ISpellingService spellingService,
  84. IEnumerable<IPlugin> plugins,
  85. IBlogService blogService,
  86. IMarkpadRegistryEditor markpadRegistryEditor)
  87. {
  88. this.settingsProvider = settingsProvider;
  89. this.eventAggregator = eventAggregator;
  90. this.pluginViewModelCreator = pluginViewModelCreator;
  91. this.spellingService = spellingService;
  92. this.blogService = blogService;
  93. this.plugins = plugins.ToList();
  94. this.markpadRegistryEditor = markpadRegistryEditor;
  95. }
  96. public void Initialize()
  97. {
  98. Extensions = markpadRegistryEditor.GetExtensionsFromRegistry();
  99. var settings = settingsProvider.GetSettings<MarkPadSettings>();
  100. var blogs = blogService.GetBlogs();
  101. Blogs = new ObservableCollection<BlogSetting>(blogs);
  102. Languages = Enum.GetValues(typeof(SpellingLanguages)).OfType<SpellingLanguages>().ToArray();
  103. FontSizes = Enum.GetValues(typeof(FontSizes)).OfType<FontSizes>().ToArray();
  104. FontFamilies = Fonts.SystemFontFamilies.OrderBy(f => f.Source);
  105. SelectedLanguage = settings.Language;
  106. var fontFamily = settings.FontFamily;
  107. SelectedFontFamily = Fonts.SystemFontFamilies.FirstOrDefault(f => f.Source == fontFamily);
  108. SelectedFontSize = settings.FontSize;
  109. if (SelectedFontFamily == null)
  110. {
  111. SelectedFontFamily = FontHelpers.TryGetFontFamilyFromStack(Constants.DEFAULT_EDITOR_FONT_FAMILY);
  112. SelectedFontSize = Constants.DEFAULT_EDITOR_FONT_SIZE;
  113. }
  114. EnableFloatingToolBar = settings.FloatingToolBarEnabled;
  115. IsColorsInverted = settings.IsEditorColorsInverted;
  116. Plugins = plugins
  117. .Where(plugin => !plugin.IsHidden)
  118. .Select(plugin => pluginViewModelCreator(plugin));
  119. EnableMarkdownExtra = settings.MarkdownExtraEnabled;
  120. }
  121. public bool AddBlog()
  122. {
  123. var blog = blogService.AddBlog();
  124. if (blog != null)
  125. {
  126. Blogs.Add(blog);
  127. return true;
  128. }
  129. return false;
  130. }
  131. public void EditBlog()
  132. {
  133. if (CurrentBlog == null) return;
  134. blogService.EditBlog(CurrentBlog);
  135. }
  136. public void RemoveBlog()
  137. {
  138. if (CurrentBlog != null)
  139. {
  140. blogService.Remove(CurrentBlog);
  141. Blogs.Remove(CurrentBlog);
  142. }
  143. }
  144. public void ResetFont()
  145. {
  146. SelectedFontFamily = FontHelpers.TryGetFontFamilyFromStack(Constants.DEFAULT_EDITOR_FONT_FAMILY);
  147. SelectedFontSize = Constants.DEFAULT_EDITOR_FONT_SIZE;
  148. IsColorsInverted = false;
  149. }
  150. public void Accept()
  151. {
  152. markpadRegistryEditor.UpdateExtensionRegistryKeys(Extensions);
  153. spellingService.SetLanguage(SelectedLanguage);
  154. UpdateMarkpadSettings();
  155. eventAggregator.Publish(new SettingsChangedEvent());
  156. }
  157. public void HideSettings()
  158. {
  159. eventAggregator.Publish(new SettingsCloseEvent());
  160. Accept();
  161. }
  162. private void UpdateMarkpadSettings()
  163. {
  164. var settings = settingsProvider.GetSettings<MarkPadSettings>();
  165. settings.FontSize = SelectedFontSize;
  166. settings.FontFamily = SelectedFontFamily.Source;
  167. settings.FloatingToolBarEnabled = EnableFloatingToolBar;
  168. settings.IsEditorColorsInverted = IsColorsInverted;
  169. settings.IndentType = IndentType;
  170. settings.Language = SelectedLanguage;
  171. settings.MarkdownExtraEnabled = EnableMarkdownExtra;
  172. settingsProvider.SaveSettings(settings);
  173. }
  174. }
  175. }