IconfontWindow.xaml.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.Close();
  51. }
  52. private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  53. {
  54. TabItem ti = this.MyTabControl.SelectedItem as TabItem;
  55. switch (ti.Tag.ToString())
  56. {
  57. case "Custom":
  58. CustomButton.IsEnabled = true;
  59. if (StringUtil.IsEmpty(appConfig.CustomIconUrl) || StringUtil.IsEmpty(appConfig.CustomIconJsonUrl))
  60. {
  61. LoadingEle.Visibility = Visibility.Visible;
  62. CustomIcon.Visibility = Visibility.Collapsed;
  63. HandyControl.Controls.Dialog.Show(new CustomIconUrlDialog(appConfig), "IconUrlDialog");
  64. } else
  65. {
  66. if (customIcons == null)
  67. {
  68. vm.Iconfonts = null;
  69. LoadingOnlineIcon();
  70. } else
  71. {
  72. vm.Iconfonts = customIcons;
  73. LoadingEle.Visibility = Visibility.Collapsed;
  74. CustomIcon.Visibility = Visibility.Visible;
  75. }
  76. }
  77. break;
  78. default:
  79. if (CustomButton != null)
  80. {
  81. CustomButton.IsEnabled = false;
  82. }
  83. if (vm != null)
  84. {
  85. vm.Iconfonts = systemIcons;
  86. }
  87. break;
  88. }
  89. }
  90. private void Confirm_Click(object sender, RoutedEventArgs e)
  91. {
  92. TabItem ti = this.MyTabControl.SelectedItem as TabItem;
  93. int index;
  94. switch (ti.Tag.ToString())
  95. {
  96. case "Custom":
  97. index = this.CustomIcon.IconListBox.SelectedIndex;
  98. if (index != -1)
  99. {
  100. menuInfo.MenuGeometry = customIcons[index].Text;
  101. }
  102. break;
  103. default:
  104. index = this.SystemIcon.IconListBox.SelectedIndex;
  105. if (index != -1)
  106. {
  107. menuInfo.MenuGeometry = systemIcons[index].Text;
  108. }
  109. break;
  110. }
  111. this.Close();
  112. }
  113. private static System.Windows.Window window = null;
  114. public static void Show(List<IconfontInfo> listInfo, MenuInfo menuInfo)
  115. {
  116. if (window == null || !window.Activate())
  117. {
  118. window = new IconfontWindow(listInfo, menuInfo);
  119. }
  120. window.Show();
  121. Keyboard.Focus(window);
  122. ShowWindowFollowMouse.Show(window, MousePosition.LEFT_CENTER, 0, 0, false);
  123. }
  124. private void CustomButton_Click(object sender, RoutedEventArgs e)
  125. {
  126. HandyControl.Controls.Dialog.Show(new CustomIconUrlDialog(appConfig));
  127. }
  128. private void CheckSettingUrl_TextChanged(object sender, TextChangedEventArgs e)
  129. {
  130. if (CheckSettingUrl.Text == "true")
  131. {
  132. LoadingOnlineIcon();
  133. } else
  134. {
  135. LoadingEle.IsRunning = true;
  136. CustomIcon.Visibility = Visibility.Collapsed;
  137. }
  138. }
  139. private void LoadingOnlineIcon()
  140. {
  141. try
  142. {
  143. string svgJsStr = HttpUtil.Get(appConfig.CustomIconUrl);
  144. string jsonStr = HttpUtil.Get(appConfig.CustomIconJsonUrl);
  145. List<IconfontInfo> icons = SvgToGeometry.GetIconfonts(svgJsStr, jsonStr);
  146. customIcons = icons;
  147. vm.Iconfonts = customIcons;
  148. LoadingEle.Visibility = Visibility.Collapsed;
  149. CustomIcon.Visibility = Visibility.Visible;
  150. }
  151. catch (Exception)
  152. {
  153. HandyControl.Controls.Growl.WarningGlobal("加载远程图标异常!");
  154. }
  155. }
  156. public void OnKeyDown(object sender, KeyEventArgs e)
  157. {
  158. if (e.Key == Key.Escape)
  159. {
  160. this.Close();
  161. }
  162. }
  163. public class IconfontViewModel : INotifyPropertyChanged
  164. {
  165. private List<IconfontInfo> iconfonts;
  166. private string isSettingUrl;
  167. public List<IconfontInfo> Iconfonts
  168. {
  169. get
  170. {
  171. return iconfonts;
  172. }
  173. set
  174. {
  175. iconfonts = value;
  176. OnPropertyChanged("Iconfonts");
  177. }
  178. }
  179. public string IsSettingUrl
  180. {
  181. get
  182. {
  183. return isSettingUrl;
  184. }
  185. set
  186. {
  187. isSettingUrl = value;
  188. OnPropertyChanged("IsSettingUrl");
  189. }
  190. }
  191. public event PropertyChangedEventHandler PropertyChanged;
  192. private void OnPropertyChanged(string propertyName)
  193. {
  194. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  195. }
  196. }
  197. }
  198. }