IconfontWindow.xaml.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using GeekDesk.Control.Other;
  2. using GeekDesk.Util;
  3. using GeekDesk.ViewModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. namespace GeekDesk.Control.Windows
  11. {
  12. /// <summary>
  13. /// IconfontWindow.xaml 的交互逻辑
  14. /// </summary>
  15. public partial class IconfontWindow : Window
  16. {
  17. private static AppConfig appConfig = MainWindow.appData.AppConfig;
  18. private static MenuInfo menuInfo;
  19. private static List<IconfontInfo> systemIcons;
  20. private static List<IconfontInfo> customIcons;
  21. public static IconfontViewModel vm;
  22. private IconfontWindow(List<IconfontInfo> icons, MenuInfo menuInfo)
  23. {
  24. InitializeComponent();
  25. systemIcons = icons;
  26. this.Topmost = true;
  27. IconfontWindow.menuInfo = menuInfo;
  28. vm = new IconfontViewModel
  29. {
  30. Iconfonts = systemIcons
  31. };
  32. this.DataContext = vm;
  33. }
  34. /// <summary>
  35. /// 移动窗口
  36. /// </summary>
  37. /// <param name="sender"></param>
  38. /// <param name="e"></param>
  39. private void DragMove(object sender, System.Windows.Input.MouseButtonEventArgs e)
  40. {
  41. if (e.LeftButton == MouseButtonState.Pressed)
  42. {
  43. DragMove();
  44. }
  45. }
  46. private void Close_Click(object sender, RoutedEventArgs e)
  47. {
  48. this.Close();
  49. }
  50. private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  51. {
  52. TabItem ti = this.MyTabControl.SelectedItem as TabItem;
  53. switch (ti.Tag.ToString())
  54. {
  55. case "Custom":
  56. CustomButton.IsEnabled = true;
  57. if (StringUtil.IsEmpty(appConfig.CustomIconUrl) || StringUtil.IsEmpty(appConfig.CustomIconJsonUrl))
  58. {
  59. LoadingEle.Visibility = Visibility.Visible;
  60. CustomIcon.Visibility = Visibility.Collapsed;
  61. HandyControl.Controls.Dialog.Show(new CustomIconUrlDialog(appConfig));
  62. } else
  63. {
  64. if (customIcons == null)
  65. {
  66. vm.Iconfonts = null;
  67. LoadingOnlineIcon();
  68. } else
  69. {
  70. vm.Iconfonts = customIcons;
  71. LoadingEle.Visibility = Visibility.Collapsed;
  72. CustomIcon.Visibility = Visibility.Visible;
  73. }
  74. }
  75. break;
  76. default:
  77. if (CustomButton != null)
  78. {
  79. CustomButton.IsEnabled = false;
  80. }
  81. if (vm != null)
  82. {
  83. vm.Iconfonts = systemIcons;
  84. }
  85. break;
  86. }
  87. }
  88. private void Confirm_Click(object sender, RoutedEventArgs e)
  89. {
  90. TabItem ti = this.MyTabControl.SelectedItem as TabItem;
  91. int index;
  92. switch (ti.Tag.ToString())
  93. {
  94. case "Custom":
  95. index = this.CustomIcon.IconListBox.SelectedIndex;
  96. if (index != -1)
  97. {
  98. menuInfo.MenuGeometry = customIcons[index].Text;
  99. }
  100. break;
  101. default:
  102. index = this.SystemIcon.IconListBox.SelectedIndex;
  103. if (index != -1)
  104. {
  105. menuInfo.MenuGeometry = systemIcons[index].Text;
  106. }
  107. break;
  108. }
  109. this.Close();
  110. }
  111. private static System.Windows.Window window = null;
  112. public static void Show(List<IconfontInfo> listInfo, MenuInfo menuInfo)
  113. {
  114. if (window == null || !window.Activate())
  115. {
  116. window = new IconfontWindow(listInfo, menuInfo);
  117. }
  118. window.Show();
  119. }
  120. private void CustomButton_Click(object sender, RoutedEventArgs e)
  121. {
  122. HandyControl.Controls.Dialog.Show(new CustomIconUrlDialog(appConfig));
  123. }
  124. private void CheckSettingUrl_TextChanged(object sender, TextChangedEventArgs e)
  125. {
  126. if (CheckSettingUrl.Text == "true")
  127. {
  128. LoadingOnlineIcon();
  129. } else
  130. {
  131. LoadingEle.IsRunning = true;
  132. CustomIcon.Visibility = Visibility.Collapsed;
  133. }
  134. }
  135. private void LoadingOnlineIcon()
  136. {
  137. try
  138. {
  139. string svgJsStr = HttpUtil.Get(appConfig.CustomIconUrl);
  140. string jsonStr = HttpUtil.Get(appConfig.CustomIconJsonUrl);
  141. List<IconfontInfo> icons = SvgToGeometry.GetIconfonts(svgJsStr, jsonStr);
  142. customIcons = icons;
  143. vm.Iconfonts = customIcons;
  144. LoadingEle.Visibility = Visibility.Collapsed;
  145. CustomIcon.Visibility = Visibility.Visible;
  146. }
  147. catch (Exception)
  148. {
  149. HandyControl.Controls.Growl.WarningGlobal("加载远程图标异常!");
  150. }
  151. }
  152. public class IconfontViewModel : INotifyPropertyChanged
  153. {
  154. private List<IconfontInfo> iconfonts;
  155. private string isSettingUrl;
  156. public List<IconfontInfo> Iconfonts
  157. {
  158. get
  159. {
  160. return iconfonts;
  161. }
  162. set
  163. {
  164. iconfonts = value;
  165. OnPropertyChanged("Iconfonts");
  166. }
  167. }
  168. public string IsSettingUrl
  169. {
  170. get
  171. {
  172. return isSettingUrl;
  173. }
  174. set
  175. {
  176. isSettingUrl = value;
  177. OnPropertyChanged("IsSettingUrl");
  178. }
  179. }
  180. public event PropertyChangedEventHandler PropertyChanged;
  181. private void OnPropertyChanged(string propertyName)
  182. {
  183. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  184. }
  185. }
  186. }
  187. }