SystemItemWindow.xaml.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. using GeekDesk.Constant;
  2. using GeekDesk.Interface;
  3. using GeekDesk.Util;
  4. using GeekDesk.ViewModel;
  5. using System;
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using System.ComponentModel;
  9. using System.IO;
  10. using System.Threading;
  11. using System.Windows;
  12. using System.Windows.Controls;
  13. using System.Windows.Input;
  14. using System.Windows.Media.Imaging;
  15. using static GeekDesk.Util.ShowWindowFollowMouse;
  16. namespace GeekDesk.Control.Windows
  17. {
  18. /// <summary>
  19. /// SystemItemWindow.xaml 的交互逻辑
  20. /// 添加系统项目到对应菜单
  21. /// </summary>
  22. public partial class SystemItemWindow : Window, IWindowCommon
  23. {
  24. private static AppConfig appConfig = MainWindow.appData.AppConfig;
  25. private static SystemItemViewModel vm;
  26. private List<IconInfo> systemIcons;
  27. private List<IconInfo> startMenuIcons;
  28. private List<IconInfo> storeIcons;
  29. private SystemItemWindow()
  30. {
  31. vm = new SystemItemViewModel();
  32. this.DataContext = vm;
  33. InitializeComponent();
  34. this.Topmost = true;
  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. /// <summary>
  54. /// 切换选项卡
  55. /// </summary>
  56. /// <param name="sender"></param>
  57. /// <param name="e"></param>
  58. private void TabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
  59. {
  60. TabItem ti = this.MyTabControl.SelectedItem as TabItem;
  61. List<IconInfo> systemInfos = vm.IconInfos;
  62. if (systemInfos == null)
  63. {
  64. systemInfos = new List<IconInfo>();
  65. }
  66. switch (ti.Tag.ToString())
  67. {
  68. case "StartMenu": //开始菜单
  69. if (startMenuIcons == null)
  70. {
  71. vm.IconInfos = null;
  72. System.Threading.Thread t = new System.Threading.Thread(new ThreadStart(GetStartMenuInfos))
  73. {
  74. IsBackground = true
  75. };
  76. t.Start();
  77. }
  78. else
  79. {
  80. StartMenuLoading.Visibility = Visibility.Collapsed;
  81. vm.IconInfos = startMenuIcons;
  82. }
  83. break;
  84. case "Store": //应用商店
  85. if (storeIcons == null)
  86. {
  87. vm.IconInfos = null;
  88. storeIcons = new List<IconInfo>();
  89. vm.IconInfos = storeIcons;
  90. }
  91. else
  92. {
  93. vm.IconInfos = storeIcons;
  94. }
  95. break;
  96. default: //默认系统项
  97. if (systemIcons == null)
  98. {
  99. vm.IconInfos = null;
  100. systemIcons = GetSysteIconInfos();
  101. vm.IconInfos = systemIcons;
  102. }
  103. else
  104. {
  105. vm.IconInfos = systemIcons;
  106. }
  107. break;
  108. }
  109. }
  110. /// <summary>
  111. /// 获取开始菜单路径下项目
  112. /// </summary>
  113. /// <returns></returns>
  114. private void GetStartMenuInfos()
  115. {
  116. App.Current.Dispatcher.Invoke((Action)(() =>
  117. {
  118. StartMenuLoading.Visibility = Visibility.Visible;
  119. }));
  120. List<IconInfo> infos = new List<IconInfo>();
  121. //获取开始菜单路径
  122. string path = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu) + "\\Programs";
  123. //递归获取信息
  124. GetInfos(path, infos);
  125. App.Current.Dispatcher.Invoke((Action)(() =>
  126. {
  127. if (StartMenu.IsSelected)
  128. {
  129. startMenuIcons = infos;
  130. vm.IconInfos = startMenuIcons;
  131. }
  132. StartMenuLoading.Visibility = Visibility.Collapsed;
  133. }));
  134. }
  135. /// <summary>
  136. /// 递归获取文件信息
  137. /// </summary>
  138. /// <param name="path"></param>
  139. /// <param name="listInfos"></param>
  140. private void GetInfos(string filePath, List<IconInfo> listInfos)
  141. {
  142. DirectoryInfo di = new DirectoryInfo(filePath);
  143. string[] filePaths = Directory.GetFiles(filePath);
  144. string[] dirPaths = Directory.GetDirectories(filePath);
  145. string[] paths = new string[filePaths.Length + dirPaths.Length];
  146. filePaths.CopyTo(paths, 0);
  147. if (filePaths == null || filePaths.Length == 0)
  148. {
  149. dirPaths.CopyTo(paths, 0);
  150. }
  151. else
  152. {
  153. dirPaths.CopyTo(paths, filePaths.Length - 1);
  154. }
  155. foreach (string path in paths)
  156. {
  157. if (File.Exists(path))
  158. {
  159. string ext = Path.GetExtension(path).ToLower();
  160. if (".exe".Equals(ext) || ".lnk".Equals(ext))
  161. {
  162. try
  163. {
  164. IconInfo iconInfo = CommonCode.GetIconInfoByPath_NoWrite(path);
  165. if (iconInfo.Path_NoWrite != null)
  166. {
  167. iconInfo.Content_NoWrite = iconInfo.Path_NoWrite + "\n" + iconInfo.Name_NoWrite;
  168. listInfos.Add(iconInfo);
  169. }
  170. }
  171. catch (Exception) { }
  172. }
  173. }
  174. else if (Directory.Exists(path))
  175. {
  176. GetInfos(path, listInfos);
  177. }
  178. }
  179. //FileSystemInfo[] fileInfoArr = di.GetFileSystemInfos();
  180. //foreach(FileSystemInfo fi in fileInfoArr)
  181. //{
  182. // string path = fi.FullName;
  183. //}
  184. }
  185. /// <summary>
  186. /// 获取系统项目
  187. /// </summary>
  188. /// <returns></returns>
  189. private List<IconInfo> GetSysteIconInfos()
  190. {
  191. List<IconInfo> iconInfos = new List<IconInfo>();
  192. Hashtable systemIcons = Constants.SYSTEM_ICONS;
  193. IconInfo iconInfo;
  194. foreach (object key in systemIcons.Keys)
  195. {
  196. string keyStr = key.ToString();
  197. iconInfo = new IconInfo
  198. {
  199. Name_NoWrite = systemIcons[key].ToString()
  200. };
  201. iconInfo.BitmapImage_NoWrite = new BitmapImage(
  202. new Uri("pack://application:,,,/GeekDesk;component/Resource/Image/SystemIcon/" + keyStr + ".png"
  203. , UriKind.RelativeOrAbsolute));
  204. iconInfo.StartArg = keyStr;
  205. iconInfo.Content_NoWrite = iconInfo.Name_NoWrite;
  206. iconInfos.Add(iconInfo);
  207. }
  208. return iconInfos;
  209. }
  210. public class SystemItemViewModel : INotifyPropertyChanged
  211. {
  212. private List<IconInfo> iconInfos;
  213. private AppConfig appConfig;
  214. public SystemItemViewModel()
  215. {
  216. this.AppConfig = MainWindow.appData.AppConfig;
  217. }
  218. public AppConfig AppConfig
  219. {
  220. get
  221. {
  222. return appConfig;
  223. }
  224. set
  225. {
  226. appConfig = value;
  227. OnPropertyChanged("AppConfig");
  228. }
  229. }
  230. public List<IconInfo> IconInfos
  231. {
  232. get
  233. {
  234. return iconInfos;
  235. }
  236. set
  237. {
  238. iconInfos = value;
  239. OnPropertyChanged("IconInfos");
  240. }
  241. }
  242. public event PropertyChangedEventHandler PropertyChanged;
  243. private void OnPropertyChanged(string propertyName)
  244. {
  245. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  246. }
  247. }
  248. private static System.Windows.Window window = null;
  249. public static void Show()
  250. {
  251. if (window == null || !window.Activate())
  252. {
  253. window = new SystemItemWindow();
  254. }
  255. window.Show();
  256. Keyboard.Focus(window);
  257. ShowWindowFollowMouse.Show(window, MousePosition.LEFT_CENTER, 0, 0);
  258. }
  259. public void OnKeyDown(object sender, KeyEventArgs e)
  260. {
  261. if (e.Key == Key.Escape)
  262. {
  263. this.DataContext = null;
  264. this.Close();
  265. }
  266. }
  267. }
  268. }