ThemesContentViewModel.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. using ComPDFKitViewer;
  2. using ComPDFKitViewer.PdfViewer;
  3. using PDF_Office.CustomControl;
  4. using PDF_Office.Helper;
  5. using PDF_Office.Model;
  6. using PDF_Office.Properties;
  7. using PDF_Office.Views.PropertyPanel.ViewModular;
  8. using PDFSettings.Settings;
  9. using Prism.Commands;
  10. using Prism.Mvvm;
  11. using Prism.Regions;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using System.Windows.Controls;
  19. using System.Windows.Documents;
  20. using System.Windows.Forms;
  21. using System.Windows.Input;
  22. using System.Windows.Media;
  23. using System.Windows.Shapes;
  24. using static PDF_Office.CustomControl.ColorDropBox;
  25. using RadioButton = System.Windows.Controls.RadioButton;
  26. namespace PDF_Office.ViewModels.PropertyPanel.ViewModular
  27. {
  28. public class ThemesContentViewModel : BindableBase, INavigationAware
  29. {
  30. public CPDFViewer PDFViewer { get; set; }
  31. public Color? SelectedColor { get; set; }
  32. private bool resedaDrawMode;
  33. public bool ResedaDrawMode
  34. {
  35. get { return resedaDrawMode; }
  36. set
  37. {
  38. SetProperty(ref resedaDrawMode, value);
  39. if (value)
  40. {
  41. SetDrawModeEvent(null);
  42. }
  43. }
  44. }
  45. private bool darkDrawMode;
  46. public bool DarkDrawMode
  47. {
  48. get { return darkDrawMode; }
  49. set
  50. {
  51. SetProperty(ref darkDrawMode, value);
  52. if (value)
  53. {
  54. SetDrawModeEvent(null);
  55. }
  56. }
  57. }
  58. private bool sepiaDrawMode;
  59. public bool SepiaDrawMode
  60. {
  61. get { return sepiaDrawMode; }
  62. set
  63. {
  64. SetProperty(ref sepiaDrawMode, value);
  65. if (value)
  66. {
  67. SetDrawModeEvent(null);
  68. }
  69. }
  70. }
  71. private bool lightDrawMode;
  72. public bool LightDrawMode
  73. {
  74. get { return lightDrawMode; }
  75. set
  76. {
  77. SetProperty(ref lightDrawMode, value);
  78. if (value)
  79. {
  80. SetDrawModeEvent(null);
  81. }
  82. }
  83. }
  84. public DelegateCommand<object> LoadedCommand { get; set; }
  85. public DelegateCommand<object> SetDrawModeCommand { get; set; }
  86. public ThemesContentViewModel()
  87. {
  88. LoadedCommand = new DelegateCommand<object>(LoadedEvent);
  89. SetDrawModeCommand = new DelegateCommand<object>(SetDrawModeEvent);
  90. }
  91. private void LoadedEvent(object obj)
  92. {
  93. if (obj is WrapPanel wrapPanel)
  94. {
  95. InitBeforeShow(wrapPanel);
  96. }
  97. }
  98. /// <summary>
  99. /// 设置主题颜色
  100. /// </summary>
  101. /// <param name="obj"></param>
  102. private void SetDrawModeEvent(object obj)
  103. {
  104. //if (obj is RadioButton radioButton)
  105. //{
  106. // bool sepia = radioButton.Name.IndexOf(ThemesDrawMode.Sepia.ToString(), StringComparison.OrdinalIgnoreCase) >= 0;
  107. // bool light = radioButton.Name.IndexOf(ThemesDrawMode.Light.ToString(), StringComparison.OrdinalIgnoreCase) >= 0;
  108. // bool reseda = radioButton.Name.IndexOf(ThemesDrawMode.Reseda.ToString(), StringComparison.OrdinalIgnoreCase) >= 0;
  109. // bool dark = radioButton.Name.IndexOf(ThemesDrawMode.Dark.ToString(), StringComparison.OrdinalIgnoreCase) >= 0;
  110. //}
  111. DrawModes draw = PDFViewer.GetDrawMode();
  112. UInt32 color = 0;
  113. if (LightDrawMode)
  114. {
  115. draw = DrawModes.Draw_Mode_Normal;
  116. SetDrawMode(draw, color);
  117. }
  118. else if (SepiaDrawMode)
  119. {
  120. draw = DrawModes.Draw_Mode_Soft;
  121. SetDrawMode(draw, color);
  122. }
  123. else if (ResedaDrawMode)
  124. {
  125. draw = DrawModes.Draw_Mode_Green;
  126. SetDrawMode(draw, color);
  127. }
  128. else if (DarkDrawMode)
  129. {
  130. draw = DrawModes.Draw_Mode_Dark;
  131. SetDrawMode(draw, color);
  132. }
  133. else
  134. {
  135. if (obj is RadioButton radioButton)
  136. {
  137. StackPanel panel = radioButton.Content as StackPanel;
  138. Rectangle rec = (panel.Children[0] as Border).Child as Rectangle;
  139. OpenFileInfo file = SettingHelper.GetFileInfo(PDFViewer?.Document.FilePath);
  140. if (file != null)
  141. {
  142. file.LastFillBrushColor = ((SolidColorBrush)rec.Fill).Color;
  143. }
  144. var color1 = rec.Fill.ToString().Substring(1, 8);
  145. UInt32 c = Convert.ToUInt32(color1, 16);
  146. PDFViewer.SetDrawMode(DrawModes.Draw_Mode_Normal);
  147. PDFViewer.SetDrawMode(DrawModes.Draw_Mode_Custom, c);
  148. UpdateFileInfoList(DrawModes.Draw_Mode_Custom, c);
  149. }
  150. }
  151. }
  152. private void SetDrawMode(DrawModes drawMode, UInt32 color)
  153. {
  154. if (PDFViewer == null)
  155. {
  156. return;
  157. }
  158. if (PDFViewer.GetDrawMode() != drawMode)
  159. {
  160. PDFViewer.SetDrawMode(drawMode);
  161. UpdateFileInfoList(drawMode, color);
  162. }
  163. }
  164. /// <summary>
  165. /// 更新文件信息
  166. /// </summary>
  167. /// <param name="modes"></param>
  168. /// <param name="color"></param>
  169. private void UpdateFileInfoList(DrawModes modes, UInt32 color)
  170. {
  171. OpenFileInfo file = SettingHelper.GetFileInfo(PDFViewer?.Document.FilePath);
  172. if (file != null)
  173. {
  174. file.LastDrawMode = modes;
  175. if (color != 0)
  176. {
  177. file.LastFillColor = color;
  178. }
  179. }
  180. SettingHelper.SetFileInfo(file);
  181. }
  182. /// <summary>
  183. /// 设置选中主题颜色
  184. /// </summary>
  185. /// <param name="wrapPanel"></param>
  186. private void InitBeforeShow(WrapPanel wrapPanel)
  187. {
  188. var mode = PDFViewer.GetDrawMode();
  189. if (mode == DrawModes.Draw_Mode_Normal)
  190. {
  191. LightDrawMode = true;
  192. }
  193. else if (mode == DrawModes.Draw_Mode_Dark)
  194. {
  195. DarkDrawMode = true;
  196. }
  197. else if (mode == DrawModes.Draw_Mode_Green)
  198. {
  199. ResedaDrawMode = true;
  200. }
  201. else if (mode == DrawModes.Draw_Mode_Soft)
  202. {
  203. SepiaDrawMode = true;
  204. }
  205. else
  206. {
  207. OpenFileInfo info = SettingHelper.GetFileInfo(PDFViewer.Document.FilePath);
  208. var color = info.LastFillBrushColor;
  209. for (int i = 4; i < wrapPanel.Children.Count; i++)
  210. {
  211. var btn = wrapPanel.Children[i] as RadioButton;
  212. if (btn != null && Color.Equals(((SolidColorBrush)(((btn.Content as StackPanel).Children[0] as Border).Child as Rectangle).Fill).Color, color))
  213. {
  214. btn.IsChecked = true;
  215. }
  216. }
  217. }
  218. }
  219. public bool IsNavigationTarget(NavigationContext navigationContext)
  220. {
  221. return true;
  222. }
  223. public void OnNavigatedFrom(NavigationContext navigationContext)
  224. {
  225. }
  226. public void OnNavigatedTo(NavigationContext navigationContext)
  227. {
  228. var pdfview = navigationContext.Parameters[ParameterNames.PDFViewer] as CPDFViewer;
  229. if (pdfview != null)
  230. {
  231. PDFViewer = pdfview;
  232. }
  233. }
  234. }
  235. }