IconfontWindow.xaml.cs 7.1 KB

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