From 425d1a4ce5e68dfb0739d2333c6729cad1cd6374 Mon Sep 17 00:00:00 2001 From: BookerLiu Date: Tue, 21 Mar 2023 17:18:18 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20feture=20=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E5=85=B3=E8=81=94=E5=AE=9E=E6=97=B6=E6=96=87=E4=BB=B6=E5=A4=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Constant/MenuType.cs | 14 ++ .../UserControls/Config/ThemeControl.xaml.cs | 2 +- .../PannelCard/LeftCardControl.xaml | 2 + .../PannelCard/LeftCardControl.xaml.cs | 88 +++++++++++ GeekDesk.csproj | 5 + MainWindow.xaml.cs | 3 + Util/FileWatcher.cs | 138 ++++++++++++++++++ ViewModel/MenuInfo.cs | 28 ++++ packages.config | 1 + 9 files changed, 280 insertions(+), 1 deletion(-) create mode 100644 Constant/MenuType.cs create mode 100644 Util/FileWatcher.cs diff --git a/Constant/MenuType.cs b/Constant/MenuType.cs new file mode 100644 index 0000000..23ebc97 --- /dev/null +++ b/Constant/MenuType.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GeekDesk.Constant +{ + public enum MenuType + { + NORMAL, //普通菜单 + LINK, //关联菜单 + } +} diff --git a/Control/UserControls/Config/ThemeControl.xaml.cs b/Control/UserControls/Config/ThemeControl.xaml.cs index 1f6ccad..2640e71 100644 --- a/Control/UserControls/Config/ThemeControl.xaml.cs +++ b/Control/UserControls/Config/ThemeControl.xaml.cs @@ -49,7 +49,7 @@ namespace GeekDesk.Control.UserControls.Config OpenFileDialog ofd = new OpenFileDialog { Multiselect = false, //只允许选中单个文件 - Filter = "图像文件(*.png, *.jpg)|*.png;*.jpg;*.gif" + Filter = "图像文件(*.png, *.jpg, *.gif)|*.png;*.jpg;*.gif" }; if (ofd.ShowDialog() == true) { diff --git a/Control/UserControls/PannelCard/LeftCardControl.xaml b/Control/UserControls/PannelCard/LeftCardControl.xaml index 4d2a4df..a094440 100644 --- a/Control/UserControls/PannelCard/LeftCardControl.xaml +++ b/Control/UserControls/PannelCard/LeftCardControl.xaml @@ -139,6 +139,7 @@ + @@ -158,6 +159,7 @@ + diff --git a/Control/UserControls/PannelCard/LeftCardControl.xaml.cs b/Control/UserControls/PannelCard/LeftCardControl.xaml.cs index cca4d9b..5dd13eb 100644 --- a/Control/UserControls/PannelCard/LeftCardControl.xaml.cs +++ b/Control/UserControls/PannelCard/LeftCardControl.xaml.cs @@ -4,14 +4,19 @@ using GeekDesk.Control.Other; using GeekDesk.Control.Windows; using GeekDesk.Util; using GeekDesk.ViewModel; +using Microsoft.Win32; using System; using System.Collections.ObjectModel; +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; using System.Threading; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Windows.Media; +using System.Windows.Threading; +using WindowsAPICodePack.Dialogs; namespace GeekDesk.Control.UserControls.PannelCard { @@ -200,6 +205,87 @@ namespace GeekDesk.Control.UserControls.PannelCard Lbi_Selected(obj, null); } + /// + /// 创建实时文件菜单 + /// + /// + /// + private void CreateLinkMenu(object sender, RoutedEventArgs e) + { + try + { + CommonOpenFileDialog dialog = new CommonOpenFileDialog + { + IsFolderPicker = true, + Title = "选择关联文件夹" + }; + if (dialog.ShowDialog() == CommonFileDialogResult.Ok) + { + string menuId = System.Guid.NewGuid().ToString(); + new Thread(() => + { + this.Dispatcher.BeginInvoke(new Action(() => + { + string path = dialog.FileName; + + MenuInfo menuInfo = new MenuInfo + { + MenuName = Path.GetFileNameWithoutExtension(path), + MenuId = menuId, + MenuType = MenuType.LINK, + LinkPath = path, + IsEncrypt = false, + }; + + appData.MenuList.Add(menuInfo); + + MenuListBox.SelectedIndex = appData.MenuList.Count - 1; + appData.AppConfig.SelectedMenuIndex = MenuListBox.SelectedIndex; + appData.AppConfig.SelectedMenuIcons = menuInfo.IconList; + //首次触发不了Selected事件 + object obj = MenuListBox.ItemContainerGenerator.ContainerFromIndex(MenuListBox.SelectedIndex); + SetListBoxItemEvent((ListBoxItem)obj); + Lbi_Selected(obj, null); + + HandyControl.Controls.Growl.Success("菜单关联成功, 后台加载列表!", "MainWindowGrowl"); + + FileWatcher.LinkMenuWatcher(menuInfo); + })); + }).Start(); + + new Thread(() => + { + Thread.Sleep(1000); + this.Dispatcher.BeginInvoke(new Action(() => + { + MenuInfo info = null; + foreach (MenuInfo menuInfo in appData.MenuList) + { + if (menuInfo.MenuId.Equals(menuId)) + { + info = menuInfo; + } + } + + DirectoryInfo dirInfo = new DirectoryInfo(info.LinkPath); + FileSystemInfo[] fileInfos = dirInfo.GetFileSystemInfos(); + foreach (FileSystemInfo fileInfo in fileInfos) + { + IconInfo iconInfo = CommonCode.GetIconInfoByPath_NoWrite(fileInfo.FullName); + info.IconList.Add(iconInfo); + } + })); + }).Start(); + + } + } + catch (Exception ex) + { + LogUtil.WriteErrorLog(ex, "新建关联菜单失败!"); + HandyControl.Controls.Growl.WarningGlobal("新建关联菜单失败!"); + } + } + /// /// 重命名菜单 将textbox 设置为可见 @@ -635,5 +721,7 @@ namespace GeekDesk.Control.UserControls.PannelCard } } + + } } diff --git a/GeekDesk.csproj b/GeekDesk.csproj index 2384338..eadb3e3 100644 --- a/GeekDesk.csproj +++ b/GeekDesk.csproj @@ -147,6 +147,9 @@ 4.0 + + packages\WindowsAPICodePack.Shell.CommonFileDialogs.1.1.5\lib\net452\WindowsAPICodePack.Shell.CommonFileDialogs.dll + @@ -167,6 +170,7 @@ + @@ -287,6 +291,7 @@ + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index b41a749..c8580fa 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -226,6 +226,9 @@ namespace GeekDesk SecondsWindow.ShowWindow(); } + //监听实时文件夹菜单 + FileWatcher.StartLinkMenuWatcher(appData); + //更新线程开启 检测更新 UpdateThread.Update(); diff --git a/Util/FileWatcher.cs b/Util/FileWatcher.cs new file mode 100644 index 0000000..126f478 --- /dev/null +++ b/Util/FileWatcher.cs @@ -0,0 +1,138 @@ +using GeekDesk.ViewModel; +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace GeekDesk.Util +{ + public class FileWatcher + { + public static Dictionary linkMenuMap = new Dictionary(); + /// + /// 实时文件夹监听 + /// + /// + public static void LinkMenuWatcher(MenuInfo menuInfo) + { + try + { + FileSystemWatcher fileSystemWatcher = new FileSystemWatcher + { + Path = menuInfo.LinkPath, + }; + linkMenuMap.Add(fileSystemWatcher, menuInfo); + fileSystemWatcher.EnableRaisingEvents = true; + fileSystemWatcher.Changed += LinkIcon_Changed; + fileSystemWatcher.Deleted += LinkIcon_Deleted; + fileSystemWatcher.Created += LinkIcon_Created; + fileSystemWatcher.Renamed += LinkIcon_Renamed; + } catch (Exception e) + { + LogUtil.WriteErrorLog(e, "添加LinkMenu监听异常"); + } + + } + + private static void LinkIcon_Renamed(object sender, RenamedEventArgs e) + { + IconInfo iconInfo = getIconInfoByPath(sender, e.OldFullPath); + iconInfo.Name = e.Name; + iconInfo.Path = e.FullPath; + } + + private static void LinkIcon_Changed(object sender, FileSystemEventArgs e) + { + IconInfo iconInfo = getIconInfoByPath(sender, e.FullPath); + if (iconInfo != null) + { + IconInfo newIconInfo = CommonCode.GetIconInfoByPath(e.FullPath); + iconInfo.BitmapImage = newIconInfo.BitmapImage; + } + + } + private static void LinkIcon_Deleted(object sender, FileSystemEventArgs e) + { + IconInfo iconInfo = getIconInfoByPath(sender, e.FullPath); + App.Current.Dispatcher.Invoke(() => + { + linkMenuMap[sender as FileSystemWatcher].IconList.Remove(iconInfo); + }); + } + private static void LinkIcon_Created(object sender, FileSystemEventArgs e) + { + IconInfo iconInfo = CommonCode.GetIconInfoByPath(e.FullPath); + App.Current.Dispatcher.Invoke(() => + { + linkMenuMap[sender as FileSystemWatcher].IconList.Add(iconInfo); + }); + } + + private static IconInfo getIconInfoByPath(object sender, string path) + { + MenuInfo menuInfo = linkMenuMap[sender as FileSystemWatcher]; + foreach (IconInfo iconInfo in menuInfo.IconList) + { + if (iconInfo.Path.Equals(path)) + { + return iconInfo; + } + } + return null; + } + + /// + /// 开启所有菜单监听 + /// + /// + public static void StartLinkMenuWatcher(AppData appData) + { + foreach (MenuInfo menuInfo in appData.MenuList) + { + if (menuInfo.MenuType == Constant.MenuType.LINK) + { + LinkMenuWatcher(menuInfo); + } + } + } + + /// + /// 移除菜单监听 + /// + /// + public static void RemoveLinkMenuWatcher(MenuInfo menuInfo) + { + try + { + foreach (FileSystemWatcher watcher in linkMenuMap.Keys) + { + if (linkMenuMap[watcher] == menuInfo) + { + //释放资源 + watcher.Changed -= LinkIcon_Changed; + watcher.Created -= LinkIcon_Created; + watcher.Deleted -= LinkIcon_Deleted; + watcher.Renamed -= LinkIcon_Renamed; + watcher.EnableRaisingEvents = false; + watcher.Dispose(); + linkMenuMap.Remove(watcher); + } + } + } + catch (Exception e) + { + //nothing + } + } + + + + + + + + } +} diff --git a/ViewModel/MenuInfo.cs b/ViewModel/MenuInfo.cs index 243604b..3cb7f25 100644 --- a/ViewModel/MenuInfo.cs +++ b/ViewModel/MenuInfo.cs @@ -21,8 +21,36 @@ namespace GeekDesk.ViewModel private string geometryColor; //几何图标颜色 private ObservableCollection iconList = new ObservableCollection(); private bool isEncrypt; //是否加密 + private MenuType menuType; //菜单类型 普通, 关联 + private string linkPath; //关联路径 + public string LinkPath + { + get + { + return linkPath; + } + set + { + linkPath = value; + OnPropertyChanged("LinkPath"); + } + } + + public MenuType MenuType + { + get + { + return menuType; + } + set + { + menuType = value; + OnPropertyChanged("MenuType"); + } + } + public bool IsEncrypt { get diff --git a/packages.config b/packages.config index 7d79221..7335133 100644 --- a/packages.config +++ b/packages.config @@ -19,5 +19,6 @@ + \ No newline at end of file